<s> from oslo_config import cfg <EOL> from oslo_utils import strutils <EOL> from webob import exc <EOL> from nova . api . openstack import api_version_request <EOL> from nova . api . openstack import common <EOL> from nova . api . openstack . compute . schemas import evacuate <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> from nova . i18n import _ <EOL> from nova import utils <EOL> CONF = cfg . CONF <EOL> CONF . import_opt ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> ALIAS = "<STR_LIT>" <EOL> authorize = extensions . os_compute_authorizer ( ALIAS ) <EOL> class EvacuateController ( wsgi . Controller ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( EvacuateController , self ) . __init__ ( * args , ** kwargs ) <EOL> self . compute_api = compute . API ( skip_policy_check = True ) <EOL> self . host_api = compute . HostAPI ( ) <EOL> def _get_on_shared_storage ( self , req , evacuate_body ) : <EOL> if api_version_request . is_supported ( req , min_version = '<STR_LIT>' ) : <EOL> return None <EOL> else : <EOL> return strutils . bool_from_string ( evacuate_body [ "<STR_LIT>" ] ) <EOL> def _get_password ( self , req , evacuate_body , on_shared_storage ) : <EOL> password = None <EOL> if '<STR_LIT>' in evacuate_body : <EOL> if on_shared_storage : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> password = evacuate_body [ '<STR_LIT>' ] <EOL> elif not on_shared_storage : <EOL> password = utils . generate_password ( ) <EOL> return password <EOL> def _get_password_v214 ( self , req , evacuate_body ) : <EOL> if '<STR_LIT>' in evacuate_body : <EOL> password = evacuate_body [ '<STR_LIT>' ] <EOL> else : <EOL> password = utils . generate_password ( ) <EOL> return password <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ wsgi . action ( '<STR_LIT>' ) <EOL> @ validation . schema ( evacuate . evacuate , "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ validation . schema ( evacuate . evacuate_v214 , "<STR_LIT>" ) <EOL> def _evacuate ( self , req , id , body ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ "<STR_LIT>" ] <EOL> authorize ( context ) <EOL> evacuate_body = body [ "<STR_LIT>" ] <EOL> host = evacuate_body . get ( "<STR_LIT:host>" ) <EOL> on_shared_storage = self . _get_on_shared_storage ( req , evacuate_body ) <EOL> if api_version_request . is_supported ( req , min_version = '<STR_LIT>' ) : <EOL> password = self . _get_password_v214 ( req , evacuate_body ) <EOL> else : <EOL> password = self . _get_password ( req , evacuate_body , <EOL> on_shared_storage ) <EOL> if host is not None : <EOL> try : <EOL> self . host_api . service_get_by_compute_host ( context , host ) <EOL> except exception . ComputeHostNotFound : <EOL> msg = _ ( "<STR_LIT>" ) % host <EOL> raise exc . HTTPNotFound ( explanation = msg ) <EOL> instance = common . get_instance ( self . compute_api , context , id ) <EOL> if instance . host == host : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> try : <EOL> self . compute_api . evacuate ( context , instance , host , <EOL> on_shared_storage , password ) <EOL> except exception . InstanceUnknownCell as e : <EOL> raise exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> except exception . InstanceInvalidState as state_error : <EOL> common . raise_http_conflict_for_instance_invalid_state ( state_error , <EOL> '<STR_LIT>' , id ) <EOL> except exception . ComputeServiceInUse as e : <EOL> raise exc . HTTPBadRequest ( explanation = e . format_message ( ) ) <EOL> if ( not api_version_request . is_supported ( req , min_version = '<STR_LIT>' ) and <EOL> CONF . enable_instance_password ) : <EOL> return { '<STR_LIT>' : password } <EOL> else : <EOL> return None <EOL> class Evacuate ( extensions . V21APIExtensionBase ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> alias = ALIAS <EOL> version = <NUM_LIT:1> <EOL> def get_resources ( self ) : <EOL> return [ ] <EOL> def get_controller_extensions ( self ) : <EOL> controller = EvacuateController ( ) <EOL> extension = extensions . ControllerExtension ( self , '<STR_LIT>' , controller ) <EOL> return [ extension ] </s>
