From 90407d9e473014c24eeab294192f9d3208f58ea7 Mon Sep 17 00:00:00 2001 From: Alexander Tivelkov Date: Fri, 27 Mar 2015 17:53:46 +0300 Subject: [PATCH] Expose 'is_base' schema property attribute Changeset I49255255 has added an 'is_base' attribute for Image Schema properties, thus allowing to differentiate between base and custom image properties, but the client hasn't make any use of it. This patch adds appropriate attribute to SchemaProperty class and a helper method which allows to validate if the given property is base or not. The added helper method (is_base_property) should not be confused with the existing is_core_property: the latter just checks if the property is known to the schema, regardless of its being base or not. Change-Id: I7c397196dad9ae5494ed2f8f3aacef3fc1ce70d8 Partial-Bug: #1323660 --- glanceclient/v2/schemas.py | 21 ++++++++++++++++++++- tests/v2/test_schemas.py | 18 ++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/glanceclient/v2/schemas.py b/glanceclient/v2/schemas.py index 5c31741e..94d1bb62 100644 --- a/glanceclient/v2/schemas.py +++ b/glanceclient/v2/schemas.py @@ -65,6 +65,7 @@ class SchemaProperty(object): def __init__(self, name, **kwargs): self.name = name self.description = kwargs.get('description') + self.is_base = kwargs.get('is_base', True) def translate_schema_properties(schema_properties): @@ -86,9 +87,27 @@ class Schema(object): self.properties = translate_schema_properties(raw_properties) def is_core_property(self, property_name): + """Checks if a property with a given name is known to the schema, + i.e. is either a base property or a custom one registered in + schema-image.json file + + :param property_name: name of the property + :returns: True if the property is known, False otherwise + """ + return self._check_property(property_name, True) + + def is_base_property(self, property_name): + """Checks if a property with a given name is a base property + + :param property_name: name of the property + :returns: True if the property is base, False otherwise + """ + return self._check_property(property_name, False) + + def _check_property(self, property_name, allow_non_base): for prop in self.properties: if property_name == prop.name: - return True + return prop.is_base or allow_non_base return False def raw(self): diff --git a/tests/v2/test_schemas.py b/tests/v2/test_schemas.py index 39625965..20d817bb 100644 --- a/tests/v2/test_schemas.py +++ b/tests/v2/test_schemas.py @@ -74,6 +74,14 @@ class TestSchemaProperty(testtools.TestCase): self.assertEqual('size', prop.name) self.assertEqual('some quantity', prop.description) + def test_property_is_base(self): + prop1 = schemas.SchemaProperty('name') + prop2 = schemas.SchemaProperty('foo', is_base=False) + prop3 = schemas.SchemaProperty('foo', is_base=True) + self.assertTrue(prop1.is_base) + self.assertFalse(prop2.is_base) + self.assertTrue(prop3.is_base) + class TestSchema(testtools.TestCase): def test_schema_minimum(self): @@ -93,6 +101,16 @@ class TestSchema(testtools.TestCase): schema = schemas.Schema(raw_schema) self.assertEqual(raw_schema, schema.raw()) + def test_property_is_base(self): + raw_schema = {'name': 'Country', + 'properties': { + 'size': {}, + 'population': {'is_base': False}}} + schema = schemas.Schema(raw_schema) + self.assertTrue(schema.is_base_property('size')) + self.assertFalse(schema.is_base_property('population')) + self.assertFalse(schema.is_base_property('foo')) + class TestController(testtools.TestCase): def setUp(self):