| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Service
- * @subpackage Rackspace
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
- require_once 'Zend/Service/Rackspace/Abstract.php';
- require_once 'Zend/Service/Rackspace/Servers/Server.php';
- require_once 'Zend/Service/Rackspace/Servers/ServerList.php';
- require_once 'Zend/Service/Rackspace/Servers/Image.php';
- require_once 'Zend/Service/Rackspace/Servers/ImageList.php';
- require_once 'Zend/Service/Rackspace/Servers/SharedIpGroup.php';
- require_once 'Zend/Service/Rackspace/Servers/SharedIpGroupList.php';
- require_once 'Zend/Validate/Ip.php';
- class Zend_Service_Rackspace_Servers extends Zend_Service_Rackspace_Abstract
- {
- const LIMIT_FILE_SIZE = 10240;
- const LIMIT_NUM_FILE = 5;
- const ERROR_SERVICE_UNAVAILABLE = 'The service is unavailable';
- const ERROR_UNAUTHORIZED = 'Unauthorized';
- const ERROR_OVERLIMIT = 'You reached the limit of requests, please wait some time before retry';
- const ERROR_PARAM_NO_ID = 'You must specify the item\'s id';
- const ERROR_PARAM_NO_NAME = 'You must specify the name';
- const ERROR_PARAM_NO_SERVERID = 'You must specify the server Id';
- const ERROR_PARAM_NO_IMAGEID = 'You must specify the server\'s image ID';
- const ERROR_PARAM_NO_FLAVORID = 'You must specify the server\'s flavor ID';
- const ERROR_PARAM_NO_ARRAY = 'You must specify an array of parameters';
- const ERROR_PARAM_NO_WEEKLY = 'You must specify a weekly backup schedule';
- const ERROR_PARAM_NO_DAILY = 'You must specify a daily backup schedule';
- const ERROR_ITEM_NOT_FOUND = 'The item specified doesn\'t exist.';
- const ERROR_NO_FILE_EXISTS = 'The file specified doesn\'t exist';
- const ERROR_LIMIT_FILE_SIZE = 'You reached the size length of a file';
- const ERROR_IN_PROGRESS = 'The item specified is still in progress';
- const ERROR_BUILD_IN_PROGRESS = 'The build is still in progress';
- const ERROR_RESIZE_NOT_ALLOWED = 'The resize is not allowed';
- /**
- * Get the list of the servers
- * If $details is true returns detail info
- *
- * @param boolean $details
- * @return Zend_Service_Rackspace_Servers_ServerList|boolean
- */
- public function listServers($details=false)
- {
- $url= '/servers';
- if ($details) {
- $url.= '/detail';
- }
- $result= $this->httpCall($this->getManagementUrl().$url,'GET');
- $status= $result->getStatus();
- switch ($status) {
- case '200' :
- case '203' : // break intentionally omitted
- $servers= json_decode($result->getBody(),true);
- return new Zend_Service_Rackspace_Servers_ServerList($this,$servers['servers']);
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Get the specified server
- *
- * @param string $id
- * @return Zend_Service_Rackspace_Servers_Server
- */
- public function getServer($id)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
- }
- $result= $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id),'GET');
- $status= $result->getStatus();
- switch ($status) {
- case '200' :
- case '203' : // break intentionally omitted
- $server = json_decode($result->getBody(),true);
- return new Zend_Service_Rackspace_Servers_Server($this,$server['server']);
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Create a new server
- *
- * The required parameters are specified in $data (name, imageId, falvorId)
- * The $files is an associative array with 'serverPath' => 'localPath'
- *
- * @param array $data
- * @param array $metadata
- * @param array $files
- * @return Zend_Service_Rackspace_Servers_Server|boolean
- */
- public function createServer(array $data, $metadata=array(),$files=array())
- {
- if (empty($data) || !is_array($data) || !is_array($metadata) || !is_array($files)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ARRAY);
- }
- if (!isset($data['name'])) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME);
- }
- if (!isset($data['flavorId'])) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_FLAVORID);
- }
- if (!isset($data['imageId'])) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_IMAGEID);
- }
- if (count($files)>self::LIMIT_NUM_FILE) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You can attach '.self::LIMIT_NUM_FILE.' files maximum');
- }
- if (!empty($metadata)) {
- $data['metadata']= $metadata;
- }
- $data['flavorId']= (integer) $data['flavorId'];
- $data['imageId']= (integer) $data['imageId'];
- if (!empty($files)) {
- foreach ($files as $serverPath => $filePath) {
- if (!file_exists($filePath)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(
- sprintf("The file %s doesn't exist",$filePath));
- }
- $content= file_get_contents($filePath);
- if (strlen($content) > self::LIMIT_FILE_SIZE) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(
- sprintf("The size of the file %s is greater than the max size of %d bytes",
- $filePath,self::LIMIT_FILE_SIZE));
- }
- $data['personality'][] = array (
- 'path' => $serverPath,
- 'contents' => base64_encode(file_get_contents($filePath))
- );
- }
- }
- $result = $this->httpCall($this->getManagementUrl().'/servers','POST',
- null,null,json_encode(array ('server' => $data)));
- $status = $result->getStatus();
- switch ($status) {
- case '200' :
- case '202' : // break intentionally omitted
- $server = json_decode($result->getBody(),true);
- return new Zend_Service_Rackspace_Servers_Server($this,$server['server']);
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Change the name or the admin password for a server
- *
- * @param string $id
- * @param string $name
- * @param string $password
- * @return boolean
- */
- protected function updateServer($id,$name=null,$password=null)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You must specify the ID of the server');
- }
- if (empty($name) && empty($password)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception("You must specify the new name or password of server");
- }
- $data= array();
- if (!empty($name)) {
- $data['name']= $name;
- }
- if (!empty($password)) {
- $data['adminPass']= $password;
- }
- $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id),'PUT',
- null,null,json_encode(array('server' => $data)));
- $status = $result->getStatus();
- switch ($status) {
- case '204' : // break intentionally omitted
- return true;
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '409' :
- $this->errorMsg= self::ERROR_IN_PROGRESS;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Change the server's name
- *
- * @param string $id
- * @param string $name
- * @return boolean
- */
- public function changeServerName($id,$name)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You must specify the ID of the server');
- }
- if (empty($name)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception("You must specify the new name of the server");
- }
- return $this->updateServer($id, $name);
- }
- /**
- * Change the admin password of the server
- *
- * @param string $id
- * @param string $password
- * @return boolean
- */
- public function changeServerPassword($id,$password)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You must specify the ID of the server');
- }
- if (empty($password)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception("You must specify the new password of the server");
- }
- return $this->updateServer($id, null,$password);
- }
- /**
- * Delete a server
- *
- * @param string $id
- * @return boolean
- */
- public function deleteServer($id)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You must specify the ID of the server');
- }
- $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id),'DELETE');
- $status = $result->getStatus();
- switch ($status) {
- case '202' : // break intentionally omitted
- return true;
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '409' :
- $this->errorMsg= self::ERROR_IN_PROGRESS;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Get the server's IPs (public and private)
- *
- * @param string $id
- * @return array|boolean
- */
- public function getServerIp($id)
- {
- $result= $this->getServer($id);
- if ($result===false) {
- return false;
- }
- $result= $result->toArray();
- return $result['addresses'];
- }
- /**
- * Get the Public IPs of a server
- *
- * @param string $id
- * @return array|boolean
- */
- public function getServerPublicIp($id)
- {
- $addresses= $this->getServerIp($id);
- if ($addresses===false) {
- return false;
- }
- return $addresses['public'];
- }
- /**
- * Get the Private IPs of a server
- *
- * @param string $id
- * @return array|boolean
- */
- public function getServerPrivateIp($id)
- {
- $addresses= $this->getServerIp($id);
- if ($addresses===false) {
- return false;
- }
- return $addresses['private'];
- }
- /**
- * Share an ip address for a server (id)
- *
- * @param string $id server
- * @param string $ip
- * @param string $groupId
- * @return boolean
- */
- public function shareIpAddress($id,$ip,$groupId,$configure=true)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
- }
- if (empty($ip)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You didn\'t specified the IP address to share');
- }
- $validator = new Zend_Validate_Ip();
- if (!$validator->isValid($ip)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception("The parameter $ip specified is not a valid IP address");
- }
- if (empty($groupId)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You didn\'t specified the group id to use');
- }
- $data= array (
- 'sharedIpGroupId' => (integer) $groupId,
- 'configureServer' => $configure
- );
- $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/ips/public/'.rawurlencode($ip),'PUT',
- null,null,json_encode(array('shareIp' => $data)));
- $status = $result->getStatus();
- switch ($status) {
- case '202' : // break intentionally omitted
- return true;
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Unshare IP address for a server ($id)
- *
- * @param string $id
- * @param string $ip
- * @return boolean
- */
- public function unshareIpAddress($id,$ip)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
- }
- if (empty($ip)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You didn\'t specified the IP address to share');
- }
- $validator = new Zend_Validate_Ip();
- if (!$validator->isValid($ip)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception("The parameter $ip specified is not a valid IP address");
- }
- $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/ips/public/'.rawurlencode($ip),
- 'DELETE');
- $status = $result->getStatus();
- switch ($status) {
- case '202' : // break intentionally omitted
- return true;
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Reboot a server
- *
- * $hard true is the equivalent of power cycling the server
- * $hard false is a graceful shutdown
- *
- * @param string $id
- * @param boolean $hard
- * @return boolean
- */
- public function rebootServer($id,$hard=false)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
- }
- if (!$hard) {
- $type= 'SOFT';
- } else {
- $type= 'HARD';
- }
- $data= array (
- 'reboot' => array (
- 'type' => $type
- )
- );
- $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
- 'POST', null, null, json_encode($data));
- $status = $result->getStatus();
- switch ($status) {
- case '200' :
- case '202' : // break intentionally omitted
- return true;
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '409' :
- $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Rebuild a server
- *
- * The rebuild function removes all data on the server and replaces it with the specified image,
- * serverId and IP addresses will remain the same.
- *
- * @param string $id
- * @param string $imageId
- * @return boolean
- */
- public function rebuildServer($id,$imageId)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
- }
- if (empty($imageId)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You didn\'t specified the new imageId of the server');
- }
- $data= array (
- 'rebuild' => array (
- 'imageId' => (integer) $imageId
- )
- );
- $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
- 'POST', null, null, json_encode($data));
- $status = $result->getStatus();
- switch ($status) {
- case '202' : // break intentionally omitted
- return true;
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '409' :
- $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Resize a server
- *
- * The resize function converts an existing server to a different flavor, in essence, scaling the
- * server up or down. The original server is saved for a period of time to allow rollback if there
- * is a problem. All resizes should be tested and explicitly confirmed, at which time the original
- * server is removed. All resizes are automatically confirmed after 24 hours if they are not
- * explicitly confirmed or reverted.
- *
- * @param string $id
- * @param string $flavorId
- * @return boolean
- */
- public function resizeServer($id,$flavorId)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
- }
- if (empty($flavorId)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You didn\'t specified the new flavorId of the server');
- }
- $data= array (
- 'resize' => array (
- 'flavorId' => (integer) $flavorId
- )
- );
- $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
- 'POST', null, null, json_encode($data));
- $status = $result->getStatus();
- switch ($status) {
- case '202' : // break intentionally omitted
- return true;
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '403' :
- $this->errorMsg= self::ERROR_RESIZE_NOT_ALLOWED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '409' :
- $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Confirm resize of a server
- *
- * During a resize operation, the original server is saved for a period of time to allow roll
- * back if there is a problem. Once the newly resized server is tested and has been confirmed
- * to be functioning properly, use this operation to confirm the resize. After confirmation,
- * the original server is removed and cannot be rolled back to. All resizes are automatically
- * confirmed after 24 hours if they are not explicitly confirmed or reverted.
- *
- * @param string $id
- * @return boolean
- */
- public function confirmResizeServer($id)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
- }
- $data= array (
- 'confirmResize' => null
- );
- $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
- 'POST', null, null, json_encode($data));
- $status = $result->getStatus();
- switch ($status) {
- case '204' : // break intentionally omitted
- return true;
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '403' :
- $this->errorMsg= self::ERROR_RESIZE_NOT_ALLOWED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '409' :
- $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Revert resize of a server
- *
- * During a resize operation, the original server is saved for a period of time to allow for roll
- * back if there is a problem. If you determine there is a problem with a newly resized server,
- * use this operation to revert the resize and roll back to the original server. All resizes are
- * automatically confirmed after 24 hours if they have not already been confirmed explicitly or
- * reverted.
- *
- * @param string $id
- * @return boolean
- */
- public function revertResizeServer($id)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
- }
- $data= array (
- 'revertResize' => null
- );
- $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
- 'POST', null, null, json_encode($data));
- $status = $result->getStatus();
- switch ($status) {
- case '202' : // break intentionally omitted
- return true;
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '403' :
- $this->errorMsg= self::ERROR_RESIZE_NOT_ALLOWED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '409' :
- $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Get the list of the flavors
- *
- * If $details is true returns detail info
- *
- * @param boolean $details
- * @return array|boolean
- */
- public function listFlavors($details=false)
- {
- $url= '/flavors';
- if ($details) {
- $url.= '/detail';
- }
- $result= $this->httpCall($this->getManagementUrl().$url,'GET');
- $status= $result->getStatus();
- switch ($status) {
- case '200' :
- case '203' : // break intentionally omitted
- $flavors= json_decode($result->getBody(),true);
- return $flavors['flavors'];
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Get the detail of a flavor
- *
- * @param string $flavorId
- * @return array|boolean
- */
- public function getFlavor($flavorId)
- {
- if (empty($flavorId)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception('You didn\'t specified the new flavorId of the server');
- }
- $result= $this->httpCall($this->getManagementUrl().'/flavors/'.rawurlencode($flavorId),'GET');
- $status= $result->getStatus();
- switch ($status) {
- case '200' :
- case '203' : // break intentionally omitted
- $flavor= json_decode($result->getBody(),true);
- return $flavor['flavor'];
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Get the list of the images
- *
- * @param boolean $details
- * @return Zend_Service_Rackspace_Servers_ImageList|boolean
- */
- public function listImages($details=false)
- {
- $url= '/images';
- if ($details) {
- $url.= '/detail';
- }
- $result= $this->httpCall($this->getManagementUrl().$url,'GET');
- $status= $result->getStatus();
- switch ($status) {
- case '200' :
- case '203' : // break intentionally omitted
- $images= json_decode($result->getBody(),true);
- return new Zend_Service_Rackspace_Servers_ImageList($this,$images['images']);
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Get detail about an image
- *
- * @param string $id
- * @return Zend_Service_Rackspace_Servers_Image|boolean
- */
- public function getImage($id)
- {
- $result= $this->httpCall($this->getManagementUrl().'/images/'.rawurlencode($id),'GET');
- $status= $result->getStatus();
- switch ($status) {
- case '200' :
- case '203' : // break intentionally omitted
- $image= json_decode($result->getBody(),true);
- return new Zend_Service_Rackspace_Servers_Image($this,$image['image']);
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Create an image for a serverId
- *
- * @param string $serverId
- * @param string $name
- * @return Zend_Service_Rackspace_Servers_Image
- */
- public function createImage($serverId,$name)
- {
- if (empty($serverId)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_SERVERID);
- }
- if (empty($name)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME);
- }
- $data = array(
- 'image' => array (
- 'serverId' => (integer) $serverId,
- 'name' => $name
- )
- );
- $result = $this->httpCall($this->getManagementUrl().'/images', 'POST',
- null, null, json_encode($data));
- $status = $result->getStatus();
- switch ($status) {
- case '202' : // break intentionally omitted
- $image= json_decode($result->getBody(),true);
- return new Zend_Service_Rackspace_Servers_Image($this,$image['image']);
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '403' :
- $this->errorMsg= self::ERROR_RESIZE_NOT_ALLOWED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '409' :
- $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Delete an image
- *
- * @param string $id
- * @return boolean
- */
- public function deleteImage($id)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
- }
- $result = $this->httpCall($this->getManagementUrl().'/images/'.rawurlencode($id),'DELETE');
- $status = $result->getStatus();
- switch ($status) {
- case '204' : // break intentionally omitted
- return true;
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Get the backup schedule of a server
- *
- * @param string $id server's Id
- * @return array|boolean
- */
- public function getBackupSchedule($id)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
- }
- $result= $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/backup_schedule',
- 'GET');
- $status= $result->getStatus();
- switch ($status) {
- case '200' :
- case '203' : // break intentionally omitted
- $backup = json_decode($result->getBody(),true);
- return $backup['backupSchedule'];
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Change the backup schedule of a server
- *
- * @param string $id server's Id
- * @param string $weekly
- * @param string $daily
- * @return boolean
- */
- public function changeBackupSchedule($id,$weekly,$daily)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
- }
- if (empty($weekly)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_WEEKLY);
- }
- if (empty($daily)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_DAILY);
- }
- $data = array (
- 'backupSchedule' => array (
- 'enabled' => true,
- 'weekly' => $weekly,
- 'daily' => $daily
- )
- );
- $result= $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/backup_schedule',
- 'POST',null,null,json_encode($data));
- $status= $result->getStatus();
- switch ($status) {
- case '204' : // break intentionally omitted
- return true;
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '409' :
- $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Disable the backup schedule for a server
- *
- * @param string $id server's Id
- * @return boolean
- */
- public function disableBackupSchedule($id)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
- }
- $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/backup_schedule',
- 'DELETE');
- $status = $result->getStatus();
- switch ($status) {
- case '204' : // break intentionally omitted
- return true;
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '409' :
- $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Get the list of shared IP groups
- *
- * @param boolean $details
- * @return Zend_Service_Rackspace_Servers_SharedIpGroupList|boolean
- */
- public function listSharedIpGroups($details=false)
- {
- $url= '/shared_ip_groups';
- if ($details) {
- $url.= '/detail';
- }
- $result= $this->httpCall($this->getManagementUrl().$url,'GET');
- $status= $result->getStatus();
- switch ($status) {
- case '200' :
- case '203' : // break intentionally omitted
- $groups= json_decode($result->getBody(),true);
- return new Zend_Service_Rackspace_Servers_SharedIpGroupList($this,$groups['sharedIpGroups']);
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Get the shared IP group
- *
- * @param integer $id
- * @return Zend_Service_Rackspace_Servers_SharedIpGroup|boolean
- */
- public function getSharedIpGroup($id)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
- }
- $result= $this->httpCall($this->getManagementUrl().'/shared_ip_groups/'.rawurlencode($id),'GET');
- $status= $result->getStatus();
- switch ($status) {
- case '200' :
- case '203' : // break intentionally omitted
- $group= json_decode($result->getBody(),true);
- return new Zend_Service_Rackspace_Servers_SharedIpGroup($this,$group['sharedIpGroup']);
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Create a shared Ip group
- *
- * @param string $name
- * @param string $serverId
- * @return array|boolean
- */
- public function createSharedIpGroup($name,$serverId)
- {
- if (empty($name)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME);
- }
- if (empty($serverId)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
- }
- $data = array (
- 'sharedIpGroup' => array (
- 'name' => $name,
- 'server' => (integer) $serverId
- )
- );
- $result= $this->httpCall($this->getManagementUrl().'/shared_ip_groups',
- 'POST',null,null,json_encode($data));
- $status= $result->getStatus();
- switch ($status) {
- case '201' : // break intentionally omitted
- $group = json_decode($result->getBody(),true);
- return new Zend_Service_Rackspace_Servers_SharedIpGroup($this,$group['sharedIpGroup']);
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- /**
- * Delete a Shared Ip Group
- *
- * @param integer $id
- * @return boolean
- */
- public function deleteSharedIpGroup($id)
- {
- if (empty($id)) {
- require_once 'Zend/Service/Rackspace/Exception.php';
- throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
- }
- $result= $this->httpCall($this->getManagementUrl().'/shared_ip_groups/'.rawurlencode($id),'DELETE');
- $status= $result->getStatus();
- switch ($status) {
- case '204' : // break intentionally omitted
- return true;
- case '503' :
- $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
- break;
- case '401' :
- $this->errorMsg= self::ERROR_UNAUTHORIZED;
- break;
- case '404' :
- $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
- break;
- case '413' :
- $this->errorMsg= self::ERROR_OVERLIMIT;
- break;
- default:
- $this->errorMsg= $result->getBody();
- break;
- }
- $this->errorCode= $status;
- return false;
- }
- }
|