From 52bf194741c2594f38539ae63d3ea3fe05152a09 Mon Sep 17 00:00:00 2001 From: Antonia Gaete Date: Tue, 16 May 2023 15:38:27 +0000 Subject: [PATCH] Add image metadef resource type command 'list' This patch will add ``image metadef resource type list`` using SDK, output matches glance client output. Change-Id: I29850abdbb06d5a4eaf8a9a60f565b46f4009f85 --- doc/source/cli/data/glance.csv | 2 +- .../image/v2/metadef_resource_types.py | 39 +++++++++++++++ openstackclient/tests/unit/image/v2/fakes.py | 40 +++++++++++++++ .../image/v2/test_metadef_resource_types.py | 50 +++++++++++++++++++ ...ce-type-list-command-020adcaa2ad14e07.yaml | 5 ++ setup.cfg | 2 + 6 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 openstackclient/image/v2/metadef_resource_types.py create mode 100644 openstackclient/tests/unit/image/v2/test_metadef_resource_types.py create mode 100644 releasenotes/notes/add-image-metadef-resource-type-list-command-020adcaa2ad14e07.yaml diff --git a/doc/source/cli/data/glance.csv b/doc/source/cli/data/glance.csv index 9d37509b97..fd3b930936 100644 --- a/doc/source/cli/data/glance.csv +++ b/doc/source/cli/data/glance.csv @@ -28,7 +28,7 @@ md-namespace-import,,Import a metadata definitions namespace from file or standa md-namespace-list,image metadef namespace list,List metadata definitions namespaces. md-namespace-objects-delete,,Delete all metadata definitions objects inside a specific namespace. md-namespace-properties-delete,,Delete all metadata definitions property inside a specific namespace. -md-namespace-resource-type-list,,List resource types associated to specific namespace. +md-namespace-resource-type-list,image metadef resource type list,List resource types associated to specific namespace. md-namespace-show,image metadef namespace show,Describe a specific metadata definitions namespace. md-namespace-tags-delete,,Delete all metadata definitions tags inside a specific namespace. md-namespace-update,,Update an existing metadata definitions namespace. diff --git a/openstackclient/image/v2/metadef_resource_types.py b/openstackclient/image/v2/metadef_resource_types.py new file mode 100644 index 0000000000..bbd77636f0 --- /dev/null +++ b/openstackclient/image/v2/metadef_resource_types.py @@ -0,0 +1,39 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Image V2 Action Implementations""" + +from osc_lib.command import command +from osc_lib import utils + +from openstackclient.i18n import _ + + +class ListMetadefResourceTypes(command.Lister): + _description = _("List metadef resource types") + + def take_action(self, parsed_args): + image_client = self.app.client_manager.image + kwargs = {} + data = image_client.metadef_resource_types(**kwargs) + columns = ['Name'] + column_headers = columns + return ( + column_headers, + ( + utils.get_item_properties( + s, + columns, + ) + for s in data + ), + ) diff --git a/openstackclient/tests/unit/image/v2/fakes.py b/openstackclient/tests/unit/image/v2/fakes.py index 590796f885..af8475cba9 100644 --- a/openstackclient/tests/unit/image/v2/fakes.py +++ b/openstackclient/tests/unit/image/v2/fakes.py @@ -19,6 +19,7 @@ import uuid from openstack.image.v2 import image from openstack.image.v2 import member from openstack.image.v2 import metadef_namespace +from openstack.image.v2 import metadef_resource_type from openstack.image.v2 import service_info as _service_info from openstack.image.v2 import task @@ -48,6 +49,7 @@ class FakeImagev2Client: self.remove_tag = mock.Mock() self.metadef_namespaces = mock.Mock() + self.metadef_resource_types = mock.Mock() self.tasks = mock.Mock() self.tasks.resource_class = fakes.FakeResource(None, {}) @@ -285,3 +287,41 @@ def create_one_metadef_namespace(attrs=None): # Overwrite default attributes if there are some attributes set metadef_namespace_list.update(attrs) return metadef_namespace.MetadefNamespace(**metadef_namespace_list) + + +def create_one_resource_type(attrs=None): + """Create a fake MetadefResourceType member. + + :param attrs: A dictionary with all attributes of + metadef_resource_type member + :type attrs: dict + :return: a fake MetadefResourceType object + :rtype: A `metadef_resource_type.MetadefResourceType` + """ + attrs = attrs or {} + + metadef_resource_type_info = { + 'name': 'OS::Compute::Quota', + 'properties_target': 'image', + } + + metadef_resource_type_info.update(attrs) + return metadef_resource_type.MetadefResourceType( + **metadef_resource_type_info + ) + + +def create_resource_types(attrs=None, count=2): + """Create multiple fake resource types. + + :param attrs: A dictionary with all attributes of + metadef_resource_type member + :type attrs: dict + :return: A list of fake MetadefResourceType objects + :rtype: list + """ + metadef_resource_types = [] + for n in range(0, count): + metadef_resource_types.append(create_one_resource_type(attrs)) + + return metadef_resource_types diff --git a/openstackclient/tests/unit/image/v2/test_metadef_resource_types.py b/openstackclient/tests/unit/image/v2/test_metadef_resource_types.py new file mode 100644 index 0000000000..6b8264ed99 --- /dev/null +++ b/openstackclient/tests/unit/image/v2/test_metadef_resource_types.py @@ -0,0 +1,50 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from openstackclient.image.v2 import metadef_resource_types +from openstackclient.tests.unit.image.v2 import fakes as resource_type_fakes + + +class TestMetadefResourceTypes(resource_type_fakes.TestImagev2): + def setUp(self): + super().setUp() + + self.client = self.app.client_manager.image + + +class TestMetadefResourceTypeList(TestMetadefResourceTypes): + resource_types = resource_type_fakes.create_resource_types() + + columns = ['Name'] + + datalist = [(resource_type.name,) for resource_type in resource_types] + + def setUp(self): + super().setUp() + + self.client.metadef_resource_types.side_effect = [ + self.resource_types, + [], + ] + + self.cmd = metadef_resource_types.ListMetadefResourceTypes( + self.app, None + ) + + def test_resource_type_list_no_options(self): + arglist = [] + parsed_args = self.check_parser(self.cmd, arglist, []) + + columns, data = self.cmd.take_action(parsed_args) + + self.assertEqual(self.columns, columns) + self.assertCountEqual(self.datalist, data) diff --git a/releasenotes/notes/add-image-metadef-resource-type-list-command-020adcaa2ad14e07.yaml b/releasenotes/notes/add-image-metadef-resource-type-list-command-020adcaa2ad14e07.yaml new file mode 100644 index 0000000000..01857b1c70 --- /dev/null +++ b/releasenotes/notes/add-image-metadef-resource-type-list-command-020adcaa2ad14e07.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Added ``image metadef resource type list`` command. This is equivalent to + the ``+md-namespace-resource-type-list`` command in glanceclient. diff --git a/setup.cfg b/setup.cfg index 8c68259a73..6578d31f1d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -395,6 +395,8 @@ 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_resource_type_list = openstackclient.image.v2.metadef_resource_types:ListMetadefResourceTypes + openstack.network.v2 = address_group_create = openstackclient.network.v2.address_group:CreateAddressGroup address_group_delete = openstackclient.network.v2.address_group:DeleteAddressGroup