<s> 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 quota <EOL> from nova import utils <EOL> QUOTAS = quota . QUOTAS <EOL> EXTENDED_QUOTAS = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> authorize = extensions . extension_authorizer ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> class QuotaClassSetsController ( 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 , quota_class , quota_set ) : <EOL> """<STR_LIT>""" <EOL> if quota_class : <EOL> result = dict ( id = str ( quota_class ) ) <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_class_set = result ) <EOL> def show ( self , req , id ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> try : <EOL> nova . context . authorize_quota_class_context ( context , id ) <EOL> values = QUOTAS . get_class_quotas ( context , id ) <EOL> return self . _format_quota_set ( id , values ) <EOL> except exception . Forbidden : <EOL> raise webob . exc . HTTPForbidden ( ) <EOL> def update ( self , req , id , body ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context ) <EOL> try : <EOL> utils . check_string_length ( id , '<STR_LIT>' , <EOL> min_length = <NUM_LIT:1> , max_length = <NUM_LIT:255> ) <EOL> except exception . InvalidInput as e : <EOL> raise webob . exc . HTTPBadRequest ( <EOL> explanation = e . format_message ( ) ) <EOL> quota_class = id <EOL> bad_keys = [ ] <EOL> if not self . is_valid_body ( body , '<STR_LIT>' ) : <EOL> msg = _ ( "<STR_LIT>" ) <EOL> raise webob . exc . HTTPBadRequest ( explanation = msg ) <EOL> quota_class_set = body [ '<STR_LIT>' ] <EOL> for key in quota_class_set . keys ( ) : <EOL> if key not in self . supported_quotas : <EOL> bad_keys . append ( key ) <EOL> continue <EOL> try : <EOL> body [ '<STR_LIT>' ] [ key ] = utils . validate_integer ( <EOL> body [ '<STR_LIT>' ] [ key ] , key , max_value = db . MAX_INT ) <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> try : <EOL> nova . context . require_admin_context ( context ) <EOL> except exception . AdminRequired : <EOL> raise webob . exc . HTTPForbidden ( ) <EOL> for key , value in quota_class_set . items ( ) : <EOL> try : <EOL> db . quota_class_update ( context , quota_class , key , value ) <EOL> except exception . QuotaClassNotFound : <EOL> db . quota_class_create ( context , quota_class , key , value ) <EOL> values = QUOTAS . get_class_quotas ( context , quota_class ) <EOL> return self . _format_quota_set ( None , values ) <EOL> class Quota_classes ( 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 ( '<STR_LIT>' , <EOL> QuotaClassSetsController ( self . ext_mgr ) ) <EOL> resources . append ( res ) <EOL> return resources </s>
