Server.php 7.9 KB

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