Keypair.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 create, delete and describe Ec2 KeyPairs.
  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_Keypair extends Zend_Service_Amazon_Ec2_Abstract
  33. {
  34. /**
  35. * Creates a new 2048 bit RSA key pair and returns a unique ID that can
  36. * be used to reference this key pair when launching new instances.
  37. *
  38. * @param string $keyName A unique name for the key pair.
  39. * @throws Zend_Service_Amazon_Ec2_Exception
  40. * @return array
  41. */
  42. public function create($keyName)
  43. {
  44. $params = array();
  45. $params['Action'] = 'CreateKeyPair';
  46. if(!$keyName) {
  47. require_once 'Zend/Service/Amazon/Ec2/Exception.php';
  48. throw new Zend_Service_Amazon_Ec2_Exception('Invalid Key Name');
  49. }
  50. $params['KeyName'] = $keyName;
  51. $response = $this->sendRequest($params);
  52. $xpath = $response->getXPath();
  53. $return = array();
  54. $return['keyName'] = $xpath->evaluate('string(//ec2:keyName/text())');
  55. $return['keyFingerprint'] = $xpath->evaluate('string(//ec2:keyFingerprint/text())');
  56. $return['keyMaterial'] = $xpath->evaluate('string(//ec2:keyMaterial/text())');
  57. return $return;
  58. }
  59. /**
  60. * Returns information about key pairs available to you. If you specify
  61. * key pairs, information about those key pairs is returned. Otherwise,
  62. * information for all registered key pairs is returned.
  63. *
  64. * @param string|rarray $keyName Key pair IDs to describe.
  65. * @return array
  66. */
  67. public function describe($keyName = null)
  68. {
  69. $params = array();
  70. $params['Action'] = 'DescribeKeyPairs';
  71. if(is_array($keyName) && !empty($keyName)) {
  72. foreach($keyName as $k=>$name) {
  73. $params['KeyName.' . ($k+1)] = $name;
  74. }
  75. } elseif($keyName) {
  76. $params['KeyName.1'] = $keyName;
  77. }
  78. $response = $this->sendRequest($params);
  79. $xpath = $response->getXPath();
  80. $nodes = $xpath->query('//ec2:keySet/ec2:item');
  81. $return = array();
  82. foreach ($nodes as $k => $node) {
  83. $item = array();
  84. $item['keyName'] = $xpath->evaluate('string(ec2:keyName/text())', $node);
  85. $item['keyFingerprint'] = $xpath->evaluate('string(ec2:keyFingerprint/text())', $node);
  86. $return[] = $item;
  87. unset($item);
  88. }
  89. return $return;
  90. }
  91. /**
  92. * Deletes a key pair
  93. *
  94. * @param string $keyName Name of the key pair to delete.
  95. * @throws Zend_Service_Amazon_Ec2_Exception
  96. * @return boolean Return true or false from the deletion.
  97. */
  98. public function delete($keyName)
  99. {
  100. $params = array();
  101. $params['Action'] = 'DeleteKeyPair';
  102. if(!$keyName) {
  103. require_once 'Zend/Service/Amazon/Ec2/Exception.php';
  104. throw new Zend_Service_Amazon_Ec2_Exception('Invalid Key Name');
  105. }
  106. $params['KeyName'] = $keyName;
  107. $response = $this->sendRequest($params);
  108. $xpath = $response->getXPath();
  109. $success = $xpath->evaluate('string(//ec2:return/text())');
  110. return ($success === "true");
  111. }
  112. }