def deserialize(
     cls,
     name: str,
     hostname: Optional[str] = None,
     port: Optional[int] = None,
     username: Optional[str] = None,
     password: Optional[str] = None,
     platform: Optional[str] = None,
     groups: Optional[List[str]] = None,
     data: Optional[Dict[str, Any]] = None,
     connection_options: Optional[Dict[str, ConnectionOptions]] = None,
     defaults: inventory.Defaults = None,
 ) -> Dict[str, Any]:
     parent_groups = inventory.ParentGroups(groups)
     connection_options = connection_options or {}
     conn_opts = {
         k: inventory.ConnectionOptions(**v)
         for k, v in connection_options.items()
     }
     return {
         "name": name,
         "hostname": hostname,
         "port": port,
         "username": username,
         "password": password,
         "platform": platform,
         "groups": parent_groups,
         "data": data,
         "connection_options": conn_opts,
         "defaults": defaults,
     }
Esempio n. 2
0
 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 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,
        )
Esempio n. 4
0
 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")