def deserialize(cls, transform_function: Optional[Callable[..., Any]] = None, transform_function_options: Optional[Dict[str, Any]] = None, *args: Any, **kwargs: Any) -> inventory.Inventory: transform_function_options = transform_function_options or {} deserialized = cls(*args, **kwargs) defaults_dict = deserialized.defaults.dict() for k, v in defaults_dict["connection_options"].items(): defaults_dict["connection_options"][ k] = inventory.ConnectionOptions(**v) defaults = inventory.Defaults(**defaults_dict) hosts = inventory.Hosts() for n, h in deserialized.hosts.items(): hosts[n] = InventoryElement.deserialize_host(defaults=defaults, name=n, **h.dict()) groups = inventory.Groups() for n, g in deserialized.groups.items(): groups[n] = InventoryElement.deserialize_group(name=n, **g.dict()) return inventory.Inventory( hosts=hosts, groups=groups, defaults=defaults, transform_function=transform_function, transform_function_options=transform_function_options, )
def test_add_group(self): connection_options = {"username": "******", "password": "******"} 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 = inventory.ConnectionOptions( extras={"device_type": "cisco_ios"}) inv.groups["g3"] = inventory.Group( name="g3", username="******", connection_options={"netmiko": g3_connection_options}, defaults=defaults, ) assert "g1" in [i.name for i in inv.groups["g2"].groups] 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")
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 = inventory.ConnectionOptions( extras={"device_type": "cisco_ios"}) inv.hosts["h3"] = inventory.Host( name="h3", groups=[g1], platform="TestPlatform", connection_options={"netmiko": h3_connection_options}, defaults=defaults, ) assert "h3" in inv.hosts assert "g1" in [i.name for i 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": "******", "password": "******"} 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="******", connection_options=g3_connection_options) assert "g1" in [i.name for i in inv.groups["g2"].groups.refs] 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") # Test with one undefined parent group with pytest.raises(KeyError): inv.add_group(name="g4", groups=["undefined"]) # Test with one defined and one undefined parent group with pytest.raises(KeyError): inv.add_group(name="g4", groups=["g1", "undefined"])
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 [i.name for i in inv.hosts["h3"].groups.refs] 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") with pytest.raises(KeyError): inv.add_host(name="h4", groups=["not_defined"]) # Test with one good and one undefined group with pytest.raises(KeyError): inv.add_host(name="h5", groups=["g1", "not_defined"])