Permalink
Browse files

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.
  • Loading branch information...
nemesisdesign committed Mar 11, 2016
1 parent ff15530 commit 3f6d2c65810c3c648c084cb15f32ea8e3642b448
@@ -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
--------
@@ -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):
@@ -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",
@@ -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:

0 comments on commit 3f6d2c6

Please sign in to comment.