summaryrefslogtreecommitdiff
path: root/openstackclient/network/v2
diff options
context:
space:
mode:
authorYan Xing'an <yanxingan@cmss.chinamobile.com>2016-10-19 02:21:41 -0700
committerYan Xing'an <yanxingan@cmss.chinamobile.com>2017-02-07 19:00:52 -0800
commite0e46bca093984926de11559e312d1f1fcade108 (patch)
tree9795751911ba901e4089c010ec99336f44cf6cb0 /openstackclient/network/v2
parente35c97a4fc323f9771908566656c172ba4bbd340 (diff)
downloadpython-openstackclient-e0e46bca093984926de11559e312d1f1fcade108.tar.gz
Add --fixed-ip option to the port list command
Add support to allow filtering ports via --fixed-ip option to the port list command. Change-Id: I2f728368d3046b2e6feadd0848bf6f8680e31aba Partial-bug: #1634799 Partially-Implements: blueprint network-commands-options
Diffstat (limited to 'openstackclient/network/v2')
-rw-r--r--openstackclient/network/v2/port.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py
index c9de47e2..7283dc07 100644
--- a/openstackclient/network/v2/port.py
+++ b/openstackclient/network/v2/port.py
@@ -194,6 +194,29 @@ def _prepare_fixed_ips(client_manager, parsed_args):
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):
parser.add_argument(
'--description',
@@ -466,6 +489,15 @@ class ListPort(command.Lister):
help=_("List ports according to their project (name or ID)")
)
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
def take_action(self, parsed_args):
@@ -516,6 +548,9 @@ class ListPort(command.Lister):
).id
filters['tenant_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)