summaryrefslogtreecommitdiff
path: root/openstackclient/network
diff options
context:
space:
mode:
authorRichard Theis <rtheis@us.ibm.com>2016-06-16 16:06:35 -0500
committerRichard Theis <rtheis@us.ibm.com>2016-06-20 15:42:56 -0500
commit5cc62d90b03714d65af04c73e01a89ac8d96c895 (patch)
treeae62a8d52e2434def9cbeb2140dfa879b68a8d9d /openstackclient/network
parent40004b5d80689f9f9cd802b1487f8e78830e6d4f (diff)
downloadpython-openstackclient-5cc62d90b03714d65af04c73e01a89ac8d96c895.tar.gz
Support JSON data for port binding profile
Update the "--binding-profile" option on the "port create" and "port set" commands to support both <key>=<value> and JSON input for the port custom binding profile data. The JSON input is sometimes needed to maintain the value type (e.g. integer) for more advanced data. The port custom binding profile data is unique across neutron so a custom argparse.Action class was created instead of writting a generic class in osc-lib. Change-Id: I82ac6d4f95afdc866f5282fc00d390f850f54d21 Implements: blueprint neutron-client
Diffstat (limited to 'openstackclient/network')
-rw-r--r--openstackclient/network/v2/port.py39
1 files changed, 33 insertions, 6 deletions
diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py
index 1c5db706..a25bdf8d 100644
--- a/openstackclient/network/v2/port.py
+++ b/openstackclient/network/v2/port.py
@@ -14,6 +14,7 @@
"""Port action implementations"""
import argparse
+import json
import logging
from osc_lib.cli import parseractions
@@ -63,6 +64,32 @@ def _get_columns(item):
return tuple(sorted(columns))
+class JSONKeyValueAction(argparse.Action):
+ """A custom action to parse arguments as JSON or key=value pairs
+
+ Ensures that ``dest`` is a dict
+ """
+
+ def __call__(self, parser, namespace, values, option_string=None):
+
+ # Make sure we have an empty dict rather than None
+ if getattr(namespace, self.dest, None) is None:
+ setattr(namespace, self.dest, {})
+
+ # Try to load JSON first before falling back to <key>=<value>.
+ current_dest = getattr(namespace, self.dest)
+ try:
+ current_dest.update(json.loads(values))
+ except ValueError as e:
+ if '=' in values:
+ current_dest.update([values.split('=', 1)])
+ else:
+ msg = _("Expected '<key>=<value>' or JSON data for option "
+ "%(option)s, but encountered JSON parsing error: "
+ "%(error)s") % {"option": option_string, "error": e}
+ raise argparse.ArgumentTypeError(msg)
+
+
def _get_attrs(client_manager, parsed_args):
attrs = {}
@@ -219,9 +246,9 @@ class CreatePort(command.ShowOne):
parser.add_argument(
'--binding-profile',
metavar='<binding-profile>',
- action=parseractions.KeyValueAction,
- help=_("Custom data to be passed as binding:profile: "
- "<key>=<value> "
+ action=JSONKeyValueAction,
+ help=_("Custom data to be passed as binding:profile. Data may "
+ "be passed as <key>=<value> or JSON. "
"(repeat option to set multiple binding:profile data)")
)
admin_group = parser.add_mutually_exclusive_group()
@@ -390,9 +417,9 @@ class SetPort(command.Command):
binding_profile.add_argument(
'--binding-profile',
metavar='<binding-profile>',
- action=parseractions.KeyValueAction,
- help=_("Custom data to be passed as binding:profile: "
- "<key>=<value> "
+ action=JSONKeyValueAction,
+ help=_("Custom data to be passed as binding:profile. Data may "
+ "be passed as <key>=<value> or JSON. "
"(repeat option to set multiple binding:profile data)")
)
binding_profile.add_argument(