Skip to content

Commit

Permalink
Merge pull request #372 from brandomando/add_host_add_group
Browse files Browse the repository at this point in the history
Added add_host and add_group functions to nornir.core.inventory.Inventory class
  • Loading branch information
dbarrosop committed Apr 8, 2019
2 parents ab5bedf + 441e42f commit 92790c2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
22 changes: 22 additions & 0 deletions nornir/core/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,25 @@ def children_of_group(self, group: Union[str, Group]) -> Set[Host]:
if host.has_parent_group(group):
hosts.add(host)
return hosts

def add_host(self, name: str, **kwargs) -> None:
"""
Add a host to the inventory after initialization
"""
host = {
name: deserializer.inventory.InventoryElement.deserialize_host(
name=name, defaults=self.defaults, **kwargs
)
}
self.hosts.update(host)

def add_group(self, name: str, **kwargs) -> None:
"""
Add a group to the inventory after initialization
"""
group = {
name: deserializer.inventory.InventoryElement.deserialize_group(
name=name, defaults=self.defaults, **kwargs
)
}
self.groups.update(group)
56 changes: 56 additions & 0 deletions tests/core/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,59 @@ def test_children_of_obj(self):
inv.hosts["dev4.group_2"],
inv.hosts["dev3.group_2"],
}

def test_add_host(self):
data = {"test_var": "test_value"}
defaults = inventory.Defaults(data=data)
g1 = inventory.Group(name="g1")
g2 = inventory.Group(name="g2", groups=inventory.ParentGroups(["g1"]))
h1 = inventory.Host(name="h1", groups=inventory.ParentGroups(["g1", "g2"]))
h2 = inventory.Host(name="h2")
hosts = {"h1": h1, "h2": h2}
groups = {"g1": g1, "g2": g2}
inv = inventory.Inventory(hosts=hosts, groups=groups, defaults=defaults)
h3_connection_options = {"netmiko": {"extras": {"device_type": "cisco_ios"}}}
inv.add_host(
name="h3",
groups=["g1"],
platform="TestPlatform",
connection_options=h3_connection_options,
)
assert "h3" in inv.hosts
assert "g1" in inv.hosts["h3"].groups
assert "test_var" in inv.hosts["h3"].defaults.data.keys()
assert inv.hosts["h3"].defaults.data.get("test_var") == "test_value"
assert inv.hosts["h3"].platform == "TestPlatform"
assert (
inv.hosts["h3"].connection_options["netmiko"].extras["device_type"]
== "cisco_ios"
)

def test_add_group(self):
connection_options = {"username": "test_user", "password": "test_pass"}
data = {"test_var": "test_value"}
defaults = inventory.Defaults(data=data, connection_options=connection_options)
g1 = inventory.Group(name="g1")
g2 = inventory.Group(name="g2", groups=inventory.ParentGroups(["g1"]))
h1 = inventory.Host(name="h1", groups=inventory.ParentGroups(["g1", "g2"]))
h2 = inventory.Host(name="h2")
hosts = {"h1": h1, "h2": h2}
groups = {"g1": g1, "g2": g2}
inv = inventory.Inventory(hosts=hosts, groups=groups, defaults=defaults)
g3_connection_options = {"netmiko": {"extras": {"device_type": "cisco_ios"}}}
inv.add_group(
name="g3", username="test_user", connection_options=g3_connection_options
)
assert "g3" in inv.groups
assert (
inv.groups["g3"].defaults.connection_options.get("username") == "test_user"
)
assert (
inv.groups["g3"].defaults.connection_options.get("password") == "test_pass"
)
assert "test_var" in inv.groups["g3"].defaults.data.keys()
assert "test_value" == inv.groups["g3"].defaults.data.get("test_var")
assert (
inv.groups["g3"].connection_options["netmiko"].extras["device_type"]
== "cisco_ios"
)

0 comments on commit 92790c2

Please sign in to comment.