Skip to content

Commit

Permalink
Changed multicontext behaviour. Removed optional context parameter fr…
Browse files Browse the repository at this point in the history
…om get_config() & cli() methods.

The ASADriver class now has an additional property 'context'. This context property can be set to execute subsequent method calls in that context.
  • Loading branch information
wvandeun committed Jun 1, 2018
1 parent cdb3582 commit 86ecaf9
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions napalm_asa/asa.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def __init__(self,
self.hostname = hostname
self.multicontext = False
self.contexts = None
self._context = None
self.port = optional_args.get('port', 443)
self.timeout = timeout
self.up = False
Expand Down Expand Up @@ -208,6 +209,15 @@ def _get_interfaces_details(self, interfaces):

return ifs_details

@property
def context(self):
return self._context

@context.setter
def context(self, value):
self._check_context_exists(value)
self._context = value

def _get_contexts(self):
try:
resp = self._send_request('/contextmgmt/securitycontexts')
Expand Down Expand Up @@ -246,14 +256,13 @@ def close(self):
else:
raise ConnectionException('Cannot connect to {}. Error {}'.format(self.hostname, code))

def cli(self, commands, context=None):
def cli(self, commands):
"""Run CLI commands via the API."""
self._check_context_exists(context)
data = {
"commands": commands
}

endp = '/cli?context={}'.format(context) if context else '/cli'
endp = '/cli?context={}'.format(self._context) if self._context else '/cli'
response = self._send_request(endp, data)

result_dict = {}
Expand Down Expand Up @@ -328,16 +337,14 @@ def _check_context_exists(self,context):
if context:
if not self.multicontext:
e = "Device is not in multicontext mode"
raise CommandException(e)
raise CommandErrorException(e)
if not context in self.contexts:
e = "Context {} does not exist on device".format(context)
raise CommandException(e)
raise CommandErrorException(e)

def get_config(self, retrieve='all',context=None):
def get_config(self, retrieve='all'):
"""Get config."""

self._check_context_exists(context)

config = {
'startup': '',
'running': '',
Expand All @@ -354,7 +361,7 @@ def get_config(self, retrieve='all',context=None):
commands.append(running_cmd)

if retrieve.lower() in ['running', 'startup', 'all']:
results = self.cli(commands, context)
results = self.cli(commands)

if retrieve.lower() in ['startup', 'all']:
config['startup'] = results[startup_cmd]
Expand Down

0 comments on commit 86ecaf9

Please sign in to comment.