PageRenderTime 64ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/protected/vendors/Zend/Service/Rackspace/Servers.php

https://bitbucket.org/negge/tlklan2
PHP | 1281 lines | 1049 code | 2 blank | 230 comment | 76 complexity | eae143dd265b7050b5dca95a46286942 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, BSD-2-Clause, GPL-3.0
  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
  17. * @subpackage Rackspace
  18. * @copyright Copyright (c) 2005-2012 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/Abstract.php';
  22. require_once 'Zend/Service/Rackspace/Servers/Server.php';
  23. require_once 'Zend/Service/Rackspace/Servers/ServerList.php';
  24. require_once 'Zend/Service/Rackspace/Servers/Image.php';
  25. require_once 'Zend/Service/Rackspace/Servers/ImageList.php';
  26. require_once 'Zend/Service/Rackspace/Servers/SharedIpGroup.php';
  27. require_once 'Zend/Service/Rackspace/Servers/SharedIpGroupList.php';
  28. require_once 'Zend/Validate/Ip.php';
  29. class Zend_Service_Rackspace_Servers extends Zend_Service_Rackspace_Abstract
  30. {
  31. const LIMIT_FILE_SIZE = 10240;
  32. const LIMIT_NUM_FILE = 5;
  33. const ERROR_SERVICE_UNAVAILABLE = 'The service is unavailable';
  34. const ERROR_UNAUTHORIZED = 'Unauthorized';
  35. const ERROR_OVERLIMIT = 'You reached the limit of requests, please wait some time before retry';
  36. const ERROR_PARAM_NO_ID = 'You must specify the item\'s id';
  37. const ERROR_PARAM_NO_NAME = 'You must specify the name';
  38. const ERROR_PARAM_NO_SERVERID = 'You must specify the server Id';
  39. const ERROR_PARAM_NO_IMAGEID = 'You must specify the server\'s image ID';
  40. const ERROR_PARAM_NO_FLAVORID = 'You must specify the server\'s flavor ID';
  41. const ERROR_PARAM_NO_ARRAY = 'You must specify an array of parameters';
  42. const ERROR_PARAM_NO_WEEKLY = 'You must specify a weekly backup schedule';
  43. const ERROR_PARAM_NO_DAILY = 'You must specify a daily backup schedule';
  44. const ERROR_ITEM_NOT_FOUND = 'The item specified doesn\'t exist.';
  45. const ERROR_NO_FILE_EXISTS = 'The file specified doesn\'t exist';
  46. const ERROR_LIMIT_FILE_SIZE = 'You reached the size length of a file';
  47. const ERROR_IN_PROGRESS = 'The item specified is still in progress';
  48. const ERROR_BUILD_IN_PROGRESS = 'The build is still in progress';
  49. const ERROR_RESIZE_NOT_ALLOWED = 'The resize is not allowed';
  50. /**
  51. * Get the list of the servers
  52. * If $details is true returns detail info
  53. *
  54. * @param boolean $details
  55. * @return Zend_Service_Rackspace_Servers_ServerList|boolean
  56. */
  57. public function listServers($details=false)
  58. {
  59. $url= '/servers';
  60. if ($details) {
  61. $url.= '/detail';
  62. }
  63. $result= $this->httpCall($this->getManagementUrl().$url,'GET');
  64. $status= $result->getStatus();
  65. switch ($status) {
  66. case '200' :
  67. case '203' : // break intentionally omitted
  68. $servers= json_decode($result->getBody(),true);
  69. return new Zend_Service_Rackspace_Servers_ServerList($this,$servers['servers']);
  70. case '503' :
  71. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  72. break;
  73. case '401' :
  74. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  75. break;
  76. case '413' :
  77. $this->errorMsg= self::ERROR_OVERLIMIT;
  78. break;
  79. default:
  80. $this->errorMsg= $result->getBody();
  81. break;
  82. }
  83. $this->errorCode= $status;
  84. return false;
  85. }
  86. /**
  87. * Get the specified server
  88. *
  89. * @param string $id
  90. * @return Zend_Service_Rackspace_Servers_Server
  91. */
  92. public function getServer($id)
  93. {
  94. if (empty($id)) {
  95. require_once 'Zend/Service/Rackspace/Exception.php';
  96. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
  97. }
  98. $result= $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id),'GET');
  99. $status= $result->getStatus();
  100. switch ($status) {
  101. case '200' :
  102. case '203' : // break intentionally omitted
  103. $server = json_decode($result->getBody(),true);
  104. return new Zend_Service_Rackspace_Servers_Server($this,$server['server']);
  105. case '503' :
  106. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  107. break;
  108. case '401' :
  109. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  110. break;
  111. case '404' :
  112. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  113. break;
  114. case '413' :
  115. $this->errorMsg= self::ERROR_OVERLIMIT;
  116. break;
  117. default:
  118. $this->errorMsg= $result->getBody();
  119. break;
  120. }
  121. $this->errorCode= $status;
  122. return false;
  123. }
  124. /**
  125. * Create a new server
  126. *
  127. * The required parameters are specified in $data (name, imageId, falvorId)
  128. * The $files is an associative array with 'serverPath' => 'localPath'
  129. *
  130. * @param array $data
  131. * @param array $metadata
  132. * @param array $files
  133. * @return Zend_Service_Rackspace_Servers_Server|boolean
  134. */
  135. public function createServer(array $data, $metadata=array(),$files=array())
  136. {
  137. if (empty($data) || !is_array($data) || !is_array($metadata) || !is_array($files)) {
  138. require_once 'Zend/Service/Rackspace/Exception.php';
  139. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ARRAY);
  140. }
  141. if (!isset($data['name'])) {
  142. require_once 'Zend/Service/Rackspace/Exception.php';
  143. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME);
  144. }
  145. if (!isset($data['flavorId'])) {
  146. require_once 'Zend/Service/Rackspace/Exception.php';
  147. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_FLAVORID);
  148. }
  149. if (!isset($data['imageId'])) {
  150. require_once 'Zend/Service/Rackspace/Exception.php';
  151. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_IMAGEID);
  152. }
  153. if (count($files)>self::LIMIT_NUM_FILE) {
  154. require_once 'Zend/Service/Rackspace/Exception.php';
  155. throw new Zend_Service_Rackspace_Exception('You can attach '.self::LIMIT_NUM_FILE.' files maximum');
  156. }
  157. if (!empty($metadata)) {
  158. $data['metadata']= $metadata;
  159. }
  160. $data['flavorId']= (integer) $data['flavorId'];
  161. $data['imageId']= (integer) $data['imageId'];
  162. if (!empty($files)) {
  163. foreach ($files as $serverPath => $filePath) {
  164. if (!file_exists($filePath)) {
  165. require_once 'Zend/Service/Rackspace/Exception.php';
  166. throw new Zend_Service_Rackspace_Exception(
  167. sprintf("The file %s doesn't exist",$filePath));
  168. }
  169. $content= file_get_contents($filePath);
  170. if (strlen($content) > self::LIMIT_FILE_SIZE) {
  171. require_once 'Zend/Service/Rackspace/Exception.php';
  172. throw new Zend_Service_Rackspace_Exception(
  173. sprintf("The size of the file %s is greater than the max size of %d bytes",
  174. $filePath,self::LIMIT_FILE_SIZE));
  175. }
  176. $data['personality'][] = array (
  177. 'path' => $serverPath,
  178. 'contents' => base64_encode(file_get_contents($filePath))
  179. );
  180. }
  181. }
  182. $result = $this->httpCall($this->getManagementUrl().'/servers','POST',
  183. null,null,json_encode(array ('server' => $data)));
  184. $status = $result->getStatus();
  185. switch ($status) {
  186. case '200' :
  187. case '202' : // break intentionally omitted
  188. $server = json_decode($result->getBody(),true);
  189. return new Zend_Service_Rackspace_Servers_Server($this,$server['server']);
  190. case '503' :
  191. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  192. break;
  193. case '401' :
  194. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  195. break;
  196. case '404' :
  197. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  198. break;
  199. case '413' :
  200. $this->errorMsg= self::ERROR_OVERLIMIT;
  201. break;
  202. default:
  203. $this->errorMsg= $result->getBody();
  204. break;
  205. }
  206. $this->errorCode= $status;
  207. return false;
  208. }
  209. /**
  210. * Change the name or the admin password for a server
  211. *
  212. * @param string $id
  213. * @param string $name
  214. * @param string $password
  215. * @return boolean
  216. */
  217. protected function updateServer($id,$name=null,$password=null)
  218. {
  219. if (empty($id)) {
  220. require_once 'Zend/Service/Rackspace/Exception.php';
  221. throw new Zend_Service_Rackspace_Exception('You must specify the ID of the server');
  222. }
  223. if (empty($name) && empty($password)) {
  224. require_once 'Zend/Service/Rackspace/Exception.php';
  225. throw new Zend_Service_Rackspace_Exception("You must specify the new name or password of server");
  226. }
  227. $data= array();
  228. if (!empty($name)) {
  229. $data['name']= $name;
  230. }
  231. if (!empty($password)) {
  232. $data['adminPass']= $password;
  233. }
  234. $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id),'PUT',
  235. null,null,json_encode(array('server' => $data)));
  236. $status = $result->getStatus();
  237. switch ($status) {
  238. case '204' : // break intentionally omitted
  239. return true;
  240. case '503' :
  241. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  242. break;
  243. case '401' :
  244. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  245. break;
  246. case '404' :
  247. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  248. break;
  249. case '409' :
  250. $this->errorMsg= self::ERROR_IN_PROGRESS;
  251. break;
  252. case '413' :
  253. $this->errorMsg= self::ERROR_OVERLIMIT;
  254. break;
  255. default:
  256. $this->errorMsg= $result->getBody();
  257. break;
  258. }
  259. $this->errorCode= $status;
  260. return false;
  261. }
  262. /**
  263. * Change the server's name
  264. *
  265. * @param string $id
  266. * @param string $name
  267. * @return boolean
  268. */
  269. public function changeServerName($id,$name)
  270. {
  271. if (empty($id)) {
  272. require_once 'Zend/Service/Rackspace/Exception.php';
  273. throw new Zend_Service_Rackspace_Exception('You must specify the ID of the server');
  274. }
  275. if (empty($name)) {
  276. require_once 'Zend/Service/Rackspace/Exception.php';
  277. throw new Zend_Service_Rackspace_Exception("You must specify the new name of the server");
  278. }
  279. return $this->updateServer($id, $name);
  280. }
  281. /**
  282. * Change the admin password of the server
  283. *
  284. * @param string $id
  285. * @param string $password
  286. * @return boolean
  287. */
  288. public function changeServerPassword($id,$password)
  289. {
  290. if (empty($id)) {
  291. require_once 'Zend/Service/Rackspace/Exception.php';
  292. throw new Zend_Service_Rackspace_Exception('You must specify the ID of the server');
  293. }
  294. if (empty($password)) {
  295. require_once 'Zend/Service/Rackspace/Exception.php';
  296. throw new Zend_Service_Rackspace_Exception("You must specify the new password of the server");
  297. }
  298. return $this->updateServer($id, null,$password);
  299. }
  300. /**
  301. * Delete a server
  302. *
  303. * @param string $id
  304. * @return boolean
  305. */
  306. public function deleteServer($id)
  307. {
  308. if (empty($id)) {
  309. require_once 'Zend/Service/Rackspace/Exception.php';
  310. throw new Zend_Service_Rackspace_Exception('You must specify the ID of the server');
  311. }
  312. $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id),'DELETE');
  313. $status = $result->getStatus();
  314. switch ($status) {
  315. case '202' : // break intentionally omitted
  316. return true;
  317. case '503' :
  318. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  319. break;
  320. case '401' :
  321. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  322. break;
  323. case '404' :
  324. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  325. break;
  326. case '409' :
  327. $this->errorMsg= self::ERROR_IN_PROGRESS;
  328. break;
  329. case '413' :
  330. $this->errorMsg= self::ERROR_OVERLIMIT;
  331. break;
  332. default:
  333. $this->errorMsg= $result->getBody();
  334. break;
  335. }
  336. $this->errorCode= $status;
  337. return false;
  338. }
  339. /**
  340. * Get the server's IPs (public and private)
  341. *
  342. * @param string $id
  343. * @return array|boolean
  344. */
  345. public function getServerIp($id)
  346. {
  347. $result= $this->getServer($id);
  348. if ($result===false) {
  349. return false;
  350. }
  351. $result= $result->toArray();
  352. return $result['addresses'];
  353. }
  354. /**
  355. * Get the Public IPs of a server
  356. *
  357. * @param string $id
  358. * @return array|boolean
  359. */
  360. public function getServerPublicIp($id)
  361. {
  362. $addresses= $this->getServerIp($id);
  363. if ($addresses===false) {
  364. return false;
  365. }
  366. return $addresses['public'];
  367. }
  368. /**
  369. * Get the Private IPs of a server
  370. *
  371. * @param string $id
  372. * @return array|boolean
  373. */
  374. public function getServerPrivateIp($id)
  375. {
  376. $addresses= $this->getServerIp($id);
  377. if ($addresses===false) {
  378. return false;
  379. }
  380. return $addresses['private'];
  381. }
  382. /**
  383. * Share an ip address for a server (id)
  384. *
  385. * @param string $id server
  386. * @param string $ip
  387. * @param string $groupId
  388. * @return boolean
  389. */
  390. public function shareIpAddress($id,$ip,$groupId,$configure=true)
  391. {
  392. if (empty($id)) {
  393. require_once 'Zend/Service/Rackspace/Exception.php';
  394. throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
  395. }
  396. if (empty($ip)) {
  397. require_once 'Zend/Service/Rackspace/Exception.php';
  398. throw new Zend_Service_Rackspace_Exception('You didn\'t specified the IP address to share');
  399. }
  400. $validator = new Zend_Validate_Ip();
  401. if (!$validator->isValid($ip)) {
  402. require_once 'Zend/Service/Rackspace/Exception.php';
  403. throw new Zend_Service_Rackspace_Exception("The parameter $ip specified is not a valid IP address");
  404. }
  405. if (empty($groupId)) {
  406. require_once 'Zend/Service/Rackspace/Exception.php';
  407. throw new Zend_Service_Rackspace_Exception('You didn\'t specified the group id to use');
  408. }
  409. $data= array (
  410. 'sharedIpGroupId' => (integer) $groupId,
  411. 'configureServer' => $configure
  412. );
  413. $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/ips/public/'.rawurlencode($ip),'PUT',
  414. null,null,json_encode(array('shareIp' => $data)));
  415. $status = $result->getStatus();
  416. switch ($status) {
  417. case '202' : // break intentionally omitted
  418. return true;
  419. case '503' :
  420. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  421. break;
  422. case '401' :
  423. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  424. break;
  425. case '404' :
  426. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  427. break;
  428. case '413' :
  429. $this->errorMsg= self::ERROR_OVERLIMIT;
  430. break;
  431. default:
  432. $this->errorMsg= $result->getBody();
  433. break;
  434. }
  435. $this->errorCode= $status;
  436. return false;
  437. }
  438. /**
  439. * Unshare IP address for a server ($id)
  440. *
  441. * @param string $id
  442. * @param string $ip
  443. * @return boolean
  444. */
  445. public function unshareIpAddress($id,$ip)
  446. {
  447. if (empty($id)) {
  448. require_once 'Zend/Service/Rackspace/Exception.php';
  449. throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
  450. }
  451. if (empty($ip)) {
  452. require_once 'Zend/Service/Rackspace/Exception.php';
  453. throw new Zend_Service_Rackspace_Exception('You didn\'t specified the IP address to share');
  454. }
  455. $validator = new Zend_Validate_Ip();
  456. if (!$validator->isValid($ip)) {
  457. require_once 'Zend/Service/Rackspace/Exception.php';
  458. throw new Zend_Service_Rackspace_Exception("The parameter $ip specified is not a valid IP address");
  459. }
  460. $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/ips/public/'.rawurlencode($ip),
  461. 'DELETE');
  462. $status = $result->getStatus();
  463. switch ($status) {
  464. case '202' : // break intentionally omitted
  465. return true;
  466. case '503' :
  467. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  468. break;
  469. case '401' :
  470. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  471. break;
  472. case '404' :
  473. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  474. break;
  475. case '413' :
  476. $this->errorMsg= self::ERROR_OVERLIMIT;
  477. break;
  478. default:
  479. $this->errorMsg= $result->getBody();
  480. break;
  481. }
  482. $this->errorCode= $status;
  483. return false;
  484. }
  485. /**
  486. * Reboot a server
  487. *
  488. * $hard true is the equivalent of power cycling the server
  489. * $hard false is a graceful shutdown
  490. *
  491. * @param string $id
  492. * @param boolean $hard
  493. * @return boolean
  494. */
  495. public function rebootServer($id,$hard=false)
  496. {
  497. if (empty($id)) {
  498. require_once 'Zend/Service/Rackspace/Exception.php';
  499. throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
  500. }
  501. if (!$hard) {
  502. $type= 'SOFT';
  503. } else {
  504. $type= 'HARD';
  505. }
  506. $data= array (
  507. 'reboot' => array (
  508. 'type' => $type
  509. )
  510. );
  511. $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
  512. 'POST', null, null, json_encode($data));
  513. $status = $result->getStatus();
  514. switch ($status) {
  515. case '200' :
  516. case '202' : // break intentionally omitted
  517. return true;
  518. case '503' :
  519. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  520. break;
  521. case '401' :
  522. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  523. break;
  524. case '404' :
  525. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  526. break;
  527. case '409' :
  528. $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
  529. break;
  530. case '413' :
  531. $this->errorMsg= self::ERROR_OVERLIMIT;
  532. break;
  533. default:
  534. $this->errorMsg= $result->getBody();
  535. break;
  536. }
  537. $this->errorCode= $status;
  538. return false;
  539. }
  540. /**
  541. * Rebuild a server
  542. *
  543. * The rebuild function removes all data on the server and replaces it with the specified image,
  544. * serverId and IP addresses will remain the same.
  545. *
  546. * @param string $id
  547. * @param string $imageId
  548. * @return boolean
  549. */
  550. public function rebuildServer($id,$imageId)
  551. {
  552. if (empty($id)) {
  553. require_once 'Zend/Service/Rackspace/Exception.php';
  554. throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
  555. }
  556. if (empty($imageId)) {
  557. require_once 'Zend/Service/Rackspace/Exception.php';
  558. throw new Zend_Service_Rackspace_Exception('You didn\'t specified the new imageId of the server');
  559. }
  560. $data= array (
  561. 'rebuild' => array (
  562. 'imageId' => (integer) $imageId
  563. )
  564. );
  565. $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
  566. 'POST', null, null, json_encode($data));
  567. $status = $result->getStatus();
  568. switch ($status) {
  569. case '202' : // break intentionally omitted
  570. return true;
  571. case '503' :
  572. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  573. break;
  574. case '401' :
  575. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  576. break;
  577. case '404' :
  578. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  579. break;
  580. case '409' :
  581. $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
  582. break;
  583. case '413' :
  584. $this->errorMsg= self::ERROR_OVERLIMIT;
  585. break;
  586. default:
  587. $this->errorMsg= $result->getBody();
  588. break;
  589. }
  590. $this->errorCode= $status;
  591. return false;
  592. }
  593. /**
  594. * Resize a server
  595. *
  596. * The resize function converts an existing server to a different flavor, in essence, scaling the
  597. * server up or down. The original server is saved for a period of time to allow rollback if there
  598. * is a problem. All resizes should be tested and explicitly confirmed, at which time the original
  599. * server is removed. All resizes are automatically confirmed after 24 hours if they are not
  600. * explicitly confirmed or reverted.
  601. *
  602. * @param string $id
  603. * @param string $flavorId
  604. * @return boolean
  605. */
  606. public function resizeServer($id,$flavorId)
  607. {
  608. if (empty($id)) {
  609. require_once 'Zend/Service/Rackspace/Exception.php';
  610. throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
  611. }
  612. if (empty($flavorId)) {
  613. require_once 'Zend/Service/Rackspace/Exception.php';
  614. throw new Zend_Service_Rackspace_Exception('You didn\'t specified the new flavorId of the server');
  615. }
  616. $data= array (
  617. 'resize' => array (
  618. 'flavorId' => (integer) $flavorId
  619. )
  620. );
  621. $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
  622. 'POST', null, null, json_encode($data));
  623. $status = $result->getStatus();
  624. switch ($status) {
  625. case '202' : // break intentionally omitted
  626. return true;
  627. case '503' :
  628. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  629. break;
  630. case '401' :
  631. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  632. break;
  633. case '403' :
  634. $this->errorMsg= self::ERROR_RESIZE_NOT_ALLOWED;
  635. break;
  636. case '404' :
  637. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  638. break;
  639. case '409' :
  640. $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
  641. break;
  642. case '413' :
  643. $this->errorMsg= self::ERROR_OVERLIMIT;
  644. break;
  645. default:
  646. $this->errorMsg= $result->getBody();
  647. break;
  648. }
  649. $this->errorCode= $status;
  650. return false;
  651. }
  652. /**
  653. * Confirm resize of a server
  654. *
  655. * During a resize operation, the original server is saved for a period of time to allow roll
  656. * back if there is a problem. Once the newly resized server is tested and has been confirmed
  657. * to be functioning properly, use this operation to confirm the resize. After confirmation,
  658. * the original server is removed and cannot be rolled back to. All resizes are automatically
  659. * confirmed after 24 hours if they are not explicitly confirmed or reverted.
  660. *
  661. * @param string $id
  662. * @return boolean
  663. */
  664. public function confirmResizeServer($id)
  665. {
  666. if (empty($id)) {
  667. require_once 'Zend/Service/Rackspace/Exception.php';
  668. throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
  669. }
  670. $data= array (
  671. 'confirmResize' => null
  672. );
  673. $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
  674. 'POST', null, null, json_encode($data));
  675. $status = $result->getStatus();
  676. switch ($status) {
  677. case '204' : // break intentionally omitted
  678. return true;
  679. case '503' :
  680. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  681. break;
  682. case '401' :
  683. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  684. break;
  685. case '403' :
  686. $this->errorMsg= self::ERROR_RESIZE_NOT_ALLOWED;
  687. break;
  688. case '404' :
  689. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  690. break;
  691. case '409' :
  692. $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
  693. break;
  694. case '413' :
  695. $this->errorMsg= self::ERROR_OVERLIMIT;
  696. break;
  697. default:
  698. $this->errorMsg= $result->getBody();
  699. break;
  700. }
  701. $this->errorCode= $status;
  702. return false;
  703. }
  704. /**
  705. * Revert resize of a server
  706. *
  707. * During a resize operation, the original server is saved for a period of time to allow for roll
  708. * back if there is a problem. If you determine there is a problem with a newly resized server,
  709. * use this operation to revert the resize and roll back to the original server. All resizes are
  710. * automatically confirmed after 24 hours if they have not already been confirmed explicitly or
  711. * reverted.
  712. *
  713. * @param string $id
  714. * @return boolean
  715. */
  716. public function revertResizeServer($id)
  717. {
  718. if (empty($id)) {
  719. require_once 'Zend/Service/Rackspace/Exception.php';
  720. throw new Zend_Service_Rackspace_Exception('You didn\'t specified the ID of the server');
  721. }
  722. $data= array (
  723. 'revertResize' => null
  724. );
  725. $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/action',
  726. 'POST', null, null, json_encode($data));
  727. $status = $result->getStatus();
  728. switch ($status) {
  729. case '202' : // break intentionally omitted
  730. return true;
  731. case '503' :
  732. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  733. break;
  734. case '401' :
  735. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  736. break;
  737. case '403' :
  738. $this->errorMsg= self::ERROR_RESIZE_NOT_ALLOWED;
  739. break;
  740. case '404' :
  741. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  742. break;
  743. case '409' :
  744. $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
  745. break;
  746. case '413' :
  747. $this->errorMsg= self::ERROR_OVERLIMIT;
  748. break;
  749. default:
  750. $this->errorMsg= $result->getBody();
  751. break;
  752. }
  753. $this->errorCode= $status;
  754. return false;
  755. }
  756. /**
  757. * Get the list of the flavors
  758. *
  759. * If $details is true returns detail info
  760. *
  761. * @param boolean $details
  762. * @return array|boolean
  763. */
  764. public function listFlavors($details=false)
  765. {
  766. $url= '/flavors';
  767. if ($details) {
  768. $url.= '/detail';
  769. }
  770. $result= $this->httpCall($this->getManagementUrl().$url,'GET');
  771. $status= $result->getStatus();
  772. switch ($status) {
  773. case '200' :
  774. case '203' : // break intentionally omitted
  775. $flavors= json_decode($result->getBody(),true);
  776. return $flavors['flavors'];
  777. case '503' :
  778. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  779. break;
  780. case '401' :
  781. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  782. break;
  783. case '413' :
  784. $this->errorMsg= self::ERROR_OVERLIMIT;
  785. break;
  786. default:
  787. $this->errorMsg= $result->getBody();
  788. break;
  789. }
  790. $this->errorCode= $status;
  791. return false;
  792. }
  793. /**
  794. * Get the detail of a flavor
  795. *
  796. * @param string $flavorId
  797. * @return array|boolean
  798. */
  799. public function getFlavor($flavorId)
  800. {
  801. if (empty($flavorId)) {
  802. require_once 'Zend/Service/Rackspace/Exception.php';
  803. throw new Zend_Service_Rackspace_Exception('You didn\'t specified the new flavorId of the server');
  804. }
  805. $result= $this->httpCall($this->getManagementUrl().'/flavors/'.rawurlencode($flavorId),'GET');
  806. $status= $result->getStatus();
  807. switch ($status) {
  808. case '200' :
  809. case '203' : // break intentionally omitted
  810. $flavor= json_decode($result->getBody(),true);
  811. return $flavor['flavor'];
  812. case '503' :
  813. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  814. break;
  815. case '401' :
  816. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  817. break;
  818. case '413' :
  819. $this->errorMsg= self::ERROR_OVERLIMIT;
  820. break;
  821. default:
  822. $this->errorMsg= $result->getBody();
  823. break;
  824. }
  825. $this->errorCode= $status;
  826. return false;
  827. }
  828. /**
  829. * Get the list of the images
  830. *
  831. * @param boolean $details
  832. * @return Zend_Service_Rackspace_Servers_ImageList|boolean
  833. */
  834. public function listImages($details=false)
  835. {
  836. $url= '/images';
  837. if ($details) {
  838. $url.= '/detail';
  839. }
  840. $result= $this->httpCall($this->getManagementUrl().$url,'GET');
  841. $status= $result->getStatus();
  842. switch ($status) {
  843. case '200' :
  844. case '203' : // break intentionally omitted
  845. $images= json_decode($result->getBody(),true);
  846. return new Zend_Service_Rackspace_Servers_ImageList($this,$images['images']);
  847. case '503' :
  848. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  849. break;
  850. case '401' :
  851. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  852. break;
  853. case '413' :
  854. $this->errorMsg= self::ERROR_OVERLIMIT;
  855. break;
  856. default:
  857. $this->errorMsg= $result->getBody();
  858. break;
  859. }
  860. $this->errorCode= $status;
  861. return false;
  862. }
  863. /**
  864. * Get detail about an image
  865. *
  866. * @param string $id
  867. * @return Zend_Service_Rackspace_Servers_Image|boolean
  868. */
  869. public function getImage($id)
  870. {
  871. $result= $this->httpCall($this->getManagementUrl().'/images/'.rawurlencode($id),'GET');
  872. $status= $result->getStatus();
  873. switch ($status) {
  874. case '200' :
  875. case '203' : // break intentionally omitted
  876. $image= json_decode($result->getBody(),true);
  877. return new Zend_Service_Rackspace_Servers_Image($this,$image['image']);
  878. case '503' :
  879. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  880. break;
  881. case '401' :
  882. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  883. break;
  884. case '404' :
  885. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  886. break;
  887. case '413' :
  888. $this->errorMsg= self::ERROR_OVERLIMIT;
  889. break;
  890. default:
  891. $this->errorMsg= $result->getBody();
  892. break;
  893. }
  894. $this->errorCode= $status;
  895. return false;
  896. }
  897. /**
  898. * Create an image for a serverId
  899. *
  900. * @param string $serverId
  901. * @param string $name
  902. * @return Zend_Service_Rackspace_Servers_Image
  903. */
  904. public function createImage($serverId,$name)
  905. {
  906. if (empty($serverId)) {
  907. require_once 'Zend/Service/Rackspace/Exception.php';
  908. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_SERVERID);
  909. }
  910. if (empty($name)) {
  911. require_once 'Zend/Service/Rackspace/Exception.php';
  912. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME);
  913. }
  914. $data = array(
  915. 'image' => array (
  916. 'serverId' => (integer) $serverId,
  917. 'name' => $name
  918. )
  919. );
  920. $result = $this->httpCall($this->getManagementUrl().'/images', 'POST',
  921. null, null, json_encode($data));
  922. $status = $result->getStatus();
  923. switch ($status) {
  924. case '202' : // break intentionally omitted
  925. $image= json_decode($result->getBody(),true);
  926. return new Zend_Service_Rackspace_Servers_Image($this,$image['image']);
  927. case '503' :
  928. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  929. break;
  930. case '401' :
  931. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  932. break;
  933. case '403' :
  934. $this->errorMsg= self::ERROR_RESIZE_NOT_ALLOWED;
  935. break;
  936. case '404' :
  937. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  938. break;
  939. case '409' :
  940. $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
  941. break;
  942. case '413' :
  943. $this->errorMsg= self::ERROR_OVERLIMIT;
  944. break;
  945. default:
  946. $this->errorMsg= $result->getBody();
  947. break;
  948. }
  949. $this->errorCode= $status;
  950. return false;
  951. }
  952. /**
  953. * Delete an image
  954. *
  955. * @param string $id
  956. * @return boolean
  957. */
  958. public function deleteImage($id)
  959. {
  960. if (empty($id)) {
  961. require_once 'Zend/Service/Rackspace/Exception.php';
  962. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
  963. }
  964. $result = $this->httpCall($this->getManagementUrl().'/images/'.rawurlencode($id),'DELETE');
  965. $status = $result->getStatus();
  966. switch ($status) {
  967. case '204' : // break intentionally omitted
  968. return true;
  969. case '503' :
  970. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  971. break;
  972. case '401' :
  973. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  974. break;
  975. case '404' :
  976. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  977. break;
  978. case '413' :
  979. $this->errorMsg= self::ERROR_OVERLIMIT;
  980. break;
  981. default:
  982. $this->errorMsg= $result->getBody();
  983. break;
  984. }
  985. $this->errorCode= $status;
  986. return false;
  987. }
  988. /**
  989. * Get the backup schedule of a server
  990. *
  991. * @param string $id server's Id
  992. * @return array|boolean
  993. */
  994. public function getBackupSchedule($id)
  995. {
  996. if (empty($id)) {
  997. require_once 'Zend/Service/Rackspace/Exception.php';
  998. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
  999. }
  1000. $result= $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/backup_schedule',
  1001. 'GET');
  1002. $status= $result->getStatus();
  1003. switch ($status) {
  1004. case '200' :
  1005. case '203' : // break intentionally omitted
  1006. $backup = json_decode($result->getBody(),true);
  1007. return $image['backupSchedule'];
  1008. case '503' :
  1009. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  1010. break;
  1011. case '401' :
  1012. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  1013. break;
  1014. case '404' :
  1015. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  1016. break;
  1017. case '413' :
  1018. $this->errorMsg= self::ERROR_OVERLIMIT;
  1019. break;
  1020. default:
  1021. $this->errorMsg= $result->getBody();
  1022. break;
  1023. }
  1024. $this->errorCode= $status;
  1025. return false;
  1026. }
  1027. /**
  1028. * Change the backup schedule of a server
  1029. *
  1030. * @param string $id server's Id
  1031. * @param string $weekly
  1032. * @param string $daily
  1033. * @return boolean
  1034. */
  1035. public function changeBackupSchedule($id,$weekly,$daily)
  1036. {
  1037. if (empty($id)) {
  1038. require_once 'Zend/Service/Rackspace/Exception.php';
  1039. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
  1040. }
  1041. if (empty($weekly)) {
  1042. require_once 'Zend/Service/Rackspace/Exception.php';
  1043. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_WEEKLY);
  1044. }
  1045. if (empty($daily)) {
  1046. require_once 'Zend/Service/Rackspace/Exception.php';
  1047. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_DAILY);
  1048. }
  1049. $data = array (
  1050. 'backupSchedule' => array (
  1051. 'enabled' => true,
  1052. 'weekly' => $weekly,
  1053. 'daily' => $daily
  1054. )
  1055. );
  1056. $result= $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/backup_schedule',
  1057. 'POST',null,null,json_encode($data));
  1058. $status= $result->getStatus();
  1059. switch ($status) {
  1060. case '204' : // break intentionally omitted
  1061. return true;
  1062. case '503' :
  1063. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  1064. break;
  1065. case '401' :
  1066. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  1067. break;
  1068. case '404' :
  1069. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  1070. break;
  1071. case '409' :
  1072. $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
  1073. break;
  1074. case '413' :
  1075. $this->errorMsg= self::ERROR_OVERLIMIT;
  1076. break;
  1077. default:
  1078. $this->errorMsg= $result->getBody();
  1079. break;
  1080. }
  1081. $this->errorCode= $status;
  1082. return false;
  1083. }
  1084. /**
  1085. * Disable the backup schedule for a server
  1086. *
  1087. * @param string $id server's Id
  1088. * @return boolean
  1089. */
  1090. public function disableBackupSchedule($id)
  1091. {
  1092. if (empty($id)) {
  1093. require_once 'Zend/Service/Rackspace/Exception.php';
  1094. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
  1095. }
  1096. $result = $this->httpCall($this->getManagementUrl().'/servers/'.rawurlencode($id).'/backup_schedule',
  1097. 'DELETE');
  1098. $status = $result->getStatus();
  1099. switch ($status) {
  1100. case '204' : // break intentionally omitted
  1101. return true;
  1102. case '503' :
  1103. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  1104. break;
  1105. case '401' :
  1106. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  1107. break;
  1108. case '404' :
  1109. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  1110. break;
  1111. case '409' :
  1112. $this->errorMsg= self::ERROR_BUILD_IN_PROGRESS;
  1113. break;
  1114. case '413' :
  1115. $this->errorMsg= self::ERROR_OVERLIMIT;
  1116. break;
  1117. default:
  1118. $this->errorMsg= $result->getBody();
  1119. break;
  1120. }
  1121. $this->errorCode= $status;
  1122. return false;
  1123. }
  1124. /**
  1125. * Get the list of shared IP groups
  1126. *
  1127. * @param boolean $details
  1128. * @return Zend_Service_Rackspace_Servers_SharedIpGroupList|boolean
  1129. */
  1130. public function listSharedIpGroups($details=false)
  1131. {
  1132. $url= '/shared_ip_groups';
  1133. if ($details) {
  1134. $url.= '/detail';
  1135. }
  1136. $result= $this->httpCall($this->getManagementUrl().$url,'GET');
  1137. $status= $result->getStatus();
  1138. switch ($status) {
  1139. case '200' :
  1140. case '203' : // break intentionally omitted
  1141. $groups= json_decode($result->getBody(),true);
  1142. return new Zend_Service_Rackspace_Servers_SharedIpGroupList($this,$groups['sharedIpGroups']);
  1143. case '503' :
  1144. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  1145. break;
  1146. case '401' :
  1147. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  1148. break;
  1149. case '413' :
  1150. $this->errorMsg= self::ERROR_OVERLIMIT;
  1151. break;
  1152. default:
  1153. $this->errorMsg= $result->getBody();
  1154. break;
  1155. }
  1156. $this->errorCode= $status;
  1157. return false;
  1158. }
  1159. /**
  1160. * Get the shared IP group
  1161. *
  1162. * @param integer $id
  1163. * @return Zend_Service_Rackspace_Servers_SharedIpGroup|boolean
  1164. */
  1165. public function getSharedIpGroup($id)
  1166. {
  1167. if (empty($id)) {
  1168. require_once 'Zend/Service/Rackspace/Exception.php';
  1169. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
  1170. }
  1171. $result= $this->httpCall($this->getManagementUrl().'/shared_ip_groups/'.rawurlencode($id),'GET');
  1172. $status= $result->getStatus();
  1173. switch ($status) {
  1174. case '200' :
  1175. case '203' : // break intentionally omitted
  1176. $group= json_decode($result->getBody(),true);
  1177. return new Zend_Service_Rackspace_Servers_SharedIpGroup($this,$group['sharedIpGroup']);
  1178. case '503' :
  1179. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  1180. break;
  1181. case '401' :
  1182. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  1183. break;
  1184. case '404' :
  1185. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  1186. break;
  1187. case '413' :
  1188. $this->errorMsg= self::ERROR_OVERLIMIT;
  1189. break;
  1190. default:
  1191. $this->errorMsg= $result->getBody();
  1192. break;
  1193. }
  1194. $this->errorCode= $status;
  1195. return false;
  1196. }
  1197. /**
  1198. * Create a shared Ip group
  1199. *
  1200. * @param string $name
  1201. * @param string $serverId
  1202. * @return array|boolean
  1203. */
  1204. public function createSharedIpGroup($name,$serverId)
  1205. {
  1206. if (empty($name)) {
  1207. require_once 'Zend/Service/Rackspace/Exception.php';
  1208. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_NAME);
  1209. }
  1210. if (empty($serverId)) {
  1211. require_once 'Zend/Service/Rackspace/Exception.php';
  1212. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
  1213. }
  1214. $data = array (
  1215. 'sharedIpGroup' => array (
  1216. 'name' => $name,
  1217. 'server' => (integer) $serverId
  1218. )
  1219. );
  1220. $result= $this->httpCall($this->getManagementUrl().'/shared_ip_groups',
  1221. 'POST',null,null,json_encode($data));
  1222. $status= $result->getStatus();
  1223. switch ($status) {
  1224. case '201' : // break intentionally omitted
  1225. $group = json_decode($result->getBody(),true);
  1226. return new Zend_Service_Rackspace_Servers_SharedIpGroup($this,$group['sharedIpGroup']);
  1227. case '503' :
  1228. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  1229. break;
  1230. case '401' :
  1231. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  1232. break;
  1233. case '413' :
  1234. $this->errorMsg= self::ERROR_OVERLIMIT;
  1235. break;
  1236. default:
  1237. $this->errorMsg= $result->getBody();
  1238. break;
  1239. }
  1240. $this->errorCode= $status;
  1241. return false;
  1242. }
  1243. /**
  1244. * Delete a Shared Ip Group
  1245. *
  1246. * @param integer $id
  1247. * @return boolean
  1248. */
  1249. public function deleteSharedIpGroup($id)
  1250. {
  1251. if (empty($id)) {
  1252. require_once 'Zend/Service/Rackspace/Exception.php';
  1253. throw new Zend_Service_Rackspace_Exception(self::ERROR_PARAM_NO_ID);
  1254. }
  1255. $result= $this->httpCall($this->getManagementUrl().'/shared_ip_groups/'.rawurlencode($id),'DELETE');
  1256. $status= $result->getStatus();
  1257. switch ($status) {
  1258. case '204' : // break intentionally omitted
  1259. return true;
  1260. case '503' :
  1261. $this->errorMsg= self::ERROR_SERVICE_UNAVAILABLE;
  1262. break;
  1263. case '401' :
  1264. $this->errorMsg= self::ERROR_UNAUTHORIZED;
  1265. break;
  1266. case '404' :
  1267. $this->errorMsg= self::ERROR_ITEM_NOT_FOUND;
  1268. break;
  1269. case '413' :
  1270. $this->errorMsg= self::ERROR_OVERLIMIT;
  1271. break;
  1272. default:
  1273. $this->errorMsg= $result->getBody();
  1274. break;
  1275. }
  1276. $this->errorCode= $status;
  1277. return false;
  1278. }
  1279. }