File indexing completed on 2025-03-02 05:29:46

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service_Amazon
0017  * @subpackage Ec2
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Service_Amazon_Ec2_Abstract
0025  */
0026 // require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
0027 
0028 /**
0029  * An Amazon EC2 interface to create, delete and describe Ec2 KeyPairs.
0030  *
0031  * @category   Zend
0032  * @package    Zend_Service_Amazon
0033  * @subpackage Ec2
0034  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0035  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0036  */
0037 class Zend_Service_Amazon_Ec2_Keypair extends Zend_Service_Amazon_Ec2_Abstract
0038 {
0039     /**
0040      * Creates a new 2048 bit RSA key pair and returns a unique ID that can
0041      * be used to reference this key pair when launching new instances.
0042      *
0043      * @param string $keyName           A unique name for the key pair.
0044      * @throws Zend_Service_Amazon_Ec2_Exception
0045      * @return array
0046      */
0047     public function create($keyName)
0048     {
0049         $params = array();
0050 
0051         $params['Action'] = 'CreateKeyPair';
0052 
0053         if(!$keyName) {
0054             // require_once 'Zend/Service/Amazon/Ec2/Exception.php';
0055             throw new Zend_Service_Amazon_Ec2_Exception('Invalid Key Name');
0056         }
0057 
0058         $params['KeyName'] = $keyName;
0059 
0060         $response = $this->sendRequest($params);
0061         $xpath = $response->getXPath();
0062 
0063         $return = array();
0064         $return['keyName']          = $xpath->evaluate('string(//ec2:keyName/text())');
0065         $return['keyFingerprint']   = $xpath->evaluate('string(//ec2:keyFingerprint/text())');
0066         $return['keyMaterial']      = $xpath->evaluate('string(//ec2:keyMaterial/text())');
0067 
0068         return $return;
0069     }
0070 
0071     /**
0072      * Returns information about key pairs available to you. If you specify
0073      * key pairs, information about those key pairs is returned. Otherwise,
0074      * information for all registered key pairs is returned.
0075      *
0076      * @param string|rarray $keyName    Key pair IDs to describe.
0077      * @return array
0078      */
0079     public function describe($keyName = null)
0080     {
0081         $params = array();
0082 
0083         $params['Action'] = 'DescribeKeyPairs';
0084         if(is_array($keyName) && !empty($keyName)) {
0085             foreach($keyName as $k=>$name) {
0086                 $params['KeyName.' . ($k+1)] = $name;
0087             }
0088         } elseif($keyName) {
0089             $params['KeyName.1'] = $keyName;
0090         }
0091 
0092         $response = $this->sendRequest($params);
0093         $xpath = $response->getXPath();
0094 
0095         $nodes  = $xpath->query('//ec2:keySet/ec2:item');
0096 
0097         $return = array();
0098         foreach ($nodes as $k => $node) {
0099             $item = array();
0100             $item['keyName']          = $xpath->evaluate('string(ec2:keyName/text())', $node);
0101             $item['keyFingerprint']   = $xpath->evaluate('string(ec2:keyFingerprint/text())', $node);
0102 
0103             $return[] = $item;
0104             unset($item);
0105         }
0106 
0107         return $return;
0108     }
0109 
0110     /**
0111      * Deletes a key pair
0112      *
0113      * @param string $keyName           Name of the key pair to delete.
0114      * @throws Zend_Service_Amazon_Ec2_Exception
0115      * @return boolean                  Return true or false from the deletion.
0116      */
0117     public function delete($keyName)
0118     {
0119         $params = array();
0120 
0121         $params['Action'] = 'DeleteKeyPair';
0122 
0123         if(!$keyName) {
0124             // require_once 'Zend/Service/Amazon/Ec2/Exception.php';
0125             throw new Zend_Service_Amazon_Ec2_Exception('Invalid Key Name');
0126         }
0127 
0128         $params['KeyName'] = $keyName;
0129 
0130         $response = $this->sendRequest($params);
0131 
0132         $xpath = $response->getXPath();
0133         $success  = $xpath->evaluate('string(//ec2:return/text())');
0134 
0135         return ($success === "true");
0136     }
0137 }