<s> import six <EOL> import webob <EOL> from nova . api . openstack import common <EOL> from nova . api . openstack . compute . schemas import flavors_extraspecs <EOL> from nova . api . openstack import extensions <EOL> from nova . api . openstack import wsgi <EOL> from nova . api import validation <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> from nova import utils <EOL> ALIAS = '<STR_LIT>' <EOL> authorize = extensions . os_compute_authorizer ( ALIAS ) <EOL> class FlavorExtraSpecsController ( wsgi . Controller ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( FlavorExtraSpecsController , self ) . __init__ ( * args , ** kwargs ) <EOL> def _get_extra_specs ( self , context , flavor_id ) : <EOL> flavor = common . get_flavor ( context , flavor_id ) <EOL> return dict ( extra_specs = flavor . extra_specs ) <EOL> def _check_extra_specs_value ( self , specs ) : <EOL> for key , value in six . iteritems ( specs ) : <EOL> try : <EOL> if isinstance ( value , ( six . integer_types , float ) ) : <EOL> value = six . text_type ( value ) <EOL> utils . check_string_length ( value , '<STR_LIT>' , <EOL> max_length = <NUM_LIT:255> ) <EOL> except exception . InvalidInput as error : <EOL> raise webob . exc . HTTPBadRequest ( <EOL> explanation = error . format_message ( ) ) <EOL> @ extensions . expected_errors ( <NUM_LIT> ) <EOL> def index ( self , req , flavor_id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context , action = '<STR_LIT:index>' ) <EOL> return self . _get_extra_specs ( context , flavor_id ) <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ validation . schema ( flavors_extraspecs . create ) <EOL> def create ( self , req , flavor_id , body ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context , action = '<STR_LIT>' ) <EOL> specs = body [ '<STR_LIT>' ] <EOL> self . _check_extra_specs_value ( specs ) <EOL> flavor = common . get_flavor ( context , flavor_id ) <EOL> try : <EOL> flavor . extra_specs = dict ( flavor . extra_specs , ** specs ) <EOL> flavor . save ( ) <EOL> except exception . FlavorExtraSpecUpdateCreateFailed as e : <EOL> raise webob . exc . HTTPConflict ( explanation = e . format_message ( ) ) <EOL> except exception . FlavorNotFound as e : <EOL> raise webob . exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> return body <EOL> @ extensions . expected_errors ( ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> @ validation . schema ( flavors_extraspecs . update ) <EOL> def update ( self , req , flavor_id , id , body ) : <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context , action = '<STR_LIT>' ) <EOL> self . _check_extra_specs_value ( body ) <EOL> if id not in body : <EOL> expl = _ ( '<STR_LIT>' ) <EOL> raise webob . exc . HTTPBadRequest ( explanation = expl ) <EOL> flavor = common . get_flavor ( context , flavor_id ) <EOL> try : <EOL> flavor . extra_specs = dict ( flavor . extra_specs , ** body ) <EOL> flavor . save ( ) <EOL> except exception . FlavorExtraSpecUpdateCreateFailed as e : <EOL> raise webob . exc . HTTPConflict ( explanation = e . format_message ( ) ) <EOL> except exception . FlavorNotFound as e : <EOL> raise webob . exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> return body <EOL> @ extensions . expected_errors ( <NUM_LIT> ) <EOL> def show ( self , req , flavor_id , id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context , action = '<STR_LIT>' ) <EOL> flavor = common . get_flavor ( context , flavor_id ) <EOL> try : <EOL> return { id : flavor . extra_specs [ id ] } <EOL> except KeyError : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % dict ( flavor_id = flavor_id , <EOL> key = id ) <EOL> raise webob . exc . HTTPNotFound ( explanation = msg ) <EOL> @ extensions . expected_errors ( <NUM_LIT> ) <EOL> def delete ( self , req , flavor_id , id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> authorize ( context , action = '<STR_LIT>' ) <EOL> flavor = common . get_flavor ( context , flavor_id ) <EOL> try : <EOL> del flavor . extra_specs [ id ] <EOL> flavor . save ( ) <EOL> except ( exception . FlavorExtraSpecsNotFound , <EOL> exception . FlavorNotFound ) as e : <EOL> raise webob . exc . HTTPNotFound ( explanation = e . format_message ( ) ) <EOL> except KeyError : <EOL> msg = _ ( "<STR_LIT>" <EOL> "<STR_LIT>" ) % dict ( flavor_id = flavor_id , <EOL> key = id ) <EOL> raise webob . exc . HTTPNotFound ( explanation = msg ) <EOL> class FlavorsExtraSpecs ( extensions . V21APIExtensionBase ) : <EOL> """<STR_LIT>""" <EOL> name = '<STR_LIT>' <EOL> alias = ALIAS <EOL> version = <NUM_LIT:1> <EOL> def get_resources ( self ) : <EOL> extra_specs = extensions . ResourceExtension ( <EOL> '<STR_LIT>' , <EOL> FlavorExtraSpecsController ( ) , <EOL> parent = dict ( member_name = '<STR_LIT>' , collection_name = '<STR_LIT>' ) ) <EOL> return [ extra_specs ] <EOL> def get_controller_extensions ( self ) : <EOL> return [ ] </s>
