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

Commit 9ef12ee

Browse files
committed
update netconf refactor task class inheritance
1 parent b085360 commit 9ef12ee

12 files changed

Lines changed: 230 additions & 124 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.14"
6+
__version__ = "0.0.15"

netnir/core/inventory.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ def nhosts(self):
6666
"hostname": f"{host}.{domain}" if domain else host,
6767
"username": host_vars.get("username", creds["username"]),
6868
"password": host_vars.get("password", creds["password"]),
69-
"platform": host_vars.get("os"),
69+
"platform": device_mapper(
70+
os_type=host_vars["os"], proto="netconf"
71+
),
7072
"port": host_vars.get("port", 830),
7173
"extras": {"hostkey_verify": False},
7274
},

netnir/core/tasks/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
"""built-in tasks initialization
2-
"""
1+
"""built-in tasks initialization"""

netnir/core/tasks/config_plan.py

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from netnir.constants import NR
1+
from netnir.helpers.scaffold.command import CommandScaffold
22
from netnir.core.template import CompileTemplate
33
from netnir.core.networking import Networking
44
from netnir.helpers import output_writer, TextColor
55
from netnir.plugins.hier import hier_host
6-
from netnir.helpers.common.args import filter_host
76
from netnir.constants import OUTPUT_DIR
87
from nornir.plugins.functions.text import print_result
98
import logging
@@ -13,30 +12,21 @@
1312
"""
1413

1514

16-
class ConfigPlan:
15+
class ConfigPlan(CommandScaffold):
1716
"""
1817
config plan cli plugin to render configuration plans, either by
1918
compiling from template or using hier_config to create a remediation
2019
plan
21-
22-
:params args: type obj
2320
"""
2421

25-
def __init__(self, args):
26-
"""
27-
initialize the config plan class
28-
"""
29-
self.args = args
30-
self.nr = NR
31-
3222
@staticmethod
3323
def parser(parser):
3424
"""
3525
cli options parser
3626
3727
:params parser: type obj
3828
"""
39-
filter_host(parser)
29+
CommandScaffold.parser(parser)
4030
parser.add_argument(
4131
"--compile",
4232
nargs="?",
@@ -65,26 +55,16 @@ def run(self, template_file="main.conf.j2"):
6555
6656
:returns: result string
6757
"""
68-
if self.args.host:
69-
self.nr = self.nr.filter(name=self.args.host)
58+
self.nr = self._inventory()
7059

60+
for host in self.nr.inventory.hosts:
7161
compiled_template = CompileTemplate(
72-
nr=self.nr, host=self.args.host, template=template_file
62+
nr=self.nr, host=host, template=template_file
7363
)
7464
output_writer(
75-
nornir_results=compiled_template.render(), output_file="compiled.conf"
65+
nornir_results=compiled_template.render(), output_file="compiled.conf",
7666
)
7767
print_result(compiled_template.render())
78-
else:
79-
for host in self.nr.inventory.hosts:
80-
compiled_template = CompileTemplate(
81-
nr=self.nr, host=host, template=template_file
82-
)
83-
output_writer(
84-
nornir_results=compiled_template.render(),
85-
output_file="compiled.conf",
86-
)
87-
print_result(compiled_template.render())
8868

8969
if self.args.compile:
9070
if self.args.host:

netnir/core/tasks/fetch/config.py

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
1-
from netnir.helpers.common.args import (
2-
filter_host,
3-
filter_group,
4-
filter_hosts,
5-
num_workers,
6-
)
7-
from netnir.helpers import filter_type, inventory_filter, output_writer
1+
from netnir.helpers.scaffold.command import CommandScaffold
2+
from netnir.helpers.common.args import num_workers
3+
from netnir.helpers import output_writer
84
from netnir.core.networking import Networking
9-
from netnir.constants import NR
105
from nornir.plugins.functions.text import print_result
116

12-
"""fetch remove device configs
13-
"""
7+
"""fetch remove device configs"""
148

159

16-
class FetchConfig:
10+
class FetchConfig(CommandScaffold):
1711
"""
1812
cli command to fetch remote device configs via nornir's netmiko_show_command plugin
1913
"""
2014

21-
def __init__(self, args):
22-
"""initialize the class
23-
"""
24-
self.args = args
25-
self.nr = NR
26-
2715
@staticmethod
2816
def parser(parser):
2917
"""cli command parser
3018
3119
:param parser: type obj
3220
"""
33-
filter_host(parser)
34-
filter_group(parser)
35-
filter_hosts(parser)
21+
CommandScaffold.parser(parser)
3622
num_workers(parser)
3723

