<s> import webob . exc <EOL> from nova . api . openstack import common <EOL> from nova . api . openstack . compute . views import images as views_images <EOL> from nova . api . openstack import wsgi <EOL> from nova import exception <EOL> from nova . i18n import _ <EOL> import nova . image <EOL> import nova . utils <EOL> SUPPORTED_FILTERS = { <EOL> '<STR_LIT:name>' : '<STR_LIT:name>' , <EOL> '<STR_LIT:status>' : '<STR_LIT:status>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:type>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class Controller ( wsgi . Controller ) : <EOL> """<STR_LIT>""" <EOL> _view_builder_class = views_images . ViewBuilder <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( Controller , self ) . __init__ ( ** kwargs ) <EOL> self . _image_api = nova . image . API ( ) <EOL> def _get_filters ( self , req ) : <EOL> """<STR_LIT>""" <EOL> filters = { } <EOL> for param in req . params : <EOL> if param in SUPPORTED_FILTERS or param . startswith ( '<STR_LIT>' ) : <EOL> filter_name = SUPPORTED_FILTERS . get ( param , param ) <EOL> filters [ filter_name ] = req . params . get ( param ) <EOL> filter_name = '<STR_LIT>' <EOL> try : <EOL> filters [ filter_name ] = filters [ filter_name ] . rsplit ( '<STR_LIT:/>' , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> except ( AttributeError , IndexError , KeyError ) : <EOL> pass <EOL> filter_name = '<STR_LIT:status>' <EOL> if filter_name in filters : <EOL> filters [ filter_name ] = filters [ filter_name ] . lower ( ) <EOL> return filters <EOL> def show ( self , req , id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> try : <EOL> image = self . _image_api . get ( context , id ) <EOL> except ( exception . NotFound , exception . InvalidImageRef ) : <EOL> explanation = _ ( "<STR_LIT>" ) <EOL> raise webob . exc . HTTPNotFound ( explanation = explanation ) <EOL> req . cache_db_items ( '<STR_LIT>' , [ image ] , '<STR_LIT:id>' ) <EOL> return self . _view_builder . show ( req , image ) <EOL> def delete ( self , req , id ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> try : <EOL> self . _image_api . delete ( context , id ) <EOL> except exception . ImageNotFound : <EOL> explanation = _ ( "<STR_LIT>" ) <EOL> raise webob . exc . HTTPNotFound ( explanation = explanation ) <EOL> except exception . ImageNotAuthorized : <EOL> explanation = _ ( "<STR_LIT>" ) <EOL> raise webob . exc . HTTPForbidden ( explanation = explanation ) <EOL> return webob . exc . HTTPNoContent ( ) <EOL> def index ( self , req ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> filters = self . _get_filters ( req ) <EOL> page_params = common . get_pagination_params ( req ) <EOL> try : <EOL> images = self . _image_api . get_all ( context , filters = filters , <EOL> ** page_params ) <EOL> except exception . Invalid as e : <EOL> raise webob . exc . HTTPBadRequest ( explanation = e . format_message ( ) ) <EOL> return self . _view_builder . index ( req , images ) <EOL> def detail ( self , req ) : <EOL> """<STR_LIT>""" <EOL> context = req . environ [ '<STR_LIT>' ] <EOL> filters = self . _get_filters ( req ) <EOL> page_params = common . get_pagination_params ( req ) <EOL> try : <EOL> images = self . _image_api . get_all ( context , filters = filters , <EOL> ** page_params ) <EOL> except exception . Invalid as e : <EOL> raise webob . exc . HTTPBadRequest ( explanation = e . format_message ( ) ) <EOL> req . cache_db_items ( '<STR_LIT>' , images , '<STR_LIT:id>' ) <EOL> return self . _view_builder . detail ( req , images ) <EOL> def create ( self , * args , ** kwargs ) : <EOL> raise webob . exc . HTTPMethodNotAllowed ( ) <EOL> def create_resource ( ) : <EOL> return wsgi . Resource ( Controller ( ) ) </s>
