diff --git a/zun/api/controllers/link.py b/zun/api/controllers/link.py index b7fc8d72c..9d36bd0f5 100644 --- a/zun/api/controllers/link.py +++ b/zun/api/controllers/link.py @@ -15,9 +15,6 @@ import pecan -from zun.api.controllers import base -from zun.api.controllers import types - def build_url(resource, resource_args, bookmark=False, base_url=None): if base_url is None: @@ -31,34 +28,11 @@ def build_url(resource, resource_args, bookmark=False, base_url=None): return template % {'url': base_url, 'res': resource, 'args': resource_args} -class Link(base.APIBase): - """A link representation.""" - - fields = { - 'href': { - 'validate': types.Text.validate - }, - 'rel': { - 'validate': types.Text.validate - }, - 'type': { - 'validate': types.Text.validate - }, - } - - @staticmethod - def make_link(rel_name, url, resource, resource_args, - bookmark=False, type=None): - href = build_url(resource, resource_args, - bookmark=bookmark, base_url=url) - if type is None: - return Link(href=href, rel=rel_name) - else: - return Link(href=href, rel=rel_name, type=type) - - @classmethod - def sample(cls): - sample = cls(href="http://localhost:8080/containers/" - "eaaca217-e7d8-47b4-bb41-3f99f20eed89", - rel="bookmark") - return sample +def make_link(rel_name, url, resource, resource_args, + bookmark=False, type=None): + href = build_url(resource, resource_args, + bookmark=bookmark, base_url=url) + if type is None: + return {'href': href, 'rel': rel_name} + else: + return {'href': href, 'rel': rel_name, 'type': type} diff --git a/zun/api/controllers/root.py b/zun/api/controllers/root.py index 7cf29a2ca..6deeee7fa 100644 --- a/zun/api/controllers/root.py +++ b/zun/api/controllers/root.py @@ -15,47 +15,34 @@ from pecan import rest from zun.api.controllers import base from zun.api.controllers import link -from zun.api.controllers import types from zun.api.controllers import v1 class Version(base.APIBase): """An API version representation.""" - fields = { - 'id': { - 'validate': types.Text.validate - }, - 'links': { - 'validate': types.List(types.Custom(link.Link)).validate - }, - } + fields = ( + 'id', + 'links', + ) @staticmethod def convert(id): version = Version() version.id = id - version.links = [link.Link.make_link('self', pecan.request.host_url, - id, '', bookmark=True)] + version.links = [link.make_link('self', pecan.request.host_url, + id, '', bookmark=True)] return version class Root(base.APIBase): - fields = { - 'id': { - 'validate': types.Text.validate - }, - 'description': { - 'validate': types.Text.validate - }, - 'versions': { - 'validate': types.List(types.Custom(Version)).validate - }, - 'default_version': { - 'validate': types.Custom(Version).validate - }, - } + fields = ( + 'id', + 'description', + 'versions', + 'default_version', + ) @staticmethod def convert(): diff --git a/zun/api/controllers/v1/__init__.py b/zun/api/controllers/v1/__init__.py index 5b0848b6f..31271bebb 100644 --- a/zun/api/controllers/v1/__init__.py +++ b/zun/api/controllers/v1/__init__.py @@ -24,7 +24,6 @@ from pecan import rest from zun.api.controllers import base as controllers_base from zun.api.controllers import link -from zun.api.controllers import types from zun.api.controllers.v1 import containers as container_controller from zun.api.controllers.v1 import images as image_controller from zun.api.controllers.v1 import zun_services @@ -35,71 +34,55 @@ LOG = logging.getLogger(__name__) class MediaType(controllers_base.APIBase): """A media type representation.""" - fields = { - 'base': { - 'validate': types.Text.validate - }, - 'type': { - 'validate': types.Text.validate - }, - } + fields = ( + 'base', + 'type', + ) class V1(controllers_base.APIBase): """The representation of the version 1 of the API.""" - fields = { - 'id': { - 'validate': types.Text.validate - }, - 'media_types': { - 'validate': types.List(types.Custom(MediaType)).validate - }, - 'links': { - 'validate': types.List(types.Custom(link.Link)).validate - }, - 'services': { - 'validate': types.List(types.Custom(link.Link)).validate - }, - 'containers': { - 'validate': types.List(types.Custom(link.Link)).validate - }, - 'images': { - 'validate': types.List(types.Custom(link.Link)).validate - }, - } + fields = ( + 'id', + 'media_types', + 'links', + 'services', + 'containers', + 'images' + ) @staticmethod def convert(): v1 = V1() v1.id = "v1" - v1.links = [link.Link.make_link('self', pecan.request.host_url, - 'v1', '', bookmark=True), - link.Link.make_link('describedby', - 'http://docs.openstack.org', - 'developer/zun/dev', - 'api-spec-v1.html', - bookmark=True, type='text/html')] + v1.links = [link.make_link('self', pecan.request.host_url, + 'v1', '', bookmark=True), + link.make_link('describedby', + 'http://docs.openstack.org', + 'developer/zun/dev', + 'api-spec-v1.html', + bookmark=True, type='text/html')] v1.media_types = [MediaType(base='application/json', type='application/vnd.openstack.zun.v1+json')] - v1.services = [link.Link.make_link('self', pecan.request.host_url, - 'services', ''), - link.Link.make_link('bookmark', - pecan.request.host_url, - 'services', '', - bookmark=True)] - v1.containers = [link.Link.make_link('self', pecan.request.host_url, - 'containers', ''), - link.Link.make_link('bookmark', - pecan.request.host_url, - 'containers', '', - bookmark=True)] - v1.images = [link.Link.make_link('self', pecan.request.host_url, - 'images', ''), - link.Link.make_link('bookmark', - pecan.request.host_url, - 'images', '', - bookmark=True)] + v1.services = [link.make_link('self', pecan.request.host_url, + 'services', ''), + link.make_link('bookmark', + pecan.request.host_url, + 'services', '', + bookmark=True)] + v1.containers = [link.make_link('self', pecan.request.host_url, + 'containers', ''), + link.make_link('bookmark', + pecan.request.host_url, + 'containers', '', + bookmark=True)] + v1.images = [link.make_link('self', pecan.request.host_url, + 'images', ''), + link.make_link('bookmark', + pecan.request.host_url, + 'images', '', + bookmark=True)] return v1 diff --git a/zun/api/controllers/v1/collection.py b/zun/api/controllers/v1/collection.py index da2487696..eb0426794 100644 --- a/zun/api/controllers/v1/collection.py +++ b/zun/api/controllers/v1/collection.py @@ -44,5 +44,5 @@ class Collection(base.APIBase): 'args': q_args, 'limit': limit, 'marker': self.collection[-1].uuid} - return link.Link.make_link('next', pecan.request.host_url, - resource_url, next_args).href + return link.make_link('next', pecan.request.host_url, + resource_url, next_args).href diff --git a/zun/api/controllers/v1/views/containers_view.py b/zun/api/controllers/v1/views/containers_view.py index ec168c471..9a7377f91 100644 --- a/zun/api/controllers/v1/views/containers_view.py +++ b/zun/api/controllers/v1/views/containers_view.py @@ -47,9 +47,9 @@ def format_container(url, container): return if key == 'uuid': yield ('uuid', value) - yield ('links', [link.Link.make_link( + yield ('links', [link.make_link( 'self', url, 'containers', value), - link.Link.make_link( + link.make_link( 'bookmark', url, 'containers', value, bookmark=True)]) diff --git a/zun/api/controllers/v1/views/images_view.py b/zun/api/controllers/v1/views/images_view.py index be23e727f..c9791782d 100644 --- a/zun/api/controllers/v1/views/images_view.py +++ b/zun/api/controllers/v1/views/images_view.py @@ -30,9 +30,9 @@ def format_image(url, image): return if key == 'uuid': yield ('uuid', value) - yield ('links', [link.Link.make_link( + yield ('links', [link.make_link( 'self', url, 'images', value), - link.Link.make_link( + link.make_link( 'bookmark', url, 'images', value, bookmark=True)]) diff --git a/zun/tests/unit/api/controllers/test_link.py b/zun/tests/unit/api/controllers/test_link.py index 9aa0c0b21..12e917456 100644 --- a/zun/tests/unit/api/controllers/test_link.py +++ b/zun/tests/unit/api/controllers/test_link.py @@ -20,11 +20,11 @@ from zun.tests import base as test_base class TestLink(test_base.BaseTestCase): def test_make_link(self): - link = link_module.Link.make_link( + link = link_module.make_link( 'self', 'http://localhost:8080', 'v1', '', bookmark=True) - ordered_link = collections.OrderedDict(sorted(link.as_dict().items())) + ordered_link = collections.OrderedDict(sorted(link.items())) expected_value = collections.OrderedDict([ ('href', 'http://localhost:8080/v1/'), ('rel', 'self')