From 7a61955d8566ec14fbabcfa5b3eab13d3631352f Mon Sep 17 00:00:00 2001 From: stupidon Date: Tue, 22 Mar 2016 17:05:39 +0100 Subject: [PATCH] indentation and comments --- server/core/Compute.php | 411 ++++++++++++++++++++-------------------- 1 file changed, 208 insertions(+), 203 deletions(-) diff --git a/server/core/Compute.php b/server/core/Compute.php index 370f5a0..4d94705 100644 --- a/server/core/Compute.php +++ b/server/core/Compute.php @@ -35,34 +35,34 @@ class compute public function listServers() { try{ - $serverList = $this->libClass->listServers(true); - $servers = Array(); - foreach($serverList as $server){ - $servers[$server->id] = Array(); - $server->flavor->retrieve(); - $server->image->retrieve(); - $server->retrieve(); - $servers[$server->id]["id"] = $server->id; - $servers[$server->id]["name"] = $server->name; - $servers[$server->id]["image"] = $server->image; - $servers[$server->id]["ram"] = $server->flavor->ram; - $servers[$server->id]["disk"] = $server->flavor->disk; - $servers[$server->id]["flavor"] = $server->flavor; - $servers[$server->id]["status"] = $server->status; - $servers[$server->id]["created"] = $server->created; - $servers[$server->id]["updated"] = $server->updated; - $servers[$server->id]["ipv4"] = $server->ipv4; - $servers[$server->id]["ipv6"] = $server->ipv6; - $servers[$server->id]["progress"] = $server->progress; - $servers[$server->id]["hostId"] = $server->hostId; - $servers[$server->id]["tenantId"] = $server->tenantId; - $servers[$server->id]["userId"] = $server->userId; - $servers[$server->id]["taskState"] = $server->taskState; - $servers[$server->id]["addresses"] = $server->addresses; - $servers[$server->id]["links"] = $server->links; - $servers[$server->id]["metadata"] = $server->metadata; - } - $this->app->setOutput("Servers", $servers); + $serverList = $this->libClass->listServers(true); + $servers = Array(); + foreach($serverList as $server){ + $servers[$server->id] = Array(); + $server->flavor->retrieve(); + $server->image->retrieve(); + $server->retrieve(); + $servers[$server->id]["id"] = $server->id; + $servers[$server->id]["name"] = $server->name; + $servers[$server->id]["image"] = $server->image; + $servers[$server->id]["ram"] = $server->flavor->ram; + $servers[$server->id]["disk"] = $server->flavor->disk; + $servers[$server->id]["flavor"] = $server->flavor; + $servers[$server->id]["status"] = $server->status; + $servers[$server->id]["created"] = $server->created; + $servers[$server->id]["updated"] = $server->updated; + $servers[$server->id]["ipv4"] = $server->ipv4; + $servers[$server->id]["ipv6"] = $server->ipv6; + $servers[$server->id]["progress"] = $server->progress; + $servers[$server->id]["hostId"] = $server->hostId; + $servers[$server->id]["tenantId"] = $server->tenantId; + $servers[$server->id]["userId"] = $server->userId; + $servers[$server->id]["taskState"] = $server->taskState; + $servers[$server->id]["addresses"] = $server->addresses; + $servers[$server->id]["links"] = $server->links; + $servers[$server->id]["metadata"] = $server->metadata; + } + $this->app->setOutput("Servers", $servers); } catch(BadResponseError $e){ $this->app->getErrorInstance()->BadResponseHandler($e); @@ -77,7 +77,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } @@ -88,17 +88,17 @@ class compute public function listFlavors() { try{ - $flavorList = $this->libClass->listFlavors(); - $flavors = Array(); - foreach($flavorList as $flavor){ - $flavors[$flavor->id] = Array(); - $flavor->retrieve(); - $flavors[$flavor->id]["id"] = $flavor->id; - $flavors[$flavor->id]["name"] = $flavor->name; - $flavors[$flavor->id]["ram"] = $flavor->ram; - $flavors[$flavor->id]["disk"] = $flavor->disk; - $flavors[$flavor->id]["vcpus"] = $flavor->vcpus; - $flavors[$flavor->id]["links"] = $flavor->links; + $flavorList = $this->libClass->listFlavors(); + $flavors = Array(); + foreach($flavorList as $flavor){ + $flavors[$flavor->id] = Array(); + $flavor->retrieve(); + $flavors[$flavor->id]["id"] = $flavor->id; + $flavors[$flavor->id]["name"] = $flavor->name; + $flavors[$flavor->id]["ram"] = $flavor->ram; + $flavors[$flavor->id]["disk"] = $flavor->disk; + $flavors[$flavor->id]["vcpus"] = $flavor->vcpus; + $flavors[$flavor->id]["links"] = $flavor->links; } $this->app->setOutput("Flavors", $flavors); } @@ -115,7 +115,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } @@ -126,21 +126,21 @@ class compute public function listImages() { try{ - $imageList = $this->libClass->listImages(); - $images = Array(); - foreach($imageList as $image){ - $images[$image->id] = Array(); - $image->retrieve(); - $images[$image->id]["id"] = $image->id; - $images[$image->id]["name"] = $image->name; - $images[$image->id]["status"] = $image->status; - $images[$image->id]["created"] = $image->created; - $images[$image->id]["updated"] = $image->updated; - $images[$image->id]["minDisk"] = $image->minDisk; - $images[$image->id]["minRam"] = $image->minRam; - $images[$image->id]["progress"] = $image->progress; - $images[$image->id]["links"] = $image->links; - $images[$image->id]["metadata"] = $image->metadata; + $imageList = $this->libClass->listImages(); + $images = Array(); + foreach($imageList as $image){ + $images[$image->id] = Array(); + $image->retrieve(); + $images[$image->id]["id"] = $image->id; + $images[$image->id]["name"] = $image->name; + $images[$image->id]["status"] = $image->status; + $images[$image->id]["created"] = $image->created; + $images[$image->id]["updated"] = $image->updated; + $images[$image->id]["minDisk"] = $image->minDisk; + $images[$image->id]["minRam"] = $image->minRam; + $images[$image->id]["progress"] = $image->progress; + $images[$image->id]["links"] = $image->links; + $images[$image->id]["metadata"] = $image->metadata; } $this->app->setOutput("Images", $images); } @@ -157,7 +157,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } @@ -168,15 +168,15 @@ class compute public function getServer() { try{ - $serverId = $this->app->getPostParam("serverId"); - if(!isset($serverId)){ - $this->app->setOutput("Error", "Server ID is missing, son!"); - return; - } - $opt = array('id' => $serverId); - $server = $this->libClass->getServer($opt); - $server->retrieve(); - $this->app->setOutput("MyServer", $server); + $serverId = $this->app->getPostParam("serverId"); + if(!isset($serverId)){ + $this->app->setOutput("Error", "Server ID is missing, son!"); + return; + } + $opt = array('id' => $serverId); + $server = $this->libClass->getServer($opt); + $server->retrieve(); + $this->app->setOutput("MyServer", $server); } catch(BadResponseError $e){ $this->app->getErrorInstance()->BadResponseHandler($e); @@ -191,7 +191,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } @@ -202,15 +202,15 @@ class compute public function getFlavor() { try{ - $flavorId = $this->app->getPostParam("flavorId"); - if(!isset($serverId)){ - $this->app->setOutput("Error", "Flavor ID is missing, son!"); - return; - } - $opt = array('id' => $flavorId); - $flavor = $this->libClass->getFlavor($opt); - $flavor->retrieve(); - $this->app->setOutput("MyFlavor", $flavor); + $flavorId = $this->app->getPostParam("flavorId"); + if(!isset($serverId)){ + $this->app->setOutput("Error", "Flavor ID is missing, son!"); + return; + } + $opt = array('id' => $flavorId); + $flavor = $this->libClass->getFlavor($opt); + $flavor->retrieve(); + $this->app->setOutput("MyFlavor", $flavor); } catch(BadResponseError $e){ $this->app->getErrorInstance()->BadResponseHandler($e); @@ -225,7 +225,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } @@ -236,15 +236,15 @@ class compute public function getImage() { try{ - $imageId = $this->app->getPostParam("imageId"); - if(!isset($serverId)){ - $this->app->setOutput("Error", "Image ID is missing, son!"); - return; - } - $opt = array('id' => $imageId); - $image = $this->libClass->getImage($opt); - $image->retrieve(); - $this->app->setOutput("MyImage", $image); + $imageId = $this->app->getPostParam("imageId"); + if(!isset($serverId)){ + $this->app->setOutput("Error", "Image ID is missing, son!"); + return; + } + $opt = array('id' => $imageId); + $image = $this->libClass->getImage($opt); + $image->retrieve(); + $this->app->setOutput("MyImage", $image); } catch(BadResponseError $e){ $this->app->getErrorInstance()->BadResponseHandler($e); @@ -259,7 +259,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } @@ -270,15 +270,15 @@ class compute public function createServer() { try{ - $name = $this->app->getPostParam("name"); - $imageId = $this->app->getPostParam("imageId"); - $flavorId = $this->app->getPostParam("flavorId"); - if(!isset($name) || !isset($imageId) || !isset($flavorId)){ - $this->app->setOutput("Error", "No, we don't let you create a server without a name OR image ID OR flavor ID."); - return; - } - $opt = array('name' => $name, 'imageId' => $imageId, 'flavorId' => $flavorId); - $server = $this->libClass->createServer($opt); + $name = $this->app->getPostParam("name"); + $imageId = $this->app->getPostParam("imageId"); + $flavorId = $this->app->getPostParam("flavorId"); + if(!isset($name) || !isset($imageId) || !isset($flavorId)){ + $this->app->setOutput("Error", "No, we don't let you create a server without a name OR image ID OR flavor ID."); + return; + } + $opt = array('name' => $name, 'imageId' => $imageId, 'flavorId' => $flavorId); + $server = $this->libClass->createServer($opt); } catch(BadResponseError $e){ $this->app->getErrorInstance()->BadResponseHandler($e); @@ -293,7 +293,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } @@ -305,27 +305,27 @@ class compute public function updateServer() { try{ - $serverId = $this->app->getPostParam("serverId"); - $newName = $this->app->getPostParam("newName"); - $newIpv4 = $this->app->getPostParam("newIpv4"); - $newIpv6 = $this->app->getPostParam("newIpv6"); - if(!isset($serverId)|| !(isset($newName) || isset($newIpv4) || isset($newIpv6)) ){ - $this->app->setOutput("Error", "You'll have to provide server ID and the new attribute(IP(v4/v6)/Name) you desire to update!"); - return; - } - $opt = array('id' => $serverId); - $server = $this->libClass->getServer($opt); - if (isset($newName)){ - if(isset($newIpv4)){ - if(isset($newIpv6)){ - $attr = array('name' => $newName, 'accessIPv4' => $newIPv4, 'accessIPv6' => $newIpv6); - } - else $attr = array('name' => $newName, 'accessIPv4' => $newIPv4); + $serverId = $this->app->getPostParam("serverId"); + $newName = $this->app->getPostParam("newName"); + $newIpv4 = $this->app->getPostParam("newIpv4"); + $newIpv6 = $this->app->getPostParam("newIpv6"); + if(!isset($serverId)|| !(isset($newName) || isset($newIpv4) || isset($newIpv6)) ){ + $this->app->setOutput("Error", "You'll have to provide server ID and the new attribute(IP(v4/v6)/Name) you desire to update!"); + return; } - else $attr = array('name' => $newName); - } - $server->update($attr); - $this->app->setOutput("Success", $serverId." has been updated successfully."); + $opt = array('id' => $serverId); + $server = $this->libClass->getServer($opt); + if (isset($newName)){ + if(isset($newIpv4)){ + if(isset($newIpv6)){ + $attr = array('name' => $newName, 'accessIPv4' => $newIPv4, 'accessIPv6' => $newIpv6); + } + else $attr = array('name' => $newName, 'accessIPv4' => $newIPv4); + } + else $attr = array('name' => $newName); + } + $server->update($attr); + $this->app->setOutput("Success", $serverId." has been updated successfully."); } catch(BadResponseError $e){ $this->app->getErrorInstance()->BadResponseHandler($e); @@ -340,7 +340,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } @@ -351,15 +351,15 @@ class compute public function deleteServer() { try{ - $serverId = $this->app->getPostParam("serverId"); - if(!isset($serverId)){ - $this->app->setOutput("Error", "Server ID is missing, son!"); - return; - } - $opt = array('id' => $serverId); - $server = $this->libClass->getServer($opt); - $server->delete(); - $this->app->setOutput("Success", $serverId." has been deleted successfully."); + $serverId = $this->app->getPostParam("serverId"); + if(!isset($serverId)){ + $this->app->setOutput("Error", "Server ID is missing, son!"); + return; + } + $opt = array('id' => $serverId); + $server = $this->libClass->getServer($opt); + $server->delete(); + $this->app->setOutput("Success", $serverId." has been deleted successfully."); } catch(BadResponseError $e){ $this->app->getErrorInstance()->BadResponseHandler($e); @@ -374,7 +374,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } @@ -385,16 +385,16 @@ class compute public function changePassword() { try{ - $serverId = $this->app->getPostParam("serverId"); - $password = $this->app->getPostParam("newPassword"); - if(!isset($serverId) || !isset($password)){ - $this->app->setOutput("Error", "Server ID or new password missing."); - return; - } - $opt = array('id' => $serverId); - $server = $this->libClass->getServer($opt); - $server->changePassword($password); - $this->app->setOutput("Success", "Password for ".$serverId." has been updated successfully."); + $serverId = $this->app->getPostParam("serverId"); + $password = $this->app->getPostParam("newPassword"); + if(!isset($serverId) || !isset($password)){ + $this->app->setOutput("Error", "Server ID or new password missing."); + return; + } + $opt = array('id' => $serverId); + $server = $this->libClass->getServer($opt); + $server->changePassword($password); + $this->app->setOutput("Success", "Password for ".$serverId." has been updated successfully."); } catch(BadResponseError $e){ $this->app->getErrorInstance()->BadResponseHandler($e); @@ -409,7 +409,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } @@ -420,15 +420,15 @@ class compute public function reboot() { try{ - $serverId = $this->app->getPostParam("serverId"); - if(!isset($serverId)){ - $this->app->setOutput("Error", "Server ID is missing, son!"); - return; - } - $opt = array('id' => $serverId); - $server = $this->libClass->getServer($opt); - $server->reboot(); - $this->app->setOutput("Success", $serverId." has been deleted successfully."); + $serverId = $this->app->getPostParam("serverId"); + if(!isset($serverId)){ + $this->app->setOutput("Error", "Server ID is missing, son!"); + return; + } + $opt = array('id' => $serverId); + $server = $this->libClass->getServer($opt); + $server->reboot(); + $this->app->setOutput("Success", $serverId." has been deleted successfully."); } catch(BadResponseError $e){ $this->app->getErrorInstance()->BadResponseHandler($e); @@ -443,7 +443,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } @@ -454,19 +454,19 @@ class compute public function rebuild() { try{ - $serverId = $this->app->getPostParam("serverId"); - $imageId = $this->app->getPostParam("imageId"); - $newName = $this->app->getPostParam("newName"); - $adminPass = $this->app->getPostParam("adminPass"); - if(!isset($serverId)|| !isset($imageId) || isset($newName) || isset($adminPass)) ){ - $this->app->setOutput("Error", "You'll have to provide server ID and the new image, name and admin password!"); - return; - } - $opt = array('id' => $serverId); - $server = $this->libClass->getServer($opt); - $attr = array('imageId' => $imageId, 'name' => $newName, 'adminPass' => $adminPass); - $server->rebuild($attr); - $this->app->setOutput("Success", $serverId." has been rebuilt successfully with the new image."); + $serverId = $this->app->getPostParam("serverId"); + $imageId = $this->app->getPostParam("imageId"); + $newName = $this->app->getPostParam("newName"); + $adminPass = $this->app->getPostParam("adminPass"); + if(!isset($serverId)|| !isset($imageId) || isset($newName) || isset($adminPass)) ){ + $this->app->setOutput("Error", "You'll have to provide server ID and the new image, name and admin password!"); + return; + } + $opt = array('id' => $serverId); + $server = $this->libClass->getServer($opt); + $attr = array('imageId' => $imageId, 'name' => $newName, 'adminPass' => $adminPass); + $server->rebuild($attr); + $this->app->setOutput("Success", $serverId." has been rebuilt successfully with the new image."); } catch(BadResponseError $e){ $this->app->getErrorInstance()->BadResponseHandler($e); @@ -481,7 +481,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } @@ -493,15 +493,15 @@ class compute public function resize() { try{ - $serverId = $this->app->getPostParam("serverId"); - $newFlavorId = $this->app->getPostParam("newFlavorId"); - if(!isset($serverId)|| !isset($flavorId)){ - $this->app->setOutput("Error", "You'll have to provide server ID and the new flavor ID!"); - return; - } - $opt = array('id' => $serverId); - $server = $this->libClass->getServer($opt); - $server->resize($newFlavorId); + $serverId = $this->app->getPostParam("serverId"); + $newFlavorId = $this->app->getPostParam("newFlavorId"); + if(!isset($serverId)|| !isset($flavorId)){ + $this->app->setOutput("Error", "You'll have to provide server ID and the new flavor ID!"); + return; + } + $opt = array('id' => $serverId); + $server = $this->libClass->getServer($opt); + $server->resize($newFlavorId); } catch(BadResponseError $e){ $this->app->getErrorInstance()->BadResponseHandler($e); @@ -516,7 +516,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } @@ -527,15 +527,15 @@ class compute public function confirmResize() { try{ - $serverId = $this->app->getPostParam("serverId"); - if(!isset($serverId)){ - $this->app->setOutput("Error", "Server ID is missing!"); - return; - } - $opt = array('id' => $serverId); - $server = $this->libClass->getServer($opt); - $server->confirmResize(); - $this->app->setOutput("Success", $serverId." has been resized successfully as the new flavor."); + $serverId = $this->app->getPostParam("serverId"); + if(!isset($serverId)){ + $this->app->setOutput("Error", "Server ID is missing!"); + return; + } + $opt = array('id' => $serverId); + $server = $this->libClass->getServer($opt); + $server->confirmResize(); + $this->app->setOutput("Success", $serverId." has been resized successfully as the new flavor."); } catch(BadResponseError $e){ $this->app->getErrorInstance()->BadResponseHandler($e); @@ -550,7 +550,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } @@ -561,15 +561,15 @@ class compute public function revertResize() { try{ - $serverId = $this->app->getPostParam("serverId"); - if(!isset($serverId)){ - $this->app->setOutput("Error", "Server ID is missing!"); - return; - } - $opt = array('id' => $serverId); - $server = $this->libClass->getServer($opt); - $server->revertResize(); - $this->app->setOutput("Success", $serverId." : resize operation has been reverted to the old flavor."); + $serverId = $this->app->getPostParam("serverId"); + if(!isset($serverId)){ + $this->app->setOutput("Error", "Server ID is missing!"); + return; + } + $opt = array('id' => $serverId); + $server = $this->libClass->getServer($opt); + $server->revertResize(); + $this->app->setOutput("Success", $serverId." : resize operation has been reverted to the old flavor."); } catch(BadResponseError $e){ $this->app->getErrorInstance()->BadResponseHandler($e); @@ -584,22 +584,27 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; } + /** + * List private and public addresses of a server + * @return void + */ + public function listAddresses(array $options = []) { try{ - $serverId = $this->app->getPostParam("serverId"); - if(!isset($serverId)){ - $this->app->setOutput("Error", "Server ID is missing!"); - return; - } - $opt = array('id' => $serverId); - $server = $this->libClass->getServer($opt); - $addresses = $server->listAddresses(); - $this->app->setOutput("Addresses", $addresses); + $serverId = $this->app->getPostParam("serverId"); + if(!isset($serverId)){ + $this->app->setOutput("Error", "Server ID is missing!"); + return; + } + $opt = array('id' => $serverId); + $server = $this->libClass->getServer($opt); + $addresses = $server->listAddresses(); + $this->app->setOutput("Addresses", $addresses); } catch(BadResponseError $e){ $this->app->getErrorInstance()->BadResponseHandler($e); @@ -614,7 +619,7 @@ class compute $this->app->getErrorInstance()->NotImplementedHandler($e); } catch(Exception $e){ - $this->app->getErrorInstance()->OtherException($e); + $this->app->getErrorInstance()->OtherException($e); } return; }