Elasticip.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_Amazon
  17. * @subpackage Ec2
  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. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Service_Amazon_Ec2_Abstract
  24. */
  25. require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
  26. /**
  27. * An Amazon EC2 interface to allocate, associate, describe and release Elastic IP address
  28. * from your account.
  29. *
  30. * @category Zend
  31. * @package Zend_Service_Amazon
  32. * @subpackage Ec2
  33. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Service_Amazon_Ec2_Elasticip extends Zend_Service_Amazon_Ec2_Abstract
  37. {
  38. /**
  39. * Acquires an elastic IP address for use with your account
  40. *
  41. * @return string Returns the newly Allocated IP Address
  42. */
  43. public function allocate()
  44. {
  45. $params = array();
  46. $params['Action'] = 'AllocateAddress';
  47. $response = $this->sendRequest($params);
  48. $xpath = $response->getXPath();
  49. $ip = $xpath->evaluate('string(//ec2:publicIp/text())');
  50. return $ip;
  51. }
  52. /**
  53. * Lists elastic IP addresses assigned to your account.
  54. *
  55. * @param string|array $publicIp Elastic IP or list of addresses to describe.
  56. * @return array
  57. */
  58. public function describe($publicIp = null)
  59. {
  60. $params = array();
  61. $params['Action'] = 'DescribeAddresses';
  62. if(is_array($publicIp) && !empty($publicIp)) {
  63. foreach($publicIp as $k=>$name) {
  64. $params['PublicIp.' . ($k+1)] = $name;
  65. }
  66. } elseif($publicIp) {
  67. $params['PublicIp.1'] = $publicIp;
  68. }
  69. $response = $this->sendRequest($params);
  70. $xpath = $response->getXPath();
  71. $nodes = $xpath->query('//ec2:item');
  72. $return = array();
  73. foreach ($nodes as $k => $node) {
  74. $item = array();
  75. $item['publicIp'] = $xpath->evaluate('string(ec2:publicIp/text())', $node);
  76. $item['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $node);
  77. $return[] = $item;
  78. unset($item);
  79. }
  80. return $return;
  81. }
  82. /**
  83. * Releases an elastic IP address that is associated with your account
  84. *
  85. * @param string $publicIp IP address that you are releasing from your account.
  86. * @return boolean
  87. */
  88. public function release($publicIp)
  89. {
  90. $params = array();
  91. $params['Action'] = 'ReleaseAddress';
  92. $params['PublicIp'] = $publicIp;
  93. $response = $this->sendRequest($params);
  94. $xpath = $response->getXPath();
  95. $return = $xpath->evaluate('string(//ec2:return/text())');
  96. return ($return === "true");
  97. }
  98. /**
  99. * Associates an elastic IP address with an instance
  100. *
  101. * @param string $instanceId The instance to which the IP address is assigned
  102. * @param string $publicIp IP address that you are assigning to the instance.
  103. * @return boolean
  104. */
  105. public function associate($instanceId, $publicIp)
  106. {
  107. $params = array();
  108. $params['Action'] = 'AssociateAddress';
  109. $params['PublicIp'] = $publicIp;
  110. $params['InstanceId'] = $instanceId;
  111. $response = $this->sendRequest($params);
  112. $xpath = $response->getXPath();
  113. $return = $xpath->evaluate('string(//ec2:return/text())');
  114. return ($return === "true");
  115. }
  116. /**
  117. * Disassociates the specified elastic IP address from the instance to which it is assigned.
  118. * This is an idempotent operation. If you enter it more than once, Amazon EC2 does not return an error.
  119. *
  120. * @param string $publicIp IP address that you are disassociating from the instance.
  121. * @return boolean
  122. */
  123. public function disassocate($publicIp)
  124. {
  125. $params = array();
  126. $params['Action'] = 'DisssociateAddress';
  127. $params['PublicIp'] = $publicIp;
  128. $response = $this->sendRequest($params);
  129. $xpath = $response->getXPath();
  130. $return = $xpath->evaluate('string(//ec2:return/text())');
  131. return ($return === "true");
  132. }
  133. }