Merge "Add an optional parameter --append"

This commit is contained in:
Zuul 2022-02-17 18:51:52 +00:00 committed by Gerrit Code Review
commit 63bb03a145
3 changed files with 35 additions and 8 deletions

View File

@ -3142,7 +3142,8 @@ class ShellV2Test(testtools.TestCase):
def test_do_md_tag_create_multiple(self): def test_do_md_tag_create_multiple(self):
args = self._make_args({'namespace': 'MyNamespace', args = self._make_args({'namespace': 'MyNamespace',
'delim': ',', 'delim': ',',
'names': 'MyTag1, MyTag2'}) 'names': 'MyTag1, MyTag2',
'append': False})
with mock.patch.object( with mock.patch.object(
self.gc.metadefs_tag, 'create_multiple') as mocked_create_tags: self.gc.metadefs_tag, 'create_multiple') as mocked_create_tags:
expect_tags = [{'tags': [{'name': 'MyTag1'}, {'name': 'MyTag2'}]}] expect_tags = [{'tags': [{'name': 'MyTag1'}, {'name': 'MyTag2'}]}]
@ -3152,7 +3153,28 @@ class ShellV2Test(testtools.TestCase):
test_shell.do_md_tag_create_multiple(self.gc, args) test_shell.do_md_tag_create_multiple(self.gc, args)
mocked_create_tags.assert_called_once_with( 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( utils.print_list.assert_called_once_with(
expect_tags, expect_tags,
['name'], ['name'],

View File

@ -490,9 +490,8 @@ class TagController(object):
"""Create the list of tags. """Create the list of tags.
:param namespace: Name of a namespace to which the Tags belong. :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', []) tag_names = kwargs.pop('tags', [])
md_tag_list = [] md_tag_list = []
@ -502,11 +501,15 @@ class TagController(object):
except (warlock.InvalidOperation) as e: except (warlock.InvalidOperation) as e:
raise TypeError(encodeutils.exception_to_unicode(e)) raise TypeError(encodeutils.exception_to_unicode(e))
tags = {'tags': md_tag_list} tags = {'tags': md_tag_list}
headers = {}
url = '/v2/metadefs/namespaces/%(namespace)s/tags' % { 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) body.pop('self', None)
for tag in body['tags']: for tag in body['tags']:
yield self.model(tag), resp yield self.model(tag), resp

View File

@ -1396,10 +1396,12 @@ def do_md_tag_create(gc, args):
@utils.arg('--delim', metavar='<DELIM>', required=False, @utils.arg('--delim', metavar='<DELIM>', required=False,
help=_('The delimiter used to separate the names' help=_('The delimiter used to separate the names'
' (if none is provided then the default is a comma).')) ' (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): def do_md_tag_create_multiple(gc, args):
"""Create new metadata definitions tags inside a namespace.""" """Create new metadata definitions tags inside a namespace."""
delim = args.delim or ',' delim = args.delim or ','
tags = [] tags = []
names_list = args.names.split(delim) names_list = args.names.split(delim)
for name in names_list: for name in names_list:
@ -1411,7 +1413,7 @@ def do_md_tag_create_multiple(gc, args):
utils.exit('Please supply at least one tag name. For example: ' utils.exit('Please supply at least one tag name. For example: '
'--names Tag1') '--names Tag1')
fields = {'tags': tags} fields = {'tags': tags, 'append': args.append}
new_tags = gc.metadefs_tag.create_multiple(args.namespace, **fields) new_tags = gc.metadefs_tag.create_multiple(args.namespace, **fields)
columns = ['name'] columns = ['name']
column_settings = { column_settings = {