From bdef40ef552aab1408d84bb7ef5ce2f5616154b1 Mon Sep 17 00:00:00 2001 From: Aarnav Bos Date: Mon, 19 Nov 2018 20:24:33 +0530 Subject: [PATCH] [backend] Added support for plugins. --- netjsonconfig/backends/base/backend.py | 12 ++++++ tests/openwrt/test_backend.py | 59 ++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/netjsonconfig/backends/base/backend.py b/netjsonconfig/backends/base/backend.py index 892c67cba..a50a05eef 100644 --- a/netjsonconfig/backends/base/backend.py +++ b/netjsonconfig/backends/base/backend.py @@ -21,6 +21,7 @@ class BaseBackend(object): schema = None FILE_SECTION_DELIMITER = '# ---------- files ---------- #' list_identifiers = [] + plugins = [] def __init__(self, config=None, native=None, templates=None, context=None): """ @@ -49,6 +50,17 @@ def __init__(self, config=None, native=None, templates=None, context=None): raise ValueError('Expecting either config or native argument to be ' 'passed during the initialization of the backend') + @classmethod + def add_plugins(cls, plugins): + for plugin in plugins: + plugin.schema.pop('$schema', None) + if getattr(cls, 'schema'): + cls.schema['properties'][plugin.key] = plugin.schema + if getattr(cls, 'converters'): + cls.converters.append(plugin.converter) + else: + cls.converters = [plugin.converter] + def _load(self, config): """ Loads config from string or dict diff --git a/tests/openwrt/test_backend.py b/tests/openwrt/test_backend.py index 2da72fc09..7053704a6 100644 --- a/tests/openwrt/test_backend.py +++ b/tests/openwrt/test_backend.py @@ -8,6 +8,34 @@ from netjsonconfig import OpenWrt from netjsonconfig.exceptions import ValidationError from netjsonconfig.utils import _TabsMixin +from netjsonconfig.backends.openwrt.converters.base import OpenWrtConverter + + +class CoovaConverter(OpenWrtConverter): + netjson_key = 'chilli' + _uci_types = ['chilli'] + + def to_intermediate_loop(self, block, result, index=None): + if block: + block.update({ + '.type': 'chilli', + '.name': 'chilli' + }) + result['chilli'] = [self.sorted_dict(block)] + return result + + +class CoovaPlugin(object): + key = 'chilli' + converter = CoovaConverter + schema = { + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "disabled": {"type": "integer"}, + "uamsecret": {"type": "string"}, + }, + } class TestBackend(unittest.TestCase, _TabsMixin): @@ -19,6 +47,37 @@ def test_config_copy(self): o.validate() self.assertDictEqual(config, {'interfaces': []}) + def test_plugin(self): + config = { + "chilli": { + 'disabled': 1, + 'uamsecret': 'asd123fghj' + }, + } + OpenWrt.add_plugins([CoovaPlugin()]) + o = OpenWrt(config) + self.assertEqual(self._tabs('''package chilli + +config chilli 'chilli' + option disabled '1' + option uamsecret 'asd123fghj' +'''), o.render()) + + def test_bad_plugin_config(self): + config = { + "chilli": { + 'disabled': 'wrong_value', + 'uamsecret': 'asd123fghj' + }, + } + OpenWrt.add_plugins([CoovaPlugin()]) + o = OpenWrt(config) + try: + o.render() + self.fail() + except ValidationError as e: + self.assertEqual(e.message, "'wrong_value' is not of type 'integer'") + def test_json_method(self): config = { "type": "DeviceConfiguration",