<s> from oslo_utils import strutils <EOL> import six . moves . urllib . parse as urlparse <EOL> import webob <EOL> from nova . api . openstack import extensions <EOL> from nova . api . openstack import wsgi <EOL> import nova . context <EOL> from nova import db <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> from nova import objects <EOL> from nova import quota <EOL> from nova import utils <EOL> QUOTAS = quota . QUOTAS <EOL> NON_QUOTA_KEYS = [ '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT>' ] <EOL> EXTENDED_QUOTAS = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> authorize_update = extensions . extension_authorizer ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> authorize_show = extensions . extension_authorizer ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> authorize_delete = extensions . extension_authorizer ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class QuotaSetsController ( wsgi . Controller ) : <EOL> supported_quotas = [ ] <EOL> def __init__ ( self , ext_mgr ) : <EOL> self . ext_mgr = ext_mgr <EOL> self . supported_quotas = QUOTAS . resources <EOL> for resource , extension in EXTENDED_QUOTAS . items ( ) : <EOL> if not self . ext_mgr . is_loaded ( extension ) : <EOL> self . supported_quotas . remove ( resource ) <EOL> def _format_quota_set ( self , project_id , quota_set ) : <EOL> """<STR_LIT>""" <EOL> if project_id : <EOL> result = dict ( id = str ( project_id ) ) <EOL> else : <EOL> result = { } <EOL> for resource in self . supported_quotas : <EOL> if resource in quota_set : <EOL> result [ resource ] = quota_set [ resource ] <EOL> return dict ( quota_set = result ) <EOL> def _validate_quota_limit ( self , resource , limit , minimum , maximum ) : <EOL> if limit < - <NUM_LIT:1> or limit > db . MAX_INT : <EOL> msg = ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> { '<STR_LIT>' : limit , '<STR_LIT>' : resource , '<STR_LIT>' : db . MAX_INT } ) <EOL> raise webob . exc . HTTPBadRequest ( explanation = msg ) <EOL> def conv_inf ( value ) : <EOL> return float ( "<STR_LIT>" ) if value == - <NUM_LIT:1> else value <EOL> if conv_inf ( limit ) < conv_inf ( minimum ) : <EOL> msg = ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> { '<STR_LIT>' : limit , '<STR_LIT>' : resource , '<STR_LIT>' : minimum } ) <EOL> raise webob . exc . HTTPBadRequest ( explanation = msg ) <EOL> if conv_inf ( limit ) > conv_inf ( maximum ) : <EOL> msg = ( _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % <EOL> { '<STR_LIT>' : limit , '<STR_LIT>' : resource , '<STR_LIT>' : maximum } ) <EOL> raise webob . exc . HTTPBadRequest ( explanation = msg ) <EOL> def _get_quotas ( self , context , id , user_id = None , usages = False ) : <EOL> if user_id : <EOL> values = QUOTAS . get_user_quotas ( context , id , user_id , <EOL> usages = usages ) <EOL> else : <EOL> values = QUOTAS . get_project_quotas ( context , id , usages = usages ) <EOL> if usages : <EOL> return values <EOL> else : <EOL> return { k : v [ '<STR_LIT>' ] for k , v in values . items ( ) } <EOL> def show ( self , req , id ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize_show ( context ) <EOL> params = urlparse . parse_qs ( req . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> user_id = None <EOL> if self . ext_mgr . is_loaded ( '<STR_LIT>' ) : <EOL> user_id = params . get ( '<STR_LIT>' , [ None ] ) [ <NUM_LIT:0> ] <EOL> try : <EOL> nova . context . authorize_project_context ( context , id ) <EOL> return self . _format_quota_set ( id , <EOL> self . _get_quotas ( context , id , user_id = user_id ) ) <EOL> except exception . Forbidden : <EOL> raise webob . exc . HTTPForbidden ( ) <EOL> def update ( self , req , id , body ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize_update ( context ) <EOL> try : <EOL> nova . context . require_admin_context ( context ) <EOL> except exception . AdminRequired : <EOL> raise webob . exc . HTTPForbidden ( ) <EOL> project_id = id <EOL> bad_keys = [ ] <EOL> force_update = True <EOL> extended_loaded = False <EOL> if self . ext_mgr . is_loaded ( '<STR_LIT>' ) : <EOL> extended_loaded = True <EOL> force_update = False <EOL> user_id = None <EOL> if self . ext_mgr . is_loaded ( '<STR_LIT>' ) : <EOL> params = urlparse . parse_qs ( req . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> user_id = params . get ( '<STR_LIT>' , [ None ] ) [ <NUM_LIT:0> ] <EOL> try : <EOL> nova . context . authorize_project_context ( context , id ) <EOL> settable_quotas = QUOTAS . get_settable_quotas ( context , project_id , <EOL> user_id = user_id ) <EOL> except exception . Forbidden : <EOL> raise webob . exc . HTTPForbidden ( ) <EOL> if not self . is_valid_body ( body , '<STR_LIT>' ) : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise webob . exc . HTTPBadRequest ( explanation = msg ) <EOL> quota_set = body [ '<STR_LIT>' ] <EOL> for key , value in quota_set . items ( ) : <EOL> if ( key not in self . supported_quotas <EOL> and key not in NON_QUOTA_KEYS ) : <EOL> bad_keys . append ( key ) <EOL> continue <EOL> if key == '<STR_LIT>' and extended_loaded : <EOL> force_update = strutils . bool_from_string ( value ) <EOL> elif key not in NON_QUOTA_KEYS and value : <EOL> try : <EOL> utils . validate_integer ( value , key ) <EOL> except exception . InvalidInput as e : <EOL> raise webob . exc . HTTPBadRequest ( <EOL> explanation = e . format_message ( ) ) <EOL> if bad_keys : <EOL> msg = _ ( "<STR_LIT>" ) % "<STR_LIT:U+002C>" . join ( bad_keys ) <EOL> raise webob . exc . HTTPBadRequest ( explanation = msg ) <EOL> valid_quotas = { } <EOL> for key , value in quota_set . items ( ) : <EOL> if key in NON_QUOTA_KEYS or ( not value and value != <NUM_LIT:0> ) : <EOL> continue <EOL> value = int ( value ) <EOL> if not force_update : <EOL> minimum = settable_quotas [ key ] [ '<STR_LIT>' ] <EOL> maximum = settable_quotas [ key ] [ '<STR_LIT>' ] <EOL> self . _validate_quota_limit ( key , value , minimum , maximum ) <EOL> valid_quotas [ key ] = value <EOL> for key , value in valid_quotas . items ( ) : <EOL> try : <EOL> objects . Quotas . create_limit ( context , project_id , <EOL> key , value , user_id = user_id ) <EOL> except exception . QuotaExists : <EOL> objects . Quotas . update_limit ( context , project_id , <EOL> key , value , user_id = user_id ) <EOL> values = self . _get_quotas ( context , id , user_id = user_id ) <EOL> return self . _format_quota_set ( None , values ) <EOL> def defaults ( self , req , id ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize_show ( context ) <EOL> values = QUOTAS . get_defaults ( context ) <EOL> return self . _format_quota_set ( id , values ) <EOL> def delete ( self , req , id ) : <EOL> if self . ext_mgr . is_loaded ( '<STR_LIT>' ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize_delete ( context ) <EOL> params = urlparse . parse_qs ( req . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> user_id = params . get ( '<STR_LIT>' , [ None ] ) [ <NUM_LIT:0> ] <EOL> if user_id and not self . ext_mgr . is_loaded ( '<STR_LIT>' ) : <EOL> raise webob . exc . HTTPNotFound ( ) <EOL> try : <EOL> nova . context . authorize_project_context ( context , id ) <EOL> nova . context . require_admin_context ( context ) <EOL> if user_id : <EOL> QUOTAS . destroy_all_by_project_and_user ( context , <EOL> id , user_id ) <EOL> else : <EOL> QUOTAS . destroy_all_by_project ( context , id ) <EOL> return webob . Response ( status_int = <NUM_LIT> ) <EOL> except exception . Forbidden : <EOL> raise webob . exc . HTTPForbidden ( ) <EOL> raise webob . exc . HTTPNotFound ( ) <EOL> class Quotas ( 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 ( '<STR_LIT>' , <EOL> QuotaSetsController ( self . ext_mgr ) , <EOL> member_actions = { '<STR_LIT>' : '<STR_LIT:GET>' } ) <EOL> resources . append ( res ) <EOL> return resources </s>
