Skip to content

Commit c7fb3b3

Browse files
committed
Add "--device-owner" option to "port list"
Add "--device-owner" option to the "port list" command to enable listing ports based on device owner. Change-Id: I0a538ec41800b9f842e86dceb6ca4180ef239c95 Implements: blueprint neutron-client
1 parent 40004b5 commit c7fb3b3

4 files changed

Lines changed: 66 additions & 4 deletions

File tree

doc/source/command-objects/port.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ Create new port
4545
4646
.. option:: --device-owner <device-owner>
4747
48-
Device owner of this port
48+
Device owner of this port. This is the entity that uses
49+
the port (for example, network:dhcp).
4950
5051
.. option:: --vnic-type <vnic-type>
5152
@@ -112,8 +113,14 @@ List ports
112113
.. code:: bash
113114
114115
os port list
116+
[--device-owner <device-owner>]
115117
[--router <router>]
116118
119+
.. option:: --device-owner <device-owner>
120+
121+
List only ports with the specified device owner. This is
122+
the entity that uses the port (for example, network:dhcp).
123+
117124
.. option:: --router <router>
118125
119126
List only ports attached to this router (name or ID)
@@ -153,7 +160,8 @@ Set port properties
153160
154161
.. option:: --device-owner <device-owner>
155162
156-
Device owner of this port
163+
Device owner of this port. This is the entity that uses
164+
the port (for example, network:dhcp).
157165
158166
.. option:: --vnic-type <vnic-type>
159167

openstackclient/network/v2/port.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ def _add_updatable_args(parser):
169169
parser.add_argument(
170170
'--device-owner',
171171
metavar='<device-owner>',
172-
help=_("Device owner of this port")
172+
help=_("Device owner of this port. This is the entity that uses "
173+
"the port (for example, network:dhcp).")
173174
)
174175
parser.add_argument(
175176
'--vnic-type',
@@ -309,6 +310,13 @@ class ListPort(command.Lister):
309310

310311
def get_parser(self, prog_name):
311312
parser = super(ListPort, self).get_parser(prog_name)
313+
parser.add_argument(
314+
'--device-owner',
315+
metavar='<device-owner>',
316+
help=_("List only ports with the specified device owner. "
317+
"This is the entity that uses the port (for example, "
318+
"network:dhcp).")
319+
)
312320
parser.add_argument(
313321
'--router',
314322
metavar='<router>',
@@ -334,10 +342,12 @@ def take_action(self, parsed_args):
334342
)
335343

336344
filters = {}
345+
if parsed_args.device_owner is not None:
346+
filters['device_owner'] = parsed_args.device_owner
337347
if parsed_args.router:
338348
_router = client.find_router(parsed_args.router,
339349
ignore_missing=False)
340-
filters = {'device_id': _router.id}
350+
filters['device_id'] = _router.id
341351

342352
data = client.ports(**filters)
343353

openstackclient/tests/network/v2/test_port.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,47 @@ def test_port_list_router_opt(self):
316316
self.assertEqual(self.columns, columns)
317317
self.assertEqual(self.data, list(data))
318318

319+
def test_port_list_device_owner_opt(self):
320+
arglist = [
321+
'--device-owner', self._ports[0].device_owner,
322+
]
323+
324+
verifylist = [
325+
('device_owner', self._ports[0].device_owner)
326+
]
327+
328+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
329+
330+
columns, data = self.cmd.take_action(parsed_args)
331+
332+
self.network.ports.assert_called_once_with(**{
333+
'device_owner': self._ports[0].device_owner
334+
})
335+
self.assertEqual(self.columns, columns)
336+
self.assertEqual(self.data, list(data))
337+
338+
def test_port_list_all_opt(self):
339+
arglist = [
340+
'--device-owner', self._ports[0].device_owner,
341+
'--router', 'fake-router-name',
342+
]
343+
344+
verifylist = [
345+
('device_owner', self._ports[0].device_owner),
346+
('router', 'fake-router-name')
347+
]
348+
349+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
350+
351+
columns, data = self.cmd.take_action(parsed_args)
352+
353+
self.network.ports.assert_called_once_with(**{
354+
'device_owner': self._ports[0].device_owner,
355+
'device_id': 'fake-router-id'
356+
})
357+
self.assertEqual(self.columns, columns)
358+
self.assertEqual(self.data, list(data))
359+
319360

320361
class TestSetPort(TestPort):
321362

releasenotes/notes/bp-neutron-client-a0552f8ca909b665.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ features:
33
- Add ``geneve`` choice to the ``network create`` command
44
``--provider-network-type`` option.
55
[Blueprint :oscbp:`neutron-client`]
6+
- Add ``--device-owner`` option to the ``port list`` command
7+
to enable listing ports based on device owner.
8+
[Blueprint :oscbp:`neutron-client`]

0 commit comments

Comments
 (0)