web/lib/Zend/Service/Amazon/Ec2/Keypair.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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: Keypair.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Service_Amazon_Ec2_Abstract
       
    25  */
       
    26 require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
       
    27 
       
    28 /**
       
    29  * An Amazon EC2 interface to create, delete and describe Ec2 KeyPairs.
       
    30  *
       
    31  * @category   Zend
       
    32  * @package    Zend_Service_Amazon
       
    33  * @subpackage Ec2
       
    34  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    35  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    36  */
       
    37 class Zend_Service_Amazon_Ec2_Keypair extends Zend_Service_Amazon_Ec2_Abstract
       
    38 {
       
    39     /**
       
    40      * Creates a new 2048 bit RSA key pair and returns a unique ID that can
       
    41      * be used to reference this key pair when launching new instances.
       
    42      *
       
    43      * @param string $keyName           A unique name for the key pair.
       
    44      * @throws Zend_Service_Amazon_Ec2_Exception
       
    45      * @return array
       
    46      */
       
    47     public function create($keyName)
       
    48     {
       
    49         $params = array();
       
    50 
       
    51         $params['Action'] = 'CreateKeyPair';
       
    52 
       
    53         if(!$keyName) {
       
    54             require_once 'Zend/Service/Amazon/Ec2/Exception.php';
       
    55             throw new Zend_Service_Amazon_Ec2_Exception('Invalid Key Name');
       
    56         }
       
    57 
       
    58         $params['KeyName'] = $keyName;
       
    59 
       
    60         $response = $this->sendRequest($params);
       
    61         $xpath = $response->getXPath();
       
    62 
       
    63         $return = array();
       
    64         $return['keyName']          = $xpath->evaluate('string(//ec2:keyName/text())');
       
    65         $return['keyFingerprint']   = $xpath->evaluate('string(//ec2:keyFingerprint/text())');
       
    66         $return['keyMaterial']      = $xpath->evaluate('string(//ec2:keyMaterial/text())');
       
    67 
       
    68         return $return;
       
    69     }
       
    70 
       
    71     /**
       
    72      * Returns information about key pairs available to you. If you specify
       
    73      * key pairs, information about those key pairs is returned. Otherwise,
       
    74      * information for all registered key pairs is returned.
       
    75      *
       
    76      * @param string|rarray $keyName    Key pair IDs to describe.
       
    77      * @return array
       
    78      */
       
    79     public function describe($keyName = null)
       
    80     {
       
    81         $params = array();
       
    82 
       
    83         $params['Action'] = 'DescribeKeyPairs';
       
    84         if(is_array($keyName) && !empty($keyName)) {
       
    85             foreach($keyName as $k=>$name) {
       
    86                 $params['KeyName.' . ($k+1)] = $name;
       
    87             }
       
    88         } elseif($keyName) {
       
    89             $params['KeyName.1'] = $keyName;
       
    90         }
       
    91 
       
    92         $response = $this->sendRequest($params);
       
    93         $xpath = $response->getXPath();
       
    94 
       
    95         $nodes  = $xpath->query('//ec2:keySet/ec2:item');
       
    96 
       
    97         $return = array();
       
    98         foreach ($nodes as $k => $node) {
       
    99             $item = array();
       
   100             $item['keyName']          = $xpath->evaluate('string(ec2:keyName/text())', $node);
       
   101             $item['keyFingerprint']   = $xpath->evaluate('string(ec2:keyFingerprint/text())', $node);
       
   102 
       
   103             $return[] = $item;
       
   104             unset($item);
       
   105         }
       
   106 
       
   107         return $return;
       
   108     }
       
   109 
       
   110     /**
       
   111      * Deletes a key pair
       
   112      *
       
   113      * @param string $keyName           Name of the key pair to delete.
       
   114      * @throws Zend_Service_Amazon_Ec2_Exception
       
   115      * @return boolean                  Return true or false from the deletion.
       
   116      */
       
   117     public function delete($keyName)
       
   118     {
       
   119         $params = array();
       
   120 
       
   121         $params['Action'] = 'DeleteKeyPair';
       
   122 
       
   123         if(!$keyName) {
       
   124             require_once 'Zend/Service/Amazon/Ec2/Exception.php';
       
   125             throw new Zend_Service_Amazon_Ec2_Exception('Invalid Key Name');
       
   126         }
       
   127 
       
   128         $params['KeyName'] = $keyName;
       
   129 
       
   130         $response = $this->sendRequest($params);
       
   131 
       
   132         $xpath = $response->getXPath();
       
   133         $success  = $xpath->evaluate('string(//ec2:return/text())');
       
   134 
       
   135         return ($success === "true");
       
   136     }
       
   137 }