Keypair.php 4.2 KB

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