Beispiel #1
0
    def add_remote(self, remote):
        """


        Args:
          remote:

        Returns:

        """
        vlan_driver = get_driver("vlan")
        self.remote = vlan_driver()
        self.remote.add(remote)
Beispiel #2
0
    def add_remote(self, remote):
        """

        Args:
          remote:

        Returns:

        """
        ip_driver = get_driver("ip_address")
        self.remote = ip_driver()
        self.remote.add(remote)

        return True
Beispiel #3
0
    def add_remote(self, remote):
        """

        Args:
          remote: object

        Returns:

        """
        prefix_driver = get_driver("prefix")
        self.remote = prefix_driver()
        self.remote.add(remote)

        return True
Beispiel #4
0
    def add_remote(self, remote):
        """


        Args:
          remote:

        Returns:

        """
        intf_driver = get_driver("interface")
        self.remote = intf_driver()
        self.remote.add(remote)

        return True
Beispiel #5
0
    def add_remote(self, remote):
        """

        Args:
          remote:

        Returns:

        """
        optic_driver = get_driver("optic")
        self.remote = optic_driver()
        self.remote.add(remote)

        if not self.id and self.remote.serial:
            self.id = self.remote.serial

        return True
Beispiel #6
0
    def update_remote(self, nb):  # pylint: disable=W0221
        """ """
        if not self.is_valid:
            return False

        if self.local and self.remote:
            return False

        if not self.local and self.remote:
            logger.debug(
                f"Cable {self.id} not present locally, deleting in netbox .. ")
            self.remote.delete()
            return True

        if self.local and not self.remote:

            if "a" not in self.interfaces or "z" not in self.interfaces:

                logger.warning(
                    f"Unable to create cable {self.id} in Netbox, both interfaces are not present"
                )
                return False

            if (not self.interfaces["a"].remote.remote
                    or not self.interfaces["z"].remote.remote):

                logger.warning(
                    f"Unable to create cable {self.id} in Netbox, both interfaces do not have a remote object"
                )
                return False

            logger.info(
                f"Cable {self.id} not present will create it in netbox ")

            nbc = nb.dcim.cables.create(
                termination_a_type="dcim.interface",
                termination_a_id=self.interfaces["a"].remote.remote.id,
                termination_b_type="dcim.interface",
                termination_b_id=self.interfaces["z"].remote.remote.id,
            )

            cable_driver = get_driver("cable")
            self.remote = cable_driver()
            self.remote.add(cable=nbc)
Beispiel #7
0
    def get_remote_cables(self):
        """ """

        cables = {}

        for intf in self.interfaces.values():
            if not intf.remote and not intf.remote.remote:
                continue

            if intf.remote.remote.connected_endpoint_type == "dcim.interface":

                cable_driver = get_driver("cable")
                cable = cable_driver()
                cable.add(interface=intf.remote.remote)
                # cable.add_device(self.name, self.intf.name)

                if cable.unique_id not in cables.keys():
                    cables[cable.unique_id] = cable
                else:
                    logger.debug(
                        f"{self.name} | Cable {cable.unique_id} is present more than once on this device"
                    )

        return cables
