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.
16 * @package Zend_Service_Amazon
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: Image.php 16971 2009-07-22 18:05:45Z mikaelkael $
23 require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
26 * An Amazon EC2 interface to register, describe and deregister Amamzon Machine Instances (AMI)
29 * @package Zend_Service_Amazon
31 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
32 * @license http://framework.zend.com/license/new-bsd New BSD License
34 class Zend_Service_Amazon_Ec2_Image
extends Zend_Service_Amazon_Ec2_Abstract
37 * Registers an AMI with Amazon EC2. Images must be registered before
38 * they can be launched.
40 * Each AMI is associated with an unique ID which is provided by the Amazon
41 * EC2 service through the RegisterImage operation. During registration, Amazon
42 * EC2 retrieves the specified image manifest from Amazon S3 and verifies that
43 * the image is owned by the user registering the image.
45 * The image manifest is retrieved once and stored within the Amazon EC2.
46 * Any modifications to an image in Amazon S3 invalidates this registration.
47 * If you make changes to an image, deregister the previous image and register
48 * the new image. For more information, see DeregisterImage.
50 * @param string $imageLocation Full path to your AMI manifest in Amazon S3 storage.
51 * @return string The ami fro the newly registred image;
53 public function register($imageLocation)
56 $params['Action'] = 'RegisterImage';
57 $params['ImageLocation']= $imageLocation;
59 $response = $this->sendRequest($params);
60 $xpath = $response->getXPath();
62 $amiId = $xpath->evaluate('string(//ec2:imageId/text())');
68 * Returns information about AMIs, AKIs, and ARIs available to the user.
69 * Information returned includes image type, product codes, architecture,
70 * and kernel and RAM disk IDs. Images available to the user include public
71 * images available for any user to launch, private images owned by the user
72 * making the request, and private images owned by other users for which the
73 * user has explicit launch permissions.
75 * Launch permissions fall into three categories:
76 * public: The owner of the AMI granted launch permissions for the AMI
77 * to the all group. All users have launch permissions for these AMIs.
78 * explicit: The owner of the AMI granted launch permissions to a specific user.
79 * implicit: A user has implicit launch permissions for all AMIs he or she owns.
81 * The list of AMIs returned can be modified by specifying AMI IDs, AMI owners,
82 * or users with launch permissions. If no options are specified, Amazon EC2 returns
83 * all AMIs for which the user has launch permissions.
85 * If you specify one or more AMI IDs, only AMIs that have the specified IDs are returned.
86 * If you specify an invalid AMI ID, a fault is returned. If you specify an AMI ID for which
87 * you do not have access, it will not be included in the returned results.
89 * If you specify one or more AMI owners, only AMIs from the specified owners and for
90 * which you have access are returned. The results can include the account IDs of the
91 * specified owners, amazon for AMIs owned by Amazon or self for AMIs that you own.
93 * If you specify a list of executable users, only users that have launch permissions
94 * for the AMIs are returned. You can specify account IDs (if you own the AMI(s)), self
95 * for AMIs for which you own or have explicit permissions, or all for public AMIs.
97 * @param string|array $imageId A list of image descriptions
98 * @param string|array $owner Owners of AMIs to describe.
99 * @param string|array $executableBy AMIs for which specified users have access.
102 public function describe($imageId = null, $owner = null, $executableBy = null)
105 $params['Action'] = 'DescribeImages';
107 if(is_array($imageId) && !empty($imageId)) {
108 foreach($imageId as $k=>$name) {
109 $params['ImageId.' . ($k+
1)] = $name;
112 $params['ImageId.1'] = $imageId;
115 if(is_array($owner) && !empty($owner)) {
116 foreach($owner as $k=>$name) {
117 $params['Owner.' . ($k+
1)] = $name;
120 $params['Owner.1'] = $owner;
123 if(is_array($executableBy) && !empty($executableBy)) {
124 foreach($executableBy as $k=>$name) {
125 $params['ExecutableBy.' . ($k+
1)] = $name;
127 } elseif($executableBy) {
128 $params['ExecutableBy.1'] = $executableBy;
131 $response = $this->sendRequest($params);
133 $xpath = $response->getXPath();
134 $nodes = $xpath->query('//ec2:imagesSet/ec2:item');
137 foreach ($nodes as $node) {
140 $item['imageId'] = $xpath->evaluate('string(ec2:imageId/text())', $node);
141 $item['imageLocation'] = $xpath->evaluate('string(ec2:imageLocation/text())', $node);
142 $item['imageState'] = $xpath->evaluate('string(ec2:imageState/text())', $node);
143 $item['imageOwnerId'] = $xpath->evaluate('string(ec2:imageOwnerId/text())', $node);
144 $item['isPublic'] = $xpath->evaluate('string(ec2:isPublic/text())', $node);
145 $item['architecture'] = $xpath->evaluate('string(ec2:architecture/text())', $node);
146 $item['imageType'] = $xpath->evaluate('string(ec2:imageType/text())', $node);
147 $item['kernelId'] = $xpath->evaluate('string(ec2:kernelId/text())', $node);
148 $item['ramdiskId'] = $xpath->evaluate('string(ec2:ramdiskId/text())', $node);
149 $item['platform'] = $xpath->evaluate('string(ec2:platform/text())', $node);
159 * Deregisters an AMI. Once deregistered, instances of the AMI can no longer be launched.
161 * @param string $imageId Unique ID of a machine image, returned by a call
162 * to RegisterImage or DescribeImages.
165 public function deregister($imageId)
168 $params['Action'] = 'DeregisterImage';
169 $params['ImageId'] = $imageId;
171 $response = $this->sendRequest($params);
172 $xpath = $response->getXPath();
174 $return = $xpath->evaluate('string(//ec2:return/text())');
176 return ($return === "true");
180 * Modifies an attribute of an AMI.
183 * launchPermission: Controls who has permission to launch the AMI. Launch permissions
184 * can be granted to specific users by adding userIds.
185 * To make the AMI public, add the all group.
186 * productCodes: Associates a product code with AMIs. This allows developers to
187 * charge users for using AMIs. The user must be signed up for the
188 * product before they can launch the AMI. This is a write once attribute;
189 * after it is set, it cannot be changed or removed.
191 * @param string $imageId AMI ID to modify.
192 * @param string $attribute Specifies the attribute to modify. See the preceding
193 * attributes table for supported attributes.
194 * @param string $operationType Specifies the operation to perform on the attribute.
195 * See the preceding attributes table for supported operations for attributes.
196 * Valid Values: add | remove
197 * Required for launchPermssion Attribute
199 * @param string|array $userId User IDs to add to or remove from the launchPermission attribute.
200 * Required for launchPermssion Attribute
201 * @param string|array $userGroup User groups to add to or remove from the launchPermission attribute.
202 * Currently, the all group is available, which will make it a public AMI.
203 * Required for launchPermssion Attribute
204 * @param string $productCode Attaches a product code to the AMI. Currently only one product code
205 * can be associated with an AMI. Once set, the product code cannot be changed or reset.
206 * Required for productCodes Attribute
209 public function modifyAttribute($imageId, $attribute, $operationType = 'add', $userId = null, $userGroup = null, $productCode = null)
212 $params['Action'] = 'ModifyImageAttribute';
213 $parmas['ImageId'] = $imageId;
214 $params['Attribute'] = $attribute;
217 case 'launchPermission':
219 case 'launchpermission':
220 $params['Attribute'] = 'launchPermission';
221 $params['OperationType'] = $operationType;
223 if(is_array($userId) && !empty($userId)) {
224 foreach($userId as $k=>$name) {
225 $params['UserId.' . ($k+
1)] = $name;
228 $params['UserId.1'] = $userId;
231 if(is_array($userGroup) && !empty($userGroup)) {
232 foreach($userGroup as $k=>$name) {
233 $params['UserGroup.' . ($k+
1)] = $name;
235 } elseif($userGroup) {
236 $params['UserGroup.1'] = $userGroup;
243 $params['Attribute'] = 'productCodes';
244 $params['ProductCode.1'] = $productCode;
247 require_once 'Zend/Service/Amazon/Ec2/Exception.php';
248 throw new Zend_Service_Amazon_Ec2_Exception('Invalid Attribute Passed In. Valid Image Attributes are launchPermission and productCode.');
252 $response = $this->sendRequest($params);
253 $xpath = $response->getXPath();
255 $return = $xpath->evaluate('string(//ec2:return/text())');
257 return ($return === "true");
261 * Returns information about an attribute of an AMI. Only one attribute can be specified per call.
263 * @param string $imageId ID of the AMI for which an attribute will be described.
264 * @param string $attribute Specifies the attribute to describe. Valid Attributes are
265 * launchPermission, productCodes
267 public function describeAttribute($imageId, $attribute)
270 $params['Action'] = 'DescribeImageAttribute';
271 $params['ImageId'] = $imageId;
272 $params['Attribute'] = $attribute;
274 $response = $this->sendRequest($params);
275 $xpath = $response->getXPath();
278 $return['imageId'] = $xpath->evaluate('string(//ec2:imageId/text())');
280 // check for launchPermission
281 if($attribute == 'launchPermission') {
282 $lPnodes = $xpath->query('//ec2:launchPermission/ec2:item');
284 if($lPnodes->length
> 0) {
285 $return['launchPermission'] = array();
286 foreach($lPnodes as $node) {
287 $return['launchPermission'][] = $xpath->evaluate('string(ec2:userId/text())', $node);
292 // check for product codes
293 if($attribute == 'productCodes') {
294 $pCnodes = $xpath->query('//ec2:productCodes/ec2:item');
295 if($pCnodes->length
> 0) {
296 $return['productCodes'] = array();
297 foreach($pCnodes as $node) {
298 $return['productCodes'][] = $xpath->evaluate('string(ec2:productCode/text())', $node);
308 * Resets an attribute of an AMI to its default value. The productCodes attribute cannot be reset
310 * @param string $imageId ID of the AMI for which an attribute will be reset.
311 * @param String $attribute Specifies the attribute to reset. Currently, only launchPermission is supported.
312 * In the case of launchPermission, all public and explicit launch permissions for
313 * the AMI are revoked.
316 public function resetAttribute($imageId, $attribute)
319 $params['Action'] = 'ResetImageAttribute';
320 $params['ImageId'] = $imageId;
321 $params['Attribute'] = $attribute;
323 $response = $this->sendRequest($params);
324 $xpath = $response->getXPath();
326 $return = $xpath->evaluate('string(//ec2:return/text())');
328 return ($return === "true");