Skip to content

Commit

Permalink
Checkpointing all more routines added for #2
Browse files Browse the repository at this point in the history
  • Loading branch information
samilliken committed Mar 4, 2015
1 parent 15386ae commit 7287116
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 44 deletions.
117 changes: 91 additions & 26 deletions repo/api/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@

// $app->get( '/cdutemplate', 'getCDUTemplate' );
// $app->get( '/cdutemplate/byid/:cdutemplateid', 'getCDUTemplateByID' );
// $app->get( '/devicetemplate', 'getDeviceTemplate' );
// $app->get( '/devicetemplate/byid/:templateid', 'getDeviceTemplateByID' );
// $app->get( '/devicetemplate/bymanufacturer/:manufacturerid', 'getDeviceTemplateByManufacturer' );
$app->get( '/devicetemplate', 'getDeviceTemplate' );
$app->get( '/devicetemplate/byid/:templateid', 'getDeviceTemplateByID' );
$app->get( '/devicetemplate/bymanufacturer/:manufacturerid', 'getDeviceTemplateByManufacturer' );
$app->get( '/manufacturer', 'getManufacturer' );
$app->get( '/manufacturer/byid/:manufacturerid', 'getManufacturerByID' );
$app->get( '/manufacturer/pending', 'getPendingManufacturer' );
$app->get( '/manufacturer/pending/byid/:requestid', 'getPendingManufacturerByID' );

$app->put( '/manufacturer', 'authenticate', 'queueManufacturer' );
$app->put( '/manufacturer/approve', 'authenticate', 'approveManufacturer' );

$app->put( '/devicetemplate', 'authenticate', 'queueDeviceTemplate' );

