Skip to content

Commit

Permalink
Added initial/poc code for multicontext devices.
Browse files Browse the repository at this point in the history
  • Loading branch information
wvandeun committed May 31, 2018
1 parent 23a3efc commit cdb3582
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions napalm_asa/asa.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ def __init__(self,
self.username = username
self.password = password
self.hostname = hostname
self.multicontext = False
self.contexts = None
self.port = optional_args.get('port', 443)
self.timeout = timeout
self.up = False
Expand Down Expand Up @@ -206,6 +208,17 @@ def _get_interfaces_details(self, interfaces):

return ifs_details

def _get_contexts(self):
try:
resp = self._send_request('/contextmgmt/securitycontexts')
except CommandErrorException as e:
if str(e)[-3:] == "400":
self.multicontext = False
return
self.multicontext = True
self.contexts = [ c['name'] for c in resp['items']]
self.contexts.append('system')

def open(self):
"""
Open a connection to the device.
Expand All @@ -217,6 +230,7 @@ def open(self):
auth_result, code = self._authenticate()
if auth_result:
self.up = True
self._get_contexts()
return True
else:
self.up = False
Expand All @@ -232,13 +246,15 @@ def close(self):
else:
raise ConnectionException('Cannot connect to {}. Error {}'.format(self.hostname, code))

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

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

result_dict = {}

Expand Down Expand Up @@ -308,8 +324,20 @@ def get_interfaces(self):

return interfaces

def get_config(self, retrieve='all'):
def _check_context_exists(self,context):
if context:
if not self.multicontext:
e = "Device is not in multicontext mode"
raise CommandException(e)
if not context in self.contexts:
e = "Context {} does not exist on device".format(context)
raise CommandException(e)

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

self._check_context_exists(context)

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

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

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

0 comments on commit cdb3582

Please sign in to comment.