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
This commit is contained in:
Mike Fedosin 2016-09-27 18:06:30 +03:00
parent 6b3c343b91
commit ffbb953c3e
2 changed files with 39 additions and 2 deletions

View File

@ -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,

View File

@ -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]})