Skip to content

Commit

Permalink
refactors nxos module to use persistent connections (ansible#21470)
Browse files Browse the repository at this point in the history
This completes the refactor of the nxos modules to use the persistent
connection.  It also updates all of the nxos modules to use the
new connection module and preserves use of nxapi as well.
  • Loading branch information
privateip authored Feb 15, 2017
1 parent eb1453a commit 21d993a
Show file tree
Hide file tree
Showing 72 changed files with 2,298 additions and 12,930 deletions.
89 changes: 89 additions & 0 deletions lib/ansible/module_utils/netcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import re

from ansible.module_utils.six.moves import zip
from ansible.module_utils.network_common import to_list

DEFAULT_COMMENT_TOKENS = ['#', '!', '/*', '*/']

Expand Down Expand Up @@ -361,3 +362,91 @@ def add(self, lines, parents=None):
self.items.append(item)


class CustomNetworkConfig(NetworkConfig):

def expand_section(self, configobj, S=None):
if S is None:
S = list()
S.append(configobj)
for child in configobj.children:
if child in S:
continue
self.expand_section(child, S)
return S

def get_object(self, path):
for item in self.items:
if item.text == path[-1]:
parents = [p.text for p in item.parents]
if parents == path[:-1]:
return item

def to_block(self, section):
return '\n'.join([item.raw for item in section])

def get_section(self, path):
try:
section = self.get_section_objects(path)
return self.to_block(section)
except ValueError:
return list()

def get_section_objects(self, path):
if not isinstance(path, list):
path = [path]
obj = self.get_object(path)
if not obj:
raise ValueError('path does not exist in config')
return self.expand_section(obj)


def add(self, lines, parents=None):
"""Adds one or lines of configuration
"""

ancestors = list()
offset = 0
obj = None

## global config command
if not parents:
for line in to_list(lines):
item = ConfigLine(line)
item.raw = line
if item not in self.items:
self.items.append(item)

else:
for index, p in enumerate(parents):
try:
i = index + 1
obj = self.get_section_objects(parents[:i])[0]
ancestors.append(obj)

except ValueError:
# add parent to config
offset = index * self.indent
obj = ConfigLine(p)
obj.raw = p.rjust(len(p) + offset)
if ancestors:
obj.parents = list(ancestors)
ancestors[-1].children.append(obj)
self.items.append(obj)
ancestors.append(obj)

# add child objects
for line in to_list(lines):
# check if child already exists
for child in ancestors[-1].children:
if child.text == line:
break
else:
offset = len(parents) * self.indent
item = ConfigLine(line)
item.raw = line.rjust(len(line) + offset)
item.parents = ancestors
ancestors[-1].children.append(item)
self.items.append(item)



Loading

0 comments on commit 21d993a

Please sign in to comment.