Skip to content

Commit a03b235

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Added "openstack image metadefs namespace list" command"
2 parents 2747910 + a726d84 commit a03b235

6 files changed

Lines changed: 192 additions & 1 deletion

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2012-2013 OpenStack Foundation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
#
15+
16+
"""Image V2 Action Implementations"""
17+
18+
from osc_lib.cli import format_columns
19+
from osc_lib.command import command
20+
from osc_lib import utils
21+
22+
from openstackclient.i18n import _
23+
24+
_formatters = {
25+
'tags': format_columns.ListColumn,
26+
}
27+
28+
29+
class ListMetadefNameSpaces(command.Lister):
30+
_description = _("List metadef namespaces")
31+
32+
def get_parser(self, prog_name):
33+
parser = super().get_parser(prog_name)
34+
parser.add_argument(
35+
"--resource-types",
36+
metavar="<resource_types>",
37+
help=_("filter resource types"),
38+
)
39+
parser.add_argument(
40+
"--visibility",
41+
metavar="<visibility>",
42+
help=_("filter on visibility"),
43+
)
44+
return parser
45+
46+
def take_action(self, parsed_args):
47+
image_client = self.app.client_manager.image
48+
filter_keys = ['resource_types', 'visibility']
49+
kwargs = {}
50+
for key in filter_keys:
51+
argument = getattr(parsed_args, key, None)
52+
if argument is not None:
53+
kwargs[key] = argument
54+
# List of namespace data received
55+
data = list(image_client.metadef_namespaces(**kwargs))
56+
columns = ['namespace']
57+
column_headers = columns
58+
return (
59+
column_headers,
60+
(utils.get_item_properties(
61+
s,
62+
columns,
63+
formatters=_formatters,
64+
) for s in data)
65+
)

openstackclient/tests/unit/image/v2/fakes.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from openstack.image.v2 import image
2020
from openstack.image.v2 import member
21+
from openstack.image.v2 import metadef_namespace
2122
from openstack.image.v2 import task
2223

2324
from openstackclient.tests.unit import fakes
@@ -44,6 +45,7 @@ def __init__(self, **kwargs):
4445
self.update_member = mock.Mock()
4546

4647
self.remove_tag = mock.Mock()
48+
self.metadef_namespaces = mock.Mock()
4749

4850
self.tasks = mock.Mock()
4951
self.get_task = mock.Mock()
@@ -55,6 +57,8 @@ def __init__(self, **kwargs):
5557
self.tasks = mock.Mock()
5658
self.tasks.resource_class = fakes.FakeResource(None, {})
5759

60+
self.metadef_namespaces = mock.Mock()
61+
5862

5963
class TestImagev2(utils.TestCommand):
6064

@@ -202,3 +206,53 @@ def create_tasks(attrs=None, count=2):
202206
tasks.append(create_one_task(attrs))
203207

