Skip to content

Commit

Permalink
Removed NetJSON type from schema
Browse files Browse the repository at this point in the history
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
nemesifier committed Mar 11, 2016
1 parent ff15530 commit 3f6d2c6
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 23 deletions.
5 changes: 2 additions & 3 deletions docs/source/general/basics.rst
Expand Up @@ -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:

Expand All @@ -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
--------
Expand Down
10 changes: 5 additions & 5 deletions netjsonconfig/backends/openwrt/openwrt.py
Expand Up @@ -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',
Expand Down Expand Up @@ -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``;
Expand All @@ -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):
Expand Down
14 changes: 2 additions & 12 deletions 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 = {
Expand Down Expand Up @@ -269,15 +267,7 @@
]
}
},
"required": [
"type"
],
"properties": {
"type": {
"type": "string",
"enum": ["DeviceConfiguration"],
"propertyOrder": 0,
},
"general": {
"type": "object",
"title": "General",
Expand Down
6 changes: 3 additions & 3 deletions tests/openwrt/test_backend.py
Expand Up @@ -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:
Expand Down

0 comments on commit 3f6d2c6

Please sign in to comment.