From ce079d22617f3129bfdf5165b9f124d7cc7dbc2e Mon Sep 17 00:00:00 2001 From: Nam Nguyen Hoai Date: Tue, 16 Aug 2016 16:00:32 +0700 Subject: [PATCH] Add '--project' and '--project-domain' options to os cmds This patch added '--project' and '--project-domain' options to filter subnets resulted by 'os subnet list', 'os floating ip create' and 'os security group list' commands. Co-Authored-By: Ha Van Tu Change-Id: I727663d49ffa6aa042fdeb60797f18bb753b0372 Closes-Bug: #1613231 Closes-Bug: #1610909 Closes-Bug: #1613629 Partially-Implements: blueprint network-commands-options --- doc/source/command-objects/floating-ip.rst | 14 ++++++ doc/source/command-objects/router.rst | 10 ++++ doc/source/command-objects/security-group.rst | 14 ++++++ openstackclient/network/v2/floating_ip.py | 16 ++++++ openstackclient/network/v2/router.py | 15 +++++- openstackclient/network/v2/security_group.py | 18 ++++++- .../tests/unit/network/v2/test_floating_ip.py | 50 +++++++++++++++++++ .../tests/unit/network/v2/test_router.py | 41 +++++++++++++++ .../unit/network/v2/test_security_group.py | 38 ++++++++++++++ .../notes/bug-1613231-386b2b1373662052.yaml | 8 +++ 10 files changed, 222 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/bug-1613231-386b2b1373662052.yaml diff --git a/doc/source/command-objects/floating-ip.rst b/doc/source/command-objects/floating-ip.rst index b2cc8af0ef..6a5e38b063 100644 --- a/doc/source/command-objects/floating-ip.rst +++ b/doc/source/command-objects/floating-ip.rst @@ -18,6 +18,7 @@ Create floating IP [--floating-ip-address ] [--fixed-ip-address ] [--description ] + [--project [--project-domain ]] .. option:: --subnet @@ -45,6 +46,19 @@ Create floating IP Set floating IP description *Network version 2 only* +.. option:: --project + + Owner's project (name or ID) + + *Network version 2 only* + +.. option:: --project-domain + + Domain the project belongs to (name or ID). + This can be used in case collisions between project names exist. + + *Network version 2 only* + .. describe:: Network to allocate floating IP from (name or ID) diff --git a/doc/source/command-objects/router.rst b/doc/source/command-objects/router.rst index 56b95ffa68..059d1a3fe0 100644 --- a/doc/source/command-objects/router.rst +++ b/doc/source/command-objects/router.rst @@ -137,6 +137,7 @@ List routers [--name ] [--enable | --disable] [--long] + [--project [--project-domain ]] .. option:: --long @@ -154,6 +155,15 @@ List routers List disabled routers +.. option:: --project + + List routers according to their project (name or ID) + +.. option:: --project-domain + + Domain the project belongs to (name or ID). + This can be used in case collisions between project names exist. + router remove port ------------------ diff --git a/doc/source/command-objects/security-group.rst b/doc/source/command-objects/security-group.rst index ba054554df..5157f53096 100644 --- a/doc/source/command-objects/security-group.rst +++ b/doc/source/command-objects/security-group.rst @@ -67,6 +67,7 @@ List security groups os security group list [--all-projects] + [--project [--project-domain ]] .. option:: --all-projects @@ -75,6 +76,19 @@ List security groups *Network version 2 ignores this option and will always display information* *for all projects (admin only).* +.. option:: --project + + List security groups according to the project (name or ID) + + *Network version 2 only* + +.. option:: --project-domain + + Domain the project belongs to (name or ID). + This can be used in case collisions between project names exist. + + *Network version 2 only* + security group set ------------------ diff --git a/openstackclient/network/v2/floating_ip.py b/openstackclient/network/v2/floating_ip.py index c787cd2f9d..7b8374e2ae 100644 --- a/openstackclient/network/v2/floating_ip.py +++ b/openstackclient/network/v2/floating_ip.py @@ -18,6 +18,7 @@ import logging from osc_lib import utils from openstackclient.i18n import _ +from openstackclient.identity import common as identity_common from openstackclient.network import common from openstackclient.network import sdk_utils @@ -66,6 +67,15 @@ def _get_attrs(client_manager, parsed_args): if parsed_args.description is not None: attrs['description'] = parsed_args.description + if parsed_args.project: + identity_client = client_manager.identity + project_id = identity_common.find_project( + identity_client, + parsed_args.project, + parsed_args.project_domain, + ).id + attrs['tenant_id'] = project_id + return attrs @@ -113,6 +123,12 @@ class CreateFloatingIP(common.NetworkAndComputeShowOne): metavar='', help=_('Set floating IP description') ) + parser.add_argument( + '--project', + metavar='', + help=_("Owner's project (name or ID)") + ) + identity_common.add_project_domain_option_to_parser(parser) return parser def take_action_network(self, client, parsed_args): diff --git a/openstackclient/network/v2/router.py b/openstackclient/network/v2/router.py index cbd412b585..50af80bb47 100644 --- a/openstackclient/network/v2/router.py +++ b/openstackclient/network/v2/router.py @@ -282,11 +282,17 @@ class ListRouter(command.Lister): default=False, help=_("List additional fields in output") ) + parser.add_argument( + '--project', + metavar='', + help=_("List routers according to their project (name or ID)") + ) + identity_common.add_project_domain_option_to_parser(parser) return parser def take_action(self, parsed_args): + identity_client = self.app.client_manager.identity client = self.app.client_manager.network - columns = ( 'id', 'name', @@ -316,6 +322,13 @@ class ListRouter(command.Lister): elif parsed_args.disable: args['admin_state_up'] = False + if parsed_args.project: + project_id = identity_common.find_project( + identity_client, + parsed_args.project, + parsed_args.project_domain, + ).id + args['tenant_id'] = project_id if parsed_args.long: columns = columns + ( 'routes', diff --git a/openstackclient/network/v2/security_group.py b/openstackclient/network/v2/security_group.py index 554dd61d6f..a02d73bbe5 100644 --- a/openstackclient/network/v2/security_group.py +++ b/openstackclient/network/v2/security_group.py @@ -201,6 +201,13 @@ class ListSecurityGroup(common.NetworkAndComputeLister): default=False, help=argparse.SUPPRESS, ) + parser.add_argument( + '--project', + metavar='', + help=_("List security groups according to the project " + "(name or ID)") + ) + identity_common.add_project_domain_option_to_parser(parser) return parser def update_parser_compute(self, parser): @@ -228,7 +235,16 @@ class ListSecurityGroup(common.NetworkAndComputeLister): ) for s in data)) def take_action_network(self, client, parsed_args): - return self._get_return_data(client.security_groups()) + filters = {} + if parsed_args.project: + identity_client = self.app.client_manager.identity + project_id = identity_common.find_project( + identity_client, + parsed_args.project, + parsed_args.project_domain, + ).id + filters['tenant_id'] = project_id + return self._get_return_data(client.security_groups(**filters)) def take_action_compute(self, client, parsed_args): search = {'all_tenants': parsed_args.all_projects} diff --git a/openstackclient/tests/unit/network/v2/test_floating_ip.py b/openstackclient/tests/unit/network/v2/test_floating_ip.py index 10f3067d9a..b3d211ba34 100644 --- a/openstackclient/tests/unit/network/v2/test_floating_ip.py +++ b/openstackclient/tests/unit/network/v2/test_floating_ip.py @@ -18,6 +18,7 @@ from osc_lib import exceptions from openstackclient.network.v2 import floating_ip from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes +from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes_v3 from openstackclient.tests.unit.network.v2 import fakes as network_fakes from openstackclient.tests.unit import utils as tests_utils @@ -31,6 +32,7 @@ class TestFloatingIPNetwork(network_fakes.TestNetworkV2): # Get a shortcut to the network client self.network = self.app.client_manager.network + self.projects_mock = self.app.client_manager.identity.projects class TestCreateFloatingIPNetwork(TestFloatingIPNetwork): @@ -145,6 +147,54 @@ class TestCreateFloatingIPNetwork(TestFloatingIPNetwork): self.assertEqual(self.columns, columns) self.assertEqual(self.data, data) + def test_floating_ip_create_project(self): + project = identity_fakes_v3.FakeProject.create_one_project() + self.projects_mock.get.return_value = project + arglist = [ + '--project', project.id, + self.floating_ip.floating_network_id, + ] + verifylist = [ + ('network', self.floating_ip.floating_network_id), + ('project', project.id), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.create_ip.assert_called_once_with(**{ + 'floating_network_id': self.floating_ip.floating_network_id, + 'tenant_id': project.id, + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) + + def test_floating_ip_create_project_domain(self): + project = identity_fakes_v3.FakeProject.create_one_project() + domain = identity_fakes_v3.FakeDomain.create_one_domain() + self.projects_mock.get.return_value = project + arglist = [ + "--project", project.name, + "--project-domain", domain.name, + self.floating_ip.floating_network_id, + ] + verifylist = [ + ('network', self.floating_ip.floating_network_id), + ('project', project.name), + ('project_domain', domain.name), + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.create_ip.assert_called_once_with(**{ + 'floating_network_id': self.floating_ip.floating_network_id, + 'tenant_id': project.id, + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, data) + class TestDeleteFloatingIPNetwork(TestFloatingIPNetwork): diff --git a/openstackclient/tests/unit/network/v2/test_router.py b/openstackclient/tests/unit/network/v2/test_router.py index 24984e47b0..b0409447dd 100644 --- a/openstackclient/tests/unit/network/v2/test_router.py +++ b/openstackclient/tests/unit/network/v2/test_router.py @@ -18,6 +18,7 @@ from osc_lib import exceptions from osc_lib import utils as osc_utils from openstackclient.network.v2 import router +from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes_v3 from openstackclient.tests.unit.network.v2 import fakes as network_fakes from openstackclient.tests.unit import utils as tests_utils @@ -29,6 +30,7 @@ class TestRouter(network_fakes.TestNetworkV2): # Get a shortcut to the network client self.network = self.app.client_manager.network + self.projects_mock = self.app.client_manager.identity.projects class TestAddPortToRouter(TestRouter): @@ -476,6 +478,45 @@ class TestListRouter(TestRouter): self.network.routers.assert_called_once_with( **{'admin_state_up': False} ) + + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_router_list_project(self): + project = identity_fakes_v3.FakeProject.create_one_project() + self.projects_mock.get.return_value = project + arglist = [ + '--project', project.id, + ] + verifylist = [ + ('project', project.id), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + filters = {'tenant_id': project.id} + + self.network.routers.assert_called_once_with(**filters) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_router_list_project_domain(self): + project = identity_fakes_v3.FakeProject.create_one_project() + self.projects_mock.get.return_value = project + arglist = [ + '--project', project.id, + '--project-domain', project.domain_id, + ] + verifylist = [ + ('project', project.id), + ('project_domain', project.domain_id), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + filters = {'tenant_id': project.id} + + self.network.routers.assert_called_once_with(**filters) self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) diff --git a/openstackclient/tests/unit/network/v2/test_security_group.py b/openstackclient/tests/unit/network/v2/test_security_group.py index 2615b77a26..43aa07ccbe 100644 --- a/openstackclient/tests/unit/network/v2/test_security_group.py +++ b/openstackclient/tests/unit/network/v2/test_security_group.py @@ -444,6 +444,44 @@ class TestListSecurityGroupNetwork(TestSecurityGroupNetwork): self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) + def test_security_group_list_project(self): + project = identity_fakes.FakeProject.create_one_project() + self.projects_mock.get.return_value = project + arglist = [ + '--project', project.id, + ] + verifylist = [ + ('project', project.id), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + filters = {'tenant_id': project.id} + + self.network.security_groups.assert_called_once_with(**filters) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_security_group_list_project_domain(self): + project = identity_fakes.FakeProject.create_one_project() + self.projects_mock.get.return_value = project + arglist = [ + '--project', project.id, + '--project-domain', project.domain_id, + ] + verifylist = [ + ('project', project.id), + ('project_domain', project.domain_id), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + filters = {'tenant_id': project.id} + + self.network.security_groups.assert_called_once_with(**filters) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + class TestListSecurityGroupCompute(TestSecurityGroupCompute): diff --git a/releasenotes/notes/bug-1613231-386b2b1373662052.yaml b/releasenotes/notes/bug-1613231-386b2b1373662052.yaml new file mode 100644 index 0000000000..a55af0dd4c --- /dev/null +++ b/releasenotes/notes/bug-1613231-386b2b1373662052.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + Add ``--project`` and ``--project-domain`` options to the ``router list``, + ``floating ip create`` and ``security group list`` commands. + [Bug `1613231 `_] + [Bug `1613629 `_] + [Bug `1610909 `_]