diff options
author | Jenkins <jenkins@review.openstack.org> | 2015-03-16 16:24:16 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2015-03-16 16:24:16 +0000 |
commit | 7c54b14cf042d5febfbd1d835af9052fa2231e8a (patch) | |
tree | eeb3a0dfa1891fc2be61a9b1c251398d0ee8e8ba | |
parent | 976fe1776b40032b1e6533bd4468041f672e5be1 (diff) | |
parent | f6f573316cfc2dd87ff7224590087e7f3be01b2e (diff) | |
download | python-glanceclient-7c54b14cf042d5febfbd1d835af9052fa2231e8a.tar.gz |
Merge "Apply expected patch format when updating tags in v2.images"
-rw-r--r-- | glanceclient/v2/schemas.py | 22 | ||||
-rw-r--r-- | tests/v2/test_images.py | 21 | ||||
-rw-r--r-- | tests/v2/test_schemas.py | 20 |
3 files changed, 57 insertions, 6 deletions
diff --git a/glanceclient/v2/schemas.py b/glanceclient/v2/schemas.py index 0dd2869..5c31741 100644 --- a/glanceclient/v2/schemas.py +++ b/glanceclient/v2/schemas.py @@ -14,6 +14,7 @@ # under the License. import copy +import json import jsonpatch import six import warlock.model as warlock @@ -29,18 +30,35 @@ class SchemaBasedModel(warlock.Model): expects. """ + def _make_custom_patch(self, new, original): + if not self.get('tags'): + tags_patch = [] + else: + tags_patch = [{"path": "/tags", + "value": self.get('tags'), + "op": "replace"}] + + patch_string = jsonpatch.make_patch(original, new).to_string() + patch = json.loads(patch_string) + if not patch: + return json.dumps(tags_patch) + else: + return json.dumps(patch + tags_patch) + @warlock.Model.patch.getter def patch(self): """Return a jsonpatch object representing the delta.""" original = copy.deepcopy(self.__dict__['__original__']) new = dict(self) - if self.__dict__['schema']: + if self.schema: for (name, prop) in six.iteritems(self.schema['properties']): if (name not in original and name in new and prop.get('is_base', True)): original[name] = None - return jsonpatch.make_patch(original, dict(self)).to_string() + original['tags'] = None + new['tags'] = None + return self._make_custom_patch(new, original) class SchemaProperty(object): diff --git a/tests/v2/test_images.py b/tests/v2/test_images.py index da3c0b1..f4abec4 100644 --- a/tests/v2/test_images.py +++ b/tests/v2/test_images.py @@ -58,7 +58,7 @@ data_fixtures = { }, 'color': {'type': 'string', 'is_base': False}, }, - 'additionalProperties': {'type': 'string'} + 'additionalProperties': {'type': 'string'}, }, ), }, @@ -418,8 +418,9 @@ schema_fixtures = { } }, 'color': {'type': 'string', 'is_base': False}, + 'tags': {'type': 'array'}, }, - 'additionalProperties': {'type': 'string'} + 'additionalProperties': {'type': 'string'}, } ) } @@ -889,6 +890,22 @@ class TestController(testtools.TestCase): self._empty_get(image_id) ]) + def test_update_tags(self): + image_id = 'a2b83adc-888e-11e3-8872-78acc0b951d8' + tag_map = {'tags': ['tag01', 'tag02', 'tag03']} + + image = self.controller.update(image_id, **tag_map) + + expected_body = [{'path': '/tags', 'op': 'replace', + 'value': tag_map['tags']}] + expected = [ + self._empty_get(image_id), + self._patch_req(image_id, expected_body), + self._empty_get(image_id) + ] + self.assertEqual(expected, self.api.calls) + self.assertEqual(image_id, image.id) + def test_update_missing_location(self): image_id = 'a2b83adc-888e-11e3-8872-78acc0b951d8' new_loc = {'url': 'http://spam.com/', 'metadata': {'spam': 'ham'}} diff --git a/tests/v2/test_schemas.py b/tests/v2/test_schemas.py index 2ea6bdb..3962596 100644 --- a/tests/v2/test_schemas.py +++ b/tests/v2/test_schemas.py @@ -37,8 +37,11 @@ fixtures = { { 'name': 'image', 'properties': { - 'name': {'type': 'string', 'description': 'Name of image'}, + 'name': {'type': 'string', + 'description': 'Name of image'}, + 'tags': {'type': 'array'} }, + }, ), }, @@ -51,6 +54,7 @@ _SCHEMA = schemas.Schema({ 'name': {'type': 'string'}, 'color': {'type': 'string'}, 'shape': {'type': 'string', 'is_base': False}, + 'tags': {'type': 'array'} }, }) @@ -99,7 +103,8 @@ class TestController(testtools.TestCase): def test_get_schema(self): schema = self.controller.get('image') self.assertEqual('image', schema.name) - self.assertEqual(['name'], [p.name for p in schema.properties]) + self.assertEqual(['name', 'tags'], + [p.name for p in schema.properties]) class TestSchemaBasedModel(testtools.TestCase): @@ -195,3 +200,14 @@ class TestSchemaBasedModel(testtools.TestCase): patch = original.patch expected = '[{"path": "/shape", "value": "square", "op": "replace"}]' self.assertTrue(compare_json_patches(patch, expected)) + + def test_patch_should_replace_tags(self): + obj = {'name': 'fred', } + + original = self.model(obj) + original['tags'] = ['tag1', 'tag2'] + + patch = original.patch + expected = '[{"path": "/tags", "value": ["tag1", "tag2"], ' \ + '"op": "replace"}]' + self.assertTrue(compare_json_patches(patch, expected)) |