Skip to content

Commit 0ee7073

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add service catalog commands"
2 parents 873ece1 + da45b34 commit 0ee7073

3 files changed

Lines changed: 208 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 v2 Service Catalog action implementations"""
15+
16+
import logging
17+
import six
18+
19+
from cliff import lister
20+
from cliff import show
21+
22+
from openstackclient.common import utils
23+
24+
25+
def _format_endpoints(eps=None):
26+
if not eps:
27+
return ""
28+
for index, ep in enumerate(eps):
29+
ret = eps[index]['region'] + '\n'
30+
for url in ['publicURL', 'internalURL', 'adminURL']:
31+
ret += " %s: %s\n" % (url, eps[index]['publicURL'])
32+
return ret
33+
34+
35+
class ListCatalog(lister.Lister):
36+
"""List services in the service catalog"""
37+
38+
log = logging.getLogger(__name__ + '.ListCatalog')
39+
40+
def take_action(self, parsed_args):
41+
self.log.debug('take_action(%s)', parsed_args)
42+
43+
# This is ugly because if auth hasn't happened yet we need
44+
# to trigger it here.
45+
sc = self.app.client_manager.session.auth.get_auth_ref(
46+
self.app.client_manager.session,
47+
).service_catalog
48+
49+
data = sc.get_data()
50+
columns = ('Name', 'Type', 'Endpoints')
51+
return (columns,
52+
(utils.get_dict_properties(
53+
s, columns,
54+
formatters={
55+
'Endpoints': _format_endpoints,
56+
},
57+
) for s in data))
58+
59+
60+
class ShowCatalog(show.ShowOne):
61+
"""Show service catalog details"""
62+
63+
log = logging.getLogger(__name__ + '.ShowCatalog')
64+
65+
def get_parser(self, prog_name):
66+
parser = super(ShowCatalog, self).get_parser(prog_name)
67+
parser.add_argument(
68+
'service',
69+
metavar='<service>',
70+
help='Service to display (type, name or ID)',
71+
)
72+
return parser
73+
74+
def take_action(self, parsed_args):
75+
self.log.debug('take_action(%s)', parsed_args)
76+
77+
# This is ugly because if auth hasn't happened yet we need
78+
# to trigger it here.
79+
sc = self.app.client_manager.session.auth.get_auth_ref(
80+
self.app.client_manager.session,
81+
).service_catalog
82+
83+
data = None
84+
for service in sc.get_data():
85+
if (
86+
'name' in service and
87+
service['name'] != parsed_args.service and
88+
'type' in service and
89+
service['type'] != parsed_args.service
90+
):
91+
continue
92+
93+
data = service
94+
data['endpoints'] = _format_endpoints(data['endpoints'])
95+
if 'endpoints_links' in data:
96+
data.pop('endpoints_links')
97+
98+
return zip(*sorted(six.iteritems(data)))
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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.v2_0 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+
'region': 'onlyone',
28+
'publicURL': 'https://public.example.com',
29+
'adminURL': 'https://admin.example.com',
30+
}],
31+
}
32+
33+
def setUp(self):
34+
super(TestCatalog, self).setUp()
35+
36+
self.sc_mock = mock.MagicMock()
37+
self.sc_mock.service_catalog.get_data.return_value = [
38+
self.fake_service,
39+
]
40+
41+
self.auth_mock = mock.MagicMock()
42+
self.app.client_manager.session = self.auth_mock
43+
44+
self.auth_mock.auth.get_auth_ref.return_value = self.sc_mock
45+
46+
47+
class TestCatalogList(TestCatalog):
48+
49+
def setUp(self):
50+
super(TestCatalogList, self).setUp()
51+
52+
# Get the command object to test
53+
self.cmd = catalog.ListCatalog(self.app, None)
54+
55+
def test_catalog_list(self):
56+
arglist = []
57+
verifylist = []
58+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
59+
60+
# DisplayCommandBase.take_action() returns two tuples
61+
columns, data = self.cmd.take_action(parsed_args)
62+
self.sc_mock.service_catalog.get_data.assert_called_with()
63+
64+
collist = ('Name', 'Type', 'Endpoints')
65+
self.assertEqual(collist, columns)
66+
datalist = ((
67+
'supernova',
68+
'compute',
69+
'onlyone\n publicURL: https://public.example.com\n '
70+
'internalURL: https://public.example.com\n '
71+
'adminURL: https://public.example.com\n',
72+
), )
73+
self.assertEqual(datalist, tuple(data))
74+
75+
76+
class TestCatalogShow(TestCatalog):
77+
78+
def setUp(self):
79+
super(TestCatalogShow, self).setUp()
80+
81+
# Get the command object to test
82+
self.cmd = catalog.ShowCatalog(self.app, None)
83+
84+
def test_catalog_show(self):
85+
arglist = [
86+
'compute',
87+
]
88+
verifylist = [
89+
('service', 'compute'),
90+
]
91+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
92+
93+
# DisplayCommandBase.take_action() returns two tuples
94+
columns, data = self.cmd.take_action(parsed_args)
95+
self.sc_mock.service_catalog.get_data.assert_called_with()
96+
97+
collist = ('endpoints', 'id', 'name', 'type')
98+
self.assertEqual(collist, columns)
99+
datalist = (
100+
'onlyone\n publicURL: https://public.example.com\n '
101+
'internalURL: https://public.example.com\n '
102+
'adminURL: https://public.example.com\n',
103+
'qwertyuiop',
104+
'supernova',
105+
'compute',
106+
)
107+
self.assertEqual(datalist, data)

‎setup.cfg‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ openstack.compute.v2 =
128128
server_unset = openstackclient.compute.v2.server:UnsetServer
129129

130130
openstack.identity.v2_0 =
131+
catalog_list = openstackclient.identity.v2_0.catalog:ListCatalog
132+
catalog_show = openstackclient.identity.v2_0.catalog:ShowCatalog
133+
131134
ec2_credentials_create = openstackclient.identity.v2_0.ec2creds:CreateEC2Creds
132135
ec2_credentials_delete = openstackclient.identity.v2_0.ec2creds:DeleteEC2Creds
133136
ec2_credentials_list = openstackclient.identity.v2_0.ec2creds:ListEC2Creds

0 commit comments

Comments
 (0)