<s> """<STR_LIT>""" <EOL> from webob import exc <EOL> from nova import db <EOL> from nova import exception <EOL> from nova import quota <EOL> from nova . volume import volume_types <EOL> from nova . api . openstack import extensions <EOL> from nova . api . openstack import faults <EOL> from nova . api . openstack import wsgi <EOL> class VolumeTypesController ( object ) : <EOL> """<STR_LIT>""" <EOL> def index ( self , req ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> return volume_types . get_all_types ( context ) <EOL> def create ( self , req , body ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> if not body or body == "<STR_LIT>" : <EOL> return faults . Fault ( exc . HTTPUnprocessableEntity ( ) ) <EOL> vol_type = body . get ( '<STR_LIT>' , None ) <EOL> if vol_type is None or vol_type == "<STR_LIT>" : <EOL> return faults . Fault ( exc . HTTPUnprocessableEntity ( ) ) <EOL> name = vol_type . get ( '<STR_LIT:name>' , None ) <EOL> specs = vol_type . get ( '<STR_LIT>' , { } ) <EOL> if name is None or name == "<STR_LIT>" : <EOL> return faults . Fault ( exc . HTTPUnprocessableEntity ( ) ) <EOL> try : <EOL> volume_types . create ( context , name , specs ) <EOL> vol_type = volume_types . get_volume_type_by_name ( context , name ) <EOL> except quota . QuotaError as error : <EOL> self . _handle_quota_error ( error ) <EOL> except exception . NotFound : <EOL> return faults . Fault ( exc . HTTPNotFound ( ) ) <EOL> return { '<STR_LIT>' : vol_type } <EOL> def show ( self , req , id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> try : <EOL> vol_type = volume_types . get_volume_type ( context , id ) <EOL> except exception . NotFound or exception . ApiError : <EOL> return faults . Fault ( exc . HTTPNotFound ( ) ) <EOL> return { '<STR_LIT>' : vol_type } <EOL> def delete ( self , req , id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> try : <EOL> vol_type = volume_types . get_volume_type ( context , id ) <EOL> volume_types . destroy ( context , vol_type [ '<STR_LIT:name>' ] ) <EOL> except exception . NotFound : <EOL> return faults . Fault ( exc . HTTPNotFound ( ) ) <EOL> def _handle_quota_error ( self , error ) : <EOL> """<STR_LIT>""" <EOL> if error . code == "<STR_LIT>" : <EOL> raise exc . HTTPBadRequest ( explanation = error . message ) <EOL> raise error <EOL> class VolumeTypeExtraSpecsController ( object ) : <EOL> """<STR_LIT>""" <EOL> def _get_extra_specs ( self , context , vol_type_id ) : <EOL> extra_specs = db . api . volume_type_extra_specs_get ( context , vol_type_id ) <EOL> specs_dict = { } <EOL> for key , value in extra_specs . iteritems ( ) : <EOL> specs_dict [ key ] = value <EOL> return dict ( extra_specs = specs_dict ) <EOL> def _check_body ( self , body ) : <EOL> if body == None or body == "<STR_LIT>" : <EOL> expl = _ ( '<STR_LIT>' ) <EOL> raise exc . HTTPBadRequest ( explanation = expl ) <EOL> def index ( self , req , vol_type_id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> return self . _get_extra_specs ( context , vol_type_id ) <EOL> def create ( self , req , vol_type_id , body ) : <EOL> self . _check_body ( body ) <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> specs = body . get ( '<STR_LIT>' ) <EOL> try : <EOL> db . api . volume_type_extra_specs_update_or_create ( context , <EOL> vol_type_id , <EOL> specs ) <EOL> except quota . QuotaError as error : <EOL> self . _handle_quota_error ( error ) <EOL> return body <EOL> def update ( self , req , vol_type_id , id , body ) : <EOL> self . _check_body ( body ) <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> if not id in body : <EOL> expl = _ ( '<STR_LIT>' ) <EOL> raise exc . HTTPBadRequest ( explanation = expl ) <EOL> if len ( body ) > <NUM_LIT:1> : <EOL> expl = _ ( '<STR_LIT>' ) <EOL> raise exc . HTTPBadRequest ( explanation = expl ) <EOL> try : <EOL> db . api . volume_type_extra_specs_update_or_create ( context , <EOL> vol_type_id , <EOL> body ) <EOL> except quota . QuotaError as error : <EOL> self . _handle_quota_error ( error ) <EOL> return body <EOL> def show ( self , req , vol_type_id , id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> specs = self . _get_extra_specs ( context , vol_type_id ) <EOL> if id in specs [ '<STR_LIT>' ] : <EOL> return { id : specs [ '<STR_LIT>' ] [ id ] } <EOL> else : <EOL> return faults . Fault ( exc . HTTPNotFound ( ) ) <EOL> def delete ( self , req , vol_type_id , id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> db . api . volume_type_extra_specs_delete ( context , vol_type_id , id ) <EOL> def _handle_quota_error ( self , error ) : <EOL> """<STR_LIT>""" <EOL> if error . code == "<STR_LIT>" : <EOL> raise exc . HTTPBadRequest ( explanation = error . message ) <EOL> raise error <EOL> class Volumetypes ( extensions . ExtensionDescriptor ) : <EOL> def get_name ( self ) : <EOL> return "<STR_LIT>" <EOL> def get_alias ( self ) : <EOL> return "<STR_LIT>" <EOL> def get_description ( self ) : <EOL> return "<STR_LIT>" <EOL> def get_namespace ( self ) : <EOL> return "<STR_LIT>" <EOL> def get_updated ( self ) : <EOL> return "<STR_LIT>" <EOL> def get_resources ( self ) : <EOL> resources = [ ] <EOL> res = extensions . ResourceExtension ( <EOL> '<STR_LIT>' , <EOL> VolumeTypesController ( ) ) <EOL> resources . append ( res ) <EOL> res = extensions . ResourceExtension ( '<STR_LIT>' , <EOL> VolumeTypeExtraSpecsController ( ) , <EOL> parent = dict ( <EOL> member_name = '<STR_LIT>' , <EOL> collection_name = '<STR_LIT>' ) ) <EOL> resources . append ( res ) <EOL> return resources </s>
