Skip to content

Commit

Permalink
Merge "Add next links to images requests"
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Sep 26, 2011
2 parents 1c8957a + 1af4c1e commit 0acc924
Show file tree
Hide file tree
Showing 4 changed files with 327 additions and 17 deletions.
29 changes: 18 additions & 11 deletions nova/api/openstack/images.py
Expand Up @@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations
# under the License.

import urlparse
import os.path

from lxml import etree
Expand Down Expand Up @@ -149,8 +148,7 @@ def index(self, req):
filters = self._get_filters(req)
images = self._image_service.index(context, filters=filters)
images = common.limited(images, req)
builder = self.get_builder(req).build
return dict(images=[builder(image, detail=False) for image in images])
return self.get_builder(req).build_list(images)

def detail(self, req):
"""Return a detailed index listing of images available to the request.
Expand Down Expand Up @@ -183,11 +181,14 @@ def index(self, req):
"""
context = req.environ['nova.context']
filters = self._get_filters(req)
params = req.GET.copy()
page_params = common.get_pagination_params(req)
for key, val in page_params.iteritems():
params[key] = val

images = self._image_service.index(context, filters=filters,
**page_params)
builder = self.get_builder(req).build
return dict(images=[builder(image, detail=False) for image in images])
return self.get_builder(req).build_list(images, **params)

def detail(self, req):
"""Return a detailed index listing of images available to the request.
Expand All @@ -197,11 +198,14 @@ def detail(self, req):
"""
context = req.environ['nova.context']
filters = self._get_filters(req)
params = req.GET.copy()
page_params = common.get_pagination_params(req)
for key, val in page_params.iteritems():
params[key] = val
images = self._image_service.detail(context, filters=filters,
**page_params)
builder = self.get_builder(req).build
return dict(images=[builder(image, detail=True) for image in images])

return self.get_builder(req).build_list(images, detail=True, **params)

def create(self, *args, **kwargs):
raise webob.exc.HTTPMethodNotAllowed()
Expand Down Expand Up @@ -253,20 +257,23 @@ def _populate_image(self, image_elem, image_dict, detailed=False):
image_dict.get('metadata', {}))
image_elem.append(meta_elem)

for link in image_dict.get('links', []):
elem = etree.SubElement(image_elem,
'{%s}link' % xmlutil.XMLNS_ATOM)
self._populate_links(image_elem, image_dict.get('links', []))

def _populate_links(self, parent, links):
for link in links:
elem = etree.SubElement(parent, '{%s}link' % xmlutil.XMLNS_ATOM)
elem.set('rel', link['rel'])
if 'type' in link:
elem.set('type', link['type'])
elem.set('href', link['href'])
return image_elem

def index(self, images_dict):
images = etree.Element('images', nsmap=self.NSMAP)
for image_dict in images_dict['images']:
image = etree.SubElement(images, 'image')
self._populate_image(image, image_dict, False)

self._populate_links(images, images_dict.get('images_links', []))
return self._to_xml(images)

def detail(self, images_dict):
Expand Down
3 changes: 3 additions & 0 deletions nova/api/openstack/schemas/v1.1/images_index.rng
Expand Up @@ -9,4 +9,7 @@
</zeroOrMore>
</element>
</zeroOrMore>
<zeroOrMore>
<externalRef href="../atom-link.rng"/>
</zeroOrMore>
</element>
36 changes: 36 additions & 0 deletions nova/api/openstack/views/images.py
Expand Up @@ -59,6 +59,15 @@ def generate_href(self, image_id):
"""Return an href string pointing to this object."""
return os.path.join(self.base_url, "images", str(image_id))

def build_list(self, image_objs, detail=False, **kwargs):
"""Return a standardized image list structure for display."""
images = []
for image_obj in image_objs:
image = self.build(image_obj, detail=detail)
images.append(image)

return dict(images=images)

def build(self, image_obj, detail=False):
"""Return a standardized image structure for display by the API."""
self._format_dates(image_obj)
Expand Down Expand Up @@ -135,6 +144,33 @@ def generate_href(self, image_id):
return os.path.join(self.base_url, self.project_id,
"images", str(image_id))

def generate_next_link(self, image_id, params):
""" Return an href string with proper limit and marker params"""
params['marker'] = image_id
return "%s?%s" % (
os.path.join(self.base_url, self.project_id, "images"),
common.dict_to_query_str(params))

def build_list(self, image_objs, detail=False, **kwargs):
"""Return a standardized image list structure for display."""
limit = kwargs.get('limit', None)
images = []
images_links = []

for image_obj in image_objs:
image = self.build(image_obj, detail=detail)
images.append(image)

if (len(images) and limit) and (limit == len(images)):
next_link = self.generate_next_link(images[-1]["id"], kwargs)
images_links = [dict(rel="next", href=next_link)]

reval = dict(images=images)
if len(images_links) > 0:
reval['images_links'] = images_links

return reval

def build(self, image_obj, detail=False):
"""Return a standardized image structure for display by the API."""
image = ViewBuilder.build(self, image_obj, detail)
Expand Down

0 comments on commit 0acc924

Please sign in to comment.