Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions pyeapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
contains the settings for nodes used by the connect_to function.

"""
from uuid import uuid4
import os
import re

Expand Down Expand Up @@ -471,6 +472,7 @@ def __init__(self, connection, **kwargs):
self._version = None
self._version_number = None
self._model = None
self._session_name = None

self._enablepwd = kwargs.get('enablepwd')
self.autorefresh = kwargs.get('autorefresh', True)
Expand Down Expand Up @@ -578,6 +580,14 @@ def config(self, commands, **kwargs):
output from each command. The function will strip the
response from any commands it prepends.
"""
if self._session_name: # If in a config session
return self._configure_session(commands, **kwargs)

return self._configure_terminal(commands, **kwargs)

def _configure_terminal(self, commands, **kwargs):
"""Configures the node with the specified commands with leading "configure terminal"
"""
commands = make_iterable(commands)
commands = list(commands)

Expand All @@ -593,6 +603,24 @@ def config(self, commands, **kwargs):

return response

def _configure_session(self, commands, **kwargs):
"""Configures the node with the specified commands with leading "configure session <session name>"
"""
if not self._session_name:
raise CommandError('Not currently in a session')

commands = make_iterable(commands)
commands = list(commands)

# push the configure command onto the command stack
commands.insert(0, 'configure session %s' % self._session_name)
response = self.run_commands(commands, **kwargs)

# pop the configure command output off the stack
response.pop(0)

return response

def section(self, regex, config='running_config'):
"""Returns a section of the config

Expand Down Expand Up @@ -824,6 +852,41 @@ def refresh(self):
self._running_config = None
self._startup_config = None

def configure_session(self):
"""Enter a config session
"""
self._session_name = self._session_name or uuid4()

def diff(self):
"""Returns session-config diffs in text encoding

Note: "show session-config diffs" doesn't support json encoding
"""
response = self._configure_session(['show session-config diffs'], encoding='text')

return response[0]['output']

def commit(self):
"""Commits the current config session
"""
return self._configure_and_exit_session(['commit'])

def abort(self):
"""Aborts the current config session
"""
return self._configure_session(['abort'])

def _configure_and_exit_session(self, commands, **kwargs):
response = self._configure_session(commands, **kwargs)

if self.autorefresh:
self.refresh()

# Exit the current config session
self._session_name = None

return response


def connect_to(name):
"""Creates a node instance based on an entry from the config
Expand Down