Skip to content

Commit

Permalink
Adds a /images/detail route to the Parallax controller, adds a unit t…
Browse files Browse the repository at this point in the history
…est for it, and cleans up Michael's suggestions.
  • Loading branch information
[email protected] committed Oct 13, 2010
1 parent bee23da commit fa66de6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
41 changes: 31 additions & 10 deletions glance/parallax/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,41 @@


class ImageController(wsgi.Controller):
"""Image Controller """

def __init__(self):
super(ImageController, self).__init__()
"""Image Controller """

def index(self, req):
"""Return data for all public, non-deleted images """
"""Return basic information for all public, non-deleted images
:param req: the Request object coming from the wsgi layer
:retval a mapping of the following form::
dict(images=[image_list])
Where image_list is a sequence of mappings::
{'id': image_id, 'name': image_name}
"""
images = db.image_get_all_public(None)
image_dicts = [self._make_image_dict(i) for i in images]
image_dicts = [dict(id=i['id'], name=i['name']) for i in images]
return dict(images=image_dicts)

def detail(self, req):
"""Detail is not currently supported """
raise exc.HTTPNotImplemented()
"""Return detailed information for all public, non-deleted images
:param req: the Request object coming from the wsgi layer
:retval a mapping of the following form::
dict(images=[image_list])
Where image_list is a sequence of mappings containing
all image model fields.
"""
images = db.image_get_all_public(None)
image_dicts = [self._make_image_dict(i) for i in images]
return dict(images=image_dicts)

def show(self, req, id):
"""Return data about the given image id."""
Expand Down Expand Up @@ -70,8 +91,7 @@ def create(self, req):
image_data = json.loads(req.body)

# Ensure the image has a status set
if 'status' not in image_data.keys():
image_data['status'] = 'available'
image_data.setdefault('status', 'available')

context = None
new_image = db.image_create(context, image_data)
Expand Down Expand Up @@ -116,6 +136,7 @@ class API(wsgi.Router):
def __init__(self):
# TODO(sirp): should we add back the middleware for parallax?
mapper = routes.Mapper()
mapper.resource("image", "images", controller=ImageController())
mapper.resource("image", "images", controller=ImageController(),
collection={'detail': 'GET'})
mapper.connect("/", controller=ImageController(), action="index")
super(API, self).__init__(mapper)
28 changes: 21 additions & 7 deletions tests/unit/test_parallax_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ def test_get_root(self):
"""
fixture = {'id': 2,
'name': 'fake image #2',
'is_public': True,
'image_type': 'kernel',
'status': 'available'
}
'name': 'fake image #2'}
req = webob.Request.blank('/')
res = req.get_response(controllers.API())
res_dict = json.loads(res.body)
Expand All @@ -59,18 +55,36 @@ def test_get_root(self):
for k,v in fixture.iteritems():
self.assertEquals(v, images[0][k])

def test_get_images(self):
def test_get_index(self):
"""Tests that the /images parallax API returns list of
public images
"""
fixture = {'id': 2,
'name': 'fake image #2'}
req = webob.Request.blank('/images')
res = req.get_response(controllers.API())
res_dict = json.loads(res.body)
self.assertEquals(res.status_int, 200)

images = res_dict['images']
self.assertEquals(len(images), 1)

for k,v in fixture.iteritems():
self.assertEquals(v, images[0][k])

def test_get_details(self):
"""Tests that the /images/detail parallax API returns
a mapping containing a list of detailed image information
"""
fixture = {'id': 2,
'name': 'fake image #2',
'is_public': True,
'image_type': 'kernel',
'status': 'available'
}
req = webob.Request.blank('/images')
req = webob.Request.blank('/images/detail')
res = req.get_response(controllers.API())
res_dict = json.loads(res.body)
self.assertEquals(res.status_int, 200)
Expand Down

0 comments on commit fa66de6

Please sign in to comment.