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

Include '--project' and '--project-domain' filtering
options to 'port list' command.

Change-Id: Ic7deae348f737c754b2f3e2113bd76d71a442400
Closes-Bug: #1648087
This commit is contained in:
Badhmapriya Boopalan 2016-12-08 14:48:25 +00:00
parent b829b210b0
commit 780ce07459
4 changed files with 72 additions and 0 deletions

View File

@ -149,6 +149,7 @@ List ports
[--network <network>]
[--mac-address <mac-address>]
[--long]
[--project <project> [--project-domain <project-domain>]]
.. option:: --device-owner <device-owner>
@ -175,6 +176,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

@ -432,11 +432,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',
@ -473,6 +480,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 = (
@ -673,6 +676,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>`_]