<s> import six <EOL> import webob <EOL> from nova . api . openstack . compute . schemas import quota_classes <EOL> from nova . api . openstack import extensions <EOL> from nova . api . openstack import wsgi <EOL> from nova . api import validation <EOL> from nova import db <EOL> from nova import exception <EOL> from nova import quota <EOL> from nova import utils <EOL> QUOTAS = quota . QUOTAS <EOL> ALIAS = "<STR_LIT>" <EOL> EXTENDED_QUOTAS = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> authorize = extensions . os_compute_authorizer ( ALIAS ) <EOL> class QuotaClassSetsController ( wsgi . Controller ) : <EOL> supported_quotas = [ ] <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . supported_quotas = QUOTAS . resources <EOL> extension_info = kwargs . pop ( '<STR_LIT>' ) . get_extensions ( ) <EOL> for resource , extension in EXTENDED_QUOTAS . items ( ) : <EOL> if extension not in extension_info : <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> @ extensions . expected_errors ( ( ) ) <EOL> def show ( self , req , id ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context , action = '<STR_LIT>' , target = { '<STR_LIT>' : id } ) <EOL> values = QUOTAS . get_class_quotas ( context , id ) <EOL> return self . _format_quota_set ( id , values ) <EOL> @ extensions . expected_errors ( <NUM_LIT> ) <EOL> @ validation . schema ( quota_classes . update ) <EOL> def update ( self , req , id , body ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context , action = '<STR_LIT>' , target = { '<STR_LIT>' : id } ) <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> for key , value in six . iteritems ( body [ '<STR_LIT>' ] ) : <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 QuotaClasses ( extensions . V21APIExtensionBase ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> alias = ALIAS <EOL> version = <NUM_LIT:1> <EOL> def get_resources ( self ) : <EOL> resources = [ ] <EOL> res = extensions . ResourceExtension ( <EOL> ALIAS , <EOL> QuotaClassSetsController ( extension_info = self . extension_info ) ) <EOL> resources . append ( res ) <EOL> return resources <EOL> def get_controller_extensions ( self ) : <EOL> return [ ] </s>
