Merge "To support '--project' and '--project-domain' options"

This commit is contained in:
Jenkins 2017-01-26 01:51:19 +00:00 committed by Gerrit Code Review
commit a05e2762dc
4 changed files with 72 additions and 0 deletions

View File

@ -156,6 +156,7 @@ List ports
[--network <network>]
[--mac-address <mac-address>]
[--long]
[--project <project> [--project-domain <project-domain>]]
.. option:: --device-owner <device-owner>
@ -182,6 +183,15 @@ List ports
List additional fields in output
.. option:: --project <project>
List ports according to their project (name or ID)
.. option:: --project-domain <project-domain>
Domain the project belongs to (name or ID).
This can be used in case collisions between project names exist.
port set
--------

View File

@ -460,11 +460,18 @@ class ListPort(command.Lister):
default=False,
help=_("List additional fields in output")
)
parser.add_argument(
'--project',
metavar='<project>',
help=_("List ports according to their project (name or ID)")
)
identity_common.add_project_domain_option_to_parser(parser)
return parser
def take_action(self, parsed_args):
network_client = self.app.client_manager.network
compute_client = self.app.client_manager.compute
identity_client = self.app.client_manager.identity
columns = (
'id',
@ -501,6 +508,14 @@ class ListPort(command.Lister):
filters['network_id'] = network.id
if parsed_args.mac_address:
filters['mac_address'] = parsed_args.mac_address
if parsed_args.project:
project_id = identity_common.find_project(
identity_client,
parsed_args.project,
parsed_args.project_domain,
).id
filters['tenant_id'] = project_id
filters['project_id'] = project_id
data = network_client.ports(**filters)

View File

@ -20,6 +20,7 @@ from osc_lib import utils
from openstackclient.network.v2 import port
from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes
from openstackclient.tests.unit.identity.v3 import fakes as identity_fakes
from openstackclient.tests.unit.network.v2 import fakes as network_fakes
from openstackclient.tests.unit import utils as tests_utils
@ -31,6 +32,8 @@ class TestPort(network_fakes.TestNetworkV2):
# Get a shortcut to the network client
self.network = self.app.client_manager.network
# Get a shortcut to the ProjectManager Mock
self.projects_mock = self.app.client_manager.identity.projects
def _get_common_cols_data(self, fake_port):
columns = (
@ -741,6 +744,44 @@ class TestListPort(TestPort):
self.assertEqual(self.columns_long, columns)
self.assertEqual(self.data_long, list(data))
def test_port_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, 'project_id': project.id}
self.network.ports.assert_called_once_with(**filters)
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data))
def test_port_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, 'project_id': project.id}
self.network.ports.assert_called_once_with(**filters)
self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data))
class TestSetPort(TestPort):

View File

@ -0,0 +1,6 @@
---
features:
- |
Add ``--project`` and ``--project-domain`` filtering options
to ``port list`` command.
[Bug `1648087 <https://bugs.launchpad.net/bugs/1648087>`_]