Reserved.php 5.5 KB

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