~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/boto/boto/ec2/keypair.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
 
2
#
 
3
# Permission is hereby granted, free of charge, to any person obtaining a
 
4
# copy of this software and associated documentation files (the
 
5
# "Software"), to deal in the Software without restriction, including
 
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
 
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
 
8
# persons to whom the Software is furnished to do so, subject to the fol-
 
9
# lowing conditions:
 
10
#
 
11
# The above copyright notice and this permission notice shall be included
 
12
# in all copies or substantial portions of the Software.
 
13
#
 
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
 
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
 
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 
20
# IN THE SOFTWARE.
 
21
 
 
22
"""
 
23
Represents an EC2 Keypair
 
24
"""
 
25
 
 
26
import os
 
27
from boto.ec2.ec2object import EC2Object
 
28
from boto.exception import BotoClientError
 
29
 
 
30
class KeyPair(EC2Object):
 
31
    
 
32
    def __init__(self, connection=None):
 
33
        EC2Object.__init__(self, connection)
 
34
        self.name = None
 
35
        self.fingerprint = None
 
36
        self.material = None
 
37
 
 
38
    def __repr__(self):
 
39
        return 'KeyPair:%s' % self.name
 
40
 
 
41
    def endElement(self, name, value, connection):
 
42
        if name == 'keyName':
 
43
            self.name = value
 
44
        elif name == 'keyFingerprint':
 
45
            self.fingerprint = value
 
46
        elif name == 'keyMaterial':
 
47
            self.material = value
 
48
        else:
 
49
            setattr(self, name, value)
 
50
 
 
51
    def delete(self):
 
52
        """
 
53
        Delete the KeyPair.
 
54
        
 
55
        :rtype: bool
 
56
        :return: True if successful, otherwise False.
 
57
        """
 
58
        return self.connection.delete_key_pair(self.name)
 
59
 
 
60
    def save(self, directory_path):
 
61
        """
 
62
        Save the material (the unencrypted PEM encoded RSA private key)
 
63
        of a newly created KeyPair to a local file.
 
64
        
 
65
        :type directory_path: string
 
66
        :param directory_path: The fully qualified path to the directory
 
67
                               in which the keypair will be saved.  The
 
68
                               keypair file will be named using the name
 
69
                               of the keypair as the base name and .pem
 
70
                               for the file extension.  If a file of that
 
71
                               name already exists in the directory, an
 
72
                               exception will be raised and the old file
 
73
                               will not be overwritten.
 
74
        
 
75
        :rtype: bool
 
76
        :return: True if successful.
 
77
        """
 
78
        if self.material:
 
79
            file_path = os.path.join(directory_path, '%s.pem' % self.name)
 
80
            if os.path.exists(file_path):
 
81
                raise BotoClientError('%s already exists, it will not be overwritten' % file_path)
 
82
            fp = open(file_path, 'wb')
 
83
            fp.write(self.material)
 
84
            fp.close()
 
85
            return True
 
86
        else:
 
87
            raise BotoClientError('KeyPair contains no material')
 
88
 
 
89
    def copy_to_region(self, region):
 
90
        """
 
91
        Create a new key pair of the same new in another region.
 
92
        Note that the new key pair will use a different ssh
 
93
        cert than the this key pair.  After doing the copy,
 
94
        you will need to save the material associated with the
 
95
        new key pair (use the save method) to a local file.
 
96
 
 
97
        :type region: :class:`boto.ec2.regioninfo.RegionInfo`
 
98
        :param region: The region to which this security group will be copied.
 
99
 
 
100
        :rtype: :class:`boto.ec2.keypair.KeyPair`
 
101
        :return: The new key pair
 
102
        """
 
103
        if region.name == self.region:
 
104
            raise BotoClientError('Unable to copy to the same Region')
 
105
        conn_params = self.connection.get_params()
 
106
        rconn = region.connect(**conn_params)
 
107
        kp = rconn.create_key_pair(self.name)
 
108
        return kp
 
109
 
 
110
 
 
111