Skip to content

Commit a216746

Browse files
committed
Add identity v3 catalog list
Change-Id: Id4c1371ca28b9fd884ec75061edca700fd69886c
1 parent 5d92fc0 commit a216746

3 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
#
13+
14+
"""Identity v3 Service Catalog action implementations"""
15+
16+
import logging
17+
18+
from cliff import lister
19+
20+
from openstackclient.common import utils
21+
22+
23+
def _format_endpoints(eps=None):
24+
if not eps:
25+
return ""
26+
ret = ''
27+
for ep in eps:
28+
region = ep.get('region_id') or ep.get('region', '<none>')
29+
ret += region + '\n'
30+
ret += " %s: %s\n" % (ep['interface'], ep['url'])
31+
return ret
32+
33+
34+
class ListCatalog(lister.Lister):
35+
"""List services in the service catalog"""
36+
37+
log = logging.getLogger(__name__ + '.ListCatalog')
38+
39+
def take_action(self, parsed_args):
40+
self.log.debug('take_action(%s)', parsed_args)
41+
42+
# This is ugly because if auth hasn't happened yet we need
43+
# to trigger it here.
44+
sc = self.app.client_manager.session.auth.get_auth_ref(
45+
self.app.client_manager.session,
46+
).service_catalog
47+
48+
data = sc.get_data()
49+
columns = ('Name', 'Type', 'Endpoints')
50+
return (columns,
51+
(utils.get_dict_properties(
52+
s, columns,
53+
formatters={
54+
'Endpoints': _format_endpoints,
55+
},
56+
) for s in data))
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
#
13+
14+
import mock
15+
16+
from openstackclient.identity.v3 import catalog
17+
from openstackclient.tests import utils
18+
19+
20+
class TestCatalog(utils.TestCommand):
21+
22+
fake_service = {
23+
'id': 'qwertyuiop',
24+
'type': 'compute',
25+
'name': 'supernova',
26+
'endpoints': [
27+
{
28+
'region': 'onlyone',
29+
'url': 'https://public.example.com',
30+
'interface': 'public',
31+
},
32+
{
33+
'region_id': 'onlyone',
34+
'url': 'https://admin.example.com',
35+
'interface': 'admin',
36+
},
37+
{
38+
'url': 'https://internal.example.com',
39+
'interface': 'internal',
40+
},
41+
],
42+
}
43+
44+
def setUp(self):
45+
super(TestCatalog, self).setUp()
46+
47+
self.sc_mock = mock.MagicMock()
48+
self.sc_mock.service_catalog.get_data.return_value = [
49+
self.fake_service,
50+
]
51+
52+
self.auth_mock = mock.MagicMock()
53+
self.app.client_manager.session = self.auth_mock
54+
55+
self.auth_mock.auth.get_auth_ref.return_value = self.sc_mock
56+
57+
58+
class TestCatalogList(TestCatalog):
59+
60+
def setUp(self):
61+
super(TestCatalogList, self).setUp()
62+
63+
# Get the command object to test
64+
self.cmd = catalog.ListCatalog(self.app, None)
65+
66+
def test_catalog_list(self):
67+
arglist = []
68+
verifylist = []
69+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
70+
71+
# DisplayCommandBase.take_action() returns two tuples
72+
columns, data = self.cmd.take_action(parsed_args)
73+
self.sc_mock.service_catalog.get_data.assert_called_with()
74+
75+
collist = ('Name', 'Type', 'Endpoints')
76+
self.assertEqual(collist, columns)
77+
datalist = ((
78+
'supernova',
79+
'compute',
80+
'onlyone\n public: https://public.example.com\n'
81+
'onlyone\n admin: https://admin.example.com\n'
82+
'<none>\n internal: https://internal.example.com\n',
83+
), )
84+
self.assertEqual(datalist, tuple(data))

‎setup.cfg‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ openstack.identity.v2 =
183183
openstack.identity.v3 =
184184
access_token_create = openstackclient.identity.v3.token:CreateAccessToken
185185

186+
catalog_list = openstackclient.identity.v3.catalog:ListCatalog
187+
186188
consumer_create = openstackclient.identity.v3.consumer:CreateConsumer
187189
consumer_delete = openstackclient.identity.v3.consumer:DeleteConsumer
188190
consumer_list = openstackclient.identity.v3.consumer:ListConsumer

0 commit comments

Comments
 (0)