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

Commit

Permalink
Merge pull request #13 from netdevops/netconf
Browse files Browse the repository at this point in the history
add netconf
  • Loading branch information
jtdub authored Jul 31, 2020
2 parents bde89e0 + aaa9154 commit 4540533
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 45 deletions.
2 changes: 1 addition & 1 deletion netnir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
netnir will create the default config and folders.
"""

__version__ = "0.0.10"
__version__ = "0.0.11"
8 changes: 4 additions & 4 deletions netnir/core/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def nhosts(self):
)
data[host] = {
"hostname": f"{host}.{domain}" if domain else host,
"username": creds["username"],
"password": creds["password"],
"username": host_vars.get("username", creds["username"]),
"password": host_vars.get("password", creds["password"]),
"port": host_vars.get("port", 22),
"platform": device_mapper(host_vars["os"]),
"groups": host_vars.get("groups", list()),
Expand All @@ -64,8 +64,8 @@ def nhosts(self):
"connection_options": {
"netconf": {
"hostname": f"{host}.{domain}" if domain else host,
"username": creds["username"],
"password": creds["password"],
"username": host_vars.get("username", creds["username"]),
"password": host_vars.get("password", creds["password"]),
"platform": host_vars.get("os"),
"port": host_vars.get("port", 830),
"extras": {"hostkey_verify": False},
Expand Down
45 changes: 6 additions & 39 deletions netnir/core/tasks/inventory.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,16 @@
from netnir.constants import NR
from netnir.helpers.common.args import filter_host, filter_hosts, filter_group
from netnir.helpers import filter_type, inventory_filter
from netnir.plugins.facts import inventory_facts
from nornir.plugins.functions.text import print_result
from netnir.helpers.scaffold.command import CommandScaffold


"""inventory cli commands
"""


class Inventory:
class Inventory(CommandScaffold):
"""
cli based inventory search
:param args: type obj
"""

def __init__(self, args):
"""
initialize the inventory class
"""
self.args = args
self.nr = NR

@staticmethod
def parser(parser):
"""
cli command parser
"""
filter_host(parser)
filter_hosts(parser)
filter_group(parser)

def run(self):
"""
cli execution
"""
devices_filter = filter_type(
host=self.args.host, filter=self.args.filter, group=self.args.group
)
self.nr = inventory_filter(
nr=self.nr,
device_filter=devices_filter["data"],
type=devices_filter["type"],
)
from netnir.plugins.facts import inventory_facts
from nornir.plugins.functions.text import print_result

self.nr = self._inventory()
results = self.nr.run(task=inventory_facts)
print_result(results)

Expand Down
19 changes: 19 additions & 0 deletions netnir/core/tasks/netconf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from netnir.helpers.scaffold.command import CommandScaffold


class NetConf(CommandScaffold):
"""netconf commands"""

def run(self):
"""execute netconf commands
:returns: nornir Result object
"""
from netnir.plugins.netconf import netconf_get
from nornir.plugins.functions.text import print_result

self.nr = self._inventory()
results = self.nr.run(task=netconf_get, name="NETCONF GET CONFIG AND STATE")
print_result(results)

return results
4 changes: 4 additions & 0 deletions netnir/helpers/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
"class": "netnir.core.tasks.ssh.Ssh",
"description": "command and config execution over SSH",
},
"netconf": {
"class": "netnir.core.tasks.netconf.NetConf",
"description": "command and config execution over NETCONF",
},
"fetch": {
"class": "netnir.core.tasks.fetch.Fetch",
"description": "fetch commands",
Expand Down
49 changes: 49 additions & 0 deletions netnir/helpers/scaffold/command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
""" netnir command scaffolding """


class CommandScaffold:
""" scaffold class """

def __init__(self, args):
"""initialize the class
:params args: type object
"""
from netnir.constants import NR

self.args = args
self.nr = NR

@staticmethod
def parser(parser):
"""command parser function
:params parser: type object
"""
from netnir.helpers.common.args import filter_host, filter_hosts, filter_group

filter_host(parser)
filter_hosts(parser)
filter_group(parser)

def run(self):
"""things to do"""
return "things to do"

def _inventory(self):
"""filter inventory
:returns: filtered nornir inventory object
"""
from netnir.helpers import inventory_filter, filter_type

devices_filter = filter_type(
host=self.args.host, filter=self.args.filter, group=self.args.group
)
self.nr = inventory_filter(
nr=self.nr,
device_filter=devices_filter["data"],
type=devices_filter["type"],
)

return self.nr
14 changes: 14 additions & 0 deletions netnir/plugins/netconf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from nornir.core.task import Task, Result


def netconf_get(task: Task) -> Result:
"""nornir netconf get task
:params task: type object
"""
manager = task.host.get_connection(
connection="netconf", configuration=task.nornir.config
)
result = manager.get()

return Result(result=result, host=task.host)
3 changes: 3 additions & 0 deletions tests/data/netnir.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ plugins:
ssh:
class: netnir.core.tasks.ssh.Ssh
description: "command and config execution over SSH"
netconf:
class: netnir.core.tasks.netconf.NetConf
description: "command and config execution over NETCONF"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def test_nornir_config(initial_setup):
def test_netnir_config(initial_setup):
from netnir.helpers import netnir_config

test_config = {
Expand Down Expand Up @@ -28,6 +28,10 @@ def test_nornir_config(initial_setup):
"class": "netnir.core.tasks.ssh.Ssh",
"description": "command and config execution over SSH",
},
"netconf": {
"class": "netnir.core.tasks.netconf.NetConf",
"description": "command and config execution over NETCONF",
},
"fetch": {
"class": "netnir.core.tasks.fetch.Fetch",
"description": "fetch commands",
Expand Down

0 comments on commit 4540533

Please sign in to comment.