|
16 | 16 | import logging |
17 | 17 |
|
18 | 18 | import openstack.exceptions |
| 19 | +from osc_lib.cli import parseractions |
19 | 20 | from osc_lib.command import command |
20 | 21 | from osc_lib import exceptions |
21 | 22 |
|
22 | 23 | from openstackclient.i18n import _ |
| 24 | +from openstackclient.network import utils |
23 | 25 |
|
24 | 26 |
|
25 | 27 | LOG = logging.getLogger(__name__) |
@@ -75,7 +77,6 @@ def _network_type(self): |
75 | 77 | """ |
76 | 78 | # Have we set it up yet for this command? |
77 | 79 | if not hasattr(self, '_net_type'): |
78 | | - # import pdb; pdb.set_trace() |
79 | 80 | try: |
80 | 81 | if self.app.client_manager.is_network_endpoint_enabled(): |
81 | 82 | net_type = _NET_TYPE_NEUTRON |
@@ -255,3 +256,74 @@ def take_action(self, parsed_args): |
255 | 256 | if exc.details: |
256 | 257 | msg += ", " + str(exc.details) |
257 | 258 | raise exceptions.CommandError(msg) |
| 259 | + |
| 260 | + |
| 261 | +class NeutronCommandWithExtraArgs(command.Command): |
| 262 | + """Create and Update commands with additional extra properties. |
| 263 | +
|
| 264 | + Extra properties can be passed to the command and are then send to the |
| 265 | + Neutron as given to the command. |
| 266 | + """ |
| 267 | + |
| 268 | + # dict of allowed types |
| 269 | + _allowed_types_dict = { |
| 270 | + 'bool': utils.str2bool, |
| 271 | + 'dict': utils.str2dict, |
| 272 | + 'list': utils.str2list, |
| 273 | + 'int': int, |
| 274 | + 'str': str, |
| 275 | + } |
| 276 | + |
| 277 | + def _get_property_converter(self, _property): |
| 278 | + if 'type' not in _property: |
| 279 | + converter = str |
| 280 | + else: |
| 281 | + converter = self._allowed_types_dict.get(_property['type']) |
| 282 | + |
| 283 | + if not converter: |
| 284 | + raise exceptions.CommandError( |
| 285 | + _("Type {property_type} of property {name} " |
| 286 | + "is not supported").format( |
| 287 | + property_type=_property['type'], |
| 288 | + name=_property['name'])) |
| 289 | + return converter |
| 290 | + |
| 291 | + def _parse_extra_properties(self, extra_properties): |
| 292 | + result = {} |
| 293 | + if extra_properties: |
| 294 | + for _property in extra_properties: |
| 295 | + converter = self._get_property_converter(_property) |
| 296 | + result[_property['name']] = converter(_property['value']) |
| 297 | + return result |
| 298 | + |
| 299 | + def get_parser(self, prog_name): |
| 300 | + parser = super(NeutronCommandWithExtraArgs, self).get_parser(prog_name) |
| 301 | + parser.add_argument( |
| 302 | + '--extra-property', |
| 303 | + metavar='type=<property_type>,name=<property_name>,' |
| 304 | + 'value=<property_value>', |
| 305 | + dest='extra_properties', |
| 306 | + action=parseractions.MultiKeyValueAction, |
| 307 | + required_keys=['name', 'value'], |
| 308 | + optional_keys=['type'], |
| 309 | + help=_("Additional parameters can be passed using this property. " |
| 310 | + "Default type of the extra property is string ('str'), but " |
| 311 | + "other types can be used as well. Available types are: " |
| 312 | + "'dict', 'list', 'str', 'bool', 'int'. " |
| 313 | + "In case of 'list' type, 'value' can be " |
| 314 | + "semicolon-separated list of values. " |
| 315 | + "For 'dict' value is semicolon-separated list of the " |
| 316 | + "key:value pairs.") |
| 317 | + ) |
| 318 | + return parser |
| 319 | + |
| 320 | + |
| 321 | +class NeutronUnsetCommandWithExtraArgs(NeutronCommandWithExtraArgs): |
| 322 | + |
| 323 | + def _parse_extra_properties(self, extra_properties): |
| 324 | + result = {} |
| 325 | + if extra_properties: |
| 326 | + for _property in extra_properties: |
| 327 | + result[_property['name']] = None |
| 328 | + |
| 329 | + return result |
0 commit comments