From 181f16da8a2433809d319441d8261b908faf2dd9 Mon Sep 17 00:00:00 2001 From: wanghong Date: Mon, 18 Aug 2014 16:41:15 +0800 Subject: [PATCH] add service/interface/region filter for endpoint v3 Change-Id: I7eac5b2ff5f5a6f3f08b22dd3a48a5ae7e2c056b Closes-Bug: #1281888 --- openstackclient/identity/v3/endpoint.py | 28 +++++- .../tests/identity/v3/test_endpoint.py | 96 +++++++++++++++++++ 2 files changed, 123 insertions(+), 1 deletion(-) diff --git a/openstackclient/identity/v3/endpoint.py b/openstackclient/identity/v3/endpoint.py index 5ab5dac466..39798b2dd4 100644 --- a/openstackclient/identity/v3/endpoint.py +++ b/openstackclient/identity/v3/endpoint.py @@ -114,12 +114,38 @@ class ListEndpoint(lister.Lister): log = logging.getLogger(__name__ + '.ListEndpoint') + def get_parser(self, prog_name): + parser = super(ListEndpoint, self).get_parser(prog_name) + parser.add_argument( + '--service', + metavar='', + help='Filter by a specific service') + parser.add_argument( + '--interface', + metavar='', + choices=['admin', 'public', 'internal'], + help='Filter by a specific interface, must be admin, public or' + ' internal') + parser.add_argument( + '--region', + metavar='', + help='Filter by a specific region') + return parser + def take_action(self, parsed_args): self.log.debug('take_action(%s)', parsed_args) identity_client = self.app.client_manager.identity columns = ('ID', 'Region', 'Service Name', 'Service Type', 'Enabled', 'Interface', 'URL') - data = identity_client.endpoints.list() + kwargs = {} + if parsed_args.service: + service = common.find_service(identity_client, parsed_args.service) + kwargs['service'] = service.id + if parsed_args.interface: + kwargs['interface'] = parsed_args.interface + if parsed_args.region: + kwargs['region'] = parsed_args.region + data = identity_client.endpoints.list(**kwargs) for ep in data: service = common.find_service(identity_client, ep.service_id) diff --git a/openstackclient/tests/identity/v3/test_endpoint.py b/openstackclient/tests/identity/v3/test_endpoint.py index b90ba71904..ea05326e20 100644 --- a/openstackclient/tests/identity/v3/test_endpoint.py +++ b/openstackclient/tests/identity/v3/test_endpoint.py @@ -317,6 +317,102 @@ class TestEndpointList(TestEndpoint): ),) self.assertEqual(datalist, tuple(data)) + def test_endpoint_list_service(self): + arglist = [ + '--service', identity_fakes.service_name, + ] + verifylist = [ + ('service', identity_fakes.service_name), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # DisplayCommandBase.take_action() returns two tuples + columns, data = self.cmd.take_action(parsed_args) + + # Set expected values + kwargs = { + 'service': identity_fakes.service_id, + } + self.endpoints_mock.list.assert_called_with(**kwargs) + + collist = ('ID', 'Region', 'Service Name', 'Service Type', + 'Enabled', 'Interface', 'URL') + self.assertEqual(collist, columns) + datalist = (( + identity_fakes.endpoint_id, + identity_fakes.endpoint_region, + identity_fakes.service_name, + identity_fakes.service_type, + True, + identity_fakes.endpoint_interface, + identity_fakes.endpoint_url, + ),) + self.assertEqual(datalist, tuple(data)) + + def test_endpoint_list_interface(self): + arglist = [ + '--interface', identity_fakes.endpoint_interface, + ] + verifylist = [ + ('interface', identity_fakes.endpoint_interface), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # DisplayCommandBase.take_action() returns two tuples + columns, data = self.cmd.take_action(parsed_args) + + # Set expected values + kwargs = { + 'interface': identity_fakes.endpoint_interface, + } + self.endpoints_mock.list.assert_called_with(**kwargs) + + collist = ('ID', 'Region', 'Service Name', 'Service Type', + 'Enabled', 'Interface', 'URL') + self.assertEqual(collist, columns) + datalist = (( + identity_fakes.endpoint_id, + identity_fakes.endpoint_region, + identity_fakes.service_name, + identity_fakes.service_type, + True, + identity_fakes.endpoint_interface, + identity_fakes.endpoint_url, + ),) + self.assertEqual(datalist, tuple(data)) + + def test_endpoint_list_region(self): + arglist = [ + '--region', identity_fakes.endpoint_region, + ] + verifylist = [ + ('region', identity_fakes.endpoint_region), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + # DisplayCommandBase.take_action() returns two tuples + columns, data = self.cmd.take_action(parsed_args) + + # Set expected values + kwargs = { + 'region': identity_fakes.endpoint_region, + } + self.endpoints_mock.list.assert_called_with(**kwargs) + + collist = ('ID', 'Region', 'Service Name', 'Service Type', + 'Enabled', 'Interface', 'URL') + self.assertEqual(collist, columns) + datalist = (( + identity_fakes.endpoint_id, + identity_fakes.endpoint_region, + identity_fakes.service_name, + identity_fakes.service_type, + True, + identity_fakes.endpoint_interface, + identity_fakes.endpoint_url, + ),) + self.assertEqual(datalist, tuple(data)) + class TestEndpointSet(TestEndpoint):