204208
return tasks
209+
210+
211+
class FakeMetadefNamespaceClient:
212+
213+
def __init__(self, **kwargs):
214+
self.metadef_namespaces = mock.Mock()
215+
216+
self.auth_token = kwargs['token']
217+
self.management_url = kwargs['endpoint']
218+
self.version = 2.0
219+
220+
221+
class TestMetadefNamespaces(utils.TestCommand):
222+
223+
def setUp(self):
224+
super().setUp()
225+
226+
self.app.client_manager.image = FakeMetadefNamespaceClient(
227+
endpoint=fakes.AUTH_URL,
228+
token=fakes.AUTH_TOKEN,
229+
)
230+
231+
self.app.client_manager.identity = identity_fakes.FakeIdentityv3Client(
232+
endpoint=fakes.AUTH_URL,
233+
token=fakes.AUTH_TOKEN,
234+
)
235+
236+
237+
def create_one_metadef_namespace(attrs=None):
238+
"""Create a fake MetadefNamespace member.
239+
240+
:param attrs: A dictionary with all attributes of metadef_namespace member
241+
:type attrs: dict
242+
:return: a list of MetadefNamespace objects
243+
:rtype: list of `metadef_namespace.MetadefNamespace`
244+
"""
245+
attrs = attrs or {}
246+
247+
metadef_namespace_list = {
248+
'created_at': '2022-08-17T11:30:22Z',
249+
'display_name': 'Flavor Quota',
250+
'namespace': 'OS::Compute::Quota',
251+
'owner': 'admin',
252+
'resource_type_associations': ['OS::Nova::Flavor'],
253+
'visibility': 'public',
254+
}
255+
256+
# Overwrite default attributes if there are some attributes set
257+
metadef_namespace_list.update(attrs)
258+
return metadef_namespace.MetadefNamespace(metadef_namespace_list)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright 2013 Nebula Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
# not use this file except in compliance with the License. You may obtain
5+
# a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
from openstackclient.image.v2 import metadef_namespaces
16+
from openstackclient.tests.unit.image.v2 import fakes as md_namespace_fakes
17+
18+
19+
class TestMetadefNamespaces(md_namespace_fakes.TestMetadefNamespaces):
20+
def setUp(self):
21+
super().setUp()
22+
23+
# Get shortcuts to mocked image client
24+
self.client = self.app.client_manager.image
25+
26+
# Get shortcut to the Mocks in identity client
27+
self.project_mock = self.app.client_manager.identity.projects
28+
self.project_mock.reset_mock()
29+
self.domain_mock = self.app.client_manager.identity.domains
30+
self.domain_mock.reset_mock()
31+
32+
33+
class TestMetadefNamespaceList(TestMetadefNamespaces):
34+
35+
_metadef_namespace = [md_namespace_fakes.create_one_metadef_namespace()]
36+
37+
columns = [
38+
'namespace'
39+
]
40+
41+
datalist = []
42+
43+
def setUp(self):
44+
super().setUp()
45+
46+
self.client.metadef_namespaces.side_effect = [
47+
self._metadef_namespace, []]
48+
49+
# Get the command object to test
50+
self.client.metadef_namespaces.return_value = iter(
51+
self._metadef_namespace
52+
)
53+
self.cmd = metadef_namespaces.ListMetadefNameSpaces(self.app, None)
54+
self.datalist = self._metadef_namespace
55+
56+
def test_namespace_list_no_options(self):
57+
arglist = []
58+
parsed_args = self.check_parser(self.cmd, arglist, [])
59+
60+
# In base command class Lister in cliff, abstract method take_action()
61+
# returns a tuple containing the column names and an iterable
62+
# containing the data to be listed.
63+
columns, data = self.cmd.take_action(parsed_args)
64+
65+
self.assertEqual(self.columns, columns)
66+
self.assertEqual(getattr(self.datalist[0], 'namespace'),
67+
next(data)[0])
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
features:
3+
- Adds ``openstack image metadef namespace list``.
4+
The output is equivalent to glance md-namespace-list.

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pbr!=2.1.0,>=2.0.0 # Apache-2.0
66

77
cliff>=3.5.0 # Apache-2.0
88
iso8601>=0.1.11 # MIT
9-
openstacksdk>=0.61.0 # Apache-2.0
9+
openstacksdk>=0.102.0 # Apache-2.0
1010
osc-lib>=2.3.0 # Apache-2.0
1111
oslo.i18n>=3.15.3 # Apache-2.0
1212
oslo.utils>=3.33.0 # Apache-2.0

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ openstack.image.v2 =
385385
image_unset = openstackclient.image.v2.image:UnsetImage
386386
image_task_show = openstackclient.image.v2.task:ShowTask
387387
image_task_list = openstackclient.image.v2.task:ListTask
388+
image_metadef_namespace_list = openstackclient.image.v2.metadef_namespaces:ListMetadefNameSpaces
388389

389390
openstack.network.v2 =
390391
address_group_create = openstackclient.network.v2.address_group:CreateAddressGroup

0 commit comments

Comments
 (0)