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  * An Amazon EC2 interface to register, describe and deregister Amamzon Machine Instances (AMI)
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_Image extends Zend_Service_Amazon_Ec2_Abstract
0038 {
0039     /**
0040      * Registers an AMI with Amazon EC2. Images must be registered before
0041      * they can be launched.
0042      *
0043      * Each AMI is associated with an unique ID which is provided by the Amazon
0044      * EC2 service through the RegisterImage operation. During registration, Amazon
0045      * EC2 retrieves the specified image manifest from Amazon S3 and verifies that
0046      * the image is owned by the user registering the image.
0047      *
0048      * The image manifest is retrieved once and stored within the Amazon EC2.
0049      * Any modifications to an image in Amazon S3 invalidates this registration.
0050      * If you make changes to an image, deregister the previous image and register
0051      * the new image. For more information, see DeregisterImage.
0052      *
0053      * @param string $imageLocation         Full path to your AMI manifest in Amazon S3 storage.
0054      * @return string                       The ami fro the newly registred image;
0055      */
0056     public function register($imageLocation)
0057     {
0058         $params                 = array();
0059         $params['Action']       = 'RegisterImage';
0060         $params['ImageLocation']= $imageLocation;
0061 
0062         $response = $this->sendRequest($params);
0063         $xpath = $response->getXPath();
0064 
0065         $amiId = $xpath->evaluate('string(//ec2:imageId/text())');
0066 
0067         return $amiId;
0068     }
0069 
0070     /**
0071      * Returns information about AMIs, AKIs, and ARIs available to the user.
0072      * Information returned includes image type, product codes, architecture,
0073      * and kernel and RAM disk IDs. Images available to the user include public
0074      * images available for any user to launch, private images owned by the user
0075      * making the request, and private images owned by other users for which the
0076      * user has explicit launch permissions.
0077      *
0078      * Launch permissions fall into three categories:
0079      *      public: The owner of the AMI granted launch permissions for the AMI
0080      *              to the all group. All users have launch permissions for these AMIs.
0081      *      explicit: The owner of the AMI granted launch permissions to a specific user.
0082      *      implicit: A user has implicit launch permissions for all AMIs he or she owns.
0083      *
0084      * The list of AMIs returned can be modified by specifying AMI IDs, AMI owners,
0085      * or users with launch permissions. If no options are specified, Amazon EC2 returns
0086      * all AMIs for which the user has launch permissions.
0087      *
0088      * If you specify one or more AMI IDs, only AMIs that have the specified IDs are returned.
0089      * If you specify an invalid AMI ID, a fault is returned. If you specify an AMI ID for which
0090      * you do not have access, it will not be included in the returned results.
0091      *
0092      * If you specify one or more AMI owners, only AMIs from the specified owners and for
0093      * which you have access are returned. The results can include the account IDs of the
0094      * specified owners, amazon for AMIs owned by Amazon or self for AMIs that you own.
0095      *
0096      * If you specify a list of executable users, only users that have launch permissions
0097      * for the AMIs are returned. You can specify account IDs (if you own the AMI(s)), self
0098      * for AMIs for which you own or have explicit permissions, or all for public AMIs.
0099      *
0100      * @param string|array $imageId             A list of image descriptions
0101      * @param string|array $owner               Owners of AMIs to describe.
0102      * @param string|array $executableBy        AMIs for which specified users have access.
0103      * @return array
0104      */
0105     public function describe($imageId = null, $owner = null, $executableBy = null)
0106     {
0107         $params = array();
0108         $params['Action'] = 'DescribeImages';
0109 
0110         if(is_array($imageId) && !empty($imageId)) {
0111             foreach($imageId as $k=>$name) {
0112                 $params['ImageId.' . ($k+1)] = $name;
0113             }
0114         } elseif($imageId) {
0115             $params['ImageId.1'] = $imageId;
0116         }
0117 
0118         if(is_array($owner) && !empty($owner)) {
0119             foreach($owner as $k=>$name) {
0120                 $params['Owner.' . ($k+1)] = $name;
0121             }
0122         } elseif($owner) {
0123             $params['Owner.1'] = $owner;
0124         }
0125 
0126         if(is_array($executableBy) && !empty($executableBy)) {
0127             foreach($executableBy as $k=>$name) {
0128                 $params['ExecutableBy.' . ($k+1)] = $name;
0129             }
0130         } elseif($executableBy) {
0131             $params['ExecutableBy.1'] = $executableBy;
0132         }
0133 
0134         $response = $this->sendRequest($params);
0135 
0136         $xpath  = $response->getXPath();
0137         $nodes = $xpath->query('//ec2:imagesSet/ec2:item');
0138 
0139         $return = array();
0140         foreach ($nodes as $node) {
0141             $item = array();
0142 
0143             $item['imageId']        = $xpath->evaluate('string(ec2:imageId/text())', $node);
0144             $item['imageLocation']  = $xpath->evaluate('string(ec2:imageLocation/text())', $node);
0145             $item['imageState']     = $xpath->evaluate('string(ec2:imageState/text())', $node);
0146             $item['imageOwnerId']   = $xpath->evaluate('string(ec2:imageOwnerId/text())', $node);
0147             $item['isPublic']       = $xpath->evaluate('string(ec2:isPublic/text())', $node);
0148             $item['architecture']   = $xpath->evaluate('string(ec2:architecture/text())', $node);
0149             $item['imageType']      = $xpath->evaluate('string(ec2:imageType/text())', $node);
0150             $item['kernelId']       = $xpath->evaluate('string(ec2:kernelId/text())', $node);
0151             $item['ramdiskId']      = $xpath->evaluate('string(ec2:ramdiskId/text())', $node);
0152             $item['platform']       = $xpath->evaluate('string(ec2:platform/text())', $node);
0153 
0154             $return[] = $item;
0155             unset($item, $node);
0156         }
0157 
0158         return $return;
0159     }
0160 
0161     /**
0162      * Deregisters an AMI. Once deregistered, instances of the AMI can no longer be launched.
0163      *
0164      * @param string $imageId                   Unique ID of a machine image, returned by a call
0165      *                                          to RegisterImage or DescribeImages.
0166      * @return boolean
0167      */
0168     public function deregister($imageId)
0169     {
0170         $params                 = array();
0171         $params['Action']       = 'DeregisterImage';
0172         $params['ImageId']      = $imageId;
0173 
0174         $response = $this->sendRequest($params);
0175         $xpath = $response->getXPath();
0176 
0177         $return = $xpath->evaluate('string(//ec2:return/text())');
0178 
0179         return ($return === "true");
0180     }
0181 
0182     /**
0183      * Modifies an attribute of an AMI.
0184      *
0185      * Valid Attributes:
0186      *       launchPermission:  Controls who has permission to launch the AMI. Launch permissions
0187      *                          can be granted to specific users by adding userIds.
0188      *                          To make the AMI public, add the all group.
0189      *       productCodes:      Associates a product code with AMIs. This allows developers to
0190      *                          charge users for using AMIs. The user must be signed up for the
0191      *                          product before they can launch the AMI. This is a write once attribute;
0192      *                          after it is set, it cannot be changed or removed.
0193      *
0194      * @param string $imageId                   AMI ID to modify.
0195      * @param string $attribute                 Specifies the attribute to modify. See the preceding
0196      *                                          attributes table for supported attributes.
0197      * @param string $operationType             Specifies the operation to perform on the attribute.
0198      *                                          See the preceding attributes table for supported operations for attributes.
0199      *                                          Valid Values: add | remove
0200      *                                          Required for launchPermssion Attribute
0201      *
0202      * @param string|array $userId              User IDs to add to or remove from the launchPermission attribute.
0203      *                                          Required for launchPermssion Attribute
0204      * @param string|array $userGroup           User groups to add to or remove from the launchPermission attribute.
0205      *                                          Currently, the all group is available, which will make it a public AMI.
0206      *                                          Required for launchPermssion Attribute
0207      * @param string $productCode               Attaches a product code to the AMI. Currently only one product code
0208      *                                          can be associated with an AMI. Once set, the product code cannot be changed or reset.
0209      *                                          Required for productCodes Attribute
0210      * @return boolean
0211      */
0212     public function modifyAttribute($imageId, $attribute, $operationType = 'add', $userId = null, $userGroup = null, $productCode = null)
0213     {
0214         $params = array();
0215         $params['Action'] = 'ModifyImageAttribute';
0216         $parmas['ImageId'] = $imageId;
0217         $params['Attribute'] = $attribute;
0218 
0219         switch($attribute) {
0220             case 'launchPermission':
0221                 // break left out
0222             case 'launchpermission':
0223                 $params['Attribute'] = 'launchPermission';
0224                 $params['OperationType'] = $operationType;
0225 
0226                 if(is_array($userId) && !empty($userId)) {
0227                     foreach($userId as $k=>$name) {
0228                         $params['UserId.' . ($k+1)] = $name;
0229                     }
0230                 } elseif($userId) {
0231                     $params['UserId.1'] = $userId;
0232                 }
0233 
0234                 if(is_array($userGroup) && !empty($userGroup)) {
0235                     foreach($userGroup as $k=>$name) {
0236                         $params['UserGroup.' . ($k+1)] = $name;
0237                     }
0238                 } elseif($userGroup) {
0239                     $params['UserGroup.1'] = $userGroup;
0240                 }
0241 
0242                 break;
0243             case 'productCodes':
0244                 // break left out
0245             case 'productcodes':
0246                 $params['Attribute'] = 'productCodes';
0247                 $params['ProductCode.1'] = $productCode;
0248                 break;
0249             default:
0250                 // require_once 'Zend/Service/Amazon/Ec2/Exception.php';
0251                 throw new Zend_Service_Amazon_Ec2_Exception('Invalid Attribute Passed In.  Valid Image Attributes are launchPermission and productCode.');
0252                 break;
0253         }
0254 
0255         $response = $this->sendRequest($params);
0256         $xpath = $response->getXPath();
0257 
0258         $return = $xpath->evaluate('string(//ec2:return/text())');
0259 
0260         return ($return === "true");
0261     }
0262 
0263     /**
0264      * Returns information about an attribute of an AMI. Only one attribute can be specified per call.
0265      *
0266      * @param string $imageId                   ID of the AMI for which an attribute will be described.
0267      * @param string $attribute                 Specifies the attribute to describe.  Valid Attributes are
0268      *                                          launchPermission, productCodes
0269      */
0270     public function describeAttribute($imageId, $attribute)
0271     {
0272         $params = array();
0273         $params['Action'] = 'DescribeImageAttribute';
0274         $params['ImageId'] = $imageId;
0275         $params['Attribute'] = $attribute;
0276 
0277         $response = $this->sendRequest($params);
0278         $xpath = $response->getXPath();
0279 
0280         $return = array();
0281         $return['imageId'] = $xpath->evaluate('string(//ec2:imageId/text())');
0282 
0283         // check for launchPermission
0284         if($attribute == 'launchPermission') {
0285             $lPnodes = $xpath->query('//ec2:launchPermission/ec2:item');
0286 
0287             if($lPnodes->length > 0) {
0288                 $return['launchPermission'] = array();
0289                 foreach($lPnodes as $node) {
0290                     $return['launchPermission'][] = $xpath->evaluate('string(ec2:userId/text())', $node);
0291                 }
0292             }
0293         }
0294 
0295         // check for product codes
0296         if($attribute == 'productCodes') {
0297             $pCnodes = $xpath->query('//ec2:productCodes/ec2:item');
0298             if($pCnodes->length > 0) {
0299                 $return['productCodes'] = array();
0300                 foreach($pCnodes as $node) {
0301                     $return['productCodes'][] = $xpath->evaluate('string(ec2:productCode/text())', $node);
0302                 }
0303             }
0304         }
0305 
0306         return $return;
0307 
0308     }
0309 
0310     /**
0311      * Resets an attribute of an AMI to its default value.  The productCodes attribute cannot be reset
0312      *
0313      * @param string $imageId                   ID of the AMI for which an attribute will be reset.
0314      * @param String $attribute                 Specifies the attribute to reset. Currently, only launchPermission is supported.
0315      *                                          In the case of launchPermission, all public and explicit launch permissions for
0316      *                                          the AMI are revoked.
0317      * @return boolean
0318      */
0319     public function resetAttribute($imageId, $attribute)
0320     {
0321         $params = array();
0322         $params['Action'] = 'ResetImageAttribute';
0323         $params['ImageId'] = $imageId;
0324         $params['Attribute'] = $attribute;
0325 
0326         $response = $this->sendRequest($params);
0327         $xpath = $response->getXPath();
0328 
0329         $return = $xpath->evaluate('string(//ec2:return/text())');
0330 
0331         return ($return === "true");
0332     }
0333 }