From ffbb953c3e8190f15f60c628c221ddb4bcbc92c6 Mon Sep 17 00:00:00 2001 From: Mike Fedosin Date: Tue, 27 Sep 2016 18:06:30 +0300 Subject: [PATCH] Allow to remove non-compound properties If user wants to remove(reset) non-compound properties, then client sets value None to the property and sends PATCH request with 'replace' operator. Compound properties are processed as well as before. Change-Id: Ib3469cf125beaca0e3c7041bc1e198e1ef7f2c19 --- glareclient/tests/unit/v1/test_artifacts.py | 29 +++++++++++++++++++++ glareclient/v1/artifacts.py | 12 +++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/glareclient/tests/unit/v1/test_artifacts.py b/glareclient/tests/unit/v1/test_artifacts.py index 1e5ffcd..35ad079 100644 --- a/glareclient/tests/unit/v1/test_artifacts.py +++ b/glareclient/tests/unit/v1/test_artifacts.py @@ -178,6 +178,35 @@ class TestController(testtools.TestCase): } expect_body = [{'path': '/name', + 'op': 'replace', + 'value': None}] + + expect = [('PATCH', '/artifacts/sample_artifact/%s' % art_id, + exp_headers, + expect_body)] + + self.assertEqual(expect, self.api.calls) + self.api.calls = [] + + self.controller.update(artifact_id=art_id, + remove_props=['metadata/key1'], + type_name='sample_artifact') + + expect_body = [{'path': '/metadata/key1', + 'op': 'remove'}] + + expect = [('PATCH', '/artifacts/sample_artifact/%s' % art_id, + exp_headers, + expect_body)] + + self.assertEqual(expect, self.api.calls) + self.api.calls = [] + + self.controller.update(artifact_id=art_id, + remove_props=['releases/1'], + type_name='sample_artifact') + + expect_body = [{'path': '/releases/1', 'op': 'remove'}] expect = [('PATCH', '/artifacts/sample_artifact/%s' % art_id, diff --git a/glareclient/v1/artifacts.py b/glareclient/v1/artifacts.py index e3ba88a..8b6b87b 100644 --- a/glareclient/v1/artifacts.py +++ b/glareclient/v1/artifacts.py @@ -77,8 +77,16 @@ class Controller(object): if remove_props: for prop_name in remove_props: if prop_name not in kwargs: - changes.append({'op': 'remove', - 'path': '/%s' % prop_name}) + if '/' in prop_name: + # we remove all values in dicts and lists explicitly, + # i.e. matadata/key or releases/1 + changes.append({'op': 'remove', + 'path': '/%s' % prop_name}) + else: + # in other cases we just replace the value with None + changes.append({'op': 'replace', + 'path': '/%s' % prop_name, + 'value': None}) for prop_name in kwargs: changes.append({'op': 'add', 'path': '/%s' % prop_name, 'value': kwargs[prop_name]})