Merge "Add --fixed-ip option to the port list command"
This commit is contained in:
commit
803d5b4c83
@ -155,6 +155,7 @@ List ports
|
|||||||
[--router <router> | --server <server>]
|
[--router <router> | --server <server>]
|
||||||
[--network <network>]
|
[--network <network>]
|
||||||
[--mac-address <mac-address>]
|
[--mac-address <mac-address>]
|
||||||
|
[--fixed-ip subnet=<subnet>,ip-address=<ip-address>]
|
||||||
[--long]
|
[--long]
|
||||||
[--project <project> [--project-domain <project-domain>]]
|
[--project <project> [--project-domain <project-domain>]]
|
||||||
|
|
||||||
@ -179,6 +180,12 @@ List ports
|
|||||||
|
|
||||||
List only ports with this MAC address
|
List only ports with this MAC address
|
||||||
|
|
||||||
|
.. option:: --fixed-ip subnet=<subnet>,ip-address=<ip-address>
|
||||||
|
|
||||||
|
Desired IP and/or subnet (name or ID) for filtering ports:
|
||||||
|
subnet=<subnet>,ip-address=<ip-address>
|
||||||
|
(repeat option to set multiple fixed IP addresses)
|
||||||
|
|
||||||
.. option:: --long
|
.. option:: --long
|
||||||
|
|
||||||
List additional fields in output
|
List additional fields in output
|
||||||
|
@ -194,6 +194,29 @@ def _prepare_fixed_ips(client_manager, parsed_args):
|
|||||||
parsed_args.fixed_ip = ips
|
parsed_args.fixed_ip = ips
|
||||||
|
|
||||||
|
|
||||||
|
def _prepare_filter_fixed_ips(client_manager, parsed_args):
|
||||||
|
"""Fix and properly format fixed_ip option for filtering.
|
||||||
|
|
||||||
|
Appropriately convert any subnet names to their respective ids.
|
||||||
|
Convert fixed_ips in parsed args to be in valid list format for filter:
|
||||||
|
['subnet_id=foo'].
|
||||||
|
"""
|
||||||
|
client = client_manager.network
|
||||||
|
ips = []
|
||||||
|
|
||||||
|
for ip_spec in parsed_args.fixed_ip:
|
||||||
|
if 'subnet' in ip_spec:
|
||||||
|
subnet_name_id = ip_spec['subnet']
|
||||||
|
if subnet_name_id:
|
||||||
|
_subnet = client.find_subnet(subnet_name_id,
|
||||||
|
ignore_missing=False)
|
||||||
|
ips.append('subnet_id=%s' % _subnet.id)
|
||||||
|
|
||||||
|
if 'ip-address' in ip_spec:
|
||||||
|
ips.append('ip_address=%s' % ip_spec['ip-address'])
|
||||||
|
return ips
|
||||||
|
|
||||||
|
|
||||||
def _add_updatable_args(parser):
|
def _add_updatable_args(parser):
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--description',
|
'--description',
|
||||||
@ -466,6 +489,15 @@ class ListPort(command.Lister):
|
|||||||
help=_("List ports according to their project (name or ID)")
|
help=_("List ports according to their project (name or ID)")
|
||||||
)
|
)
|
||||||
identity_common.add_project_domain_option_to_parser(parser)
|
identity_common.add_project_domain_option_to_parser(parser)
|
||||||
|
parser.add_argument(
|
||||||
|
'--fixed-ip',
|
||||||
|
metavar='subnet=<subnet>,ip-address=<ip-address>',
|
||||||
|
action=parseractions.MultiKeyValueAction,
|
||||||
|
optional_keys=['subnet', 'ip-address'],
|
||||||
|
help=_("Desired IP and/or subnet (name or ID) for filtering "
|
||||||
|
"ports: subnet=<subnet>,ip-address=<ip-address> "
|
||||||
|
"(repeat option to set multiple fixed IP addresses)")
|
||||||
|
)
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def take_action(self, parsed_args):
|
def take_action(self, parsed_args):
|
||||||
@ -516,6 +548,9 @@ class ListPort(command.Lister):
|
|||||||
).id
|
).id
|
||||||
filters['tenant_id'] = project_id
|
filters['tenant_id'] = project_id
|
||||||
filters['project_id'] = project_id
|
filters['project_id'] = project_id
|
||||||
|
if parsed_args.fixed_ip:
|
||||||
|
filters['fixed_ips'] = _prepare_filter_fixed_ips(
|
||||||
|
self.app.client_manager, parsed_args)
|
||||||
|
|
||||||
data = network_client.ports(**filters)
|
data = network_client.ports(**filters)
|
||||||
|
|
||||||
|
@ -470,7 +470,8 @@ class FakePort(object):
|
|||||||
'dns_assignment': [{}],
|
'dns_assignment': [{}],
|
||||||
'dns_name': 'dns-name-' + uuid.uuid4().hex,
|
'dns_name': 'dns-name-' + uuid.uuid4().hex,
|
||||||
'extra_dhcp_opts': [{}],
|
'extra_dhcp_opts': [{}],
|
||||||
'fixed_ips': [{}],
|
'fixed_ips': [{'ip_address': '10.0.0.3',
|
||||||
|
'subnet_id': 'subnet-id-' + uuid.uuid4().hex}],
|
||||||
'id': 'port-id-' + uuid.uuid4().hex,
|
'id': 'port-id-' + uuid.uuid4().hex,
|
||||||
'mac_address': 'fa:16:3e:a9:4e:72',
|
'mac_address': 'fa:16:3e:a9:4e:72',
|
||||||
'name': 'port-name-' + uuid.uuid4().hex,
|
'name': 'port-name-' + uuid.uuid4().hex,
|
||||||
|
@ -727,6 +727,92 @@ 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))
|
||||||
|
|
||||||
|
def test_port_list_fixed_ip_opt_ip_address(self):
|
||||||
|
ip_address = self._ports[0].fixed_ips[0]['ip_address']
|
||||||
|
arglist = [
|
||||||
|
'--fixed-ip', "ip-address=%s" % ip_address,
|
||||||
|
]
|
||||||
|
verifylist = [
|
||||||
|
('fixed_ip', [{'ip-address': ip_address}])
|
||||||
|
]
|
||||||
|
|
||||||
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||||
|
|
||||||
|
columns, data = self.cmd.take_action(parsed_args)
|
||||||
|
|
||||||
|
self.network.ports.assert_called_once_with(**{
|
||||||
|
'fixed_ips': ['ip_address=%s' % ip_address]})
|
||||||
|
self.assertEqual(self.columns, columns)
|
||||||
|
self.assertEqual(self.data, list(data))
|
||||||
|
|
||||||
|
def test_port_list_fixed_ip_opt_subnet_id(self):
|
||||||
|
subnet_id = self._ports[0].fixed_ips[0]['subnet_id']
|
||||||
|
arglist = [
|
||||||
|
'--fixed-ip', "subnet=%s" % subnet_id,
|
||||||
|
]
|
||||||
|
verifylist = [
|
||||||
|
('fixed_ip', [{'subnet': subnet_id}])
|
||||||
|
]
|
||||||
|
|
||||||
|
self.fake_subnet = network_fakes.FakeSubnet.create_one_subnet(
|
||||||
|
{'id': subnet_id})
|
||||||
|
self.network.find_subnet = mock.Mock(return_value=self.fake_subnet)
|
||||||
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||||
|
columns, data = self.cmd.take_action(parsed_args)
|
||||||
|
|
||||||
|
self.network.ports.assert_called_once_with(**{
|
||||||
|
'fixed_ips': ['subnet_id=%s' % subnet_id]})
|
||||||
|
self.assertEqual(self.columns, columns)
|
||||||
|
self.assertEqual(self.data, list(data))
|
||||||
|
|
||||||
|
def test_port_list_fixed_ip_opts(self):
|
||||||
|
subnet_id = self._ports[0].fixed_ips[0]['subnet_id']
|
||||||
|
ip_address = self._ports[0].fixed_ips[0]['ip_address']
|
||||||
|
arglist = [
|
||||||
|
'--fixed-ip', "subnet=%s,ip-address=%s" % (subnet_id,
|
||||||
|
ip_address)
|
||||||
|
]
|
||||||
|
verifylist = [
|
||||||
|
('fixed_ip', [{'subnet': subnet_id,
|
||||||
|
'ip-address': ip_address}])
|
||||||
|
]
|
||||||
|
|
||||||
|
self.fake_subnet = network_fakes.FakeSubnet.create_one_subnet(
|
||||||
|
{'id': subnet_id})
|
||||||
|
self.network.find_subnet = mock.Mock(return_value=self.fake_subnet)
|
||||||
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||||
|
columns, data = self.cmd.take_action(parsed_args)
|
||||||
|
|
||||||
|
self.network.ports.assert_called_once_with(**{
|
||||||
|
'fixed_ips': ['subnet_id=%s' % subnet_id,
|
||||||
|
'ip_address=%s' % ip_address]})
|
||||||
|
self.assertEqual(self.columns, columns)
|
||||||
|
self.assertEqual(self.data, list(data))
|
||||||
|
|
||||||
|
def test_port_list_fixed_ips(self):
|
||||||
|
subnet_id = self._ports[0].fixed_ips[0]['subnet_id']
|
||||||
|
ip_address = self._ports[0].fixed_ips[0]['ip_address']
|
||||||
|
arglist = [
|
||||||
|
'--fixed-ip', "subnet=%s" % subnet_id,
|
||||||
|
'--fixed-ip', "ip-address=%s" % ip_address,
|
||||||
|
]
|
||||||
|
verifylist = [
|
||||||
|
('fixed_ip', [{'subnet': subnet_id},
|
||||||
|
{'ip-address': ip_address}])
|
||||||
|
]
|
||||||
|
|
||||||
|
self.fake_subnet = network_fakes.FakeSubnet.create_one_subnet(
|
||||||
|
{'id': subnet_id})
|
||||||
|
self.network.find_subnet = mock.Mock(return_value=self.fake_subnet)
|
||||||
|
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||||
|
columns, data = self.cmd.take_action(parsed_args)
|
||||||
|
|
||||||
|
self.network.ports.assert_called_once_with(**{
|
||||||
|
'fixed_ips': ['subnet_id=%s' % subnet_id,
|
||||||
|
'ip_address=%s' % ip_address]})
|
||||||
|
self.assertEqual(self.columns, columns)
|
||||||
|
self.assertEqual(self.data, list(data))
|
||||||
|
|
||||||
def test_list_port_with_long(self):
|
def test_list_port_with_long(self):
|
||||||
arglist = [
|
arglist = [
|
||||||
'--long',
|
'--long',
|
||||||
@ -801,6 +887,7 @@ class TestSetPort(TestPort):
|
|||||||
arglist = [
|
arglist = [
|
||||||
'--fixed-ip', 'ip-address=10.0.0.11',
|
'--fixed-ip', 'ip-address=10.0.0.11',
|
||||||
self._port.name,
|
self._port.name,
|
||||||
|
'--no-fixed-ip',
|
||||||
]
|
]
|
||||||
verifylist = [
|
verifylist = [
|
||||||
('fixed_ip', [{'ip-address': '10.0.0.11'}]),
|
('fixed_ip', [{'ip-address': '10.0.0.11'}]),
|
||||||
|
5
releasenotes/notes/bug-1634799-1322227c9b0188ca.yaml
Normal file
5
releasenotes/notes/bug-1634799-1322227c9b0188ca.yaml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
features:
|
||||||
|
- |
|
||||||
|
Add ``--fixed-ip`` option to the ``port list`` command.
|
||||||
|
[Bug `1634799 <https://bugs.launchpad.net/bugs/1634799>`_]
|
Loading…
Reference in New Issue
Block a user