2
0

Elasticip.php 4.7 KB

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