Add link descriptor objects to schemas

Part of blueprint api-v2-links

Change-Id: I1e03b38c51931a380c197def88501c4dd483273c
This commit is contained in:
Mark J. Washenberger 2012-06-14 16:45:24 -04:00 committed by Mark Washenberger
parent c9599a6755
commit 202439f409
2 changed files with 86 additions and 2 deletions

View File

@ -20,11 +20,12 @@ from glance.common import exception
class Schema(object):
def __init__(self, name, properties=None):
def __init__(self, name, properties=None, links=None):
self.name = name
if properties is None:
properties = {}
self.properties = properties
self.links = links
def validate(self, obj):
try:
@ -55,11 +56,14 @@ class Schema(object):
self.properties.update(properties)
def raw(self):
return {
raw = {
'name': self.name,
'properties': self.properties,
'additionalProperties': False,
}
if self.links:
raw['links'] = self.links
return raw
class PermissiveSchema(Schema):
@ -70,3 +74,29 @@ class PermissiveSchema(Schema):
raw = super(PermissiveSchema, self).raw()
raw['additionalProperties'] = {'type': 'string'}
return raw
class CollectionSchema(object):
def __init__(self, name, item_schema):
self.name = name
self.item_schema = item_schema
def raw(self):
return {
'name': self.name,
'properties': {
self.name: {
'type': 'array',
'items': self.item_schema.raw(),
},
'first': {'type': 'string'},
'next': {'type': 'string'},
'schema': {'type': 'string'},
},
'links': [
{'rel': 'first', 'href': '{first}'},
{'rel': 'next', 'href': '{next}'},
{'rel': 'describedby', 'href': '{schema}'},
],
}

View File

@ -76,6 +76,34 @@ class TestBasicSchema(test_utils.BaseTestCase):
self.assertEqual(self.schema.raw(), expected)
class TestBasicSchemaLinks(test_utils.BaseTestCase):
def setUp(self):
super(TestBasicSchemaLinks, self).setUp()
properties = {
'ham': {'type': 'string'},
'eggs': {'type': 'string'},
}
links = [
{'rel': 'up', 'href': '/menu'},
]
self.schema = glance.schema.Schema('basic', properties, links)
def test_raw_json_schema(self):
expected = {
'name': 'basic',
'properties': {
'ham': {'type': 'string'},
'eggs': {'type': 'string'},
},
'links': [
{'rel': 'up', 'href': '/menu'},
],
'additionalProperties': False,
}
self.assertEqual(self.schema.raw(), expected)
class TestPermissiveSchema(test_utils.BaseTestCase):
def setUp(self):
@ -109,3 +137,29 @@ class TestPermissiveSchema(test_utils.BaseTestCase):
'additionalProperties': {'type': 'string'},
}
self.assertEqual(self.schema.raw(), expected)
class TestCollectionSchema(test_utils.BaseTestCase):
def test_raw_json_schema(self):
item_properties = {'cheese': {'type': 'string'}}
item_schema = glance.schema.Schema('mouse', item_properties)
collection_schema = glance.schema.CollectionSchema('mice', item_schema)
expected = {
'name': 'mice',
'properties': {
'mice': {
'type': 'array',
'items': item_schema.raw(),
},
'first': {'type': 'string'},
'next': {'type': 'string'},
'schema': {'type': 'string'},
},
'links': [
{'rel': 'first', 'href': '{first}'},
{'rel': 'next', 'href': '{next}'},
{'rel': 'describedby', 'href': '{schema}'},
],
}
self.assertEqual(collection_schema.raw(), expected)