<s> """<STR_LIT>""" <EOL> import webob <EOL> from nova . api . openstack import common <EOL> from nova . api . openstack import extensions <EOL> from nova . api . openstack import wsgi <EOL> from nova import context as nova_context <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> from nova import objects <EOL> soft_authorize = extensions . soft_extension_authorizer ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> authorize = extensions . extension_authorizer ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _marshall_flavor_access ( flavor ) : <EOL> rval = [ ] <EOL> for project_id in flavor . projects : <EOL> rval . append ( { '<STR_LIT>' : flavor . flavorid , <EOL> '<STR_LIT>' : project_id } ) <EOL> return { '<STR_LIT>' : rval } <EOL> class FlavorAccessController ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( FlavorAccessController , self ) . __init__ ( ) <EOL> def index ( self , req , flavor_id ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> nova_context . require_admin_context ( context ) <EOL> flavor = common . get_flavor ( context , flavor_id ) <EOL> if flavor . is_public : <EOL> explanation = _ ( "<STR_LIT>" ) <EOL> raise webob . exc . HTTPNotFound ( explanation = explanation ) <EOL> return _marshall_flavor_access ( flavor ) <EOL> class FlavorActionController ( wsgi . Controller ) : <EOL> """<STR_LIT>""" <EOL> def _check_body ( self , body ) : <EOL> if body is None or body == "<STR_LIT>" : <EOL> raise webob . exc . HTTPBadRequest ( explanation = _ ( "<STR_LIT>" ) ) <EOL> def _extend_flavor ( self , flavor_rval , flavor_ref ) : <EOL> key = "<STR_LIT>" % ( Flavor_access . alias ) <EOL> flavor_rval [ key ] = flavor_ref [ '<STR_LIT>' ] <EOL> @ wsgi . extends <EOL> def show ( self , req , resp_obj , id ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> if soft_authorize ( context ) : <EOL> db_flavor = req . get_db_flavor ( id ) <EOL> self . _extend_flavor ( resp_obj . obj [ '<STR_LIT>' ] , db_flavor ) <EOL> @ wsgi . extends <EOL> def detail ( self , req , resp_obj ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> if soft_authorize ( context ) : <EOL> flavors = list ( resp_obj . obj [ '<STR_LIT>' ] ) <EOL> for flavor_rval in flavors : <EOL> db_flavor = req . get_db_flavor ( flavor_rval [ '<STR_LIT:id>' ] ) <EOL> self . _extend_flavor ( flavor_rval , db_flavor ) <EOL> @ wsgi . extends ( action = '<STR_LIT>' ) <EOL> def create ( self , req , body , resp_obj ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> if soft_authorize ( context ) : <EOL> db_flavor = req . get_db_flavor ( resp_obj . obj [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] ) <EOL> self . _extend_flavor ( resp_obj . obj [ '<STR_LIT>' ] , db_flavor ) <EOL> @ wsgi . action ( "<STR_LIT>" ) <EOL> def _addTenantAccess ( self , req , id , body ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context , action = "<STR_LIT>" ) <EOL> nova_context . require_admin_context ( context ) <EOL> self . _check_body ( body ) <EOL> vals = body [ '<STR_LIT>' ] <EOL> tenant = vals . get ( '<STR_LIT>' ) <EOL> if not tenant : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise webob . exc . HTTPBadRequest ( explanation = msg ) <EOL> flavor = objects . Flavor ( context = context , flavorid = id ) <EOL> try : <EOL> flavor . add_access ( tenant ) <EOL> except exception . FlavorAccessExists as err : <EOL> raise webob . exc . HTTPConflict ( explanation = err . format_message ( ) ) <EOL> except exception . FlavorNotFound as err : <EOL> raise webob . exc . HTTPNotFound ( explanation = err . format_message ( ) ) <EOL> return _marshall_flavor_access ( flavor ) <EOL> @ wsgi . action ( "<STR_LIT>" ) <EOL> def _removeTenantAccess ( self , req , id , body ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context , action = "<STR_LIT>" ) <EOL> nova_context . require_admin_context ( context ) <EOL> self . _check_body ( body ) <EOL> vals = body [ '<STR_LIT>' ] <EOL> tenant = vals . get ( '<STR_LIT>' ) <EOL> if not tenant : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise webob . exc . HTTPBadRequest ( explanation = msg ) <EOL> flavor = objects . Flavor ( context = context , flavorid = id ) <EOL> try : <EOL> flavor . remove_access ( tenant ) <EOL> except ( exception . FlavorNotFound , <EOL> exception . FlavorAccessNotFound ) as err : <EOL> raise webob . exc . HTTPNotFound ( explanation = err . format_message ( ) ) <EOL> return _marshall_flavor_access ( flavor ) <EOL> class Flavor_access ( extensions . ExtensionDescriptor ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> alias = "<STR_LIT>" <EOL> namespace = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> updated = "<STR_LIT>" <EOL> def get_resources ( self ) : <EOL> resources = [ ] <EOL> res = extensions . ResourceExtension ( <EOL> '<STR_LIT>' , <EOL> controller = FlavorAccessController ( ) , <EOL> parent = dict ( member_name = '<STR_LIT>' , collection_name = '<STR_LIT>' ) ) <EOL> resources . append ( res ) <EOL> return resources <EOL> def get_controller_extensions ( self ) : <EOL> extension = extensions . ControllerExtension ( <EOL> self , '<STR_LIT>' , FlavorActionController ( ) ) <EOL> return [ extension ] </s>
