From 3f6d2c65810c3c648c084cb15f32ea8e3642b448 Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Fri, 11 Mar 2016 16:28:52 +0100 Subject: [PATCH] Removed NetJSON type from schema This has a few advantages: * simplifies code a bit * simplifies automatically generated UI The type attribute is still added when using the json method. --- docs/source/general/basics.rst | 5 ++--- netjsonconfig/backends/openwrt/openwrt.py | 10 +++++----- netjsonconfig/schema.py | 14 ++------------ tests/openwrt/test_backend.py | 6 +++--- 4 files changed, 12 insertions(+), 23 deletions(-) diff --git a/docs/source/general/basics.rst b/docs/source/general/basics.rst index b0a23cc93..4dedbea58 100644 --- a/docs/source/general/basics.rst +++ b/docs/source/general/basics.rst @@ -53,8 +53,7 @@ The previous example describes a device named ``RouterA`` which has a single network interface named ``eth0`` with a statically assigned ip address ``192.168.1.1/24`` (CIDR notation). -Because netjsonconfig deals only with ``DeviceConfiguration`` objects, the ``type`` -attribute can be omitted, the library will add the correct type automatically. +Because netjsonconfig deals only with ``DeviceConfiguration`` objects, the ``type`` attribute can be omitted. The previous configuration object therefore can be shortened to: @@ -81,7 +80,7 @@ The previous configuration object therefore can be shortened to: } From now on we will use the term *configuration dictionary* to refer to -NetJSON DeviceConfiguration objects. +*NetJSON DeviceConfiguration* objects. Backends -------- diff --git a/netjsonconfig/backends/openwrt/openwrt.py b/netjsonconfig/backends/openwrt/openwrt.py index 028b58b3a..b7d49034c 100644 --- a/netjsonconfig/backends/openwrt/openwrt.py +++ b/netjsonconfig/backends/openwrt/openwrt.py @@ -39,9 +39,6 @@ def __init__(self, config, templates=[], context={}): """ # perform deepcopy to avoid modifying the original config argument config = deepcopy(self._load(config)) - # allow omitting NetJSON type - if 'type' not in config: - config.update({'type': 'DeviceConfiguration'}) self.config = self._merge_config(config, templates) self.config = self._evaluate_vars(self.config, context) self.env = Environment(loader=PackageLoader('netjsonconfig.backends.openwrt', @@ -139,7 +136,7 @@ def validate(self): def json(self, validate=True, *args, **kwargs): """ - returns a string formatted in **NetJSON**; + returns a string formatted as **NetJSON DeviceConfiguration**; performs validation before returning output; ``*args`` and ``*kwargs`` will be passed to ``json.dumps``; @@ -148,7 +145,10 @@ def json(self, validate=True, *args, **kwargs): """ if validate: self.validate() - return json.dumps(self.config, *args, **kwargs) + # automatically adds NetJSON type + config = deepcopy(self.config) + config.update({'type': 'DeviceConfiguration'}) + return json.dumps(config, *args, **kwargs) @classmethod def get_packages(cls): diff --git a/netjsonconfig/schema.py b/netjsonconfig/schema.py index 726d105da..a4aaa0f61 100644 --- a/netjsonconfig/schema.py +++ b/netjsonconfig/schema.py @@ -1,8 +1,6 @@ """ -NetJSON DeviceConfiguration JSON-Schema definition -this should be up to date with the official spec: - -http://netjson.org/rfc.html#DeviceConfiguration-schema +NetJSON DeviceConfiguration JSON-Schema implementation +http://netjson.org/rfc.html """ schema = { @@ -269,15 +267,7 @@ ] } }, - "required": [ - "type" - ], "properties": { - "type": { - "type": "string", - "enum": ["DeviceConfiguration"], - "propertyOrder": 0, - }, "general": { "type": "object", "title": "General", diff --git a/tests/openwrt/test_backend.py b/tests/openwrt/test_backend.py index a1b236711..f042f69c9 100644 --- a/tests/openwrt/test_backend.py +++ b/tests/openwrt/test_backend.py @@ -45,14 +45,14 @@ def test_string_argument(self): OpenWrt('{}') def test_validate(self): - o = OpenWrt({'type': 'WRONG'}) + o = OpenWrt({'interfaces': 'WRONG'}) with self.assertRaises(ValidationError): o.validate() - o = OpenWrt({'type': 'DeviceConfiguration'}) + o = OpenWrt({'interfaces': []}) o.validate() - o.config['type'] = 'CHANGED' + o.config['interfaces'] = 'CHANGED' try: o.validate() except ValidationError as e: