File indexing completed on 2024-12-29 05:28:02
0001 <?php 0002 0003 /** 0004 * Zend Framework 0005 * 0006 * LICENSE 0007 * 0008 * This source file is subject to the new BSD license that is bundled 0009 * with this package in the file LICENSE.txt. 0010 * It is also available through the world-wide-web at this URL: 0011 * http://framework.zend.com/license/new-bsd 0012 * If you did not receive a copy of the license and are unable to 0013 * obtain it through the world-wide-web, please send an email 0014 * to license@zend.com so we can send you a copy immediately. 0015 * 0016 * @category Zend 0017 * @package Zend_Service_Rackspace 0018 * @subpackage Servers 0019 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0020 * @license http://framework.zend.com/license/new-bsd New BSD License 0021 */ 0022 0023 // require_once 'Zend/Service/Rackspace/Servers.php'; 0024 0025 class Zend_Service_Rackspace_Servers_Server 0026 { 0027 const ERROR_PARAM_CONSTRUCT = 'You must pass a Zend_Service_Rackspace_Servers object and an array'; 0028 const ERROR_PARAM_NO_NAME = 'You must pass the server\'s name in the array (name)'; 0029 const ERROR_PARAM_NO_ID = 'You must pass the server\'s id in the array (id)'; 0030 /** 0031 * Server's name 0032 * 0033 * @var string 0034 */ 0035 protected $name; 0036 /** 0037 * Server's id 0038 * 0039 * @var string 0040 */ 0041 protected $id; 0042 /** 0043 * Image id of the server 0044 * 0045 * @var string 0046 */ 0047 protected $imageId; 0048 /** 0049 * Flavor id of the server 0050 * 0051 * @var string 0052 */ 0053 protected $flavorId; 0054 /** 0055 * Host id 0056 * 0057 * @var string 0058 */ 0059 protected $hostId; 0060 /** 0061 * Server's status 0062 * 0063 * @var string 0064 */ 0065 protected $status; 0066 /** 0067 * Progress of the status 0068 * 0069 * @var integer 0070 */ 0071 protected $progress; 0072 /** 0073 * Admin password, generated on a new server 0074 * 0075 * @var string 0076 */ 0077 protected $adminPass; 0078 /** 0079 * Public and private IP addresses 0080 * 0081 * @var array 0082 */ 0083 protected $addresses = array(); 0084 /** 0085 * @var array 0086 */ 0087 protected $metadata = array(); 0088 /** 0089 * The service that has created the server object 0090 * 0091 * @var Zend_Service_Rackspace_Servers 0092 */ 0093 protected $service; 0094 /** 0095 * Constructor 0096 * 0097 * @param Zend_Service_Rackspace_Servers $service 0098 * @param array $data 0099 * @return void 0100 */ 0101 public function __construct($service, $data) 0102 { 0103 if (!($service instanceof Zend_Service_Rackspace_Servers) || !is_array($data)) { 0104 // require_once 'Zend/Service/Rackspace/Servers/Exception.php'; 0105 throw new Zend_Service_Rackspace_Servers_Exception(self::ERROR_PARAM_CONSTRUCT); 0106 } 0107 if (!array_key_exists('name', $data)) { 0108 // require_once 'Zend/Service/Rackspace/Servers/Exception.php'; 0109 throw new Zend_Service_Rackspace_Servers_Exception(self::ERROR_PARAM_NO_NAME); 0110 } 0111 if (!array_key_exists('id', $data)) { 0112 // require_once 'Zend/Service/Rackspace/Servers/Exception.php'; 0113 throw new Zend_Service_Rackspace_Servers_Exception(self::ERROR_PARAM_NO_ID); 0114 } 0115 $this->service = $service; 0116 $this->name = $data['name']; 0117 $this->id = $data['id']; 0118 if (isset($data['imageId'])) { 0119 $this->imageId= $data['imageId']; 0120 } 0121 if (isset($data['flavorId'])) { 0122 $this->flavorId= $data['flavorId']; 0123 } 0124 if (isset($data['hostId'])) { 0125 $this->hostId= $data['hostId']; 0126 } 0127 if (isset($data['status'])) { 0128 $this->status= $data['status']; 0129 } 0130 if (isset($data['progress'])) { 0131 $this->progress= $data['progress']; 0132 } 0133 if (isset($data['adminPass'])) { 0134 $this->adminPass= $data['adminPass']; 0135 } 0136 if (isset($data['addresses']) && is_array($data['addresses'])) { 0137 $this->addresses= $data['addresses']; 0138 } 0139 if (isset($data['metadata']) && is_array($data['metadata'])) { 0140 $this->metadata= $data['metadata']; 0141 } 0142 } 0143 /** 0144 * Get the name of the server 0145 * 0146 * @return string 0147 */ 0148 public function getName() 0149 { 0150 return $this->name; 0151 } 0152 /** 0153 * Get the server's id 0154 * 0155 * @return string 0156 */ 0157 public function getId() 0158 { 0159 return $this->id; 0160 } 0161 /** 0162 * Get the server's image Id 0163 * 0164 * @return string 0165 */ 0166 public function getImageId() 0167 { 0168 return $this->imageId; 0169 } 0170 /** 0171 * Get the server's flavor Id 0172 * 0173 * @return string 0174 */ 0175 public function getFlavorId() 0176 { 0177 return $this->flavorId; 0178 } 0179 /** 0180 * Get the server's host Id 0181 * 0182 * @return string 0183 */ 0184 public function getHostId() 0185 { 0186 return $this->hostId; 0187 } 0188 /** 0189 * Ge the server's admin password 0190 * 0191 * @return string 0192 */ 0193 public function getAdminPass() 0194 { 0195 return $this->adminPass; 0196 } 0197 /** 0198 * Get the server's status 0199 * 0200 * @return string|boolean 0201 */ 0202 public function getStatus() 0203 { 0204 $data= $this->service->getServer($this->id); 0205 if ($data!==false) { 0206 $data= $data->toArray(); 0207 $this->status= $data['status']; 0208 return $this->status; 0209 } 0210 return false; 0211 } 0212 /** 0213 * Get the progress's status 0214 * 0215 * @return integer|boolean 0216 */ 0217 public function getProgress() 0218 { 0219 $data= $this->service->getServer($this->id); 0220 if ($data!==false) { 0221 $data= $data->toArray(); 0222 $this->progress= $data['progress']; 0223 return $this->progress; 0224 } 0225 return false; 0226 } 0227 /** 0228 * Get the private IPs 0229 * 0230 * @return array|boolean 0231 */ 0232 public function getPrivateIp() 0233 { 0234 if (isset($this->addresses['private'])) { 0235 return $this->addresses['private']; 0236 } 0237 return false; 0238 } 0239 /** 0240 * Get the public IPs 0241 * 0242 * @return array|boolean 0243 */ 0244 public function getPublicIp() 0245 { 0246 if (isset($this->addresses['public'])) { 0247 return $this->addresses['public']; 0248 } 0249 return false; 0250 } 0251 /** 0252 * Get the metadata of the container 0253 * 0254 * If $key is empty return the array of metadata 0255 * 0256 * @param string $key 0257 * @return array|string 0258 */ 0259 public function getMetadata($key=null) 0260 { 0261 if (!empty($key) && isset($this->metadata[$key])) { 0262 return $this->metadata[$key]; 0263 } 0264 return $this->metadata; 0265 } 0266 /** 0267 * Change the name of the server 0268 * 0269 * @param string $name 0270 * @return boolean 0271 */ 0272 public function changeName($name) 0273 { 0274 $result= $this->service->changeServerName($this->id, $name); 0275 if ($result!==false) { 0276 $this->name= $name; 0277 return true; 0278 } 0279 return false; 0280 } 0281 /** 0282 * Change the admin password of the server 0283 * 0284 * @param string $password 0285 * @return boolean 0286 */ 0287 public function changePassword($password) 0288 { 0289 $result= $this->service->changeServerPassword($this->id, $password); 0290 if ($result!==false) { 0291 $this->adminPass= $password; 0292 return true; 0293 } 0294 return false; 0295 } 0296 /** 0297 * Reboot the server 0298 * 0299 * @return boolean 0300 */ 0301 public function reboot($hard=false) 0302 { 0303 return $this->service->rebootServer($this->id,$hard); 0304 } 0305 /** 0306 * To Array 0307 * 0308 * @return array 0309 */ 0310 public function toArray() 0311 { 0312 return array ( 0313 'name' => $this->name, 0314 'id' => $this->id, 0315 'imageId' => $this->imageId, 0316 'flavorId' => $this->flavorId, 0317 'hostId' => $this->hostId, 0318 'status' => $this->status, 0319 'progress' => $this->progress, 0320 'adminPass' => $this->adminPass, 0321 'addresses' => $this->addresses, 0322 'metadata' => $this->metadata 0323 ); 0324 } 0325 }