<s> import webob <EOL> from nova . api . openstack import common <EOL> from nova . api . openstack . compute . schemas import remote_consoles <EOL> from nova . api . openstack import extensions <EOL> from nova . api . openstack import wsgi <EOL> from nova . api import validation <EOL> from nova import compute <EOL> from nova import exception <EOL> ALIAS = "<STR_LIT>" <EOL> authorize = extensions . os_compute_authorizer ( ALIAS ) <EOL> class RemoteConsolesController ( wsgi . Controller ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . compute_api = compute . API ( skip_policy_check = True ) <EOL> self . handlers = { '<STR_LIT>' : self . compute_api . get_vnc_console , <EOL> '<STR_LIT>' : self . compute_api . get_spice_console , <EOL> '<STR_LIT>' : self . compute_api . get_rdp_console , <EOL> '<STR_LIT>' : self . compute_api . get_serial_console , <EOL> '<STR_LIT>' : self . compute_api . get_mks_console } <EOL> super ( RemoteConsolesController , self ) . __init__ ( * args , ** kwargs ) <EOL> @ wsgi . Controller . api_version ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ wsgi . action ( '<STR_LIT>' ) <EOL> @ validation . schema ( remote_consoles . get_vnc_console ) <EOL> def get_vnc_console ( self , req , id , body ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> console_type = body [ '<STR_LIT>' ] . get ( '<STR_LIT:type>' ) <EOL> try : <EOL> instance = common . get_instance ( self . compute_api , context , id ) <EOL> output = self . compute_api . get_vnc_console ( context , <EOL> instance , <EOL> console_type ) <EOL> except exception . ConsoleTypeUnavailable as e : <EOL> raise webob . exc . HTTPBadRequest ( explanation = e . format_message ( ) ) <EOL> except ( exception . InstanceUnknownCell , <EOL> exception . InstanceNotFound ) as e : <EOL> raise webob . exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> except exception . InstanceNotReady as e : <EOL> raise webob . exc . HTTPConflict ( explanation = e . format_message ( ) ) <EOL> except NotImplementedError : <EOL> common . raise_feature_not_supported ( ) <EOL> return { '<STR_LIT>' : { '<STR_LIT:type>' : console_type , '<STR_LIT:url>' : output [ '<STR_LIT:url>' ] } } <EOL> @ wsgi . Controller . api_version ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ wsgi . action ( '<STR_LIT>' ) <EOL> @ validation . schema ( remote_consoles . get_spice_console ) <EOL> def get_spice_console ( self , req , id , body ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> console_type = body [ '<STR_LIT>' ] . get ( '<STR_LIT:type>' ) <EOL> try : <EOL> instance = common . get_instance ( self . compute_api , context , id ) <EOL> output = self . compute_api . get_spice_console ( context , <EOL> instance , <EOL> console_type ) <EOL> except exception . ConsoleTypeUnavailable as e : <EOL> raise webob . exc . HTTPBadRequest ( explanation = e . format_message ( ) ) <EOL> except ( exception . InstanceUnknownCell , <EOL> exception . InstanceNotFound ) as e : <EOL> raise webob . exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> except exception . InstanceNotReady as e : <EOL> raise webob . exc . HTTPConflict ( explanation = e . format_message ( ) ) <EOL> except NotImplementedError : <EOL> common . raise_feature_not_supported ( ) <EOL> return { '<STR_LIT>' : { '<STR_LIT:type>' : console_type , '<STR_LIT:url>' : output [ '<STR_LIT:url>' ] } } <EOL> @ wsgi . Controller . api_version ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ wsgi . action ( '<STR_LIT>' ) <EOL> @ validation . schema ( remote_consoles . get_rdp_console ) <EOL> def get_rdp_console ( self , req , id , body ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> console_type = body [ '<STR_LIT>' ] . get ( '<STR_LIT:type>' ) <EOL> instance = common . get_instance ( self . compute_api , context , id ) <EOL> try : <EOL> output = self . compute_api . get_rdp_console ( context , <EOL> instance , <EOL> console_type ) <EOL> except exception . ConsoleTypeUnavailable as e : <EOL> raise webob . exc . HTTPBadRequest ( explanation = e . format_message ( ) ) <EOL> except ( exception . InstanceUnknownCell , <EOL> exception . InstanceNotFound ) as e : <EOL> raise webob . exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> except exception . InstanceNotReady as e : <EOL> raise webob . exc . HTTPConflict ( explanation = e . format_message ( ) ) <EOL> except NotImplementedError : <EOL> common . raise_feature_not_supported ( ) <EOL> return { '<STR_LIT>' : { '<STR_LIT:type>' : console_type , '<STR_LIT:url>' : output [ '<STR_LIT:url>' ] } } <EOL> @ wsgi . Controller . api_version ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ wsgi . action ( '<STR_LIT>' ) <EOL> @ validation . schema ( remote_consoles . get_serial_console ) <EOL> def get_serial_console ( self , req , id , body ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> console_type = body [ '<STR_LIT>' ] . get ( '<STR_LIT:type>' ) <EOL> try : <EOL> instance = common . get_instance ( self . compute_api , context , id ) <EOL> output = self . compute_api . get_serial_console ( context , <EOL> instance , <EOL> console_type ) <EOL> except ( exception . InstanceUnknownCell , <EOL> exception . InstanceNotFound ) as e : <EOL> raise webob . exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> except exception . InstanceNotReady as e : <EOL> raise webob . exc . HTTPConflict ( explanation = e . format_message ( ) ) <EOL> except ( exception . ConsoleTypeUnavailable , <EOL> exception . ImageSerialPortNumberInvalid , <EOL> exception . ImageSerialPortNumberExceedFlavorValue , <EOL> exception . SocketPortRangeExhaustedException ) as e : <EOL> raise webob . exc . HTTPBadRequest ( explanation = e . format_message ( ) ) <EOL> except NotImplementedError : <EOL> common . raise_feature_not_supported ( ) <EOL> return { '<STR_LIT>' : { '<STR_LIT:type>' : console_type , '<STR_LIT:url>' : output [ '<STR_LIT:url>' ] } } <EOL> @ wsgi . Controller . api_version ( "<STR_LIT>" ) <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ validation . schema ( remote_consoles . create_v26 , "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ validation . schema ( remote_consoles . create_v28 , "<STR_LIT>" ) <EOL> def create ( self , req , server_id , body ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> instance = common . get_instance ( self . compute_api , context , server_id ) <EOL> protocol = body [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> console_type = body [ '<STR_LIT>' ] [ '<STR_LIT:type>' ] <EOL> try : <EOL> handler = self . handlers . get ( protocol ) <EOL> output = handler ( context , instance , console_type ) <EOL> return { '<STR_LIT>' : { '<STR_LIT>' : protocol , <EOL> '<STR_LIT:type>' : console_type , <EOL> '<STR_LIT:url>' : output [ '<STR_LIT:url>' ] } } <EOL> except exception . InstanceNotFound as e : <EOL> raise webob . exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> except exception . InstanceNotReady as e : <EOL> raise webob . exc . HTTPConflict ( explanation = e . format_message ( ) ) <EOL> except ( exception . ConsoleTypeInvalid , <EOL> exception . ConsoleTypeUnavailable , <EOL> exception . ImageSerialPortNumberInvalid , <EOL> exception . ImageSerialPortNumberExceedFlavorValue , <EOL> exception . SocketPortRangeExhaustedException ) as e : <EOL> raise webob . exc . HTTPBadRequest ( explanation = e . format_message ( ) ) <EOL> except NotImplementedError : <EOL> common . raise_feature_not_supported ( ) <EOL> class RemoteConsoles ( extensions . V21APIExtensionBase ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> alias = ALIAS <EOL> version = <NUM_LIT:1> <EOL> def get_controller_extensions ( self ) : <EOL> controller = RemoteConsolesController ( ) <EOL> extension = extensions . ControllerExtension ( self , '<STR_LIT>' , controller ) <EOL> return [ extension ] <EOL> def get_resources ( self ) : <EOL> parent = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> resources = [ <EOL> extensions . ResourceExtension ( <EOL> '<STR_LIT>' , RemoteConsolesController ( ) , parent = parent , <EOL> member_name = '<STR_LIT>' ) ] <EOL> return resources </s>
