<s> """<STR_LIT>""" <EOL> import webob <EOL> import webob . exc <EOL> from nova . api . openstack . compute . legacy_v2 import servers <EOL> from nova . api . openstack import extensions <EOL> from nova . api . openstack import wsgi <EOL> from nova . compute import api as compute_api <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> authorize = extensions . extension_authorizer ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> soft_authorize = extensions . soft_extension_authorizer ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class KeypairController ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . api = compute_api . KeypairAPI ( ) <EOL> def _filter_keypair ( self , keypair , ** attrs ) : <EOL> clean = { <EOL> '<STR_LIT:name>' : keypair . name , <EOL> '<STR_LIT>' : keypair . public_key , <EOL> '<STR_LIT>' : keypair . fingerprint , <EOL> } <EOL> for attr in attrs : <EOL> clean [ attr ] = keypair [ attr ] <EOL> return clean <EOL> def create ( self , req , body ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context , action = '<STR_LIT>' ) <EOL> try : <EOL> params = body [ '<STR_LIT>' ] <EOL> name = params [ '<STR_LIT:name>' ] <EOL> except KeyError : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise webob . exc . HTTPBadRequest ( explanation = msg ) <EOL> try : <EOL> if '<STR_LIT>' in params : <EOL> keypair = self . api . import_key_pair ( context , <EOL> context . user_id , name , <EOL> params [ '<STR_LIT>' ] ) <EOL> keypair = self . _filter_keypair ( keypair , user_id = True ) <EOL> else : <EOL> keypair , private_key = self . api . create_key_pair ( <EOL> context , context . user_id , name ) <EOL> keypair = self . _filter_keypair ( keypair , user_id = True ) <EOL> keypair [ '<STR_LIT>' ] = private_key <EOL> return { '<STR_LIT>' : keypair } <EOL> except exception . KeypairLimitExceeded : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise webob . exc . HTTPForbidden ( explanation = msg ) <EOL> except exception . InvalidKeypair as exc : <EOL> raise webob . exc . HTTPBadRequest ( explanation = exc . format_message ( ) ) <EOL> except exception . KeyPairExists as exc : <EOL> raise webob . exc . HTTPConflict ( explanation = exc . format_message ( ) ) <EOL> def delete ( self , req , id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context , action = '<STR_LIT>' ) <EOL> try : <EOL> self . api . delete_key_pair ( context , context . user_id , id ) <EOL> except exception . KeypairNotFound as exc : <EOL> raise webob . exc . HTTPNotFound ( explanation = exc . format_message ( ) ) <EOL> return webob . Response ( status_int = <NUM_LIT> ) <EOL> def show ( self , req , id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context , action = '<STR_LIT>' ) <EOL> try : <EOL> keypair = self . api . get_key_pair ( context , context . user_id , id ) <EOL> keypair = self . _filter_keypair ( keypair , created_at = True , <EOL> deleted = True , deleted_at = True , <EOL> id = True , user_id = True , <EOL> updated_at = True ) <EOL> except exception . KeypairNotFound as exc : <EOL> raise webob . exc . HTTPNotFound ( explanation = exc . format_message ( ) ) <EOL> return { '<STR_LIT>' : keypair } <EOL> def index ( self , req ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context , action = '<STR_LIT:index>' ) <EOL> key_pairs = self . api . get_key_pairs ( context , context . user_id ) <EOL> rval = [ ] <EOL> for key_pair in key_pairs : <EOL> rval . append ( { '<STR_LIT>' : self . _filter_keypair ( key_pair ) } ) <EOL> return { '<STR_LIT>' : rval } <EOL> class Controller ( servers . Controller ) : <EOL> def _add_key_name ( self , req , servers ) : <EOL> for server in servers : <EOL> db_server = req . get_db_instance ( server [ '<STR_LIT:id>' ] ) <EOL> server [ '<STR_LIT>' ] = db_server [ '<STR_LIT>' ] <EOL> def _show ( self , req , resp_obj ) : <EOL> if '<STR_LIT>' in resp_obj . obj : <EOL> server = resp_obj . obj [ '<STR_LIT>' ] <EOL> self . _add_key_name ( req , [ server ] ) <EOL> @ wsgi . extends <EOL> def show ( self , req , resp_obj , id ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> if soft_authorize ( context ) : <EOL> self . _show ( req , resp_obj ) <EOL> @ wsgi . extends <EOL> def detail ( self , req , resp_obj ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> if '<STR_LIT>' in resp_obj . obj and soft_authorize ( context ) : <EOL> servers = resp_obj . obj [ '<STR_LIT>' ] <EOL> self . _add_key_name ( req , servers ) <EOL> class Keypairs ( extensions . ExtensionDescriptor ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> alias = "<STR_LIT>" <EOL> namespace = "<STR_LIT>" <EOL> updated = "<STR_LIT>" <EOL> def get_resources ( self ) : <EOL> resources = [ ] <EOL> res = extensions . ResourceExtension ( <EOL> '<STR_LIT>' , <EOL> KeypairController ( ) ) <EOL> resources . append ( res ) <EOL> return resources <EOL> def get_controller_extensions ( self ) : <EOL> controller = Controller ( self . ext_mgr ) <EOL> extension = extensions . ControllerExtension ( self , '<STR_LIT>' , controller ) <EOL> return [ extension ] </s>
