Skip to content

Commit

Permalink
Merge pull request #156 from nornir-automation/napalm_kwargs
Browse files Browse the repository at this point in the history
allow passing options to napalm getters
  • Loading branch information
dbarrosop committed Jun 18, 2018
2 parents 8c263ce + 5243ce4 commit 6014afe
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
32 changes: 30 additions & 2 deletions nornir/plugins/tasks/networking/napalm_get.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,54 @@
import copy

from nornir.core.task import Result


def napalm_get(task, getters):
def napalm_get(task, getters, getters_options=None, **kwargs):
"""
Gather information from network devices using napalm
Arguments:
getters (list of str): getters to use
getters_options (dict of dicts): When passing multiple getters you
pass a dictionary where the outer key is the getter name
and the included dictionary represents the options to pass
to the getter
**kwargs: will be passed as they are to the getters
Examples:
Simple example::
> nr.run(task=napalm_get,
> getters=["interfaces", "facts"])
Passing options using ``**kwargs``::
> nr.run(task=napalm_get,
> getters=["config"],
> retrieve="all")
Passing options using ``getters_options``::
> nr.run(task=napalm_get,
> getters=["config", "interfaces"],
> getters_options={"config": {"retrieve": "all"}})
Returns:
:obj:`nornir.core.task.Result`:
* result (``dict``): dictionary with the result of the getter
"""
device = task.host.get_connection("napalm")
getters_options = getters_options or {}

if isinstance(getters, str):
getters = [getters]

result = {}
for g in getters:
options = copy.deepcopy(kwargs)
options.update(getters_options.get(g, {}))
getter = g if g.startswith("get_") else "get_{}".format(g)
method = getattr(device, getter)
result[g] = method()
result[g] = method(**options)
return Result(host=task.host, result=result)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"candidate": "asdadasd"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"fqdn": "localhost",
"hostname": "localhost",
"interface_list": [
"Ethernet1",
"Ethernet2",
"Ethernet3",
"Ethernet4",
"Management1"
],
"model": "vEOS",
"os_version": "4.15.5M-3054042.4155M",
"serial_number": "",
"uptime": "...",
"vendor": "Arista"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"candidate": "asdadasd"}
55 changes: 55 additions & 0 deletions tests/plugins/tasks/networking/test_napalm_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,58 @@ def test_napalm_getters_error(self, nornir):
assert isinstance(result.exception, KeyError)
assert processed
nornir.data.reset_failed_hosts()

def test_napalm_getters_with_options_error(self, nornir):
opt = {"path": THIS_DIR + "/test_napalm_getters_single_with_options"}
d = nornir.filter(name="dev3.group_2")
d.run(connections.napalm_connection, optional_args=opt)
result = d.run(
task=networking.napalm_get, getters=["config"], nonexistent="asdsa"
)
assert result
assert result.failed
for h, r in result.items():
assert "unexpected keyword argument 'nonexistent'" in r.result
nornir.data.reset_failed_hosts()

def test_napalm_getters_with_options_error_optional_args(self, nornir):
opt = {"path": THIS_DIR + "/test_napalm_getters_single_with_options"}
d = nornir.filter(name="dev3.group_2")
d.run(connections.napalm_connection, optional_args=opt)
result = d.run(
task=networking.napalm_get,
getters=["config"],
getters_options={"config": {"nonexistent": "asdasd"}},
)
assert result
assert result.failed
for h, r in result.items():
assert "unexpected keyword argument 'nonexistent'" in r.result
nornir.data.reset_failed_hosts()

def test_napalm_getters_single_with_options(self, nornir):
opt = {"path": THIS_DIR + "/test_napalm_getters_single_with_options"}
d = nornir.filter(name="dev3.group_2")
d.run(connections.napalm_connection, optional_args=opt)
result = d.run(
task=networking.napalm_get, getters=["config"], retrieve="candidate"
)
assert result
assert not result.failed
for h, r in result.items():
assert r.result["config"]

def test_napalm_getters_multiple_with_options(self, nornir):
opt = {"path": THIS_DIR + "/test_napalm_getters_multiple_with_options"}
d = nornir.filter(name="dev3.group_2")
d.run(connections.napalm_connection, optional_args=opt)
result = d.run(
task=networking.napalm_get,
getters=["config", "facts"],
getters_options={"config": {"retrieve": "candidate"}},
)
assert result
assert not result.failed
for h, r in result.items():
assert r.result["config"]
assert r.result["facts"]

0 comments on commit 6014afe

Please sign in to comment.