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 }, } @staticmethod def convert(): root = Root() root.name = "OpenStack Zun API" root.description = ("Zun is an OpenStack project which aims to " "provide container management.") root.versions = [Version.convert('v1')] root.default_version = Version.convert('v1') return root
class ImageCollection(collection.Collection): """API representation of a collection of images.""" fields = { 'images': { 'validate': types.List(types.Custom(Image)).validate, }, } """A list containing images objects""" def __init__(self, **kwargs): self._type = 'images' @staticmethod def convert_with_links(rpc_images, limit, url=None, expand=False, **kwargs): collection = ImageCollection() # TODO(sbiswas7): This is the ugly part of the deal. # We need to convert this p thing below as dict for now # Removal of dict-compat lead to this change. collection.images = [ Image.convert_with_links(p.as_dict(), expand) for p in rpc_images ] collection.next = collection.get_next(limit, url=url, **kwargs) return collection @classmethod def sample(cls): sample = cls() sample.images = [Image.sample(expand=False)] return sample
class ZunServiceCollection(collection.Collection): fields = { 'services': { 'validate': types.List(types.Custom(ZunService)).validate, }, } def __init__(self, **kwargs): super(ZunServiceCollection, self).__init__() self._type = 'services' @staticmethod def convert_db_rec_list_to_collection(servicegroup_api, rpc_hsvcs, **kwargs): collection = ZunServiceCollection() collection.services = [] for p in rpc_hsvcs: alive = servicegroup_api.service_is_up(p) state = 'up' if alive else 'down' hsvc = ZunService(state, **p.as_dict()) collection.services.append(hsvc) next = collection.get_next(limit=None, url=None, **kwargs) if next is not None: collection.next = next return collection
def test_list_with_text_type(self): list_type = types.List(types.Text) self.assertIsNone(list_type.validate(None)) value = list_type.validate(['test1', 'test2']) self.assertEqual(['test1', 'test2'], value) self.assertRaises(exception.InvalidValue, list_type.validate, 'invalid_value')
def test_list_with_custom_type(self): class TestAPI(base.APIBase): fields = { 'test': { 'validate': lambda v: v }, } list_type = types.List(types.Custom(TestAPI)) self.assertIsNone(list_type.validate(None)) value = [{'test': 'test_value'}] value = list_type.validate(value) self.assertIsInstance(value, list) self.assertIsInstance(value[0], TestAPI) self.assertEqual({'test': 'test_value'}, value[0].as_dict())
class Version(base.APIBase): """An API version representation.""" fields = { 'id': { 'validate': types.Text.validate }, 'links': { 'validate': types.List(types.Custom(link.Link)).validate }, } @staticmethod def convert(id): version = Version() version.id = id version.links = [ link.Link.make_link('self', pecan.request.host_url, id, '', bookmark=True) ] return version
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 }, } @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.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) ] return v1