Support listing specified server's ports
Add new option "--server" for "port list" command to list all of the ports that are attached on the specified server. Change-Id: I8b5550ea5068405b163711303465b704b5207410 Closes-Bug: #1614385
This commit is contained in:
parent
6f326acd26
commit
96a8ed435c
@ -115,7 +115,7 @@ List ports
|
|||||||
|
|
||||||
os port list
|
os port list
|
||||||
[--device-owner <device-owner>]
|
[--device-owner <device-owner>]
|
||||||
[--router <router>]
|
[--router <router> | --server <server>]
|
||||||
|
|
||||||
.. option:: --device-owner <device-owner>
|
.. option:: --device-owner <device-owner>
|
||||||
|
|
||||||
@ -126,6 +126,10 @@ List ports
|
|||||||
|
|
||||||
List only ports attached to this router (name or ID)
|
List only ports attached to this router (name or ID)
|
||||||
|
|
||||||
|
.. option:: --server <server>
|
||||||
|
|
||||||
|
List only ports attached to this server (name or ID)
|
||||||
|
|
||||||
port set
|
port set
|
||||||
--------
|
--------
|
||||||
|
|
||||||
|
@ -345,16 +345,23 @@ class ListPort(command.Lister):
|
|||||||
"This is the entity that uses the port (for example, "
|
"This is the entity that uses the port (for example, "
|
||||||
"network:dhcp).")
|
"network:dhcp).")
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
device_group = parser.add_mutually_exclusive_group()
|
||||||
|
device_group.add_argument(
|
||||||
'--router',
|
'--router',
|
||||||
metavar='<router>',
|
metavar='<router>',
|
||||||
dest='router',
|
dest='router',
|
||||||
help=_("List only ports attached to this router (name or ID)")
|
help=_("List only ports attached to this router (name or ID)")
|
||||||
)
|
)
|
||||||
|
device_group.add_argument(
|
||||||
|
'--server',
|
||||||
|
metavar='<server>',
|
||||||
|
help=_("List only ports attached to this server (name or ID)"),
|
||||||
|
)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
client = self.app.client_manager.network
|
network_client = self.app.client_manager.network
|
||||||
|
compute_client = self.app.client_manager.compute
|
||||||
|
|
||||||
columns = (
|
columns = (
|
||||||
'id',
|
'id',
|
||||||
@ -373,11 +380,15 @@ class ListPort(command.Lister):
|
|||||||
if parsed_args.device_owner is not None:
|
if parsed_args.device_owner is not None:
|
||||||
filters['device_owner'] = parsed_args.device_owner
|
filters['device_owner'] = parsed_args.device_owner
|
||||||
if parsed_args.router:
|
if parsed_args.router:
|
||||||
_router = client.find_router(parsed_args.router,
|
_router = network_client.find_router(parsed_args.router,
|
||||||
ignore_missing=False)
|
ignore_missing=False)
|
||||||
filters['device_id'] = _router.id
|
filters['device_id'] = _router.id
|
||||||
|
if parsed_args.server:
|
||||||
|
server = utils.find_resource(compute_client.servers,
|
||||||
|
parsed_args.server)
|
||||||
|
filters['device_id'] = server.id
|
||||||
|
|
||||||
data = client.ports(**filters)
|
data = network_client.ports(**filters)
|
||||||
|
|
||||||
return (column_headers,
|
return (column_headers,
|
||||||
(utils.get_item_properties(
|
(utils.get_item_properties(
|
||||||
|
@ -19,6 +19,7 @@ from osc_lib import exceptions
|
|||||||
from osc_lib import utils
|
from osc_lib import utils
|
||||||
|
|
||||||
from openstackclient.network.v2 import port
|
from openstackclient.network.v2 import port
|
||||||
|
from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes
|
||||||
from openstackclient.tests.unit.network.v2 import fakes as network_fakes
|
from openstackclient.tests.unit.network.v2 import fakes as network_fakes
|
||||||
from openstackclient.tests.unit import utils as tests_utils
|
from openstackclient.tests.unit import utils as tests_utils
|
||||||
|
|
||||||
@ -337,6 +338,7 @@ class TestListPort(TestPort):
|
|||||||
'id': 'fake-router-id',
|
'id': 'fake-router-id',
|
||||||
})
|
})
|
||||||
self.network.find_router = mock.Mock(return_value=fake_router)
|
self.network.find_router = mock.Mock(return_value=fake_router)
|
||||||
|
self.app.client_manager.compute = mock.Mock()
|
||||||
|
|
||||||
def test_port_list_no_options(self):
|
def test_port_list_no_options(self):
|
||||||
arglist = []
|
arglist = []
|
||||||
@ -369,6 +371,26 @@ class TestListPort(TestPort):
|
|||||||
self.assertEqual(self.columns, columns)
|
self.assertEqual(self.columns, columns)
|
||||||
self.assertEqual(self.data, list(data))
|
self.assertEqual(self.data, list(data))
|
||||||
|
|
||||||
|
@mock.patch.object(utils, 'find_resource')
|
||||||
|
def test_port_list_with_server_option(self, mock_find):
|
||||||
|
fake_server = compute_fakes.FakeServer.create_one_server()
|
||||||
|
mock_find.return_value = fake_server
|
||||||
|
|
||||||
|
arglist = [
|
||||||
|
'--server', 'fake-server-name',
|
||||||
|
]
|
||||||
|
verifylist = [
|
||||||
|
('server', 'fake-server-name'),
|
||||||
|
]
|
||||||
|
|
||||||
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||||
|
columns, data = self.cmd.take_action(parsed_args)
|
||||||
|
self.network.ports.assert_called_once_with(
|
||||||
|
device_id=fake_server.id)
|
||||||
|
mock_find.assert_called_once_with(mock.ANY, 'fake-server-name')
|
||||||
|
self.assertEqual(self.columns, columns)
|
||||||
|
self.assertEqual(self.data, list(data))
|
||||||
|
|
||||||
def test_port_list_device_owner_opt(self):
|
def test_port_list_device_owner_opt(self):
|
||||||
arglist = [
|
arglist = [
|
||||||
'--device-owner', self._ports[0].device_owner,
|
'--device-owner', self._ports[0].device_owner,
|
||||||
|
5
releasenotes/notes/bug-1614385-460b5034ba372463.yaml
Normal file
5
releasenotes/notes/bug-1614385-460b5034ba372463.yaml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
features:
|
||||||
|
- Support listing the specified server's ports by new option ``--server`` of
|
||||||
|
``port list`` command.
|
||||||
|
[Bug `1614385 <https://bugs.launchpad.net/python-openstackclient/+bug/1614385>`_]
|
Loading…
Reference in New Issue
Block a user