Package yakumo :: Package nova :: Package v2 :: Module key_pair
[hide private]
[frames] | no frames]

Source Code for Module yakumo.nova.v2.key_pair

 1  # Copyright 2014-2017 by Akira Yoshiyama <akirayoshiyama@gmail.com>. 
 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  """ 
17  Resource class and its manager for key pairs in Compute API v2 
18  """ 
19   
20  import os 
21  import stat 
22   
23  from yakumo import base 
24  from yakumo.constant import UNDEF 
25  from yakumo import mapper 
26  from yakumo import utils 
27   
28   
29  ATTRIBUTE_MAPPING = [ 
30      ('name', 'name', mapper.Noop), 
31      ('fingerprint', 'fingerprint', mapper.Noop), 
32      ('public_key', 'public_key', mapper.Noop), 
33      ('private_key', 'private_key', mapper.Noop), 
34      ('type', 'type', mapper.Noop), 
35      ('user', 'user_id', mapper.Resource('user')), 
36  ] 
37   
38   
39 -class Resource(base.Resource):
40 """Resource class for key pairs in Compute API v2"""
41 42
43 -class Manager(base.Manager):
44 """Manager class for key pairs in Compute API v2""" 45 46 resource_class = Resource 47 service_type = 'compute' 48 _attr_mapping = ATTRIBUTE_MAPPING 49 _hidden_methods = ["update"] 50 _id_attr = 'name' 51 _json_resource_key = 'keypair' 52 _json_resources_key = 'keypairs' 53 _url_resource_path = '/os-keypairs' 54
55 - def create(self, name=UNDEF, public_key=UNDEF, private_key_file=UNDEF, 56 type=UNDEF, user=UNDEF):
57 """ 58 Register a SSH public key 59 60 @keyword name: Key name 61 @type name: str 62 @keyword public_key: content of SSH public key 63 @type public_key: str 64 @keyword type: 'ssh' or 'x509' 65 @type type: str 66 @keyword user: Owner 67 @type user: yakumo.user.Resource 68 @return: Registered key 69 @rtype: yakumo.nova.v2.key_pair.Resource 70 """ 71 kwargs = dict(name=name, 72 public_key=public_key, 73 type=type, 74 user=user) 75 json_params = self._attr2json(kwargs) 76 ret = self._http.post(self._url_resource_path, 77 data={self._json_resource_key: json_params}) 78 attrs = self._json2attr(ret[self._json_resource_key]) 79 if 'private_key' in attrs and private_key_file != UNDEF: 80 with open(private_key_file, "w") as f: 81 f.write(attrs['private_key']) 82 os.chmod(private_key_file, stat.S_IRUSR) 83 return self.resource_class(self, **attrs)
84
85 - def _find_gen(self, **kwargs):
86 ret = self._http.get(self._url_resource_list_path) 87 for x in ret[self._json_resources_key]: 88 ret = self.get(x[self._json_resource_key]['name']) 89 for k, v in kwargs.items(): 90 if getattr(ret, k, None) != v: 91 break 92 else: 93 yield ret
94