From b8863535a823ef2c271ebc4ddd1afd039bbc3f93 Mon Sep 17 00:00:00 2001 From: Mridula Joshi Date: Tue, 12 Oct 2021 07:32:36 +0000 Subject: [PATCH] Add an optional parameter --append This patch will add an optional parameter --append to the glanceclient command md-tag-create-multiple to provide the facility of appending the tags. If the parameter is present it will append the tags to existing one, else it will overwrite the existing tags. Depends-On: https://review.opendev.org/c/openstack/glance/+/804966 Change-Id: I1841e7146da76b13f4cd8925e19f59d0eaf08f7a --- glanceclient/tests/unit/v2/test_shell_v2.py | 26 +++++++++++++++++++-- glanceclient/v2/metadefs.py | 11 +++++---- glanceclient/v2/shell.py | 6 +++-- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/glanceclient/tests/unit/v2/test_shell_v2.py b/glanceclient/tests/unit/v2/test_shell_v2.py index 83c47273..057cacde 100644 --- a/glanceclient/tests/unit/v2/test_shell_v2.py +++ b/glanceclient/tests/unit/v2/test_shell_v2.py @@ -3084,7 +3084,8 @@ class ShellV2Test(testtools.TestCase): def test_do_md_tag_create_multiple(self): args = self._make_args({'namespace': 'MyNamespace', 'delim': ',', - 'names': 'MyTag1, MyTag2'}) + 'names': 'MyTag1, MyTag2', + 'append': False}) with mock.patch.object( self.gc.metadefs_tag, 'create_multiple') as mocked_create_tags: expect_tags = [{'tags': [{'name': 'MyTag1'}, {'name': 'MyTag2'}]}] @@ -3094,7 +3095,28 @@ class ShellV2Test(testtools.TestCase): test_shell.do_md_tag_create_multiple(self.gc, args) mocked_create_tags.assert_called_once_with( - 'MyNamespace', tags=['MyTag1', 'MyTag2']) + 'MyNamespace', tags=['MyTag1', 'MyTag2'], append=False) + utils.print_list.assert_called_once_with( + expect_tags, + ['name'], + field_settings={ + 'description': {'align': 'l', 'max_width': 50}}) + + def test_do_md_tag_create_multiple_with_append(self): + args = self._make_args({'namespace': 'MyNamespace', + 'delim': ',', + 'names': 'MyTag1, MyTag2', + 'append': True}) + with mock.patch.object( + self.gc.metadefs_tag, 'create_multiple') as mocked_create_tags: + expect_tags = [{'tags': [{'name': 'MyTag1'}, {'name': 'MyTag2'}]}] + + mocked_create_tags.return_value = expect_tags + + test_shell.do_md_tag_create_multiple(self.gc, args) + + mocked_create_tags.assert_called_once_with( + 'MyNamespace', tags=['MyTag1', 'MyTag2'], append=True) utils.print_list.assert_called_once_with( expect_tags, ['name'], diff --git a/glanceclient/v2/metadefs.py b/glanceclient/v2/metadefs.py index 1b641ac7..a98a9fec 100644 --- a/glanceclient/v2/metadefs.py +++ b/glanceclient/v2/metadefs.py @@ -490,9 +490,8 @@ class TagController(object): """Create the list of tags. :param namespace: Name of a namespace to which the Tags belong. - :param kwargs: list of tags. + :param kwargs: list of tags, optional parameter append. """ - tag_names = kwargs.pop('tags', []) md_tag_list = [] @@ -502,11 +501,15 @@ class TagController(object): except (warlock.InvalidOperation) as e: raise TypeError(encodeutils.exception_to_unicode(e)) tags = {'tags': md_tag_list} + headers = {} url = '/v2/metadefs/namespaces/%(namespace)s/tags' % { - 'namespace': namespace} + 'namespace': namespace} - resp, body = self.http_client.post(url, data=tags) + append = kwargs.pop('append', False) + if append: + headers['X-Openstack-Append'] = True + resp, body = self.http_client.post(url, headers=headers, data=tags) body.pop('self', None) for tag in body['tags']: yield self.model(tag), resp diff --git a/glanceclient/v2/shell.py b/glanceclient/v2/shell.py index 5f83bd27..407fa44f 100644 --- a/glanceclient/v2/shell.py +++ b/glanceclient/v2/shell.py @@ -1383,10 +1383,12 @@ def do_md_tag_create(gc, args): @utils.arg('--delim', metavar='', required=False, help=_('The delimiter used to separate the names' ' (if none is provided then the default is a comma).')) +@utils.arg('--append', default=False, action='store_true', required=False, + help=_('Append the new tags to the existing ones instead of' + 'overwriting them')) def do_md_tag_create_multiple(gc, args): """Create new metadata definitions tags inside a namespace.""" delim = args.delim or ',' - tags = [] names_list = args.names.split(delim) for name in names_list: @@ -1398,7 +1400,7 @@ def do_md_tag_create_multiple(gc, args): utils.exit('Please supply at least one tag name. For example: ' '--names Tag1') - fields = {'tags': tags} + fields = {'tags': tags, 'append': args.append} new_tags = gc.metadefs_tag.create_multiple(args.namespace, **fields) columns = ['name'] column_settings = {