Beispiel #8
0
    def update_interface_remote(self, intf):
        """
        Update Interface on the remote system
          Create or Update interface as needed
          Create/Update or Delete Ips as needed

        Args:
          intf:

        Returns:

        """
        if intf.exist_local():
            intf_driver = get_driver("interface")
            intf_properties = intf_driver.get_properties(intf.local)

            if config.main["import_vlans"] != "no":
                if intf.local.mode in ["TRUNK", "ACCESS"
                                       ] and intf.local.access_vlan:
                    intf_properties[
                        "untagged_vlan"] = self.site.convert_vid_to_nid(
                            intf.local.access_vlan)
                elif (intf.local.mode in ["TRUNK", "ACCESS"]
                      and not intf.local.access_vlan):
                    intf_properties["untagged_vlan"] = None

                if (intf.local.mode in ["TRUNK", "L3_SUB_VLAN"]
                        and intf.local.allowed_vlans):
                    intf_properties[
                        "tagged_vlans"] = self.site.convert_vids_to_nids(
                            intf.local.allowed_vlans)
                elif (intf.local.mode in ["TRUNK", "L3_SUB_VLAN"]
                      and not intf.local.allowed_vlans):
                    intf_properties["tagged_vlans"] = []

            if intf.local.is_lag_member:
                if intf.local.parent in self.interfaces.keys():
                    if not self.interfaces[intf.local.parent].exist_remote():
                        logger.warning(
                            f"{self.name} | Interface {intf.name} is a member of lag {intf.local.parent}, but {intf.local.parent} do not exist remotely"
                        )
                    else:
                        intf_properties["lag"] = self.interfaces[
                            intf.local.parent].remote.remote.id

                else:
                    logger.warning(
                        f"{self.name} | Interface {intf.local.name} is a member of lag {intf.local.parent}, but {intf.local.parent} is not in the list"
                    )

            elif (not intf.local.is_lag_member and intf.remote
                  and intf.remote.is_lag_member):
                intf_properties["lag"] = None

            if intf.exist_local() and not intf.exist_remote():

                intf_properties["device"] = self.remote.id
                intf_properties["name"] = intf.name

                try:
                    remote = self.nb.dcim.interfaces.create(**intf_properties)
                except:
                    logger.warning(
                        f"{self.name} | Something went wrong while trying to create interface {intf.name} in netbox",
                        exc_info=True,
                    )
                    logger.debug(
                        f"{self.name} | {intf.name}: properties {intf_properties}"
                    )
                    return False

                intf.add_remote(remote)
                logger.debug(
                    f"{self.name} | Interface {intf.name} created in Netbox")
                changelog_create(
                    "interface",
                    f"{self.name}::{intf.name}",
                    remote.id,
                    params=intf_properties,
                )

            elif (intf.exist_local() and intf.exist_remote()
                  and intf.diff().nbr_diffs() != 0):
                diff = intf.diff()
                try:
                    intf_updated = intf.remote.remote.update(
                        data=intf_properties)
                except:
                    logger.warning(
                        f"{self.name} | Something went wrong while trying to update the interface {intf.name} in netbox",
                        exc_info=True,
                    )
                    logger.debug(
                        f"{self.name} | {intf.name}: properties {intf_properties}"
                    )
                    return False

                logger.debug(
                    f"{self.name} | Interface {intf.name} updated in Netbox: {intf_properties}"
                )
                changelog_update(
                    "interface",
                    f"{self.name}::{intf.name}",
                    intf.remote.remote.id,
                    # params=diff.items_to_dict(),
                    params=intf_properties,
                )

        # ----------------------------------------------------------
        # Update IPs
        # ----------------------------------------------------------
        for ip in intf.ips.values():
            if ip.exist_local() and not ip.exist_remote():
                try:
                    ip_address = self.nb.ipam.ip_addresses.create(
                        address=ip.address, interface=intf.remote.remote.id)
                except:
                    logger.warning(
                        f"{self.name} | Something went wrong while trying to create the IP {ip.address} (intf:{intf.remote.remote.id}) in netbox",
                        exc_info=True,
                    )

                    return False

                ip.add_remote(ip_address)
                logger.debug(
                    f"{self.name} | IP {ip.address} created in Netbox")
                changelog_create(
                    "ipaddress",
                    ip.address,
                    ip_address.id,
                    params={
                        "interface": intf.name,
                        "device": self.name
                    },
                )

            # TODO need to implement IP update
            # If IP address isn't on the device, delete it from netbox, unless it's the primary IP
            elif not ip.exist_local() and ip.exist_remote():
                if (self.remote.primary_ip and ip.remote.address
                        == self.remote.primary_ip.address):
                    logger.warning(
                        f"{self.name} | Unable to delete IP {ip.address}, currently primary IP"
                    )
                else:
                    ip.delete_remote()
                    logger.debug(
                        f"{self.name} | IP {ip.address} deleted in Netbox")

        # ----------------------------------------------------------
        # Update Optic is defined
        # ----------------------------------------------------------
        if intf.optic:
            if intf.optic.exist_local() and not intf.optic.exist_remote():

                optic = self.nb.dcim.inventory_items.create(
                    name=intf.optic.local.serial,
                    part_id=intf.optic.local.optic_type,
                    device=self.remote.id,
                    description=intf.name,
                    serial=intf.optic.local.serial,
                    tags=["optic"],
                )
                intf.optic.add_remote(optic)
                logger.debug(
                    f"{self.name} | Optic for {intf.name} created in Netbox")
                changelog_create(
                    "optic",
                    intf.optic.local.serial,
                    optic.id,
                    params=dict(
                        name=intf.optic.local.serial,
                        part_id=intf.optic.local.optic_type,
                        device=self.remote.id,
                        description=intf.name,
                        serial=intf.optic.local.serial,
                        tags=["optic"],
                    ),
                )

            elif (intf.optic.exist_local() and intf.optic.exist_remote()
                  and intf.optic.diff().has_diffs()):

                intf.optic.remote.remote.update(data=dict(
                    name=intf.optic.local.serial,
                    part_id=intf.optic.local.optic_type,
                    device=self.remote.id,
                    description=intf.name,
                    serial=intf.optic.local.serial,
                    tags=["optic"],
                ))
                # TODO need to redo this part to clean it up and ensure the object gets properly updated
                logger.debug(
                    f"{self.name} | Optic for {intf.name} updated in Netbox")
                changelog_update(
                    "optic",
                    intf.optic.local.serial,
                    intf.optic.remote.remote.id,
                    params=dict(
                        name=intf.optic.local.serial,
                        part_id=intf.optic.local.optic_type,
                        device=self.remote.id,
                        description=intf.name,
                        serial=intf.optic.local.serial,
                        tags=["optic"],
                    ),
                )

            elif not intf.optic.exist_local() and intf.optic.exist_remote():
                intf.optic.delete_remote()
                logger.debug(
                    f"{self.name} | Optic {intf.optic.remote.serial} deleted in Netbox"
                )