From e77322c17931810f4029ef339a791f702f2f4580 Mon Sep 17 00:00:00 2001 From: Mike Fedosin Date: Wed, 1 Jun 2016 21:02:29 +0300 Subject: [PATCH] Don't update tags every time This code removes unnecessary PATCH requests for tags updating if they were not modified. Change-Id: I8eced32f39d0c98e0b26014e7b2341ab6580ff01 Closes-bug: 1587999 --- glanceclient/tests/unit/v2/test_images.py | 31 +++++++++++++++++++++++ glanceclient/v2/schemas.py | 9 +++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/glanceclient/tests/unit/v2/test_images.py b/glanceclient/tests/unit/v2/test_images.py index 73bb074c..c7baf943 100644 --- a/glanceclient/tests/unit/v2/test_images.py +++ b/glanceclient/tests/unit/v2/test_images.py @@ -381,6 +381,20 @@ data_fixtures = { '', ) }, + '/v2/images/a2b83adc-888e-11e3-8872-78acc0b951d9': { + 'GET': ( + {}, + { + 'id': 'a2b83adc-888e-11e3-8872-78acc0b951d9', + 'name': 'image-1', + 'tags': ['tag1', 'tag2'], + }, + ), + 'PATCH': ( + {}, + '', + ) + }, '/v2/images?limit=%d&os_distro=NixOS' % images.DEFAULT_PAGE_SIZE: { 'GET': ( {}, @@ -982,6 +996,23 @@ class TestController(testtools.TestCase): # will not actually change - yet in real life it will... self.assertEqual('image-3', image.name) + def test_update_add_prop_with_tags(self): + image_id = 'a2b83adc-888e-11e3-8872-78acc0b951d9' + params = {'finn': 'human'} + image = self.controller.update(image_id, **params) + expect_hdrs = { + 'Content-Type': 'application/openstack-images-v2.1-json-patch', + } + expect_body = [[('op', 'add'), ('path', '/finn'), ('value', 'human')]] + expect = [ + ('GET', '/v2/images/%s' % image_id, {}, None), + ('PATCH', '/v2/images/%s' % image_id, expect_hdrs, expect_body), + ('GET', '/v2/images/%s' % image_id, {}, None), + ] + self.assertEqual(expect, self.api.calls) + self.assertEqual(image_id, image.id) + self.assertEqual('image-1', image.name) + def test_update_bad_additionalProperty_type(self): image_id = 'e7e59ff6-fa2e-4075-87d3-1a1398a07dc3' params = {'name': 'pong', 'bad_prop': False} diff --git a/glanceclient/v2/schemas.py b/glanceclient/v2/schemas.py index 8247d313..8c838f8c 100644 --- a/glanceclient/v2/schemas.py +++ b/glanceclient/v2/schemas.py @@ -31,12 +31,12 @@ class SchemaBasedModel(warlock.Model): """ def _make_custom_patch(self, new, original): - if not self.get('tags'): - tags_patch = [] - else: + if 'tags' in new and 'tags' not in original: tags_patch = [{"path": "/tags", "value": self.get('tags'), "op": "replace"}] + else: + tags_patch = [] patch_string = jsonpatch.make_patch(original, new).to_string() patch = json.loads(patch_string) @@ -55,9 +55,6 @@ class SchemaBasedModel(warlock.Model): if (name not in original and name in new and prop.get('is_base', True)): original[name] = None - - original['tags'] = None - new['tags'] = None return self._make_custom_patch(new, original)