Skip to content
This repository was archived by the owner on Nov 15, 2024. It is now read-only.

Commit 7c6ec3d

Browse files
committed
utilize ncclient natively
1 parent 55cc43d commit 7c6ec3d

3 files changed

Lines changed: 44 additions & 18 deletions

File tree

netnir/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
netnir will create the default config and folders.
44
"""
55

6-
__version__ = "0.0.16"
6+
__version__ = "0.0.17"

netnir/plugins/netconf.py

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from nornir.core.task import Task, Result
2+
from netnir.helpers import device_mapper
3+
from ncclient import manager
24

35

46
def netconf_get_config(
57
task: Task,
6-
source: str = "candidate",
8+
source: str = "running",
79
nc_filter: str = None,
810
nc_filter_type: str = None,
911
) -> Result:
@@ -15,19 +17,31 @@ def netconf_get_config(
1517
:params nc_filter_type: type str
1618
:returns: nornir result object
1719
"""
18-
manager = task.host.get_connection(
19-
connection="netconf", configuration=task.nornir.config
20-
)
21-
if nc_filter and nc_filter_type:
22-
result = manager.get_config(source=source, filter=(nc_filter_type, nc_filter))
23-
else:
24-
result = manager.get_config(source=source)
20+
device_params = {
21+
"host": task.host.hostname,
22+
"port": task.host.port or 830,
23+
"username": task.host.username,
24+
"password": task.host.password,
25+
"hostkey_verify": False,
26+
"device_params": {
27+
"name": device_mapper(os_type=task.host.data["os"], proto="netconf")
28+
},
29+
}
30+
31+
with manager.connect(**device_params) as conn:
32+
if nc_filter and nc_filter_type:
33+
with open(nc_filter) as xml:
34+
nc_filter = xml.read()
35+
36+
result = conn.get_config(source=source, filter=(nc_filter_type, nc_filter))
37+
else:
38+
result = conn.get_config(source=source)
2539

2640
return Result(result=result, host=task.host)
2741

2842

2943
def netconf_edit_config(
30-
task: Task, target: str = "candidate", nc_config: str = None
44+
task: Task, target: str = "running", nc_config: str = None
3145
) -> Result:
3246
"""nornir netconf edit config task
3347
@@ -36,26 +50,37 @@ def netconf_edit_config(
3650
:params nc_config: type str - yang config model
3751
:returns: nornir result object
3852
"""
39-
manager = task.host.get_connection(
40-
connection="netconf", configuration=task.nornir.config
41-
)
42-
with manager.lock(target=target):
43-
config_response = manager.edit_config(target=target, config=nc_config)
44-
config_validate = manager.validate(source=target)
53+
device_params = {
54+
"host": task.host.hostname,
55+
"port": task.host.port or 830,
56+
"username": task.host.username,
57+
"password": task.host.password,
58+
"hostkey_verify": False,
59+
"device_params": {
60+
"name": device_mapper(os_type=task.host.data["os"], proto="netconf")
61+
},
62+
}
63+
64+
with manager.connect(**device_params) as conn:
65+
with open(nc_config) as xml:
66+
nc_config = xml.read()
67+
68+
config_response = conn.edit_config(target=target, config=nc_config)
69+
config_validate = conn.validate(source=target)
4570

4671
if config_response.ok and config_validate.ok:
4772
result = {
4873
"config_response": config_response.ok,
4974
"config_validate": config_validate.ok,
5075
}
5176
failed = False
52-
manager.commit()
77+
conn.commit()
5378
else:
5479
result = {
5580
"config_response": config_response.error,
5681
"config_validate": config_validate.error,
5782
}
5883
failed = True
59-
manager.discard_changes()
84+
conn.discard_changes()
6085

6186
return Result(result=result, host=task.host, failed=failed)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"pytest-runner",
1414
"nornir==2.4.0",
1515
"netmiko==2.4.2",
16+
"ncclient>=0.6.7",
1617
"hier_config==v1.6.1",
1718
"keyring>=21.2.1",
1819
"keyrings.alt>=3.4.0",

0 commit comments

Comments
 (0)