<s> """<STR_LIT>""" <EOL> import oslo_messaging as messaging <EOL> from oslo_utils import strutils <EOL> from oslo_utils import timeutils <EOL> import six <EOL> from webob import exc <EOL> from nova . api . openstack import common <EOL> from nova . api . openstack import extensions <EOL> from nova . cells import rpcapi as cells_rpcapi <EOL> import nova . conf <EOL> from nova import context as nova_context <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> from nova import rpc <EOL> CONF = nova . conf . CONF <EOL> authorize = extensions . extension_authorizer ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _filter_keys ( item , keys ) : <EOL> """<STR_LIT>""" <EOL> return { k : v for k , v in six . iteritems ( item ) if k in keys } <EOL> def _fixup_cell_info ( cell_info , keys ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in cell_info : <EOL> return <EOL> transport_url = cell_info . pop ( '<STR_LIT>' ) <EOL> try : <EOL> transport_url = rpc . get_transport_url ( transport_url ) <EOL> except messaging . InvalidTransportURL : <EOL> for key in keys : <EOL> cell_info . setdefault ( key , None ) <EOL> return <EOL> if not transport_url . hosts : <EOL> return <EOL> transport_host = transport_url . hosts [ <NUM_LIT:0> ] <EOL> transport_field_map = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:port>' } <EOL> for key in keys : <EOL> if key in cell_info : <EOL> continue <EOL> transport_field = transport_field_map . get ( key , key ) <EOL> cell_info [ key ] = getattr ( transport_host , transport_field ) <EOL> def _scrub_cell ( cell , detail = False ) : <EOL> keys = [ '<STR_LIT:name>' , '<STR_LIT:username>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> if detail : <EOL> keys . append ( '<STR_LIT>' ) <EOL> cell_info = _filter_keys ( cell , keys + [ '<STR_LIT>' ] ) <EOL> _fixup_cell_info ( cell_info , keys ) <EOL> cell_info [ '<STR_LIT:type>' ] = '<STR_LIT>' if cell [ '<STR_LIT>' ] else '<STR_LIT>' <EOL> return cell_info <EOL> class Controller ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ext_mgr ) : <EOL> self . cells_rpcapi = cells_rpcapi . CellsAPI ( ) <EOL> self . ext_mgr = ext_mgr <EOL> def _get_cells ( self , ctxt , req , detail = False ) : <EOL> """<STR_LIT>""" <EOL> items = self . cells_rpcapi . get_cell_info_for_neighbors ( ctxt ) <EOL> items = common . limited ( items , req ) <EOL> items = [ _scrub_cell ( item , detail = detail ) for item in items ] <EOL> return dict ( cells = items ) <EOL> @ common . check_cells_enabled <EOL> def index ( self , req ) : <EOL> """<STR_LIT>""" <EOL> ctxt = req . environ [ '<STR_LIT>' ] <EOL> authorize ( ctxt ) <EOL> return self . _get_cells ( ctxt , req ) <EOL> @ common . check_cells_enabled <EOL> def detail ( self , req ) : <EOL> """<STR_LIT>""" <EOL> ctxt = req . environ [ '<STR_LIT>' ] <EOL> authorize ( ctxt ) <EOL> return self . _get_cells ( ctxt , req , detail = True ) <EOL> @ common . check_cells_enabled <EOL> def info ( self , req ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> cell_capabs = { } <EOL> my_caps = CONF . cells . capabilities <EOL> for cap in my_caps : <EOL> key , value = cap . split ( '<STR_LIT:=>' ) <EOL> cell_capabs [ key ] = value <EOL> cell = { '<STR_LIT:name>' : CONF . cells . name , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT:username>' : None , <EOL> '<STR_LIT>' : cell_capabs } <EOL> return dict ( cell = cell ) <EOL> @ common . check_cells_enabled <EOL> def capacities ( self , req , id = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . ext_mgr . is_loaded ( '<STR_LIT>' ) : <EOL> raise exc . HTTPNotFound ( ) <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> try : <EOL> capacities = self . cells_rpcapi . get_capacities ( context , <EOL> cell_name = id ) <EOL> except exception . CellNotFound : <EOL> msg = ( _ ( "<STR_LIT>" ) % { '<STR_LIT:id>' : id } ) <EOL> raise exc . HTTPNotFound ( explanation = msg ) <EOL> return dict ( cell = { "<STR_LIT>" : capacities } ) <EOL> @ common . check_cells_enabled <EOL> def show ( self , req , id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> try : <EOL> cell = self . cells_rpcapi . cell_get ( context , id ) <EOL> except exception . CellNotFound as e : <EOL> raise exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> return dict ( cell = _scrub_cell ( cell ) ) <EOL> @ common . check_cells_enabled <EOL> def delete ( self , req , id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> authorize ( context , action = "<STR_LIT>" ) <EOL> nova_context . require_admin_context ( context ) <EOL> try : <EOL> num_deleted = self . cells_rpcapi . cell_delete ( context , id ) <EOL> except exception . CellsUpdateUnsupported as e : <EOL> raise exc . HTTPForbidden ( explanation = e . format_message ( ) ) <EOL> if num_deleted == <NUM_LIT:0> : <EOL> raise exc . HTTPNotFound ( ) <EOL> return { } <EOL> def _validate_cell_name ( self , cell_name ) : <EOL> """<STR_LIT>""" <EOL> if not cell_name : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> if '<STR_LIT:!>' in cell_name or '<STR_LIT:.>' in cell_name or '<STR_LIT:@>' in cell_name : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> def _validate_cell_type ( self , cell_type ) : <EOL> """<STR_LIT>""" <EOL> if cell_type not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> def _normalize_cell ( self , cell , existing = None ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:type>' in cell : <EOL> self . _validate_cell_type ( cell [ '<STR_LIT:type>' ] ) <EOL> cell [ '<STR_LIT>' ] = cell [ '<STR_LIT:type>' ] == '<STR_LIT>' <EOL> del cell [ '<STR_LIT:type>' ] <EOL> elif existing : <EOL> cell [ '<STR_LIT>' ] = existing [ '<STR_LIT>' ] <EOL> else : <EOL> cell [ '<STR_LIT>' ] = False <EOL> transport_url = existing . get ( '<STR_LIT>' ) if existing else None <EOL> transport_url = rpc . get_transport_url ( transport_url ) <EOL> if '<STR_LIT>' in cell : <EOL> transport_url . virtual_host = cell . pop ( '<STR_LIT>' ) <EOL> if not transport_url . hosts : <EOL> transport_url . hosts . append ( messaging . TransportHost ( ) ) <EOL> transport_host = transport_url . hosts [ <NUM_LIT:0> ] <EOL> if cell . get ( '<STR_LIT>' ) is not None : <EOL> try : <EOL> cell [ '<STR_LIT>' ] = int ( cell [ '<STR_LIT>' ] ) <EOL> except ValueError : <EOL> raise exc . HTTPBadRequest ( <EOL> explanation = _ ( '<STR_LIT>' ) ) <EOL> transport_field_map = { <EOL> '<STR_LIT:username>' : '<STR_LIT:username>' , <EOL> '<STR_LIT:password>' : '<STR_LIT:password>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:port>' : '<STR_LIT>' , <EOL> } <EOL> for key , input_field in transport_field_map . items ( ) : <EOL> if input_field in cell : <EOL> setattr ( transport_host , key , cell . pop ( input_field ) ) <EOL> cell [ '<STR_LIT>' ] = str ( transport_url ) <EOL> @ common . check_cells_enabled <EOL> def create ( self , req , body ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> authorize ( context , action = "<STR_LIT>" ) <EOL> nova_context . require_admin_context ( context ) <EOL> if '<STR_LIT>' not in body : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> cell = body [ '<STR_LIT>' ] <EOL> if '<STR_LIT:name>' not in cell : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> self . _validate_cell_name ( cell [ '<STR_LIT:name>' ] ) <EOL> self . _normalize_cell ( cell ) <EOL> try : <EOL> cell = self . cells_rpcapi . cell_create ( context , cell ) <EOL> except exception . CellsUpdateUnsupported as e : <EOL> raise exc . HTTPForbidden ( explanation = e . format_message ( ) ) <EOL> return dict ( cell = _scrub_cell ( cell ) ) <EOL> @ common . check_cells_enabled <EOL> def update ( self , req , id , body ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> authorize ( context , action = "<STR_LIT>" ) <EOL> nova_context . require_admin_context ( context ) <EOL> if '<STR_LIT>' not in body : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> cell = body [ '<STR_LIT>' ] <EOL> cell . pop ( '<STR_LIT:id>' , None ) <EOL> if '<STR_LIT:name>' in cell : <EOL> self . _validate_cell_name ( cell [ '<STR_LIT:name>' ] ) <EOL> try : <EOL> existing = self . cells_rpcapi . cell_get ( context , id ) <EOL> except exception . CellNotFound : <EOL> raise exc . HTTPNotFound ( ) <EOL> self . _normalize_cell ( cell , existing ) <EOL> try : <EOL> cell = self . cells_rpcapi . cell_update ( context , id , cell ) <EOL> except exception . CellNotFound : <EOL> raise exc . HTTPNotFound ( ) <EOL> except exception . CellsUpdateUnsupported as e : <EOL> raise exc . HTTPForbidden ( explanation = e . format_message ( ) ) <EOL> return dict ( cell = _scrub_cell ( cell ) ) <EOL> @ common . check_cells_enabled <EOL> def sync_instances ( self , req , body ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> authorize ( context , action = "<STR_LIT>" ) <EOL> project_id = body . pop ( '<STR_LIT>' , None ) <EOL> deleted = body . pop ( '<STR_LIT>' , False ) <EOL> updated_since = body . pop ( '<STR_LIT>' , None ) <EOL> if body : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> if isinstance ( deleted , six . string_types ) : <EOL> try : <EOL> deleted = strutils . bool_from_string ( deleted , strict = True ) <EOL> except ValueError as err : <EOL> raise exc . HTTPBadRequest ( explanation = six . text_type ( err ) ) <EOL> if updated_since : <EOL> try : <EOL> timeutils . parse_isotime ( updated_since ) <EOL> except ValueError : <EOL> msg = _ ( '<STR_LIT>' ) <EOL> raise exc . HTTPBadRequest ( explanation = msg ) <EOL> self . cells_rpcapi . sync_instances ( context , project_id = project_id , <EOL> updated_since = updated_since , deleted = deleted ) <EOL> class Cells ( 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> coll_actions = { <EOL> '<STR_LIT>' : '<STR_LIT:GET>' , <EOL> '<STR_LIT:info>' : '<STR_LIT:GET>' , <EOL> '<STR_LIT>' : '<STR_LIT:POST>' , <EOL> '<STR_LIT>' : '<STR_LIT:GET>' , <EOL> } <EOL> memb_actions = { <EOL> '<STR_LIT>' : '<STR_LIT:GET>' , <EOL> } <EOL> res = extensions . ResourceExtension ( '<STR_LIT>' , <EOL> Controller ( self . ext_mgr ) , collection_actions = coll_actions , <EOL> member_actions = memb_actions ) <EOL> return [ res ] </s>
