updates junos shared module methods

* cleans up load_config() arguments to simply
* removes unused methods in Cli transport
* updates error regexp
This commit is contained in:
Peter Sprygada 2016-09-11 23:33:20 -04:00
parent f5cff2ced5
commit e5d931de6b

View file

@ -56,7 +56,7 @@ except ImportError:
import xml.etree.ElementTree as etree import xml.etree.ElementTree as etree
SUPPORTED_CONFIG_FORMATS = ['text', 'set', 'json', 'xml'] SUPPORTED_CONFIG_FORMATS = ['text', 'xml']
def xml_to_json(val): def xml_to_json(val):
@ -151,23 +151,34 @@ class Netconf(object):
ele = self.rpc('get_configuration', output=config_format) ele = self.rpc('get_configuration', output=config_format)
if config_format in ['text', 'set']: if config_format == 'text':
return str(ele.text).strip() return str(ele.text).strip()
else: else:
return ele return ele
def load_config(self, candidate, update='merge', comment=None, def load_config(self, config, commit=False, replace=False, confirm=None,
confirm=None, format='text', commit=True): comment=None, config_format='text'):
merge = update == 'merge' #def load_config(self, candidate, update='merge', comment=None,
overwrite = update == 'overwrite' # confirm=None, format='text', commit=True):
if replace:
merge = False
overwrite = True
else:
merge = True
overwrite = False
if overwrite and config_format == 'set':
self.raise_exc('replace cannot be True when config_format is `set`')
self.lock_config() self.lock_config()
try: try:
candidate = '\n'.join(candidate) candidate = '\n'.join(candidate)
self.config.load(candidate, format=format, merge=merge, self.config.load(candidate, format=config_format, merge=merge,
overwrite=overwrite) overwrite=overwrite)
except ConfigLoadError: except ConfigLoadError:
exc = get_exception() exc = get_exception()
self.raise_exc('Unable to load config: %s' % str(exc)) self.raise_exc('Unable to load config: %s' % str(exc))
@ -251,27 +262,20 @@ class Cli(CliBase):
] ]
CLI_ERRORS_RE = [ CLI_ERRORS_RE = [
re.compile(r"% ?Error"), re.compile(r"unkown command")
re.compile(r"% ?Bad secret"),
re.compile(r"invalid input", re.I),
re.compile(r"(?:incomplete|ambiguous) command", re.I),
re.compile(r"connection timed out", re.I),
re.compile(r"[^\r\n]+ not found", re.I),
re.compile(r"'[^']' +returned error code: ?\d+"),
] ]
def connect(self, params, **kwargs): def connect(self, params, **kwargs):
super(Cli, self).connect(params, **kwargs) super(Cli, self).connect(params, **kwargs)
if self.shell._matched_prompt.strip().endswith('%'): if self.shell._matched_prompt.strip().endswith('%'):
self.execute('cli') self.execute('cli')
self.execute('set cli screen-length 0') self.execute('set cli screen-length 0')
def configure(self, commands, **kwargs): def configure(self, commands, comment=None):
cmds = ['configure'] cmds = ['configure']
cmds.extend(to_list(commands)) cmds.extend(to_list(commands))
if kwargs.get('comment'): if comment:
cmds.append('commit and-quit comment "%s"' % kwargs.get('comment')) cmds.append('commit and-quit comment "%s"' % kwargs.get('comment'))
else: else:
cmds.append('commit and-quit') cmds.append('commit and-quit')
@ -279,15 +283,6 @@ class Cli(CliBase):
responses = self.execute(cmds) responses = self.execute(cmds)
return responses[1:-1] return responses[1:-1]
def load_config(self, commands):
return self.configure(commands)
def get_config(self, output='block'):
cmd = 'show configuration'
if output == 'set':
cmd += ' | display set'
return self.execute([cmd])[0]
Cli = register_transport('cli', default=True)(Cli) Cli = register_transport('cli', default=True)(Cli)
def split(value): def split(value):