File indexing completed on 2024-06-16 05:30:25

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service
0017  * @subpackage Rackspace
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  */
0021 
0022 // require_once 'Zend/Service/Rackspace/Abstract.php';
0023 // require_once 'Zend/Service/Rackspace/Servers/Server.php';
0024 // require_once 'Zend/Service/Rackspace/Servers/ServerList.php';
0025 // require_once 'Zend/Service/Rackspace/Servers/Image.php';
0026 // require_once 'Zend/Service/Rackspace/Servers/ImageList.php';
0027 // require_once 'Zend/Service/Rackspace/Servers/SharedIpGroup.php';
0028 // require_once 'Zend/Service/Rackspace/Servers/SharedIpGroupList.php';
0029 // require_once 'Zend/Validate/Ip.php';
0030 
0031 class Zend_Service_Rackspace_Servers extends Zend_Service_Rackspace_Abstract
0032 {
0033     const LIMIT_FILE_SIZE           = 10240;
0034     const LIMIT_NUM_FILE            = 5;
0035     const ERROR_SERVICE_UNAVAILABLE = 'The service is unavailable';
0036     const ERROR_UNAUTHORIZED        = 'Unauthorized';
0037     const ERROR_OVERLIMIT           = 'You reached the limit of requests, please wait some time before retry';
0038     const ERROR_PARAM_NO_ID         = 'You must specify the item\'s id';
0039     const ERROR_PARAM_NO_NAME       = 'You must specify the name';
0040     const ERROR_PARAM_NO_SERVERID   = 'You must specify the server Id';
0041     const ERROR_PARAM_NO_IMAGEID    = 'You must specify the server\'s image ID';
0042     const ERROR_PARAM_NO_FLAVORID   = 'You must specify the server\'s flavor ID';
0043     const ERROR_PARAM_NO_ARRAY      = 'You must specify an array of parameters';
0044     const ERROR_PARAM_NO_WEEKLY     = 'You must specify a weekly backup schedule';
0045     const ERROR_PARAM_NO_DAILY      = 'You must specify a daily backup schedule';
0046     const ERROR_ITEM_NOT_FOUND      = 'The item specified doesn\'t exist.';
0047     const ERROR_NO_FILE_EXISTS      = 'The file specified doesn\'t exist';
0048     const ERROR_LIMIT_FILE_SIZE     = 'You reached the size length of a file';
0049     const ERROR_IN_PROGRESS         = 'The item specified is still in progress';
0050     const ERROR_BUILD_IN_PROGRESS   = 'The build is still in progress';
0051     const ERROR_RESIZE_NOT_ALLOWED  = 'The resize is not allowed';
0052     /**
0053      * Get the list of the servers
0054      * If $details is true returns detail info
0055      * 
0056      * @param  boolean $details
0057      * @return Zend_Service_Rackspace_Servers_ServerList|boolean
0058      */
0059     public function listServers($details=false)
0060     {
0061         $url= '/servers';
0062         if ($details) {
0063             $url.= '/detail';
0064         } 
0065         $result= $this->httpCall($this->getManagementUrl().$url,'GET');
0066         $status= $result->getStatus();
0067         switch ($status) {
0068             case '200' : 
0069             case '203' : // break intentionally omitted   
0070                 $servers= json_decode($result->getBody(),true);
0071                 return new Zend_Service_Rackspace_Servers_ServerList($this,$servers['servers']);
0072             case '503' :
0073                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0074                 break;
0075             case '401' :
0076                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0077                 break;
0078             case '413' :
0079                 $this->errorMsg= self::ERROR_OVERLIMIT;
0080                 break;
0081             default:
0082                 $this->errorMsg= $result->getBody();
0083                 break;
0084         }
0085         $this->errorCode= $status;
0086         return false;
0087     }
0088     /**
0089      * Get the specified server
0090      * 
0091      * @param  string $id 
0092      * @return Zend_Service_Rackspace_Servers_Server
0093      */
0094     public function getServer($id) 
0095     {
0096         if (empty($id)) {
0097             // require_once 'Zend/Service/Rackspace/Exception.php';
0098             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
0099         }
0100         $result= $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id),'GET');
0101         $status= $result->getStatus();
0102         switch ($status) {
0103             case '200' : 
0104             case '203' : // break intentionally omitted   
0105                 $server = json_decode($result->getBody(),true);
0106                 return new Zend_Service_Rackspace_Servers_Server($this,$server['server']);
0107             case '503' :
0108                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0109                 break;
0110             case '401' :
0111                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0112                 break;
0113             case '404' :
0114                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
0115                 break;
0116             case '413' :
0117                 $this->errorMsg= self::ERROR_OVERLIMIT;
0118                 break;
0119             default:
0120                 $this->errorMsg= $result->getBody();
0121                 break;
0122         }
0123         $this->errorCode= $status;
0124         return false;
0125     }
0126     /**
0127      * Create a new server
0128      * 
0129      * The required parameters are specified in $data (name, imageId, falvorId)
0130      * The $files is an associative array with 'serverPath' => 'localPath'
0131      * 
0132      * @param  array $data 
0133      * @param  array $metadata
0134      * @param  array $files
0135      * @return Zend_Service_Rackspace_Servers_Server|boolean
0136      */
0137     public function createServer(array $data, $metadata=array(),$files=array())
0138     {
0139         if (empty($data) || !is_array($data) || !is_array($metadata) || !is_array($files)) {
0140             // require_once 'Zend/Service/Rackspace/Exception.php';
0141             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ARRAY);
0142         }
0143         if (!isset($data['name'])) {
0144             // require_once 'Zend/Service/Rackspace/Exception.php';
0145             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME);
0146         }
0147         if (!isset($data['flavorId'])) {
0148             // require_once 'Zend/Service/Rackspace/Exception.php';
0149             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_FLAVORID);
0150         }
0151         if (!isset($data['imageId'])) {
0152             // require_once 'Zend/Service/Rackspace/Exception.php';
0153             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_IMAGEID);
0154         }
0155         if (count($files)>self::LIMIT_NUM_FILE) {
0156             // require_once 'Zend/Service/Rackspace/Exception.php';
0157             throw new Zend_Service_Rackspace_Exception('You can attach '.self::LIMIT_NUM_FILE.' files maximum');
0158         }
0159         if (!empty($metadata)) {
0160             $data['metadata']= $metadata;
0161         }
0162         $data['flavorId']= (integer) $data['flavorId'];
0163         $data['imageId']= (integer) $data['imageId'];
0164         if (!empty($files)) {
0165             foreach ($files as $serverPath => $filePath) {
0166                 if (!file_exists($filePath)) {
0167                     // require_once 'Zend/Service/Rackspace/Exception.php';
0168                     throw new Zend_Service_Rackspace_Exception(
0169                             sprintf("The file %s doesn't exist",$filePath));
0170                 }
0171                 $content= file_get_contents($filePath);
0172                 if (strlen($content) > self::LIMIT_FILE_SIZE) {
0173                     // require_once 'Zend/Service/Rackspace/Exception.php';
0174                     throw new Zend_Service_Rackspace_Exception(
0175                             sprintf("The size of the file %s is greater than the max size of %d bytes",
0176                                     $filePath,self::LIMIT_FILE_SIZE));
0177                 }
0178                 $data['personality'][] = array (
0179                     'path'     => $serverPath,
0180                     'contents' => base64_encode(file_get_contents($filePath))
0181                 );
0182             }
0183         }
0184         $result = $this->httpCall($this->getManagementUrl().'/servers','POST',
0185                 null,null,json_encode(array ('server' => $data)));
0186         $status = $result->getStatus();
0187         switch ($status) {
0188             case '200' :
0189             case '202' : // break intentionally omitted   
0190                 $server = json_decode($result->getBody(),true);
0191                 return new Zend_Service_Rackspace_Servers_Server($this,$server['server']);
0192             case '503' :
0193                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0194                 break;
0195             case '401' :
0196                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0197                 break;
0198             case '404' :
0199                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
0200                 break;
0201             case '413' :
0202                 $this->errorMsg= self::ERROR_OVERLIMIT;
0203                 break;
0204             default:
0205                 $this->errorMsg= $result->getBody();
0206                 break;
0207         }
0208         $this->errorCode= $status;
0209         return false;
0210     }
0211     /**
0212      * Change the name or the admin password for a server
0213      * 
0214      * @param  string $id
0215      * @param  string $name
0216      * @param  string $password
0217      * @return boolean 
0218      */
0219     protected function updateServer($id,$name=null,$password=null)
0220     {
0221         if (empty($id)) {
0222             // require_once 'Zend/Service/Rackspace/Exception.php';
0223             throw new Zend_Service_Rackspace_Exception('You must specify the ID of the server');
0224         }
0225         if (empty($name) && empty($password)) {
0226             // require_once 'Zend/Service/Rackspace/Exception.php';
0227             throw new Zend_Service_Rackspace_Exception("You must specify the new name or password of server");
0228         }
0229         $data= array();
0230         if (!empty($name)) {
0231             $data['name']= $name;
0232         }
0233         if (!empty($password)) {
0234             $data['adminPass']= $password;
0235         }
0236         $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id),'PUT',
0237                 null,null,json_encode(array('server' => $data)));
0238         $status = $result->getStatus();
0239         switch ($status) {
0240             case '204' : // break intentionally omitted   
0241                 return true;
0242             case '503' :
0243                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0244                 break;
0245             case '401' :
0246                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0247                 break;
0248             case '404' :
0249                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
0250                 break;
0251             case '409' :
0252                 $this->errorMsg= self::ERROR_IN_PROGRESS;
0253                 break;
0254             case '413' :
0255                 $this->errorMsg= self::ERROR_OVERLIMIT;
0256                 break;
0257             default:
0258                 $this->errorMsg= $result->getBody();
0259                 break;
0260         }
0261         $this->errorCode= $status;
0262         return false;
0263     }
0264     /**
0265      * Change the server's name
0266      * 
0267      * @param  string $id
0268      * @param  string $name
0269      * @return boolean 
0270      */
0271     public function changeServerName($id,$name)
0272     {
0273         if (empty($id)) {
0274             // require_once 'Zend/Service/Rackspace/Exception.php';
0275             throw new Zend_Service_Rackspace_Exception('You must specify the ID of the server');
0276         }
0277         if (empty($name)) {
0278             // require_once 'Zend/Service/Rackspace/Exception.php';
0279             throw new Zend_Service_Rackspace_Exception("You must specify the new name of the server");
0280         }
0281         return $this->updateServer($id, $name);
0282     }
0283     /**
0284      * Change the admin password of the server
0285      * 
0286      * @param  string $id
0287      * @param  string $password
0288      * @return boolean 
0289      */
0290     public function changeServerPassword($id,$password)
0291     {
0292         if (empty($id)) {
0293             // require_once 'Zend/Service/Rackspace/Exception.php';
0294             throw new Zend_Service_Rackspace_Exception('You must specify the ID of the server');
0295         }
0296         if (empty($password)) {
0297             // require_once 'Zend/Service/Rackspace/Exception.php';
0298             throw new Zend_Service_Rackspace_Exception("You must specify the new password of the server");
0299         }
0300         return $this->updateServer($id, null,$password);
0301     }
0302     /**
0303      * Delete a server
0304      * 
0305      * @param  string $id
0306      * @return boolean 
0307      */
0308     public function deleteServer($id)
0309     {
0310         if (empty($id)) {
0311             // require_once 'Zend/Service/Rackspace/Exception.php';
0312             throw new Zend_Service_Rackspace_Exception('You must specify the ID of the server');
0313         }
0314         $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id),'DELETE');
0315         $status = $result->getStatus();
0316         switch ($status) {
0317             case '202' : // break intentionally omitted   
0318                 return true;
0319             case '503' :
0320                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0321                 break;
0322             case '401' :
0323                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0324                 break;
0325             case '404' :
0326                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
0327                 break;
0328             case '409' :
0329                 $this->errorMsg= self::ERROR_IN_PROGRESS;
0330                 break;
0331             case '413' :
0332                 $this->errorMsg= self::ERROR_OVERLIMIT;
0333                 break;
0334             default:
0335                 $this->errorMsg= $result->getBody();
0336                 break;
0337         }
0338         $this->errorCode= $status;
0339         return false;
0340     }
0341     /**
0342      * Get the server's IPs (public and private)
0343      * 
0344      * @param  string $id
0345      * @return array|boolean 
0346      */
0347     public function getServerIp($id)
0348     {
0349         $result= $this->getServer($id);
0350         if ($result===false) {
0351             return false;
0352         }
0353         $result= $result->toArray();
0354         return $result['addresses'];
0355     }
0356     /**
0357      * Get the Public IPs of a server
0358      * 
0359      * @param  string $id
0360      * @return array|boolean
0361      */
0362     public function getServerPublicIp($id)
0363     {
0364         $addresses= $this->getServerIp($id);
0365         if ($addresses===false) {
0366             return false;
0367         }
0368         return $addresses['public'];
0369     }
0370     /**
0371      * Get the Private IPs of a server
0372      * 
0373      * @param  string $id
0374      * @return array|boolean
0375      */
0376     public function getServerPrivateIp($id)
0377     {
0378         $addresses= $this->getServerIp($id);
0379         if ($addresses===false) {
0380             return false;
0381         }
0382         return $addresses['private'];
0383     }
0384     /**
0385      * Share an ip address for a server (id)
0386      * 
0387      * @param  string $id server
0388      * @param  string $ip 
0389      * @param  string $groupId
0390      * @return boolean
0391      */
0392     public function shareIpAddress($id,$ip,$groupId,$configure=true)
0393     {
0394         if (empty($id)) {
0395             // require_once 'Zend/Service/Rackspace/Exception.php';
0396             throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
0397         }
0398         if (empty($ip)) {
0399             // require_once 'Zend/Service/Rackspace/Exception.php';
0400             throw new Zend_Service_Rackspace_Exception('You didn\'t specified the IP address to share');
0401         }
0402         $validator = new Zend_Validate_Ip();
0403         if (!$validator->isValid($ip)) {
0404             // require_once 'Zend/Service/Rackspace/Exception.php';
0405             throw new Zend_Service_Rackspace_Exception("The parameter $ip specified is not a valid IP address");
0406         }
0407         if (empty($groupId)) {
0408             // require_once 'Zend/Service/Rackspace/Exception.php';
0409             throw new Zend_Service_Rackspace_Exception('You didn\'t specified the group id to use');
0410         }
0411         $data= array (
0412             'sharedIpGroupId' => (integer) $groupId,
0413             'configureServer' => $configure
0414         );
0415         $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/ips/public/'.rawurlencode($ip),'PUT',
0416                 null,null,json_encode(array('shareIp' => $data)));
0417         $status = $result->getStatus();
0418         switch ($status) {
0419             case '202' : // break intentionally omitted   
0420                 return true;
0421             case '503' :
0422                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0423                 break;
0424             case '401' :
0425                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0426                 break;
0427             case '404' :
0428                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
0429                 break;
0430             case '413' :
0431                 $this->errorMsg= self::ERROR_OVERLIMIT;
0432                 break;
0433             default:
0434                 $this->errorMsg= $result->getBody();
0435                 break;
0436         }
0437         $this->errorCode= $status;
0438         return false;
0439     }
0440     /**
0441      * Unshare IP address for a server ($id)
0442      * 
0443      * @param  string $id
0444      * @param  string $ip
0445      * @return boolean 
0446      */
0447     public function unshareIpAddress($id,$ip)
0448     {
0449         if (empty($id)) {
0450             // require_once 'Zend/Service/Rackspace/Exception.php';
0451             throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
0452         }
0453         if (empty($ip)) {
0454             // require_once 'Zend/Service/Rackspace/Exception.php';
0455             throw new Zend_Service_Rackspace_Exception('You didn\'t specified the IP address to share');
0456         }
0457         $validator = new Zend_Validate_Ip();
0458         if (!$validator->isValid($ip)) {
0459             // require_once 'Zend/Service/Rackspace/Exception.php';
0460             throw new Zend_Service_Rackspace_Exception("The parameter $ip specified is not a valid IP address");
0461         }
0462         $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/ips/public/'.rawurlencode($ip),
0463                 'DELETE');
0464         $status = $result->getStatus();
0465         switch ($status) {
0466             case '202' : // break intentionally omitted   
0467                 return true;
0468             case '503' :
0469                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0470                 break;
0471             case '401' :
0472                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0473                 break;
0474             case '404' :
0475                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
0476                 break;
0477             case '413' :
0478                 $this->errorMsg= self::ERROR_OVERLIMIT;
0479                 break;
0480             default:
0481                 $this->errorMsg= $result->getBody();
0482                 break;
0483         }
0484         $this->errorCode= $status;
0485         return false;
0486     }
0487     /**
0488      * Reboot a server
0489      * 
0490      * $hard true is the equivalent of power cycling the server
0491      * $hard false is a graceful shutdown
0492      * 
0493      * @param  string $id
0494      * @param  boolean $hard 
0495      * @return boolean
0496      */
0497     public function rebootServer($id,$hard=false)
0498     {
0499         if (empty($id)) {
0500             // require_once 'Zend/Service/Rackspace/Exception.php';
0501             throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
0502         }
0503         if (!$hard) {
0504             $type= 'SOFT';
0505         } else {
0506             $type= 'HARD';
0507         }
0508         $data= array (
0509             'reboot' => array (
0510                 'type' => $type
0511             )
0512         );
0513         $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
0514                                   'POST', null, null, json_encode($data));
0515         $status = $result->getStatus();
0516         switch ($status) {
0517             case '200' :
0518             case '202' : // break intentionally omitted   
0519                 return true;
0520             case '503' :
0521                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0522                 break;
0523             case '401' :
0524                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0525                 break;
0526             case '404' :
0527                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
0528                 break;
0529             case '409' :
0530                 $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
0531                 break;
0532             case '413' :
0533                 $this->errorMsg= self::ERROR_OVERLIMIT;
0534                 break;
0535             default:
0536                 $this->errorMsg= $result->getBody();
0537                 break;
0538         }
0539         $this->errorCode= $status;
0540         return false;
0541     }
0542     /**
0543      * Rebuild a server
0544      * 
0545      * The rebuild function removes all data on the server and replaces it with the specified image,
0546      * serverId and IP addresses will remain the same.
0547      * 
0548      * @param  string $id
0549      * @param  string $imageId
0550      * @return boolean
0551      */
0552     public function rebuildServer($id,$imageId)
0553     {
0554         if (empty($id)) {
0555             // require_once 'Zend/Service/Rackspace/Exception.php';
0556             throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
0557         }
0558         if (empty($imageId)) {
0559             // require_once 'Zend/Service/Rackspace/Exception.php';
0560             throw new Zend_Service_Rackspace_Exception('You didn\'t specified the new imageId of the server');
0561         }
0562         $data= array (
0563             'rebuild' => array (
0564                 'imageId' => (integer) $imageId
0565             )
0566         );
0567         $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
0568                                   'POST', null, null, json_encode($data));
0569         $status = $result->getStatus();
0570         switch ($status) {
0571             case '202' : // break intentionally omitted   
0572                 return true;
0573             case '503' :
0574                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0575                 break;
0576             case '401' :
0577                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0578                 break;
0579             case '404' :
0580                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
0581                 break;
0582             case '409' :
0583                 $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
0584                 break;
0585             case '413' :
0586                 $this->errorMsg= self::ERROR_OVERLIMIT;
0587                 break;
0588             default:
0589                 $this->errorMsg= $result->getBody();
0590                 break;
0591         }
0592         $this->errorCode= $status;
0593         return false;
0594     }
0595     /**
0596      * Resize a server
0597      * 
0598      * The resize function converts an existing server to a different flavor, in essence, scaling the
0599      * server up or down. The original server is saved for a period of time to allow rollback if there
0600      * is a problem. All resizes should be tested and explicitly confirmed, at which time the original
0601      * server is removed. All resizes are automatically confirmed after 24 hours if they are not
0602      * explicitly confirmed or reverted.
0603      * 
0604      * @param  string $id
0605      * @param  string $flavorId
0606      * @return boolean
0607      */
0608     public function resizeServer($id,$flavorId)
0609     {
0610         if (empty($id)) {
0611             // require_once 'Zend/Service/Rackspace/Exception.php';
0612             throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
0613         }
0614         if (empty($flavorId)) {
0615             // require_once 'Zend/Service/Rackspace/Exception.php';
0616             throw new Zend_Service_Rackspace_Exception('You didn\'t specified the new flavorId of the server');
0617         }
0618         $data= array (
0619             'resize' => array (
0620                 'flavorId' => (integer) $flavorId
0621             )
0622         );
0623         $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
0624                                   'POST', null, null, json_encode($data));
0625         $status = $result->getStatus();
0626         switch ($status) {
0627             case '202' : // break intentionally omitted   
0628                 return true;
0629             case '503' :
0630                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0631                 break;
0632             case '401' :
0633                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0634                 break;
0635             case '403' :
0636                 $this->errorMsg= self::ERROR_RESIZE_NOT_ALLOWED;
0637                 break;
0638             case '404' :
0639                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
0640                 break;
0641             case '409' :
0642                 $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
0643                 break;
0644             case '413' :
0645                 $this->errorMsg= self::ERROR_OVERLIMIT;
0646                 break;
0647             default:
0648                 $this->errorMsg= $result->getBody();
0649                 break;
0650         }
0651         $this->errorCode= $status;
0652         return false;
0653     }
0654     /**
0655      * Confirm resize of a server
0656      * 
0657      * During a resize operation, the original server is saved for a period of time to allow roll
0658      * back if there is a problem. Once the newly resized server is tested and has been confirmed
0659      * to be functioning properly, use this operation to confirm the resize. After confirmation,
0660      * the original server is removed and cannot be rolled back to. All resizes are automatically
0661      * confirmed after 24 hours if they are not explicitly confirmed or reverted.
0662      *
0663      * @param  string $id
0664      * @return boolean 
0665      */
0666     public function confirmResizeServer($id)
0667     {
0668         if (empty($id)) {
0669             // require_once 'Zend/Service/Rackspace/Exception.php';
0670             throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
0671         }
0672         $data= array (
0673             'confirmResize' => null
0674         );
0675         $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
0676                                   'POST', null, null, json_encode($data));
0677         $status = $result->getStatus();
0678         switch ($status) {
0679             case '204' : // break intentionally omitted   
0680                 return true;
0681             case '503' :
0682                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0683                 break;
0684             case '401' :
0685                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0686                 break;
0687             case '403' :
0688                 $this->errorMsg= self::ERROR_RESIZE_NOT_ALLOWED;
0689                 break;
0690             case '404' :
0691                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
0692                 break;
0693             case '409' :
0694                 $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
0695                 break;
0696             case '413' :
0697                 $this->errorMsg= self::ERROR_OVERLIMIT;
0698                 break;
0699             default:
0700                 $this->errorMsg= $result->getBody();
0701                 break;
0702         }
0703         $this->errorCode= $status;
0704         return false;
0705     }
0706     /**
0707      * Revert resize of a server
0708      * 
0709      * During a resize operation, the original server is saved for a period of time to allow for roll
0710      * back if there is a problem. If you determine there is a problem with a newly resized server,
0711      * use this operation to revert the resize and roll back to the original server. All resizes are
0712      * automatically confirmed after 24 hours if they have not already been confirmed explicitly or
0713      * reverted.
0714      *
0715      * @param  string $id
0716      * @return boolean 
0717      */
0718     public function revertResizeServer($id)
0719     {
0720         if (empty($id)) {
0721             // require_once 'Zend/Service/Rackspace/Exception.php';
0722             throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
0723         }
0724         $data= array (
0725             'revertResize' => null
0726         );
0727         $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
0728                                   'POST', null, null, json_encode($data));
0729         $status = $result->getStatus();
0730         switch ($status) {
0731             case '202' : // break intentionally omitted   
0732                 return true;
0733             case '503' :
0734                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0735                 break;
0736             case '401' :
0737                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0738                 break;
0739             case '403' :
0740                 $this->errorMsg= self::ERROR_RESIZE_NOT_ALLOWED;
0741                 break;
0742             case '404' :
0743                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
0744                 break;
0745             case '409' :
0746                 $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
0747                 break;
0748             case '413' :
0749                 $this->errorMsg= self::ERROR_OVERLIMIT;
0750                 break;
0751             default:
0752                 $this->errorMsg= $result->getBody();
0753                 break;
0754         }
0755         $this->errorCode= $status;
0756         return false;
0757     }
0758     /**
0759      * Get the list of the flavors
0760      * 
0761      * If $details is true returns detail info
0762      * 
0763      * @param  boolean $details
0764      * @return array|boolean
0765      */
0766     public function listFlavors($details=false)
0767     {
0768         $url= '/flavors';
0769         if ($details) {
0770             $url.= '/detail';
0771         } 
0772         $result= $this->httpCall($this->getManagementUrl().$url,'GET');
0773         $status= $result->getStatus();
0774         switch ($status) {
0775             case '200' : 
0776             case '203' : // break intentionally omitted   
0777                 $flavors= json_decode($result->getBody(),true);
0778                 return $flavors['flavors'];
0779             case '503' :
0780                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0781                 break;
0782             case '401' :
0783                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0784                 break;
0785             case '413' :
0786                 $this->errorMsg= self::ERROR_OVERLIMIT;
0787                 break;
0788             default:
0789                 $this->errorMsg= $result->getBody();
0790                 break;
0791         }
0792         $this->errorCode= $status;
0793         return false;
0794     }
0795     /**
0796      * Get the detail of a flavor
0797      * 
0798      * @param  string $flavorId
0799      * @return array|boolean
0800      */
0801     public function getFlavor($flavorId)
0802     {
0803         if (empty($flavorId)) {
0804             // require_once 'Zend/Service/Rackspace/Exception.php';
0805             throw new Zend_Service_Rackspace_Exception('You didn\'t specified the new flavorId of the server');
0806         }
0807         $result= $this->httpCall($this->getManagementUrl().'/flavors/'.rawurlencode($flavorId),'GET');
0808         $status= $result->getStatus();
0809         switch ($status) {
0810             case '200' : 
0811             case '203' : // break intentionally omitted   
0812                 $flavor= json_decode($result->getBody(),true);
0813                 return $flavor['flavor'];
0814             case '503' :
0815                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0816                 break;
0817             case '401' :
0818                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0819                 break;
0820             case '413' :
0821                 $this->errorMsg= self::ERROR_OVERLIMIT;
0822                 break;
0823             default:
0824                 $this->errorMsg= $result->getBody();
0825                 break;
0826         }
0827         $this->errorCode= $status;
0828         return false;
0829     }
0830     /**
0831      * Get the list of the images
0832      * 
0833      * @param  boolean $details
0834      * @return Zend_Service_Rackspace_Servers_ImageList|boolean 
0835      */
0836     public function listImages($details=false)
0837     {
0838         $url= '/images';
0839         if ($details) {
0840             $url.= '/detail';
0841         } 
0842         $result= $this->httpCall($this->getManagementUrl().$url,'GET');
0843         $status= $result->getStatus();
0844         switch ($status) {
0845             case '200' : 
0846             case '203' : // break intentionally omitted   
0847                 $images= json_decode($result->getBody(),true);
0848                 return new Zend_Service_Rackspace_Servers_ImageList($this,$images['images']);
0849             case '503' :
0850                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0851                 break;
0852             case '401' :
0853                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0854                 break;
0855             case '413' :
0856                 $this->errorMsg= self::ERROR_OVERLIMIT;
0857                 break;
0858             default:
0859                 $this->errorMsg= $result->getBody();
0860                 break;
0861         }
0862         $this->errorCode= $status;
0863         return false;
0864     }
0865     /**
0866      * Get detail about an image
0867      * 
0868      * @param  string $id
0869      * @return Zend_Service_Rackspace_Servers_Image|boolean
0870      */
0871     public function getImage($id)
0872     {
0873         $result= $this->httpCall($this->getManagementUrl().'/images/'.rawurlencode($id),'GET');
0874         $status= $result->getStatus();
0875         switch ($status) {
0876             case '200' : 
0877             case '203' : // break intentionally omitted   
0878                 $image= json_decode($result->getBody(),true);
0879                 return new Zend_Service_Rackspace_Servers_Image($this,$image['image']);
0880             case '503' :
0881                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0882                 break;
0883             case '401' :
0884                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0885                 break;
0886             case '404' :
0887                  $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
0888                 break;
0889             case '413' :
0890                 $this->errorMsg= self::ERROR_OVERLIMIT;
0891                 break;
0892             default:
0893                 $this->errorMsg= $result->getBody();
0894                 break;
0895         }
0896         $this->errorCode= $status;
0897         return false;
0898     }
0899     /**
0900      * Create an image for a serverId
0901      * 
0902      * @param  string $serverId
0903      * @param  string $name 
0904      * @return Zend_Service_Rackspace_Servers_Image
0905      */
0906     public function createImage($serverId,$name)
0907     {
0908         if (empty($serverId)) {
0909             // require_once 'Zend/Service/Rackspace/Exception.php';
0910             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_SERVERID);
0911         }
0912         if (empty($name)) {
0913             // require_once 'Zend/Service/Rackspace/Exception.php';
0914             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME);
0915         }
0916         $data = array(
0917             'image' => array (
0918                 'serverId' => (integer) $serverId,
0919                 'name'     => $name
0920             )
0921         );
0922         $result = $this->httpCall($this->getManagementUrl().'/images', 'POST',
0923                                   null, null, json_encode($data));
0924         $status = $result->getStatus();
0925         switch ($status) {
0926             case '202' : // break intentionally omitted   
0927                 $image= json_decode($result->getBody(),true);
0928                 return new Zend_Service_Rackspace_Servers_Image($this,$image['image']);
0929             case '503' :
0930                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0931                 break;
0932             case '401' :
0933                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0934                 break;
0935             case '403' :
0936                 $this->errorMsg= self::ERROR_RESIZE_NOT_ALLOWED;
0937                 break;
0938             case '404' :
0939                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
0940                 break;
0941             case '409' :
0942                 $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
0943                 break;
0944             case '413' :
0945                 $this->errorMsg= self::ERROR_OVERLIMIT;
0946                 break;
0947             default:
0948                 $this->errorMsg= $result->getBody();
0949                 break;
0950         }
0951         $this->errorCode= $status;
0952         return false;
0953     }
0954     /**
0955      * Delete an image
0956      * 
0957      * @param  string $id
0958      * @return boolean 
0959      */
0960     public function deleteImage($id)
0961     {
0962         if (empty($id)) {
0963             // require_once 'Zend/Service/Rackspace/Exception.php';
0964             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
0965         }
0966         $result = $this->httpCall($this->getManagementUrl().'/images/'.rawurlencode($id),'DELETE');
0967         $status = $result->getStatus();
0968         switch ($status) {
0969             case '204' : // break intentionally omitted   
0970                 return true;
0971             case '503' :
0972                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
0973                 break;
0974             case '401' :
0975                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
0976                 break;
0977             case '404' :
0978                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
0979                 break;
0980             case '413' :
0981                 $this->errorMsg= self::ERROR_OVERLIMIT;
0982                 break;
0983             default:
0984                 $this->errorMsg= $result->getBody();
0985                 break;
0986         }
0987         $this->errorCode= $status;
0988         return false;
0989     }
0990     /**
0991      * Get the backup schedule of a server
0992      * 
0993      * @param  string $id server's Id
0994      * @return array|boolean 
0995      */
0996     public function getBackupSchedule($id)
0997     {
0998         if (empty($id)) {
0999             // require_once 'Zend/Service/Rackspace/Exception.php';
1000             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
1001         }
1002         $result= $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/backup_schedule',
1003                                  'GET');
1004         $status= $result->getStatus();
1005         switch ($status) {
1006             case '200' : 
1007             case '203' : // break intentionally omitted   
1008                 $backup = json_decode($result->getBody(),true);
1009                 return $backup['backupSchedule'];
1010             case '503' :
1011                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
1012                 break;
1013             case '401' :
1014                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
1015                 break;
1016             case '404' :
1017                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
1018                 break;
1019             case '413' :
1020                 $this->errorMsg= self::ERROR_OVERLIMIT;
1021                 break;
1022             default:
1023                 $this->errorMsg= $result->getBody();
1024                 break;
1025         }
1026         $this->errorCode= $status;
1027         return false;
1028     }
1029     /**
1030      * Change the backup schedule of a server
1031      * 
1032      * @param  string $id server's Id
1033      * @param  string $weekly
1034      * @param  string $daily
1035      * @return boolean 
1036      */
1037     public function changeBackupSchedule($id,$weekly,$daily)
1038     {
1039         if (empty($id)) {
1040             // require_once 'Zend/Service/Rackspace/Exception.php';
1041             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
1042         }
1043         if (empty($weekly)) {
1044             // require_once 'Zend/Service/Rackspace/Exception.php';
1045             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_WEEKLY);
1046         }
1047         if (empty($daily)) {
1048             // require_once 'Zend/Service/Rackspace/Exception.php';
1049             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_DAILY);
1050         }
1051         $data = array (
1052             'backupSchedule' => array (
1053                 'enabled' => true,
1054                 'weekly'  => $weekly,
1055                 'daily'   => $daily
1056             )
1057         );
1058         $result= $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/backup_schedule',
1059                                  'POST',null,null,json_encode($data));
1060         $status= $result->getStatus();
1061         switch ($status) {
1062             case '204' : // break intentionally omitted   
1063                 return true;
1064             case '503' :
1065                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
1066                 break;
1067             case '401' :
1068                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
1069                 break;
1070             case '404' :
1071                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
1072                 break;
1073             case '409' :
1074                 $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
1075                 break;
1076             case '413' :
1077                 $this->errorMsg= self::ERROR_OVERLIMIT;
1078                 break;
1079             default:
1080                 $this->errorMsg= $result->getBody();
1081                 break;
1082         }
1083         $this->errorCode= $status;
1084         return false;
1085     }
1086     /**
1087      * Disable the backup schedule for a server
1088      * 
1089      * @param  string $id server's Id
1090      * @return boolean
1091      */
1092     public function disableBackupSchedule($id)
1093     {
1094         if (empty($id)) {
1095             // require_once 'Zend/Service/Rackspace/Exception.php';
1096             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
1097         }
1098         $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/backup_schedule',
1099                                   'DELETE');
1100         $status = $result->getStatus();
1101         switch ($status) {
1102             case '204' : // break intentionally omitted   
1103                 return true;
1104             case '503' :
1105                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
1106                 break;
1107             case '401' :
1108                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
1109                 break;
1110             case '404' :
1111                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
1112                 break;
1113             case '409' :
1114                 $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
1115                 break;
1116             case '413' :
1117                 $this->errorMsg= self::ERROR_OVERLIMIT;
1118                 break;
1119             default:
1120                 $this->errorMsg= $result->getBody();
1121                 break;
1122         }
1123         $this->errorCode= $status;
1124         return false;
1125     }
1126     /**
1127      * Get the list of shared IP groups
1128      * 
1129      * @param  boolean $details
1130      * @return Zend_Service_Rackspace_Servers_SharedIpGroupList|boolean 
1131      */
1132     public function listSharedIpGroups($details=false)
1133     {
1134         $url= '/shared_ip_groups';
1135         if ($details) {
1136             $url.= '/detail';
1137         } 
1138         $result= $this->httpCall($this->getManagementUrl().$url,'GET');
1139         $status= $result->getStatus();
1140         switch ($status) {
1141             case '200' : 
1142             case '203' : // break intentionally omitted   
1143                 $groups= json_decode($result->getBody(),true);
1144                 return new Zend_Service_Rackspace_Servers_SharedIpGroupList($this,$groups['sharedIpGroups']);
1145             case '503' :
1146                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
1147                 break;
1148             case '401' :
1149                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
1150                 break;
1151             case '413' :
1152                 $this->errorMsg= self::ERROR_OVERLIMIT;
1153                 break;
1154             default:
1155                 $this->errorMsg= $result->getBody();
1156                 break;
1157         }
1158         $this->errorCode= $status;
1159         return false;
1160     }
1161     /**
1162      * Get the shared IP group
1163      * 
1164      * @param  integer $id
1165      * @return Zend_Service_Rackspace_Servers_SharedIpGroup|boolean 
1166      */
1167     public function getSharedIpGroup($id)
1168     {
1169         if (empty($id)) {
1170             // require_once 'Zend/Service/Rackspace/Exception.php';
1171             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
1172         }
1173         $result= $this->httpCall($this->getManagementUrl().'/shared_ip_groups/'.rawurlencode($id),'GET');
1174         $status= $result->getStatus();
1175         switch ($status) {
1176             case '200' : 
1177             case '203' : // break intentionally omitted   
1178                 $group= json_decode($result->getBody(),true);
1179                 return new Zend_Service_Rackspace_Servers_SharedIpGroup($this,$group['sharedIpGroup']);
1180             case '503' :
1181                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
1182                 break;
1183             case '401' :
1184                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
1185                 break;
1186             case '404' :
1187                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
1188                 break;
1189             case '413' :
1190                 $this->errorMsg= self::ERROR_OVERLIMIT;
1191                 break;
1192             default:
1193                 $this->errorMsg= $result->getBody();
1194                 break;
1195         }
1196         $this->errorCode= $status;
1197         return false;
1198     }
1199     /**
1200      * Create a shared Ip group
1201      * 
1202      * @param  string $name
1203      * @param  string $serverId
1204      * @return array|boolean 
1205      */
1206     public function createSharedIpGroup($name,$serverId)
1207     {
1208         if (empty($name)) {
1209             // require_once 'Zend/Service/Rackspace/Exception.php';
1210             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME);
1211         }
1212         if (empty($serverId)) {
1213             // require_once 'Zend/Service/Rackspace/Exception.php';
1214             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
1215         }
1216         $data = array (
1217             'sharedIpGroup' => array (
1218                 'name'   => $name,
1219                 'server' => (integer) $serverId
1220             )
1221         );
1222         $result= $this->httpCall($this->getManagementUrl().'/shared_ip_groups',
1223                                  'POST',null,null,json_encode($data));
1224         $status= $result->getStatus();
1225         switch ($status) {
1226             case '201' : // break intentionally omitted   
1227                 $group = json_decode($result->getBody(),true);
1228                 return new Zend_Service_Rackspace_Servers_SharedIpGroup($this,$group['sharedIpGroup']);
1229             case '503' :
1230                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
1231                 break;
1232             case '401' :
1233                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
1234                 break;
1235             case '413' :
1236                 $this->errorMsg= self::ERROR_OVERLIMIT;
1237                 break;
1238             default:
1239                 $this->errorMsg= $result->getBody();
1240                 break;
1241         }
1242         $this->errorCode= $status;
1243         return false;
1244     }
1245     /**
1246      * Delete a Shared Ip Group
1247      * 
1248      * @param  integer $id 
1249      * @return boolean
1250      */
1251     public function deleteSharedIpGroup($id)
1252     {
1253         if (empty($id)) {
1254             // require_once 'Zend/Service/Rackspace/Exception.php';
1255             throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
1256         }
1257         $result= $this->httpCall($this->getManagementUrl().'/shared_ip_groups/'.rawurlencode($id),'DELETE');
1258         $status= $result->getStatus();
1259         switch ($status) {
1260             case '204' : // break intentionally omitted   
1261                 return true;
1262             case '503' :
1263                 $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
1264                 break;
1265             case '401' :
1266                 $this->errorMsg= self::ERROR_UNAUTHORIZED;
1267                 break;
1268             case '404' :
1269                 $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
1270                 break;
1271             case '413' :
1272                 $this->errorMsg= self::ERROR_OVERLIMIT;
1273                 break;
1274             default:
1275                 $this->errorMsg= $result->getBody();
1276                 break;
1277         }
1278         $this->errorCode= $status;
1279         return false;
1280     }
1281 }