From 86cbaeb09311cb19010e69763f3afbbf40966754 Mon Sep 17 00:00:00 2001 From: Ajay Tripathi Date: Mon, 20 Apr 2020 01:58:44 +0530 Subject: [PATCH] [backward-conversion] Recognize templates #100 Closes #100 --- .gitignore | 4 +++ netjsonconfig/backends/base/backend.py | 42 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/.gitignore b/.gitignore index 766c20f81..29ed7feea 100644 --- a/.gitignore +++ b/.gitignore @@ -54,8 +54,12 @@ target/ # editors *.komodoproject +.vscode # other *.DS_Store* .__* __DEV + +# TODO: Remove +test.py diff --git a/netjsonconfig/backends/base/backend.py b/netjsonconfig/backends/base/backend.py index 71336d117..489ea12b9 100644 --- a/netjsonconfig/backends/base/backend.py +++ b/netjsonconfig/backends/base/backend.py @@ -161,6 +161,48 @@ def json(self, validate=True, *args, **kwargs): config.update({'type': 'DeviceConfiguration'}) return json.dumps(config, *args, **kwargs) + def merge_native(self, templates=None): + """ + :param templates: ``list`` containing **NetJSON** configuration dictionaries + + returns a dict of configuration which contains configurations + that do not exist in given templates. + + :returns: dict + """ + config_to_send = {} + controller_config = self._merge_config({}, templates) + for section in controller_config: + device_section = self.config.get(section, None) + + if device_section and isinstance(device_section, list): + for element in device_section: + """ + TODO: Discuss case where dns_servers set but + someone removed it from device, will cause + information loss. + TODO: Question: in an array like interfaces, + if say, mtu is changed, do I copy the entire interface + or just the mtu value with interface name? + Currently copying the entire thing because I feel it + Should be stored together. + Same for "radios" + """ + element_dict = dict(element) + if element_dict not in controller_config[section]: + config_to_send[section] = element_dict + + + if device_section and isinstance(device_section, dict): + for config in controller_config[section]: + device_config = device_section.get(config, None) + if device_config and device_config != controller_config[section][config]: + if not config_to_send.get(section, None): + config_to_send[section] = {} + config_to_send[section][config] = device_config + + return config_to_send + def generate(self): """ Returns a ``BytesIO`` instance representing an in-memory tar.gz archive