From 4bb6efa8f8ec60f288b1e7a246e97dacb0f6ad33 Mon Sep 17 00:00:00 2001 From: Mridula Joshi Date: Fri, 23 Jun 2023 12:27:50 +0000 Subject: [PATCH] Adds command ``image metadef object list`` Depends-on: https://review.opendev.org/c/openstack/openstacksdk/+/858350 Change-Id: I0691f7519e8fb9f01836e6232e0bcebd2c428ac3 --- doc/source/cli/data/glance.csv | 2 +- openstackclient/image/v2/metadef_objects.py | 32 +++++++++++++++++ .../unit/image/v2/test_metadef_objects.py | 35 +++++++++++++++++++ ...-metadef-object-list-c8831e73c696b9d9.yaml | 5 +++ setup.cfg | 3 ++ 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/add-metadef-object-list-c8831e73c696b9d9.yaml diff --git a/doc/source/cli/data/glance.csv b/doc/source/cli/data/glance.csv index d8bc984cf7..e2e2f7e87a 100644 --- a/doc/source/cli/data/glance.csv +++ b/doc/source/cli/data/glance.csv @@ -34,7 +34,7 @@ md-namespace-tags-delete,,Delete all metadata definitions tags inside a specific md-namespace-update,,Update an existing metadata definitions namespace. md-object-create,image metadef object create,Create a new metadata definitions object inside a namespace. md-object-delete,,Delete a specific metadata definitions object inside a namespace. -md-object-list,,List metadata definitions objects inside a specific namespace. +md-object-list,image metadef object list,List metadata definitions objects inside a specific namespace. md-object-property-show,,Describe a specific metadata definitions property inside an object. md-object-show,image metadef object show,Describe a specific metadata definitions object inside a namespace. md-object-update,,Update metadata definitions object inside a namespace. diff --git a/openstackclient/image/v2/metadef_objects.py b/openstackclient/image/v2/metadef_objects.py index 3792ed8fc8..a88c19cd89 100644 --- a/openstackclient/image/v2/metadef_objects.py +++ b/openstackclient/image/v2/metadef_objects.py @@ -105,3 +105,35 @@ class ShowMetadefObjects(command.ShowOne): fields, value = _format_object(data) return fields, value + + +class ListMetadefObjects(command.Lister): + _description = _("List metadef objects inside a specific namespace.") + + def get_parser(self, prog_name): + parser = super().get_parser(prog_name) + parser.add_argument( + "namespace", + metavar="", + help=_("Namespace (name) for the namespace"), + ) + return parser + + def take_action(self, parsed_args): + image_client = self.app.client_manager.image + + namespace_name = parsed_args.namespace + columns = ['name', 'description'] + + md_objects = list(image_client.metadef_objects(namespace_name)) + column_headers = columns + return ( + column_headers, + ( + utils.get_item_properties( + md_object, + columns, + ) + for md_object in md_objects + ), + ) diff --git a/openstackclient/tests/unit/image/v2/test_metadef_objects.py b/openstackclient/tests/unit/image/v2/test_metadef_objects.py index 76a26f0ee4..7b8c238b9a 100644 --- a/openstackclient/tests/unit/image/v2/test_metadef_objects.py +++ b/openstackclient/tests/unit/image/v2/test_metadef_objects.py @@ -102,3 +102,38 @@ class TestMetadefObjectsShow(fakes.TestImagev2): self.assertEqual(self.expected_columns, columns) self.assertEqual(self.expected_data, data) + + +class TestMetadefObjectList(fakes.TestImagev2): + _metadef_namespace = fakes.create_one_metadef_namespace() + _metadef_objects = [fakes.create_one_metadef_object()] + columns = ['name', 'description'] + + datalist = [] + + def setUp(self): + super().setUp() + + self.image_client.metadef_objects.side_effect = [ + self._metadef_objects, + [], + ] + + # Get the command object to test + self.image_client.metadef_objects.return_value = iter( + self._metadef_objects + ) + self.cmd = metadef_objects.ListMetadefObjects(self.app, None) + self.datalist = self._metadef_objects + + def test_metadef_objects_list(self): + arglist = [self._metadef_namespace.namespace] + parsed_args = self.check_parser(self.cmd, arglist, []) + + # In base command class Lister in cliff, abstract method take_action() + # returns a tuple containing the column names and an iterable + # containing the data to be listed. + columns, data = self.cmd.take_action(parsed_args) + + self.assertEqual(self.columns, columns) + self.assertEqual(getattr(self.datalist[0], 'name'), next(data)[0]) diff --git a/releasenotes/notes/add-metadef-object-list-c8831e73c696b9d9.yaml b/releasenotes/notes/add-metadef-object-list-c8831e73c696b9d9.yaml new file mode 100644 index 0000000000..bfa89036a5 --- /dev/null +++ b/releasenotes/notes/add-metadef-object-list-c8831e73c696b9d9.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add ``image metadef object list`` command to list the + metadata definitions objects inside a specific namespace diff --git a/setup.cfg b/setup.cfg index 2e2acbed39..a42fafccf7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -397,8 +397,11 @@ openstack.image.v2 = image_metadef_namespace_set = openstackclient.image.v2.metadef_namespaces:SetMetadefNameSpace image_metadef_namespace_show = openstackclient.image.v2.metadef_namespaces:ShowMetadefNameSpace + image_metadef_object_create = openstackclient.image.v2.metadef_objects:CreateMetadefObjects image_metadef_object_show = openstackclient.image.v2.metadef_objects:ShowMetadefObjects + image_metadef_object_list = openstackclient.image.v2.metadef_objects:ListMetadefObjects + image_metadef_property_create = openstackclient.image.v2.metadef_properties:CreateMetadefProperty image_metadef_property_list = openstackclient.image.v2.metadef_properties:ListMetadefProperties