Skip to content
This repository has been archived by the owner on Jul 9, 2020. It is now read-only.

Commit

Permalink
Added checksum method to AbstractConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Jan 11, 2016
1 parent 6ccc18c commit adaf9aa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 6 additions & 0 deletions django_netjsonconfig/models/device.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import hashlib
import collections

from django.db import models
Expand Down Expand Up @@ -80,6 +81,11 @@ def get_backend_instance(self, config=None, template_instances=None):
kwargs['templates'] = [t.config for t in template_instances]
return backend(**kwargs)

@property
def checksum(self):
""" returns checksum of configuration """
return hashlib.md5(self.json().encode()).hexdigest()

def json(self, dict=False, **kwargs):
config = self.backend_instance.config
if dict:
Expand Down
17 changes: 13 additions & 4 deletions django_netjsonconfig/tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_backend_class(self):
self.assertIs(d.backend_class, OpenWrt)

def test_backend_instance(self):
config = {'general':{'hostname':'device'}}
config = {'general': {'hostname': 'device'}}
d = Device(name='test', backend='netjsonconfig.OpenWrt', config=config)
self.assertIsInstance(d.backend_instance, OpenWrt)

Expand All @@ -46,7 +46,7 @@ def test_json(self):
radio = Template.objects.get(name='radio0')
d = Device(name='test',
backend='netjsonconfig.OpenWrt',
config={'general':{'hostname':'json-test'}},
config={'general': {'hostname': 'json-test'}},
key=self.TEST_KEY)
d.full_clean()
d.save()
Expand All @@ -55,7 +55,7 @@ def test_json(self):
full_config = {
'type': 'DeviceConfiguration',
'general': {
'hostname':'json-test'
'hostname': 'json-test'
},
"interfaces": [
{
Expand Down Expand Up @@ -128,7 +128,7 @@ def test_m2m_validation(self):
def test_key_validation(self):
d = Device(name='test',
backend='netjsonconfig.OpenWrt',
config={'general':{'hostname':'json-test'}})
config={'general': {'hostname': 'json-test'}})
d.key = 'key/key'
with self.assertRaises(ValidationError):
d.full_clean()
Expand All @@ -140,3 +140,12 @@ def test_key_validation(self):
d.full_clean()
d.key = self.TEST_KEY
d.full_clean()

def test_checksum(self):
d = Device(name='test',
backend='netjsonconfig.OpenWrt',
config={'general': {'hostname': 'json-test'}},
key=self.TEST_KEY)
d.full_clean()
d.save()
self.assertEqual(len(d.checksum), 32)

0 comments on commit adaf9aa

Please sign in to comment.