Change default is_public = True to just set a default filter instead of hard coding so it can be overridden

This commit is contained in:
Vishvananda Ishaya 2011-07-18 10:28:18 -07:00
parent 5929f143de
commit ee35df8b4f
3 changed files with 23 additions and 19 deletions

View File

@ -128,10 +128,10 @@ def image_get(context, image_id, session=None):
raise exception.NotFound("No image found with ID %s" % image_id)
def image_get_all_public(context, filters=None, marker=None, limit=None,
sort_key='created_at', sort_dir='desc'):
def image_get_all(context, filters=None, marker=None, limit=None,
sort_key='created_at', sort_dir='desc'):
"""
Get all public images that match zero or more filters.
Get all images that match zero or more filters.
:param filters: dict of filter keys and values. If a 'properties'
key is present, it is treated as a dict of key/value
@ -147,7 +147,6 @@ def image_get_all_public(context, filters=None, marker=None, limit=None,
query = session.query(models.Image).\
options(joinedload(models.Image.properties)).\
filter_by(deleted=_deleted(context)).\
filter_by(is_public=True).\
filter(models.Image.status != 'killed')
sort_dir_func = {

View File

@ -56,6 +56,19 @@ class Controller(object):
self.options = options
db_api.configure_db(options)
def _get_images(self, context, **params):
"""
Get images, adding is_public filter if not specified.
"""
params['filters'] = params.get('filters', {})
if not 'is_public' in params['filters']:
params['filters']['is_public'] = True
try:
return db_api.image_get_all(None, **params)
except exception.NotFound, e:
msg = "Invalid marker. Image could not be found."
raise exc.HTTPBadRequest(explanation=msg)
def index(self, req):
"""
Return a basic filtered list of public, non-deleted images
@ -77,11 +90,7 @@ class Controller(object):
}
"""
params = self._get_query_params(req)
try:
images = db_api.image_get_all_public(None, **params)
except exception.NotFound, e:
msg = "Invalid marker. Image could not be found."
raise exc.HTTPBadRequest(explanation=msg)
images = self._get_images(None, **params)
results = []
for image in images:
@ -104,12 +113,8 @@ class Controller(object):
all image model fields.
"""
params = self._get_query_params(req)
try:
images = db_api.image_get_all_public(None, **params)
except exception.NotFound, e:
msg = "Invalid marker. Image could not be found."
raise exc.HTTPBadRequest(explanation=msg)
images = self._get_images(None, **params)
image_dicts = [make_image_dict(i) for i in images]
return dict(images=image_dicts)

View File

@ -390,9 +390,9 @@ def stub_out_registry_db_image_api(stubs):
else:
return images[0]
def image_get_all_public(self, _context, filters=None, marker=None,
limit=1000, sort_key=None, sort_dir=None):
images = [f for f in self.images if f['is_public'] == True]
def image_get_all(self, _context, filters=None, marker=None,
limit=1000, sort_key=None, sort_dir=None):
images = self.images
if 'size_min' in filters:
size_min = int(filters.pop('size_min'))
@ -461,5 +461,5 @@ def stub_out_registry_db_image_api(stubs):
fake_datastore.image_destroy)
stubs.Set(glance.registry.db.api, 'image_get',
fake_datastore.image_get)
stubs.Set(glance.registry.db.api, 'image_get_all_public',
fake_datastore.image_get_all_public)
stubs.Set(glance.registry.db.api, 'image_get_all',
fake_datastore.image_get_all)