summaryrefslogtreecommitdiff
path: root/novaclient/v2/keypairs.py
diff options
context:
space:
mode:
Diffstat (limited to 'novaclient/v2/keypairs.py')
-rw-r--r--novaclient/v2/keypairs.py92
1 files changed, 92 insertions, 0 deletions
diff --git a/novaclient/v2/keypairs.py b/novaclient/v2/keypairs.py
new file mode 100644
index 0000000..96caff6
--- /dev/null
+++ b/novaclient/v2/keypairs.py
@@ -0,0 +1,92 @@
1# Copyright 2011 OpenStack Foundation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
16"""
17Keypair interface (1.1 extension).
18"""
19
20from novaclient import base
21
22
23class Keypair(base.Resource):
24 """
25 A keypair is a ssh key that can be injected into a server on launch.
26 """
27
28 def __repr__(self):
29 return "<Keypair: %s>" % self.id
30
31 def _add_details(self, info):
32 dico = 'keypair' in info and \
33 info['keypair'] or info
34 for (k, v) in dico.items():
35 # NOTE(rpodolyaka): keypair name allows us to uniquely identify
36 # a specific keypair, while its id attribute
37 # is nothing more than an implementation
38 # detail. We can safely omit the id attribute
39 # here to ensure setattr() won't raise
40 # AttributeError trying to set read-only
41 # property id
42 if k != 'id':
43 setattr(self, k, v)
44
45 @property
46 def id(self):
47 return self.name
48
49 def delete(self):
50 self.manager.delete(self)
51
52
53class KeypairManager(base.ManagerWithFind):
54 resource_class = Keypair
55 keypair_prefix = "os-keypairs"
56 is_alphanum_id_allowed = True
57
58 def get(self, keypair):
59 """
60 Get a keypair.
61
62 :param keypair: The ID of the keypair to get.
63 :rtype: :class:`Keypair`
64 """
65 return self._get("/%s/%s" % (self.keypair_prefix, base.getid(keypair)),
66 "keypair")
67
68 def create(self, name, public_key=None):
69 """
70 Create a keypair
71
72 :param name: name for the keypair to create
73 :param public_key: existing public key to import
74 """
75 body = {'keypair': {'name': name}}
76 if public_key:
77 body['keypair']['public_key'] = public_key
78 return self._create('/%s' % self.keypair_prefix, body, 'keypair')
79
80 def delete(self, key):
81 """
82 Delete a keypair
83
84 :param key: The :class:`Keypair` (or its ID) to delete.
85 """
86 self._delete('/%s/%s' % (self.keypair_prefix, base.getid(key)))
87
88 def list(self):
89 """
90 Get a list of keypairs.
91 """
92 return self._list('/%s' % self.keypair_prefix, 'keypairs')