File indexing completed on 2025-03-02 05:29:46

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service_Amazon
0017  * @subpackage Ec2
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Service_Amazon_Ec2_Abstract
0025  */
0026 // require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
0027 
0028 /**
0029  * Allows you to interface with the reserved instances on Amazon Ec2
0030  *
0031  * @category   Zend
0032  * @package    Zend_Service_Amazon
0033  * @subpackage Ec2
0034  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0035  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0036  */
0037 class Zend_Service_Amazon_Ec2_Instance_Reserved extends Zend_Service_Amazon_Ec2_Abstract
0038 {
0039     /**
0040      * Describes Reserved Instances that you purchased.
0041      *
0042      * @param string|array $instanceId        IDs of the Reserved Instance to describe.
0043      * @return array
0044      */
0045     public function describeInstances($instanceId)
0046     {
0047         $params = array();
0048         $params['Action'] = 'DescribeReservedInstances';
0049 
0050         if(is_array($instanceId) && !empty($instanceId)) {
0051             foreach($instanceId as $k=>$name) {
0052                 $params['ReservedInstancesId.' . ($k+1)] = $name;
0053             }
0054         } elseif($instanceId) {
0055             $params['ReservedInstancesId.1'] = $instanceId;
0056         }
0057 
0058         $response = $this->sendRequest($params);
0059 
0060         $xpath = $response->getXPath();
0061         $items = $xpath->query('//ec2:reservedInstancesSet/ec2:item');
0062 
0063         $return = array();
0064         foreach($items as $item) {
0065             $i = array();
0066             $i['reservedInstancesId'] = $xpath->evaluate('string(ec2:reservedInstancesId/text())', $item);
0067             $i['instanceType'] = $xpath->evaluate('string(ec2:instanceType/text())', $item);
0068             $i['availabilityZone'] = $xpath->evaluate('string(ec2:availabilityZone/text())', $item);
0069             $i['duration'] = $xpath->evaluate('string(ec2:duration/text())', $item);
0070             $i['fixedPrice'] = $xpath->evaluate('string(ec2:fixedPrice/text())', $item);
0071             $i['usagePrice'] = $xpath->evaluate('string(ec2:usagePrice/text())', $item);
0072             $i['productDescription'] = $xpath->evaluate('string(ec2:productDescription/text())', $item);
0073             $i['instanceCount'] = $xpath->evaluate('string(ec2:instanceCount/text())', $item);
0074             $i['state'] = $xpath->evaluate('string(ec2:state/text())', $item);
0075 
0076             $return[] = $i;
0077             unset($i);
0078         }
0079 
0080         return $return;
0081     }
0082 
0083     /**
0084      * Describes Reserved Instance offerings that are available for purchase.
0085      * With Amazon EC2 Reserved Instances, you purchase the right to launch Amazon
0086      * EC2 instances for a period of time (without getting insufficient capacity
0087      * errors) and pay a lower usage rate for the actual time used.
0088      *
0089      * @return array
0090      */
0091     public function describeOfferings()
0092     {
0093         $params = array();
0094         $params['Action'] = 'DescribeReservedInstancesOfferings';
0095 
0096         $response = $this->sendRequest($params);
0097 
0098         $xpath = $response->getXPath();
0099         $items = $xpath->query('//ec2:reservedInstancesOfferingsSet/ec2:item');
0100 
0101         $return = array();
0102         foreach($items as $item) {
0103             $i = array();
0104             $i['reservedInstancesOfferingId'] = $xpath->evaluate('string(ec2:reservedInstancesOfferingId/text())', $item);
0105             $i['instanceType'] = $xpath->evaluate('string(ec2:instanceType/text())', $item);
0106             $i['availabilityZone'] = $xpath->evaluate('string(ec2:availabilityZone/text())', $item);
0107             $i['duration'] = $xpath->evaluate('string(ec2:duration/text())', $item);
0108             $i['fixedPrice'] = $xpath->evaluate('string(ec2:fixedPrice/text())', $item);
0109             $i['usagePrice'] = $xpath->evaluate('string(ec2:usagePrice/text())', $item);
0110             $i['productDescription'] = $xpath->evaluate('string(ec2:productDescription/text())', $item);
0111 
0112             $return[] = $i;
0113             unset($i);
0114         }
0115 
0116         return $return;
0117     }
0118 
0119     /**
0120      * Purchases a Reserved Instance for use with your account. With Amazon EC2
0121      * Reserved Instances, you purchase the right to launch Amazon EC2 instances
0122      * for a period of time (without getting insufficient capacity errors) and
0123      * pay a lower usage rate for the actual time used.
0124      *
0125      * @param string $offeringId            The offering ID of the Reserved Instance to purchase
0126      * @param integer $intanceCount         The number of Reserved Instances to purchase.
0127      * @return string                       The ID of the purchased Reserved Instances.
0128      */
0129     public function purchaseOffering($offeringId, $intanceCount = 1)
0130     {
0131         $params = array();
0132         $params['Action'] = 'PurchaseReservedInstancesOffering';
0133         $params['OfferingId.1'] = $offeringId;
0134         $params['instanceCount.1'] = intval($intanceCount);
0135 
0136         $response = $this->sendRequest($params);
0137 
0138         $xpath = $response->getXPath();
0139         $reservedInstancesId = $xpath->evaluate('string(//ec2:reservedInstancesId/text())');
0140 
0141         return $reservedInstancesId;
0142     }
0143 }