Reserved.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * Allows you to interface with the reserved instances on Amazon Ec2
  28. *
  29. * @category Zend
  30. * @package Zend_Service_Amazon
  31. * @subpackage Ec2
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Service_Amazon_Ec2_Instance_Reserved extends Zend_Service_Amazon_Ec2_Abstract
  36. {
  37. /**
  38. * Describes Reserved Instances that you purchased.
  39. *
  40. * @param string|array $instanceId IDs of the Reserved Instance to describe.
  41. * @return array
  42. */
  43. public function describeInstances($instanceId)
  44. {
  45. $params = array();
  46. $params['Action'] = 'DescribeReservedInstances';
  47. if(is_array($instanceId) && !empty($instanceId)) {
  48. foreach($instanceId as $k=>$name) {
  49. $params['ReservedInstancesId.' . ($k+1)] = $name;
  50. }
  51. } elseif($instanceId) {
  52. $params['ReservedInstancesId.1'] = $instanceId;
  53. }
  54. $response = $this->sendRequest($params);
  55. $xpath = $response->getXPath();
  56. $items = $xpath->query('//ec2:reservedInstancesSet/ec2:item');
  57. $return = array();
  58. foreach($items as $item) {
  59. $i = array();
  60. $i['reservedInstancesId'] = $xpath->evaluate('string(ec2:reservedInstancesId/text())', $item);
  61. $i['instanceType'] = $xpath->evaluate('string(ec2:instanceType/text())', $item);
  62. $i['availabilityZone'] = $xpath->evaluate('string(ec2:availabilityZone/text())', $item);
  63. $i['duration'] = $xpath->evaluate('string(ec2:duration/text())', $item);
  64. $i['fixedPrice'] = $xpath->evaluate('string(ec2:fixedPrice/text())', $item);
  65. $i['usagePrice'] = $xpath->evaluate('string(ec2:usagePrice/text())', $item);
  66. $i['productDescription'] = $xpath->evaluate('string(ec2:productDescription/text())', $item);
  67. $i['instanceCount'] = $xpath->evaluate('string(ec2:instanceCount/text())', $item);
  68. $i['state'] = $xpath->evaluate('string(ec2:state/text())', $item);
  69. $return[] = $i;
  70. unset($i);
  71. }
  72. return $return;
  73. }
  74. /**
  75. * Describes Reserved Instance offerings that are available for purchase.
  76. * With Amazon EC2 Reserved Instances, you purchase the right to launch Amazon
  77. * EC2 instances for a period of time (without getting insufficient capacity
  78. * errors) and pay a lower usage rate for the actual time used.
  79. *
  80. * @return array
  81. */
  82. public function describeOfferings()
  83. {
  84. $params = array();
  85. $params['Action'] = 'DescribeReservedInstancesOfferings';
  86. $response = $this->sendRequest($params);
  87. $xpath = $response->getXPath();
  88. $items = $xpath->query('//ec2:reservedInstancesOfferingsSet/ec2:item');
  89. $return = array();
  90. foreach($items as $item) {
  91. $i = array();
  92. $i['reservedInstancesOfferingId'] = $xpath->evaluate('string(ec2:reservedInstancesOfferingId/text())', $item);
  93. $i['instanceType'] = $xpath->evaluate('string(ec2:instanceType/text())', $item);
  94. $i['availabilityZone'] = $xpath->evaluate('string(ec2:availabilityZone/text())', $item);
  95. $i['duration'] = $xpath->evaluate('string(ec2:duration/text())', $item);
  96. $i['fixedPrice'] = $xpath->evaluate('string(ec2:fixedPrice/text())', $item);
  97. $i['usagePrice'] = $xpath->evaluate('string(ec2:usagePrice/text())', $item);
  98. $i['productDescription'] = $xpath->evaluate('string(ec2:productDescription/text())', $item);
  99. $return[] = $i;
  100. unset($i);
  101. }
  102. return $return;
  103. }
  104. /**
  105. * Purchases a Reserved Instance for use with your account. With Amazon EC2
  106. * Reserved Instances, you purchase the right to launch Amazon EC2 instances
  107. * for a period of time (without getting insufficient capacity errors) and
  108. * pay a lower usage rate for the actual time used.
  109. *
  110. * @param string $offeringId The offering ID of the Reserved Instance to purchase
  111. * @param integer $intanceCount The number of Reserved Instances to purchase.
  112. * @return string The ID of the purchased Reserved Instances.
  113. */
  114. public function purchaseOffering($offeringId, $intanceCount = 1)
  115. {
  116. $params = array();
  117. $params['Action'] = 'PurchaseReservedInstancesOffering';
  118. $params['OfferingId.1'] = $offeringId;
  119. $params['instanceCount.1'] = intval($intanceCount);
  120. $response = $this->sendRequest($params);
  121. $xpath = $response->getXPath();
  122. $reservedInstancesId = $xpath->evaluate('string(//ec2:reservedInstancesId/text())');
  123. return $reservedInstancesId;
  124. }
  125. }