3824
def run(self):
@@ -41,12 +27,7 @@ def run(self):
4127
:return: nornir results
4228
"""
4329

44-
device_filter = filter_type(
45-
host=self.args.host, filter=self.args.filter, group=self.args.group
46-
)
47-
self.nr = inventory_filter(
48-
nr=self.nr, device_filter=device_filter["data"], type=device_filter["type"]
49-
)
30+
self.nr = self._inventory()
5031
networking = Networking(nr=self.nr, num_workers=self.args.workers)
5132
results = networking.fetch(commands="show running")
5233
output_writer(nornir_results=results, output_file="running.conf")

netnir/core/tasks/netconf.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,51 @@
11
from netnir.helpers.scaffold.command import CommandScaffold
2+
from netnir.helpers.common.args import (
3+
netconf_config,
4+
netconf_filter,
5+
netconf_source,
6+
netconf_target,
7+
config,
8+
)
29

310

411
class NetConf(CommandScaffold):
512
"""netconf commands"""
613

14+
@staticmethod
15+
def parser(parser):
16+
CommandScaffold.parser(parser)
17+
netconf_config(parser)
18+
netconf_filter(parser)
19+
netconf_source(parser)
20+
netconf_target(parser)
21+
config(parser)
22+
723
def run(self):
824
"""execute netconf commands
925
1026
:returns: nornir Result object
1127
"""
12-
from netnir.plugins.netconf import netconf_get
28+
from netnir.plugins.netconf import netconf_get_config, netconf_edit_config
1329
from nornir.plugins.functions.text import print_result
1430

1531
self.nr = self._inventory()
16-
results = self.nr.run(task=netconf_get, name="NETCONF GET CONFIG AND STATE")
32+
33+
if self.args.config:
34+
results = self.nr.run(
35+
task=netconf_edit_config,
36+
name="NETCONF EDIT CONFIG",
37+
target=self.args.target,
38+
nc_config=self.args.nc_config,
39+
)
40+
else:
41+
results = self.nr.run(
42+
task=netconf_get_config,
43+
source=self.args.source,
44+
name="NETCONF GET FILTERED CONFIG",
45+
nc_filter_type=self.args.nc_filter,
46+
nc_filter=self.args.nc_config,
47+
)
48+
1749
print_result(results)
1850

1951
return results

netnir/core/tasks/ssh.py

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,41 @@
1-
from netnir.constants import NR
1+
from netnir.helpers.scaffold.command import CommandScaffold
22
from netnir.core.networking import Networking
3-
from netnir.helpers import output_writer, filter_type, inventory_filter
3+
from netnir.helpers import output_writer
44
from netnir.helpers.common.args import (
5-
filter_host,
6-
filter_hosts,
7-
filter_group,
85
output,
96
num_workers,
7+
commands,
8+
config,
109
)
1110
from nornir.plugins.functions.text import print_result
1211
import sys
1312

14-
"""ssh cli commands
15-
"""
13+
"""ssh cli commands"""
1614

1715

18-
class Ssh:
16+
class Ssh(CommandScaffold):
1917
"""
2018
cli command to execute show and config commands via SSH
21-
22-
:params args: type obj
2319
"""
2420

25-
def __init__(self, args):
26-
"""
27-
initialize the ssh class
28-
"""
29-
self.args = args
30-
self.nr = NR
31-
3221
@staticmethod
3322
def parser(parser):
3423
"""
3524
cli command parser
3625
"""
37-
filter_host(parser)
38-
filter_group(parser)
39-
filter_hosts(parser)
26+
CommandScaffold.parser(parser)
4027
output(parser)
4128
num_workers(parser)
42-
43-
parser.add_argument(
44-
"--commands",
45-
"-c",
46-
help="commands to execute",
47-
action="append",
48-
required=False,
49-
)
50-
parser.add_argument(
51-
"--config",
52-
help="execute commands in config mode",
53-
required=False,
54-
nargs="?",
55-
const=True,
56-
)
29+
commands(parser)
30+
config(parser)
5731

5832
def run(self):
5933
"""
6034
cli command execution
6135
"""
6236

63-
device_filter = filter_type(
64-
host=self.args.host, filter=self.args.filter, group=self.args.group
65-
)
66-
hosts = inventory_filter(
67-
self.nr, device_filter=device_filter["data"], type=device_filter["type"]
68-
)
69-
networking = Networking(nr=hosts, num_workers=self.args.workers,)
37+
self.nr = self._inventory()
38+
networking = Networking(nr=self.nr, num_workers=self.args.workers,)
7039

7140
if self.args.config:
7241
results = networking.config(self.args.commands)

netnir/helpers/__init__.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,43 @@
99
"""
1010

1111

12-
def device_mapper(os_type: str):
12+
def device_mapper(os_type: str, proto: str = "netmiko"):
1313
"""
1414
map an os type to a netmiko device_type
1515
16-
:param os_type: type str
16+
:params os_type: type str
17+
:params proto: type str, default "netmiko"
1718
18-
:return: device_type string
19+
:returns: device_type string
1920
"""
20-
device_types = {
21-
"ios": "cisco_ios",
22-
"iosxr": "cisco_xr",
23-
"iosxe": "cisco_xe",
24-
"nxos": "cisco_nxos",
25-
"eos": "arista_eos",
26-
}
27-
28-
return device_types[os_type]
21+
if proto == "netmiko":
22+
device_types = {
23+
"ios": "cisco_ios",
24+
"iosxr": "cisco_xr",
25+
"iosxe": "cisco_xe",
26+
"nxos": "cisco_nxos",
27+
"eos": "arista_eos",
28+
}
29+
try:
30+
result = device_types[os_type]
31+
except KeyError:
32+
return os_type
33+
elif proto == "netconf":
34+
device_types = {
35+
"csr": "csr",
36+
"iosxr": "iosxr",
37+
"iosxe": "iosxe",
38+
"nxos": "nexus",
39+
"junos": "junos",
40+
}
41+
try:
42+
result = device_types[os_type]
43+
except KeyError:
44+
return "default"
45+
else:
46+
result = os_type
47+
48+
return result
2949

3050

3151
def render_filter(pattern: list):

0 commit comments

Comments
 (0)