Image.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. * An Amazon EC2 interface to register, describe and deregister Amamzon Machine Instances (AMI)
  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_Image extends Zend_Service_Amazon_Ec2_Abstract
  33. {
  34. /**
  35. * Registers an AMI with Amazon EC2. Images must be registered before
  36. * they can be launched.
  37. *
  38. * Each AMI is associated with an unique ID which is provided by the Amazon
  39. * EC2 service through the RegisterImage operation. During registration, Amazon
  40. * EC2 retrieves the specified image manifest from Amazon S3 and verifies that
  41. * the image is owned by the user registering the image.
  42. *
  43. * The image manifest is retrieved once and stored within the Amazon EC2.
  44. * Any modifications to an image in Amazon S3 invalidates this registration.
  45. * If you make changes to an image, deregister the previous image and register
  46. * the new image. For more information, see DeregisterImage.
  47. *
  48. * @param string $imageLocation Full path to your AMI manifest in Amazon S3 storage.
  49. * @return string The ami fro the newly registred image;
  50. */
  51. public function register($imageLocation)
  52. {
  53. $params = array();
  54. $params['Action'] = 'RegisterImage';
  55. $params['ImageLocation']= $imageLocation;
  56. $response = $this->sendRequest($params);
  57. $xpath = $response->getXPath();
  58. $amiId = $xpath->evaluate('string(//ec2:imageId/text())');
  59. return $amiId;
  60. }
  61. /**
  62. * Returns information about AMIs, AKIs, and ARIs available to the user.
  63. * Information returned includes image type, product codes, architecture,
  64. * and kernel and RAM disk IDs. Images available to the user include public
  65. * images available for any user to launch, private images owned by the user
  66. * making the request, and private images owned by other users for which the
  67. * user has explicit launch permissions.
  68. *
  69. * Launch permissions fall into three categories:
  70. * public: The owner of the AMI granted launch permissions for the AMI
  71. * to the all group. All users have launch permissions for these AMIs.
  72. * explicit: The owner of the AMI granted launch permissions to a specific user.
  73. * implicit: A user has implicit launch permissions for all AMIs he or she owns.
  74. *
  75. * The list of AMIs returned can be modified by specifying AMI IDs, AMI owners,
  76. * or users with launch permissions. If no options are specified, Amazon EC2 returns
  77. * all AMIs for which the user has launch permissions.
  78. *
  79. * If you specify one or more AMI IDs, only AMIs that have the specified IDs are returned.
  80. * If you specify an invalid AMI ID, a fault is returned. If you specify an AMI ID for which
  81. * you do not have access, it will not be included in the returned results.
  82. *
  83. * If you specify one or more AMI owners, only AMIs from the specified owners and for
  84. * which you have access are returned. The results can include the account IDs of the
  85. * specified owners, amazon for AMIs owned by Amazon or self for AMIs that you own.
  86. *
  87. * If you specify a list of executable users, only users that have launch permissions
  88. * for the AMIs are returned. You can specify account IDs (if you own the AMI(s)), self
  89. * for AMIs for which you own or have explicit permissions, or all for public AMIs.
  90. *
  91. * @param string|array $imageId A list of image descriptions
  92. * @param string|array $owner Owners of AMIs to describe.
  93. * @param string|array $executableBy AMIs for which specified users have access.
  94. * @return array
  95. */
  96. public function describe($imageId = null, $owner = null, $executableBy = null)
  97. {
  98. $params = array();
  99. $params['Action'] = 'DescribeImages';
  100. if(is_array($imageId) && !empty($imageId)) {
  101. foreach($imageId as $k=>$name) {
  102. $params['ImageId.' . ($k+1)] = $name;
  103. }
  104. } elseif($imageId) {
  105. $params['ImageId.1'] = $imageId;
  106. }
  107. if(is_array($owner) && !empty($owner)) {
  108. foreach($owner as $k=>$name) {
  109. $params['Owner.' . ($k+1)] = $name;
  110. }
  111. } elseif($owner) {
  112. $params['Owner.1'] = $owner;
  113. }
  114. if(is_array($executableBy) && !empty($executableBy)) {
  115. foreach($executableBy as $k=>$name) {
  116. $params['ExecutableBy.' . ($k+1)] = $name;
  117. }
  118. } elseif($executableBy) {
  119. $params['ExecutableBy.1'] = $executableBy;
  120. }
  121. $response = $this->sendRequest($params);
  122. $xpath = $response->getXPath();
  123. $nodes = $xpath->query('//ec2:imagesSet/ec2:item');
  124. $return = array();
  125. foreach ($nodes as $node) {
  126. $item = array();
  127. $item['imageId'] = $xpath->evaluate('string(ec2:imageId/text())', $node);
  128. $item['imageLocation'] = $xpath->evaluate('string(ec2:imageLocation/text())', $node);
  129. $item['imageState'] = $xpath->evaluate('string(ec2:imageState/text())', $node);
  130. $item['imageOwnerId'] = $xpath->evaluate('string(ec2:imageOwnerId/text())', $node);
  131. $item['isPublic'] = $xpath->evaluate('string(ec2:isPublic/text())', $node);
  132. $item['architecture'] = $xpath->evaluate('string(ec2:architecture/text())', $node);
  133. $item['imageType'] = $xpath->evaluate('string(ec2:imageType/text())', $node);
  134. $item['kernelId'] = $xpath->evaluate('string(ec2:kernelId/text())', $node);
  135. $item['ramdiskId'] = $xpath->evaluate('string(ec2:ramdiskId/text())', $node);
  136. $item['platform'] = $xpath->evaluate('string(ec2:platform/text())', $node);
  137. $return[] = $item;
  138. unset($item, $node);
  139. }
  140. return $return;
  141. }
  142. /**
  143. * Deregisters an AMI. Once deregistered, instances of the AMI can no longer be launched.
  144. *
  145. * @param string $imageId Unique ID of a machine image, returned by a call
  146. * to RegisterImage or DescribeImages.
  147. * @return boolean
  148. */
  149. public function deregister($imageId)
  150. {
  151. $params = array();
  152. $params['Action'] = 'DeregisterImage';
  153. $params['ImageId'] = $imageId;
  154. $response = $this->sendRequest($params);
  155. $xpath = $response->getXPath();
  156. $return = $xpath->evaluate('string(//ec2:return/text())');
  157. return ($return === "true");
  158. }
  159. /**
  160. * Modifies an attribute of an AMI.
  161. *
  162. * Valid Attributes:
  163. * launchPermission: Controls who has permission to launch the AMI. Launch permissions
  164. * can be granted to specific users by adding userIds.
  165. * To make the AMI public, add the all group.
  166. * productCodes: Associates a product code with AMIs. This allows developers to
  167. * charge users for using AMIs. The user must be signed up for the
  168. * product before they can launch the AMI. This is a write once attribute;
  169. * after it is set, it cannot be changed or removed.
  170. *
  171. * @param string $imageId AMI ID to modify.
  172. * @param string $attribute Specifies the attribute to modify. See the preceding
  173. * attributes table for supported attributes.
  174. * @param string $operationType Specifies the operation to perform on the attribute.
  175. * See the preceding attributes table for supported operations for attributes.
  176. * Valid Values: add | remove
  177. * Required for launchPermssion Attribute
  178. *
  179. * @param string|array $userId User IDs to add to or remove from the launchPermission attribute.
  180. * Required for launchPermssion Attribute
  181. * @param string|array $userGroup User groups to add to or remove from the launchPermission attribute.
  182. * Currently, the all group is available, which will make it a public AMI.
  183. * Required for launchPermssion Attribute
  184. * @param string $productCode Attaches a product code to the AMI. Currently only one product code
  185. * can be associated with an AMI. Once set, the product code cannot be changed or reset.
  186. * Required for productCodes Attribute
  187. * @return boolean
  188. */
  189. public function modifyAttribute($imageId, $attribute, $operationType = 'add', $userId = null, $userGroup = null, $productCode = null)
  190. {
  191. $params = array();
  192. $params['Action'] = 'ModifyImageAttribute';
  193. $parmas['ImageId'] = $imageId;
  194. $params['Attribute'] = $attribute;
  195. switch($attribute) {
  196. case 'launchPermission':
  197. // break left out
  198. case 'launchpermission':
  199. $params['Attribute'] = 'launchPermission';
  200. $params['OperationType'] = $operationType;
  201. if(is_array($userId) && !empty($userId)) {
  202. foreach($userId as $k=>$name) {
  203. $params['UserId.' . ($k+1)] = $name;
  204. }
  205. } elseif($userId) {
  206. $params['UserId.1'] = $userId;
  207. }
  208. if(is_array($userGroup) && !empty($userGroup)) {
  209. foreach($userGroup as $k=>$name) {
  210. $params['UserGroup.' . ($k+1)] = $name;
  211. }
  212. } elseif($userGroup) {
  213. $params['UserGroup.1'] = $userGroup;
  214. }
  215. break;
  216. case 'productCodes':
  217. // break left out
  218. case 'productcodes':
  219. $params['Attribute'] = 'productCodes';
  220. $params['ProductCode.1'] = $productCode;
  221. break;
  222. default:
  223. require_once 'Zend/Service/Amazon/Ec2/Exception.php';
  224. throw new Zend_Service_Amazon_Ec2_Exception('Invalid Attribute Passed In. Valid Image Attributes are launchPermission and productCode.');
  225. break;
  226. }
  227. $response = $this->sendRequest($params);
  228. $xpath = $response->getXPath();
  229. $return = $xpath->evaluate('string(//ec2:return/text())');
  230. return ($return === "true");
  231. }
  232. /**
  233. * Returns information about an attribute of an AMI. Only one attribute can be specified per call.
  234. *
  235. * @param string $imageId ID of the AMI for which an attribute will be described.
  236. * @param string $attribute Specifies the attribute to describe. Valid Attributes are
  237. * launchPermission, productCodes
  238. */
  239. public function describeAttribute($imageId, $attribute)
  240. {
  241. $params = array();
  242. $params['Action'] = 'DescribeImageAttribute';
  243. $params['ImageId'] = $imageId;
  244. $params['Attribute'] = $attribute;
  245. $response = $this->sendRequest($params);
  246. $xpath = $response->getXPath();
  247. $return = array();
  248. $return['imageId'] = $xpath->evaluate('string(//ec2:imageId/text())');
  249. // check for launchPermission
  250. if($attribute == 'launchPermission') {
  251. $lPnodes = $xpath->query('//ec2:launchPermission/ec2:item');
  252. if($lPnodes->length > 0) {
  253. $return['launchPermission'] = array();
  254. foreach($lPnodes as $node) {
  255. $return['launchPermission'][] = $xpath->evaluate('string(ec2:userId/text())', $node);
  256. }
  257. }
  258. }
  259. // check for product codes
  260. if($attribute == 'productCodes') {
  261. $pCnodes = $xpath->query('//ec2:productCodes/ec2:item');
  262. if($pCnodes->length > 0) {
  263. $return['productCodes'] = array();
  264. foreach($pCnodes as $node) {
  265. $return['productCodes'][] = $xpath->evaluate('string(ec2:productCode/text())', $node);
  266. }
  267. }
  268. }
  269. return $return;
  270. }
  271. /**
  272. * Resets an attribute of an AMI to its default value. The productCodes attribute cannot be reset
  273. *
  274. * @param string $imageId ID of the AMI for which an attribute will be reset.
  275. * @param String $attribute Specifies the attribute to reset. Currently, only launchPermission is supported.
  276. * In the case of launchPermission, all public and explicit launch permissions for
  277. * the AMI are revoked.
  278. * @return boolean
  279. */
  280. public function resetAttribute($imageId, $attribute)
  281. {
  282. $params = array();
  283. $params['Action'] = 'ResetImageAttribute';
  284. $params['ImageId'] = $imageId;
  285. $params['Attribute'] = $attribute;
  286. $response = $this->sendRequest($params);
  287. $xpath = $response->getXPath();
  288. $return = $xpath->evaluate('string(//ec2:return/text())');
  289. return ($return === "true");
  290. }
  291. }