Skip to content
This repository has been archived by the owner on Feb 10, 2018. It is now read-only.

Commit

Permalink
Merge pull request #132 from mirceaulinic/cfg-fmt
Browse files Browse the repository at this point in the history
Detect the format of the config and try loading it
  • Loading branch information
dbarrosop authored Mar 26, 2017
2 parents a61cbef + a563339 commit f67ad16
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion napalm_junos/junos.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

# import stdlib
import re
import json
import logging
import collections
from copy import deepcopy
Expand Down Expand Up @@ -116,6 +117,24 @@ def is_alive(self):
'is_alive': self.device._conn._session.transport.is_active() and self.device.connected
}

@staticmethod
def _is_json_format(config):
try:
_ = json.loads(config) # noqa
except (TypeError, ValueError):
return False
return True

def _detect_config_format(self, config):
fmt = 'text'
if config.strip().startswith('<'):
return 'xml'
elif config.strip().startswith('set'):
return 'set'
elif self._is_json_format(config):
return 'json'
return fmt

def _load_candidate(self, filename, config, overwrite):
if filename is None:
configuration = config
Expand All @@ -130,7 +149,8 @@ def _load_candidate(self, filename, config, overwrite):
# and the device will be locked till first commit/rollback

try:
self.device.cu.load(configuration, format='text', overwrite=overwrite)
fmt = self._detect_config_format(configuration)
self.device.cu.load(configuration, format=fmt, overwrite=overwrite)
except ConfigLoadError as e:
if self.config_replace:
raise ReplaceConfigException(e.message)
Expand Down

0 comments on commit f67ad16

Please sign in to comment.