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
This commit is contained in:
parent
8aac1597cd
commit
b8863535a8
@ -3084,7 +3084,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'}]}]
|
||||||
@ -3094,7 +3095,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'],
|
||||||
|
@ -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
|
||||||
|
@ -1383,10 +1383,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:
|
||||||
@ -1398,7 +1400,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 = {
|
||||||
|
Loading…
Reference in New Issue
Block a user