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

Commit 4540533

Browse files
authored
Merge pull request #13 from netdevops/netconf
add netconf
2 parents bde89e0 + aaa9154 commit 4540533

File tree

9 files changed

+105
-45
lines changed

9 files changed

+105
-45
lines changed

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.10"
6+
__version__ = "0.0.11"

netnir/core/inventory.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ def nhosts(self):
4444
)
4545
data[host] = {
4646
"hostname": f"{host}.{domain}" if domain else host,
47-
"username": creds["username"],
48-
"password": creds["password"],
47+
"username": host_vars.get("username", creds["username"]),
48+
"password": host_vars.get("password", creds["password"]),
4949
"port": host_vars.get("port", 22),
5050
"platform": device_mapper(host_vars["os"]),
5151
"groups": host_vars.get("groups", list()),
@@ -64,8 +64,8 @@ def nhosts(self):
6464
"connection_options": {
6565
"netconf": {
6666
"hostname": f"{host}.{domain}" if domain else host,
67-
"username": creds["username"],
68-
"password": creds["password"],
67+
"username": host_vars.get("username", creds["username"]),
68+
"password": host_vars.get("password", creds["password"]),
6969
"platform": host_vars.get("os"),
7070
"port": host_vars.get("port", 830),
7171
"extras": {"hostkey_verify": False},

netnir/core/tasks/inventory.py

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,16 @@
1-
from netnir.constants import NR
2-
from netnir.helpers.common.args import filter_host, filter_hosts, filter_group
3-
from netnir.helpers import filter_type, inventory_filter
4-
from netnir.plugins.facts import inventory_facts
5-
from nornir.plugins.functions.text import print_result
1+
from netnir.helpers.scaffold.command import CommandScaffold
62

73

8-
"""inventory cli commands
9-
"""
10-
11-
12-
class Inventory:
4+
class Inventory(CommandScaffold):
135
"""
146
cli based inventory search
15-
16-
:param args: type obj
177
"""
188

19-
def __init__(self, args):
20-
"""
21-
initialize the inventory class
22-
"""
23-
self.args = args
24-
self.nr = NR
25-
26-
@staticmethod
27-
def parser(parser):
28-
"""
29-
cli command parser
30-
"""
31-
filter_host(parser)
32-
filter_hosts(parser)
33-
filter_group(parser)
34-
359
def run(self):
36-
"""
37-
cli execution
38-
"""
39-
devices_filter = filter_type(
40-
host=self.args.host, filter=self.args.filter, group=self.args.group
41-
)
42-
self.nr = inventory_filter(
43-
nr=self.nr,
44-
device_filter=devices_filter["data"],
45-
type=devices_filter["type"],
46-
)
10+
from netnir.plugins.facts import inventory_facts
11+
from nornir.plugins.functions.text import print_result
12+
13+
self.nr = self._inventory()
4714
results = self.nr.run(task=inventory_facts)
4815
print_result(results)
4916

netnir/core/tasks/netconf.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from netnir.helpers.scaffold.command import CommandScaffold
2+
3+
4+
class NetConf(CommandScaffold):
5+
"""netconf commands"""
6+
7+
def run(self):
8+
"""execute netconf commands
9+
10+
:returns: nornir Result object
11+
"""
12+
from netnir.plugins.netconf import netconf_get
13+
from nornir.plugins.functions.text import print_result
14+
15+
self.nr = self._inventory()
16+
results = self.nr.run(task=netconf_get, name="NETCONF GET CONFIG AND STATE")
17+
print_result(results)
18+
19+
return results

netnir/helpers/defaults.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
"class": "netnir.core.tasks.ssh.Ssh",
2828
"description": "command and config execution over SSH",
2929
},
30+
"netconf": {
31+
"class": "netnir.core.tasks.netconf.NetConf",
32+
"description": "command and config execution over NETCONF",
33+
},
3034
"fetch": {
3135
"class": "netnir.core.tasks.fetch.Fetch",
3236
"description": "fetch commands",

netnir/helpers/scaffold/command.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
""" netnir command scaffolding """
2+
3+
4+
class CommandScaffold:
5+
""" scaffold class """
6+
7+
def __init__(self, args):
8+
"""initialize the class
9+
10+
:params args: type object
11+
"""
12+
from netnir.constants import NR
13+
14+
self.args = args
15+
self.nr = NR
16+
17+
@staticmethod
18+
def parser(parser):
19+
"""command parser function
20+
21+
:params parser: type object
22+
"""
23+
from netnir.helpers.common.args import filter_host, filter_hosts, filter_group
24+
25+
filter_host(parser)
26+
filter_hosts(parser)
27+
filter_group(parser)
28+
29+
def run(self):
30+
"""things to do"""
31+
return "things to do"
32+
33+
def _inventory(self):
34+
"""filter inventory
35+
36+
:returns: filtered nornir inventory object
37+
"""
38+
from netnir.helpers import inventory_filter, filter_type
39+
40+
devices_filter = filter_type(
41+
host=self.args.host, filter=self.args.filter, group=self.args.group
42+
)
43+
self.nr = inventory_filter(
44+
nr=self.nr,
45+
device_filter=devices_filter["data"],
46+
type=devices_filter["type"],
47+
)
48+
49+
return self.nr

netnir/plugins/netconf.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from nornir.core.task import Task, Result
2+
3+
4+
def netconf_get(task: Task) -> Result:
5+
"""nornir netconf get task
6+
7+
:params task: type object
8+
"""
9+
manager = task.host.get_connection(
10+
connection="netconf", configuration=task.nornir.config
11+
)
12+
result = manager.get()
13+
14+
return Result(result=result, host=task.host)

tests/data/netnir.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ plugins:
2727
ssh:
2828
class: netnir.core.tasks.ssh.Ssh
2929
description: "command and config execution over SSH"
30+
netconf:
31+
class: netnir.core.tasks.netconf.NetConf
32+
description: "command and config execution over NETCONF"

tests/netnir/helpers/test_nornir_config.py renamed to tests/netnir/helpers/test_netnir_config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
def test_nornir_config(initial_setup):
1+
def test_netnir_config(initial_setup):
22
from netnir.helpers import netnir_config
33

44
test_config = {
@@ -28,6 +28,10 @@ def test_nornir_config(initial_setup):
2828
"class": "netnir.core.tasks.ssh.Ssh",
2929
"description": "command and config execution over SSH",
3030
},
31+
"netconf": {
32+
"class": "netnir.core.tasks.netconf.NetConf",
33+
"description": "command and config execution over NETCONF",
34+
},
3135
"fetch": {
3236
"class": "netnir.core.tasks.fetch.Fetch",
3337
"description": "fetch commands",

0 commit comments

Comments
 (0)