SharedIpGroup.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_SharedIpGroup
  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 image\'s name in the array (name)';
  26. const ERROR_PARAM_NO_ID = 'You must pass the image\'s id in the array (id)';
  27. const ERROR_PARAM_NO_SERVERS = 'The servers parameter must be an array of Ids';
  28. /**
  29. * Name of the shared IP group
  30. *
  31. * @var string
  32. */
  33. protected $name;
  34. /**
  35. * Id of the shared IP group
  36. *
  37. * @var string
  38. */
  39. protected $id;
  40. /**
  41. * Array of servers of the shared IP group
  42. *
  43. * @var array
  44. */
  45. protected $serversId = array();
  46. /**
  47. * The service that has created the image object
  48. *
  49. * @var Zend_Service_Rackspace_Servers
  50. */
  51. protected $service;
  52. /**
  53. * Construct
  54. *
  55. * @param Zend_Service_Rackspace_Servers $service
  56. * @param array $data
  57. * @return void
  58. */
  59. public function __construct($service, $data)
  60. {
  61. if (!($service instanceof Zend_Service_Rackspace_Servers) || !is_array($data)) {
  62. require_once 'Zend/Service/Rackspace/Servers/Exception.php';
  63. throw new Zend_Service_Rackspace_Servers_Exception(self::ERROR_PARAM_CONSTRUCT);
  64. }
  65. if (!array_key_exists('name', $data)) {
  66. require_once 'Zend/Service/Rackspace/Servers/Exception.php';
  67. throw new Zend_Service_Rackspace_Servers_Exception(self::ERROR_PARAM_NO_NAME);
  68. }
  69. if (!array_key_exists('id', $data)) {
  70. require_once 'Zend/Service/Rackspace/Servers/Exception.php';
  71. throw new Zend_Service_Rackspace_Servers_Exception(self::ERROR_PARAM_NO_ID);
  72. }
  73. if (isset($data['servers']) && !is_array($data['servers'])) {
  74. require_once 'Zend/Service/Rackspace/Servers/Exception.php';
  75. throw new Zend_Service_Rackspace_Servers_Exception(self::ERROR_PARAM_NO_SERVERS);
  76. }
  77. $this->service= $service;
  78. $this->name = $data['name'];
  79. $this->id = $data['id'];
  80. if (isset($data['servers'])) {
  81. $this->serversId= $data['servers'];
  82. }
  83. }
  84. /**
  85. * Get the name of the shared IP group
  86. *
  87. * @return string
  88. */
  89. public function getName()
  90. {
  91. return $this->name;
  92. }
  93. /**
  94. * Get the id of the shared IP group
  95. *
  96. * @return string
  97. */
  98. public function getId()
  99. {
  100. return $this->id;
  101. }
  102. /**
  103. * Get the server's array of the shared IP group
  104. *
  105. * @return string
  106. */
  107. public function getServersId()
  108. {
  109. if (empty($this->serversId)) {
  110. $info= $this->service->getSharedIpGroup($this->id);
  111. if (($info!==false)) {
  112. $info= $info->toArray();
  113. if (isset($info['servers'])) {
  114. $this->serversId= $info['servers'];
  115. }
  116. }
  117. }
  118. return $this->serversId;
  119. }
  120. /**
  121. * Get the server
  122. *
  123. * @param integer $id
  124. * @return Zend_Service_Rackspace_Servers_Server|boolean
  125. */
  126. public function getServer($id)
  127. {
  128. if (empty($this->serversId)) {
  129. $this->getServersId();
  130. }
  131. if (in_array($id,$this->serversId)) {
  132. return $this->service->getServer($id);
  133. }
  134. return false;
  135. }
  136. /**
  137. * Create a server in the shared Ip Group
  138. *
  139. * @param array $data
  140. * @param array $metadata
  141. * @param array $files
  142. * @return Zend_Service_Rackspace_Servers_Server|boolean
  143. */
  144. public function createServer(array $data, $metadata=array(),$files=array())
  145. {
  146. $data['sharedIpGroupId']= (integer) $this->id;
  147. return $this->service->createServer($data,$metadata,$files);
  148. }
  149. /**
  150. * To Array
  151. *
  152. * @return array
  153. */
  154. public function toArray()
  155. {
  156. return array (
  157. 'name' => $this->name,
  158. 'id' => $this->id,
  159. 'servers' => $this->serversId
  160. );
  161. }
  162. }