/**
* Need to accept all options requests for PUT calls to work via jquery
Expand Down Expand Up @@ -81,19 +84,26 @@ function echoRespnse($status_code, $response) {
* Checking if the request has valid api key in the 'Authorization' header
*/
function authenticate(\Slim\Route $route) {
global $currUser;
$currUser = new Users();

// If being called from the same server, short circuit this process
if ( $_SERVER["REMOTE_ADDR"] == "127.0.0.1" ) {
return;
}

// If the Session variable 'userid' exists, this is an interactive session
// Rights are adminstered by the UI, rather than the API
if ( isset( $_SESSION['userid'] ) ) {
$currUser->UserID = $_SESSION['userid'];
return;
}

// Getting request headers
$headers = apache_request_headers();
$response = array();
$app = \Slim\Slim::getInstance();

global $currUser;
$u = new Users();

// Verifying Authorization Header
if (isset($headers['APIKey'])) {
Expand All @@ -103,7 +113,7 @@ function authenticate(\Slim\Route $route) {
// validating api key

// An API key was passed, so check to see if it's real or not
if (! $currUser = $u->verifyAPIKey($apikey, $ipaddress)) {
if (! $currUser->verifyAPIKey($apikey, $ipaddress)) {
// api key is not present in users table
$response["error"] = true;
$response["errorcode"] = 401;
Expand Down Expand Up @@ -146,25 +156,47 @@ function getDeviceTemplate() {
echoRespnse( 200, $response );
}

function getDeviceTemplateById( $templateid ) {
$dt = new DeviceTemplates();
$dtList = $dt->getDeviceTemplateById( $templateid );

$response['error'] = false;
$response['errorcode'] = 200;
$response['devicetemplates'] = array();
$response['error'] = false;
$response['errorcode'] = 200;
$response['devicetemplates'] = array();
foreach ( $dtList as $devtmp ) {
$tmp = array();
foreach ( $devtmp as $prop=>$value ) {
$tmp[$prop] = $value;
}
array_push( $response['devicetemplates'], $tmp );
}

echoRespnse( 200, $response );

}

function getDeviceTemplateByManufacturer( $manufacturerid ) {
$dt = new DeviceTemplates();
$dtList = $dt->getDeviceTemplateByMFG( $manufacturerid );

$response['error'] = false;
$response['errorcode'] = 200;
$response['devicetemplates'] = array();
$response['error'] = false;
$response['errorcode'] = 200;
$response['devicetemplates'] = array();
foreach ( $dtList as $devtmp ) {
$tmp = array();
foreach ( $devtmp as $prop=>$value ) {
$tmp[$prop] = $value;
}
array_push( $response['devicetemplates'], $tmp );
}
$response['error'] = false;
$response['errorcode'] = 200;
$response['devicetemplates'] = array();
$response['error'] = false;
$response['errorcode'] = 200;
$response['devicetemplates'] = array();
foreach ( $dtList as $devtmp ) {
$tmp = array();
foreach ( $devtmp as $prop=>$value ) {
$tmp[$prop] = $value;
}
array_push( $response['devicetemplates'], $tmp );
}

echoRespnse( 200, $response );
echoRespnse( 200, $response );
}

//
Expand Down Expand Up @@ -258,10 +290,10 @@ function getManufacturerByID($ManufacturerID) {
// Returns: 200 if successful
//
function queueManufacturer() {
$request = \Slim\Slim::getInstance()->request();
$app = \Slim\Slim::getInstance();
$response = array();
$m = new ManufacturersQueue();
$m->Name = $request->put('Name');
$m->Name = $app->request->put('Name');
if ( $m->queueManufacturer() ) {
$response['error'] = false;
$response['errorcode'] = 200;
Expand All @@ -276,12 +308,34 @@ function queueManufacturer() {
}
}

function approveManufacturer() {
global $currUser;

$app = \Slim\Slim::getInstance();
$response = array();
$m = new ManufacturersQueue();
$vars = json_decode( $app->request->getBody() );
$m->Name = $vars->Name;
$m->RequestID = $vars->RequestID;
if ( $m->approveRequest( $currUser ) ) {
$response['error'] = false;
$response['errorcode'] = 200;
$response['message'] = 'Manufacturer has been approved.';
echoRespnse( 200, $response );
} else {
$response['error'] = true;
$response['errorcode'] = 403;
$response['message'] = 'Error processing request.';
echoRespnse( 403, $response );
}
}

function queueDeviceTemplate() {
$request = Slim::getInstance()->request();
global $currUser;
$app = \Slim\Slim::getInstance();

$response = array();

/* $t = new DeviceTemplateQueue();
$t = new DeviceTemplatesQueue();
$t->ManufacturerID = $app->request->put('ManufacturerID');
$t->Model = $app->request->put('Model');
$t->Height = $app->request->put('Height');
Expand All @@ -296,7 +350,18 @@ function queueDeviceTemplate() {
$t->ChassisSlots = $app->request->put('ChassisSlots');
$t->RearChassisSlots = $app->request->put('RearChassisSlots');
$t->SubmittedBy = $currUser->UserID;
*/

if ( $t->queueDeviceTemplate() ) {
$response['error'] = false;
$response['errorcode'] = 200;
$response['message'] = 'Device template queued for approval.';
} else {
$response['error'] = true;
$response['errorcode'] = 403;
$response['message'] = 'Error processing request.';
}

echoRespnse( $response['errorcode'], $response );
}

$app->run();
Expand Down
34 changes: 29 additions & 5 deletions repo/create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ CREATE TABLE DeviceTemplatesQueue (
Height int(11) NOT NULL,
Weight int(11) NOT NULL,
Wattage int(11) NOT NULL,
DeviceType enum('Server','Appliance','Storage Array','Switch','Chassis','Patch Panel','Physical Infrastructure') NOT NULL default 'Server',
DeviceType varchar(23) NOT NULL default 'Server',
PSCount int(11) NOT NULL,
NumPorts int(11) NOT NULL,
Notes text NOT NULL,
Expand All @@ -69,14 +69,13 @@ CREATE TABLE DeviceTemplatesQueue (

DROP TABLE IF EXISTS DeviceTemplates;
CREATE TABLE DeviceTemplates (
RequestID INT(11) NOT NULL AUTO_INCREMENT,
TemplateID INT(11) NOT NULL,
TemplateID INT(11) NOT NULL AUTO_INCREMENT,
ManufacturerID int(11) NOT NULL,
Model varchar(80) NOT NULL,
Height int(11) NOT NULL,
Weight int(11) NOT NULL,
Wattage int(11) NOT NULL,
DeviceType enum('Server','Appliance','Storage Array','Switch','Chassis','Patch Panel','Physical Infrastructure') NOT NULL default 'Server',
DeviceType varchar(23) NOT NULL default 'Server',
PSCount int(11) NOT NULL,
NumPorts int(11) NOT NULL,
Notes text NOT NULL,
Expand All @@ -85,7 +84,7 @@ CREATE TABLE DeviceTemplates (
ChassisSlots SMALLINT(6) NOT NULL,
RearChassisSlots SMALLINT(6) NOT NULL,
LastModified DATETIME NOT NULL,
PRIMARY KEY (RequestID)
PRIMARY KEY (TemplateID)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


Expand Down Expand Up @@ -123,6 +122,31 @@ CREATE TABLE TemplatePortQueues (
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS CDUTemplates;
CREATE TABLE fac_CDUTemplate (
TemplateID int(11) NOT NULL AUTO_INCREMENT,
ManufacturerID int(11) NOT NULL,
Model varchar(80) NOT NULL,
Managed int(1) NOT NULL,
ATS int(1) NOT NULL,
SNMPVersion varchar(2) NOT NULL DEFAULT '2c',
VersionOID varchar(80) NOT NULL,
Multiplier varchar(6),
OID1 varchar(80) NOT NULL,
OID2 varchar(80) NOT NULL,
OID3 varchar(80) NOT NULL,
ATSStatusOID varchar(80) NOT NULL,
ATSDesiredResult varchar(80) NOT NULL,
ProcessingProfile enum('SingleOIDWatts','SingleOIDAmperes','Combine3OIDWatts','Combine3OIDAmperes','Convert3PhAmperes'),
Voltage int(11) NOT NULL,
Amperage int(11) NOT NULL,
NumOutlets int(11) NOT NULL,
GlobalID int(11) NOT NULL,
ShareToRepo tinyint(1) NOT NULL DEFAULT 0,
KeepLocal tinyint(1) NOT NULL DEFAULT 0,
PRIMARY KEY (TemplateID),
KEY ManufacturerID (ManufacturerID),
UNIQUE KEY (ManufacturerID, Model)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS SensorTemplates;

Loading

0 comments on commit 7287116

Please sign in to comment.