Ejemplo n.º 1
0
def update_description(inventory: Inventory) -> None:
    infra_devices = inventory.filter(
        F(has_parent_group='infra')).hosts.values()
    for device in infra_devices:
        for interface in device.get('interfaces', []):
            if 'connected_device' in interface:
                connected_device_info = interface["connected_device"]
                connected_device_name = connected_device_info["name"]
                port = connected_device_info["port"]
                connected_device = inventory.hosts[connected_device_name]
                rack = connected_device['rack']
                rack_unit = connected_device['rack_unit']
                description = (
                    f"To Rack {rack} RU {rack_unit} -> {connected_device_name} {port}"
                )
                interface["description"] = description
def update_matrix(
    inventory: Inventory,
    topologies: Dict[str, TopologyDict],
    deployment: DeploymentDict,
) -> None:

    # (lab_hostname, pod_number) -> device hostname in inventory
    # (edge-1, 1000) -> 9348-1

    hostname_and_pod_num_to_device: Dict[Tuple[str, int], str] = {}

    for device, device_info in deployment["devices"].items():
        pod_number = device_info["pod_number"]
        lab_hostname = device_info["lab_hostname"]
        hostname_and_pod_num_to_device[(lab_hostname, pod_number)] = device

    # (R1, Ethernet1/1) -> ('matrix-1', 1)
    device_port_to_matrix_port = {}

    matrix_switches = inventory.filter(F(has_parent_group="matrix-switches")).hosts.values()

    for matrix_switch in matrix_switches:
        for port_number, matrix_interface in enumerate(
            matrix_switch.get("interfaces", [])
        ):
            if matrix_interface.get("mode") == "dot1q-tunnel":
                if "connected_device" not in matrix_interface:
                    raise ValueError(
                        f"Interface {matrix_interface['name']} on the switch "
                        f"{matrix_switch.name} is "
                        f"dot1q-tunnel but does not have connected_device variable"
                    )

                connected_device = matrix_interface["connected_device"]["name"]
                connected_device_port = matrix_interface["connected_device"]["port"]
                # print(connected_device_port, connected_device_name)
                device_port_to_matrix_port[
                    (connected_device, connected_device_port)
                ] = (matrix_switch.name, port_number)

    current_dot1q_tunnel_vlan = matrix_switch.get("dot1q_tunnel_vlan_start")

    for pod in deployment["pods"]:
        pod_number = pod["pod_number"]
        topology = pod["topology"]
        for connection in topologies[topology]["connections"]:
            for connection_end in connection:
                lab_hostname = connection_end["lab_hostname"]
                port = connection_end["port"]
                device = hostname_and_pod_num_to_device[(lab_hostname, pod_number)]
                pod_device = inventory.hosts[device]
                if (
                    pod_device.get("lab_hostname", lab_hostname) != lab_hostname
                    and pod_device.get("pod_number", pod_number) != pod_number
                ):
                    raise ValueError(
                        f"Trying to assign lab hostname '{lab_hostname}' "
                        f"and pod number '{pod_number}', "
                        f"but this device already has"
                        f"lab hostname '{pod_device.name}'"
                        f'and pod number "{pod_device.get("pod_number")}" assigned'
                    )

                else:
                    pod_device["lab_hostname"] = lab_hostname
                    pod_device["pod_number"] = pod_number
                    pod_device["lab_template"] = topology
                    pod_device["updated_vars"] = [
                        "lab_hostname",
                        "pod_number",
                        "lab_template",
                    ]

                (
                    matrix_switch_name,
                    matrix_switch_port_number,
                ) = device_port_to_matrix_port[(device, port)]

                matrix_switch = inventory.hosts[matrix_switch_name]
                matrix_interface = matrix_switch["interfaces"][
                    matrix_switch_port_number
                ]
                if "access_vlan" in matrix_interface:
                    raise ValueError(
                        f"{matrix_switch.name} already has vlan "
                        f"assigned to the interface {mastrix_interface['name']}"
                    )

                elif matrix_interface.get("mode") != "dot1q-tunnel":
                    raise ValueError(
                        f"{matrix_switch.name} interface {matrix_interface['name']} "
                        f"has mode {matrix_interface['mode']} "
                        f"instead of 'dot1q-tunnel'"
                    )

                else:
                    matrix_interface["access_vlan"] = current_dot1q_tunnel_vlan
                    matrix_interface["shutdown"] = False
                    matrix_interface["dynamic"] = True
                    matrix_interface["description"] = (
                        f"connected to {port} {device} "
                        f"| lab hostname: {lab_hostname} "
                        f"| pod: {pod_number}"
                    )
                    # matrix_switch_vars["updated_vars"] = ["interfaces"]

            current_dot1q_tunnel_vlan += 1

        # for the next pod start with the vlan divisible by 10
        remainder = current_dot1q_tunnel_vlan % 10
        if remainder:
            current_dot1q_tunnel_vlan += 10 - remainder

    for matrix_switch in matrix_switches:
        for matrix_interface in matrix_switch["interfaces"]:
            if (
                matrix_interface.get("access_vlan") is None
                and matrix_interface.get("mode") == "dot1q-tunnel"
            ):
                matrix_interface["shutdown"] = True