summaryrefslogtreecommitdiff
path: root/openstackclient/network/common.py
diff options
context:
space:
mode:
authorSlawek Kaplonski <skaplons@redhat.com>2020-12-22 15:31:44 +0100
committerSlawek Kaplonski <skaplons@redhat.com>2021-05-26 09:29:15 +0200
commitb26b7f3440d4f756c0b7906b93751d7e83a733f7 (patch)
treee318e4700f87222b87a4e677e2f7f4dbcf518fae /openstackclient/network/common.py
parent6bdf030953d449693c97bff8812b7ced981a2015 (diff)
downloadpython-openstackclient-b26b7f3440d4f756c0b7906b93751d7e83a733f7.tar.gz
Allow to send extra attributes in Neutron related commands
To deprecate and drop support for neutronclient CLI and use only OSC we need feature parity between OSC and neutronclient. Last missing piece here is possibility to send in POST/PUT requests unknown parameters to the Neutron server. This patch adds such possibility to the OSC. Change-Id: Iba09297c2be9fb9fa0be1b3dc65755277b79230e
Diffstat (limited to 'openstackclient/network/common.py')
-rw-r--r--openstackclient/network/common.py74
1 files changed, 73 insertions, 1 deletions
diff --git a/openstackclient/network/common.py b/openstackclient/network/common.py
index 47ffbe77..b1902a6c 100644
--- a/openstackclient/network/common.py
+++ b/openstackclient/network/common.py
@@ -16,10 +16,12 @@ import contextlib
import logging
import openstack.exceptions
+from osc_lib.cli import parseractions
from osc_lib.command import command
from osc_lib import exceptions
from openstackclient.i18n import _
+from openstackclient.network import utils
LOG = logging.getLogger(__name__)
@@ -75,7 +77,6 @@ class NetDetectionMixin(metaclass=abc.ABCMeta):
"""
# Have we set it up yet for this command?
if not hasattr(self, '_net_type'):
- # import pdb; pdb.set_trace()
try:
if self.app.client_manager.is_network_endpoint_enabled():
net_type = _NET_TYPE_NEUTRON
@@ -255,3 +256,74 @@ class NetworkAndComputeShowOne(NetDetectionMixin, command.ShowOne,
if exc.details:
msg += ", " + str(exc.details)
raise exceptions.CommandError(msg)
+
+
+class NeutronCommandWithExtraArgs(command.Command):
+ """Create and Update commands with additional extra properties.
+
+ Extra properties can be passed to the command and are then send to the
+ Neutron as given to the command.
+ """
+
+ # dict of allowed types
+ _allowed_types_dict = {
+ 'bool': utils.str2bool,
+ 'dict': utils.str2dict,
+ 'list': utils.str2list,
+ 'int': int,
+ 'str': str,
+ }
+
+ def _get_property_converter(self, _property):
+ if 'type' not in _property:
+ converter = str
+ else:
+ converter = self._allowed_types_dict.get(_property['type'])
+
+ if not converter:
+ raise exceptions.CommandError(
+ _("Type {property_type} of property {name} "
+ "is not supported").format(
+ property_type=_property['type'],
+ name=_property['name']))
+ return converter
+
+ def _parse_extra_properties(self, extra_properties):
+ result = {}
+ if extra_properties:
+ for _property in extra_properties:
+ converter = self._get_property_converter(_property)
+ result[_property['name']] = converter(_property['value'])
+ return result
+
+ def get_parser(self, prog_name):
+ parser = super(NeutronCommandWithExtraArgs, self).get_parser(prog_name)
+ parser.add_argument(
+ '--extra-property',
+ metavar='type=<property_type>,name=<property_name>,'
+ 'value=<property_value>',
+ dest='extra_properties',
+ action=parseractions.MultiKeyValueAction,
+ required_keys=['name', 'value'],
+ optional_keys=['type'],
+ help=_("Additional parameters can be passed using this property. "
+ "Default type of the extra property is string ('str'), but "
+ "other types can be used as well. Available types are: "
+ "'dict', 'list', 'str', 'bool', 'int'. "
+ "In case of 'list' type, 'value' can be "
+ "semicolon-separated list of values. "
+ "For 'dict' value is semicolon-separated list of the "
+ "key:value pairs.")
+ )
+ return parser
+
+
+class NeutronUnsetCommandWithExtraArgs(NeutronCommandWithExtraArgs):
+
+ def _parse_extra_properties(self, extra_properties):
+ result = {}
+ if extra_properties:
+ for _property in extra_properties:
+ result[_property['name']] = None
+
+ return result