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

Commit

Permalink
[Config] Auto detect hostname unless overridden
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed Jan 20, 2016
1 parent d5dd7cf commit 4905bbb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
18 changes: 17 additions & 1 deletion django_netjsonconfig/models/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import hashlib
import collections
from copy import deepcopy

from django.db import models
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -41,6 +42,8 @@ class AbstractConfig(TimeStampedEditableModel):
load_kwargs={'object_pairs_hook': collections.OrderedDict},
dump_kwargs={'indent': 4})

__template__ = False

class Meta:
abstract = True

Expand All @@ -63,6 +66,19 @@ def clean(self):
else:
self.validate_netjsonconfig_backend(backend)

def get_config(self):
"""
config preprocessing (skipped for templates):
* inserts hostname automatically if not defined
"""
if self.__template__:
return self.config
c = deepcopy(self.config)
c.setdefault('general', {})
if 'hostname' not in c['general']:
c['general']['hostname'] = self.name
return c

@classmethod
def validate_netjsonconfig_backend(self, backend):
"""
Expand Down Expand Up @@ -95,7 +111,7 @@ def get_backend_instance(self, template_instances=None):
needed for pre validation of m2m
"""
backend = self.backend_class
kwargs = {'config': self.config}
kwargs = {'config': self.get_config()}
# determine if we can pass templates
# expecting a many2many relationship
if hasattr(self, 'templates'):
Expand Down
2 changes: 2 additions & 0 deletions django_netjsonconfig/models/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class BaseTemplate(AbstractConfig):
Abstract model implementing a
netjsonconfig template
"""
__template__ = True

class Meta:
abstract = True

Expand Down
15 changes: 15 additions & 0 deletions django_netjsonconfig/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,18 @@ def test_status_modified_after_templates_changed(self):
c.templates.remove(t)
c.refresh_from_db()
self.assertEqual(c.status, 'modified')

def test_auto_hostname(self):
c = Config(name='automate-me',
backend='netjsonconfig.OpenWrt',
config={'general': {}},
key=self.TEST_KEY)
c.full_clean()
c.save()
expected = {
'type': 'DeviceConfiguration',
'general': {'hostname': 'automate-me'}
}
self.assertDictEqual(c.backend_instance.config, expected)
c.refresh_from_db()
self.assertDictEqual(c.config, {'general': {}})
8 changes: 7 additions & 1 deletion django_netjsonconfig/tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_backend_class(self):
self.assertIs(t.backend_class, OpenWrt)

def test_backend_instance(self):
config = {'general':{'hostname':'template'}}
config = {'general': {'hostname': 'template'}}
t = Template(name='test', backend='netjsonconfig.OpenWrt', config=config)
self.assertIsInstance(t.backend_instance, OpenWrt)

Expand Down Expand Up @@ -64,3 +64,9 @@ def test_config_status_modified_after_change(self):
t.save()
c.refresh_from_db()
self.assertEqual(c.status, 'modified')

def test_no_auto_hostname(self):
t = self._create_template()
self.assertNotIn('general', t.backend_instance.config)
t.refresh_from_db()
self.assertNotIn('general', t.config)

0 comments on commit 4905bbb

Please sign in to comment.