PageRenderTime 49ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Service/Amazon/Ec2/Image.php

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