web/lib/Zend/Service/Rackspace/Servers/Server.php
changeset 808 6b6c2214f778
child 1230 68c69c656a2c
equal deleted inserted replaced
807:877f952ae2bd 808:6b6c2214f778
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * Zend Framework
       
     5  *
       
     6  * LICENSE
       
     7  *
       
     8  * This source file is subject to the new BSD license that is bundled
       
     9  * with this package in the file LICENSE.txt.
       
    10  * It is also available through the world-wide-web at this URL:
       
    11  * http://framework.zend.com/license/new-bsd
       
    12  * If you did not receive a copy of the license and are unable to
       
    13  * obtain it through the world-wide-web, please send an email
       
    14  * to license@zend.com so we can send you a copy immediately.
       
    15  *
       
    16  * @category   Zend
       
    17  * @package    Zend_Service_Rackspace
       
    18  * @subpackage Servers
       
    19  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
       
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    21  */
       
    22 
       
    23 require_once 'Zend/Service/Rackspace/Servers.php';
       
    24 
       
    25 class Zend_Service_Rackspace_Servers_Server
       
    26 {
       
    27     const ERROR_PARAM_CONSTRUCT = 'You must pass a Zend_Service_Rackspace_Servers object and an array';
       
    28     const ERROR_PARAM_NO_NAME   = 'You must pass the server\'s name in the array (name)';
       
    29     const ERROR_PARAM_NO_ID     = 'You must pass the server\'s id in the array (id)';
       
    30     /**
       
    31      * Server's name
       
    32      * 
       
    33      * @var string
       
    34      */
       
    35     protected $name;
       
    36     /**
       
    37      * Server's id
       
    38      *
       
    39      * @var string
       
    40      */
       
    41     protected $id;
       
    42     /**
       
    43      * Image id of the server
       
    44      *
       
    45      * @var string
       
    46      */
       
    47     protected $imageId;
       
    48     /**
       
    49      * Flavor id of the server
       
    50      * 
       
    51      * @var string 
       
    52      */
       
    53     protected $flavorId;
       
    54     /**
       
    55      * Host id
       
    56      * 
       
    57      * @var string
       
    58      */
       
    59     protected $hostId;
       
    60     /**
       
    61      * Server's status
       
    62      * 
       
    63      * @var string 
       
    64      */
       
    65     protected $status;
       
    66     /**
       
    67      * Progress of the status
       
    68      * 
       
    69      * @var integer
       
    70      */
       
    71     protected $progress;
       
    72     /**
       
    73      * Admin password, generated on a new server
       
    74      * 
       
    75      * @var string 
       
    76      */
       
    77     protected $adminPass;
       
    78     /**
       
    79      * Public and private IP addresses
       
    80      * 
       
    81      * @var array 
       
    82      */
       
    83     protected $addresses = array();
       
    84     /**
       
    85      * @var array
       
    86      */
       
    87     protected $metadata = array();
       
    88     /**
       
    89      * The service that has created the server object
       
    90      *
       
    91      * @var Zend_Service_Rackspace_Servers
       
    92      */
       
    93     protected $service;
       
    94     /**
       
    95      * Constructor
       
    96      *
       
    97      * @param  Zend_Service_Rackspace_Servers $service
       
    98      * @param  array $data
       
    99      * @return void
       
   100      */
       
   101     public function __construct($service, $data)
       
   102     {
       
   103         if (!($service instanceof Zend_Service_Rackspace_Servers) || !is_array($data)) {
       
   104             require_once 'Zend/Service/Rackspace/Servers/Exception.php';
       
   105             throw new Zend_Service_Rackspace_Servers_Exception(self::ERROR_PARAM_CONSTRUCT);
       
   106         }
       
   107         if (!array_key_exists('name', $data)) {
       
   108             require_once 'Zend/Service/Rackspace/Servers/Exception.php';
       
   109             throw new Zend_Service_Rackspace_Servers_Exception(self::ERROR_PARAM_NO_NAME);
       
   110         }
       
   111         if (!array_key_exists('id', $data)) {
       
   112             require_once 'Zend/Service/Rackspace/Servers/Exception.php';
       
   113             throw new Zend_Service_Rackspace_Servers_Exception(self::ERROR_PARAM_NO_ID);
       
   114         }
       
   115         $this->service = $service;
       
   116         $this->name = $data['name'];
       
   117         $this->id = $data['id'];
       
   118         if (isset($data['imageId'])) {
       
   119             $this->imageId= $data['imageId'];
       
   120         }
       
   121         if (isset($data['flavorId'])) {
       
   122             $this->flavorId= $data['flavorId'];
       
   123         }
       
   124         if (isset($data['hostId'])) {
       
   125             $this->hostId= $data['hostId'];
       
   126         }
       
   127         if (isset($data['status'])) {
       
   128             $this->status= $data['status'];
       
   129         }
       
   130         if (isset($data['progress'])) {
       
   131             $this->progress= $data['progress'];
       
   132         }
       
   133         if (isset($data['adminPass'])) {
       
   134             $this->adminPass= $data['adminPass'];
       
   135         }
       
   136         if (isset($data['addresses']) && is_array($data['addresses'])) {
       
   137             $this->addresses= $data['addresses'];
       
   138         }
       
   139         if (isset($data['metadata']) && is_array($data['metadata'])) {
       
   140             $this->metadata= $data['metadata'];
       
   141         } 
       
   142     }
       
   143     /**
       
   144      * Get the name of the server
       
   145      *
       
   146      * @return string
       
   147      */
       
   148     public function getName()
       
   149     {
       
   150         return $this->name;
       
   151     }
       
   152     /**
       
   153      * Get the server's id
       
   154      * 
       
   155      * @return string 
       
   156      */
       
   157     public function getId()
       
   158     {
       
   159         return $this->id;
       
   160     }
       
   161     /**
       
   162      * Get the server's image Id
       
   163      * 
       
   164      * @return string 
       
   165      */
       
   166     public function getImageId()
       
   167     {
       
   168         return $this->imageId;
       
   169     }
       
   170     /**
       
   171      * Get the server's flavor Id
       
   172      * 
       
   173      * @return string 
       
   174      */
       
   175     public function getFlavorId()
       
   176     {
       
   177         return $this->flavorId;
       
   178     }
       
   179     /**
       
   180      * Get the server's host Id
       
   181      * 
       
   182      * @return string 
       
   183      */
       
   184     public function getHostId()
       
   185     {
       
   186         return $this->hostId;
       
   187     }
       
   188     /**
       
   189      * Ge the server's admin password
       
   190      * 
       
   191      * @return string 
       
   192      */
       
   193     public function getAdminPass()
       
   194     {
       
   195         return $this->adminPass;
       
   196     }
       
   197     /**
       
   198      * Get the server's status
       
   199      * 
       
   200      * @return string|boolean
       
   201      */
       
   202     public function getStatus()
       
   203     {
       
   204         $data= $this->service->getServer($this->id);
       
   205         if ($data!==false) {
       
   206             $data= $data->toArray();
       
   207             $this->status= $data['status'];
       
   208             return $this->status;
       
   209         }
       
   210         return false;
       
   211     }
       
   212     /**
       
   213      * Get the progress's status
       
   214      * 
       
   215      * @return integer|boolean
       
   216      */
       
   217     public function getProgress()
       
   218     {
       
   219         $data= $this->service->getServer($this->id);
       
   220         if ($data!==false) {
       
   221             $data= $data->toArray();
       
   222             $this->progress= $data['progress'];
       
   223             return $this->progress;
       
   224         }
       
   225         return false;
       
   226     }
       
   227     /**
       
   228      * Get the private IPs
       
   229      * 
       
   230      * @return array|boolean
       
   231      */
       
   232     public function getPrivateIp()
       
   233     {
       
   234         if (isset($this->addresses['private'])) {
       
   235             return $this->addresses['private'];
       
   236         }
       
   237         return false;
       
   238     }
       
   239     /**
       
   240      * Get the public IPs
       
   241      * 
       
   242      * @return array|boolean
       
   243      */
       
   244     public function getPublicIp()
       
   245     {
       
   246         if (isset($this->addresses['public'])) {
       
   247             return $this->addresses['public'];
       
   248         }
       
   249         return false;
       
   250     }
       
   251     /**
       
   252      * Get the metadata of the container
       
   253      *
       
   254      * If $key is empty return the array of metadata
       
   255      *
       
   256      * @param string $key
       
   257      * @return array|string
       
   258      */
       
   259     public function getMetadata($key=null)
       
   260     {
       
   261         if (!empty($key) && isset($this->metadata[$key])) {
       
   262             return $this->metadata[$key];
       
   263         }
       
   264         return $this->metadata;
       
   265     }
       
   266     /**
       
   267      * Change the name of the server
       
   268      * 
       
   269      * @param string $name
       
   270      * @return boolean 
       
   271      */
       
   272     public function changeName($name) 
       
   273     {
       
   274         $result= $this->service->changeServerName($this->id, $name);
       
   275         if ($result!==false) {
       
   276             $this->name= $name;
       
   277             return true;
       
   278         }
       
   279         return false;
       
   280     }
       
   281     /**
       
   282      * Change the admin password of the server
       
   283      * 
       
   284      * @param string $password
       
   285      * @return boolean 
       
   286      */
       
   287     public function changePassword($password)
       
   288     {
       
   289         $result=  $this->service->changeServerPassword($this->id, $password);
       
   290         if ($result!==false) {
       
   291             $this->adminPass= $password;
       
   292             return true;
       
   293         }
       
   294         return false;
       
   295     }
       
   296     /**
       
   297      * Reboot the server
       
   298      * 
       
   299      * @return boolean 
       
   300      */
       
   301     public function reboot($hard=false)
       
   302     {
       
   303         return $this->service->rebootServer($this->id,$hard);
       
   304     }
       
   305     /**
       
   306      * To Array
       
   307      * 
       
   308      * @return array 
       
   309      */
       
   310     public function toArray()
       
   311     {
       
   312         return array (
       
   313             'name'      => $this->name,
       
   314             'id'        => $this->id,
       
   315             'imageId'   => $this->imageId,
       
   316             'flavorId'  => $this->flavorId,
       
   317             'hostId'    => $this->hostId,
       
   318             'status'    => $this->status,
       
   319             'progress'  => $this->progress,
       
   320             'adminPass' => $this->adminPass,
       
   321             'addresses' => $this->addresses,
       
   322             'metadata'  => $this->metadata
       
   323         );
       
   324     }
       
   325 }