public
Created

WIP of keypair extension

  • Download Gist
keypairs.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
 
""" Keypair management extension"""
 
from webob import exc
 
from nova import db
from nova import crypto
from nova import exception
from nova.api.openstack import extensions
 
 
class KeypairExtraSpecsController(object):
""" Keypair API controller for the Openstack API """
 
def _gen_key(self):
"""
Generate a key
"""
# TODO(jesse): crypto.generate_key_pair is currently a slow method
# and should probably be moved to a process pool?
private_key, public_key, fingerprint = crypto.generate_key_pair()
return {'private_key': private_key,
'public_key': public_key,
'fingerprint': fingerprint}
 
def create(self, req, body):
"""
Create or import keypair.
 
Sending key_name will generate a key and return private_key
and fingerprint.
 
You can send a public_key to add an existing ssh key
 
params: keypair object with:
key_name (required) - string
fingerprint (optional) - string
public_key (optional) - string
"""
 
context = req.environ['nova.context']
params = body['keypair']
key_name = params['key_name']
 
# NOTE(ja): generation is slow, so shortcut invalid key_name exception
try:
db.key_pair_get(context, context.user_id, key_name)
raise exception.KeyPairExists(key_name=key_name)
except exception.NotFound:
pass
 
keypair = {'user_id': context.user_id,
'name': key_name}
 
# import if public_key is sent
if 'public_key' in params:
keypair['public_key'] = params['public_key']
keypair['fingerprint'] = params.get('fingerprint', None)
else:
generated_key = self._gen_key()
keypair['private_key'] = generated_key['private_key']
keypair['public_key'] = generated_key['public_key']
keypair['fingerprint'] = generated_key['fingerprint']
 
db.key_pair_create(context, keypair)
return {'keypair': keypair}
 
 
def delete(self, req, name):
"""
Delete a keypair with a given name
"""
context = req.environ['nova.context']
db.key_pair_destroy(context, context.user_id, name)
return exc.HTTPAccepted()
 
def index(self, req):
"""
List of keypairs for a user
"""
context = req.environ['nova.context']
key_pairs = db.key_pair_get_all_by_user(context, context.user_id)
rval = []
for key_pair in key_pairs:
rval.append({
'name': key_pair['name'],
'key_name': key_pair['name'],
'fingerprint': key_pair['fingerprint'],
})
 
return {'keypairs': rval}
 
 
class Keypairextraspecs(extensions.ExtensionDescriptor):
 
def get_name(self):
return "KeypairExtraSpecs"
 
def get_alias(self):
return "os-keypair-extra-specs"
 
def get_description(self):
return "Keypair extra specs"
 
def get_namespace(self):
return \
"http://docs.openstack.org/ext/keypair_extra_specs/api/v1.1"
 
def get_updated(self):
return "2011-08-08T00:00:00+00:00"
 
def get_resources(self):
resources = []
res = extensions.ResourceExtension(
'os-extra_specs',
KeypairExtraSpecsController(),
parent=dict(member_name='keypair', collection_name='keypairs'))
 
resources.append(res)
return resources

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.