From 354abb5b6ebf444341a717addd4cf9b1fe77ca00 Mon Sep 17 00:00:00 2001 From: Dan Sneddon Date: Fri, 27 Jan 2017 14:58:53 -0800 Subject: Allow ovs_extra to be specified as either a string or list This change allows the commands in ovs_extra to be expressed as a string or as a list. If a string is passed, it will be treated as a list with a single element, and other ovs_extra elements will be appended to the same list, such as fail_mode. In order for more than one command to be passed as a string, the commands will need to be separated with double-dashes, similar to the format used by OVS command-line utilities. Change-Id: I3f69e7ab96ff6e06953c3838b3e187b93545d623 --- os_net_config/objects.py | 23 ++++++++--------------- os_net_config/tests/test_objects.py | 12 ++++++------ 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/os_net_config/objects.py b/os_net_config/objects.py index e3d8978..3136be0 100644 --- a/os_net_config/objects.py +++ b/os_net_config/objects.py @@ -441,8 +441,7 @@ class OvsBridge(_BaseOpts): ovs_options = json.get('ovs_options') ovs_extra = json.get('ovs_extra', []) if not isinstance(ovs_extra, list): - msg = 'ovs_extra must be a list.' - raise InvalidConfigException(msg) + ovs_extra = [ovs_extra] fail_mode = json.get('ovs_fail_mode', DEFAULT_OVS_BRIDGE_FAIL_MODE) members = [] @@ -509,8 +508,7 @@ class OvsUserBridge(_BaseOpts): ovs_options = json.get('ovs_options') ovs_extra = json.get('ovs_extra', []) if not isinstance(ovs_extra, list): - msg = 'ovs_extra must be a list.' - raise InvalidConfigException(msg) + ovs_extra = [ovs_extra] fail_mode = json.get('ovs_fail_mode', DEFAULT_OVS_BRIDGE_FAIL_MODE) members = [] @@ -866,9 +864,8 @@ class OvsBond(_BaseOpts): json, include_primary=False) ovs_options = json.get('ovs_options') ovs_extra = json.get('ovs_extra', []) - if ovs_extra and not isinstance(ovs_extra, list): - msg = 'ovs_extra must be a list.' - raise InvalidConfigException(msg) + if not isinstance(ovs_extra, list): + ovs_extra = [ovs_extra] members = [] # members @@ -916,8 +913,7 @@ class OvsTunnel(_BaseOpts): ovs_options = ['options:%s' % opt for opt in ovs_options] ovs_extra = json.get('ovs_extra', []) if not isinstance(ovs_extra, list): - msg = 'ovs_extra must be a list.' - raise InvalidConfigException(msg) + ovs_extra = [ovs_extra] opts = _BaseOpts.base_opts_from_json(json) return OvsTunnel(name, *opts, tunnel_type=tunnel_type, ovs_options=ovs_options, ovs_extra=ovs_extra) @@ -953,8 +949,7 @@ class OvsPatchPort(_BaseOpts): ovs_options = ['options:%s' % opt for opt in ovs_options] ovs_extra = json.get('ovs_extra', []) if not isinstance(ovs_extra, list): - msg = 'ovs_extra must be a list.' - raise InvalidConfigException(msg) + ovs_extra = [ovs_extra] opts = _BaseOpts.base_opts_from_json(json) return OvsPatchPort(name, *opts, bridge_name=bridge_name, peer=peer, ovs_options=ovs_options, ovs_extra=ovs_extra) @@ -1039,8 +1034,7 @@ class OvsDpdkPort(_BaseOpts): ovs_options = ['options:%s' % opt for opt in ovs_options] ovs_extra = json.get('ovs_extra', []) if not isinstance(ovs_extra, list): - msg = 'ovs_extra must be a list.' - raise InvalidConfigException(msg) + ovs_extra = [ovs_extra] opts = _BaseOpts.base_opts_from_json(json) return OvsDpdkPort(name, *opts, members=members, driver=driver, ovs_options=ovs_options, ovs_extra=ovs_extra) @@ -1086,8 +1080,7 @@ class OvsDpdkBond(_BaseOpts): ovs_options = json.get('ovs_options') ovs_extra = json.get('ovs_extra', []) if not isinstance(ovs_extra, list): - msg = 'ovs_extra must be a list.' - raise InvalidConfigException(msg) + ovs_extra = [ovs_extra] members = [] # members diff --git a/os_net_config/tests/test_objects.py b/os_net_config/tests/test_objects.py index 0ff653c..f5daf31 100644 --- a/os_net_config/tests/test_objects.py +++ b/os_net_config/tests/test_objects.py @@ -309,7 +309,7 @@ class TestBridge(base.TestCase): self.assertEqual("set bridge br-foo fail_mode=standalone", bridge.ovs_extra[1]) - def test_from_json_ovs_extra_invalid(self): + def test_from_json_ovs_extra_string(self): data = """{ "type": "ovs_bridge", "name": "br-foo", @@ -317,11 +317,11 @@ class TestBridge(base.TestCase): "ovs_fail_mode": "standalone" } """ - json_data = json.loads(data) - err = self.assertRaises(objects.InvalidConfigException, - objects.object_from_json, json_data) - expected = 'ovs_extra must be a list.' - self.assertIn(expected, six.text_type(err)) + bridge = objects.object_from_json(json.loads(data)) + self.assertTrue(2 == len(bridge.ovs_extra)) + self.assertEqual("bar", bridge.ovs_extra[0]) + self.assertEqual("set bridge br-foo fail_mode=standalone", + bridge.ovs_extra[1]) class TestLinuxBridge(base.TestCase): -- cgit 1.2.3-korg