Skip to content

Commit

Permalink
added support to initialize SimpleInventory with native objects (#348)
Browse files Browse the repository at this point in the history
* added support to initialize SimpleInventory with native objects

* fix mypy

* fix pylama
  • Loading branch information
dbarrosop committed Apr 27, 2019
1 parent 90c7627 commit d5eefae
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 21 deletions.
53 changes: 32 additions & 21 deletions nornir/plugins/inventory/simple.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import logging
import os
from typing import Any
from typing import Any, Optional

from nornir.core.deserializer.inventory import GroupsDict, Inventory, VarsDict
from nornir.core.deserializer.inventory import (
HostsDict,
GroupsDict,
Inventory,
VarsDict,
)

import ruamel.yaml

Expand All @@ -15,28 +20,34 @@ def __init__(
host_file: str = "hosts.yaml",
group_file: str = "groups.yaml",
defaults_file: str = "defaults.yaml",
hosts: Optional[HostsDict] = None,
groups: Optional[GroupsDict] = None,
defaults: Optional[VarsDict] = None,
*args: Any,
**kwargs: Any
) -> None:
yml = ruamel.yaml.YAML(typ="safe")
with open(host_file, "r") as f:
hosts = yml.load(f)
if hosts is None:
yml = ruamel.yaml.YAML(typ="safe")
with open(host_file, "r") as f:
hosts = yml.load(f)

groups: GroupsDict = {}
if group_file:
if os.path.exists(group_file):
with open(group_file, "r") as f:
groups = yml.load(f) or {}
else:
logger.debug("File %r was not found", group_file)
groups = {}
if groups is None:
groups = {}
if group_file:
if os.path.exists(group_file):
with open(group_file, "r") as f:
groups = yml.load(f) or {}
else:
logger.debug("File %r was not found", group_file)
groups = {}

defaults: VarsDict = {}
if defaults_file:
if os.path.exists(defaults_file):
with open(defaults_file, "r") as f:
defaults = yml.load(f) or {}
else:
logger.debug("File %r was not found", defaults_file)
defaults = {}
if defaults is None:
defaults = {}
if defaults_file:
if os.path.exists(defaults_file):
with open(defaults_file, "r") as f:
defaults = yml.load(f) or {}
else:
logger.debug("File %r was not found", defaults_file)
defaults = {}
super().__init__(hosts=hosts, groups=groups, defaults=defaults, *args, **kwargs)
68 changes: 68 additions & 0 deletions tests/plugins/inventory/test_simple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import os

from nornir.plugins.inventory import simple
from nornir.core.deserializer.inventory import Inventory

BASE_PATH = os.path.join(os.path.dirname(__file__), "nsot")


class Test(object):
def test_inventory(self):
hosts = {
"host1": {
"username": "user",
"groups": ["group_a"],
"data": {"a": 1, "b": 2},
},
"host2": {"username": "user2", "data": {"a": 1, "b": 2}},
}
groups = {"group_a": {"platform": "linux"}}
defaults = {"data": {"a_default": "asd"}}
inv = simple.SimpleInventory.deserialize(
hosts=hosts, groups=groups, defaults=defaults
)
assert Inventory.serialize(inv).dict() == {
"hosts": {
"host1": {
"hostname": None,
"port": None,
"username": "user",
"password": None,
"platform": None,
"groups": ["group_a"],
"data": {"a": 1, "b": 2},
"connection_options": {},
},
"host2": {
"hostname": None,
"port": None,
"username": "user2",
"password": None,
"platform": None,
"groups": [],
"data": {"a": 1, "b": 2},
"connection_options": {},
},
},
"groups": {
"group_a": {
"hostname": None,
"port": None,
"username": None,
"password": None,
"platform": "linux",
"groups": [],
"data": {},
"connection_options": {},
}
},
"defaults": {
"hostname": None,
"port": None,
"username": None,
"password": None,
"platform": None,
"data": {"a_default": "asd"},
"connection_options": {},
},
}

0 comments on commit d5eefae

Please sign in to comment.