How to use the peeringdb.models.Network.objects.get function in peeringdb

To help you get started, we’ve selected a few peeringdb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github respawner / peering-manager / peeringdb / models.py View on Github external
self.asn,
                self.ixlan_id,
            )
            return

        # Trigger the build of a new PeerRecord or just ignore if it already exists,
        # assumes it exists
        # Note that there is not point of doing the same thing when a Network
        # or a NetworkIXLAN is deleted because it will automatically delete the
        # PeerRecord linked to it using the foreign key (with the CASCADE mode)
        network = None
        peer_record_exists = True

        try:
            # Try guessing the Network given its ASN
            network = Network.objects.get(asn=self.asn)
            # Try finding if the PeerRecord already exists
            PeerRecord.objects.get(network=network, network_ixlan=self)
        except Network.DoesNotExist:
            # The network does not exist, well not much to do
            self.logger.debug(
                "network with as%s does not exist, required for "
                "peer record creation",
                self.asn,
            )
        except PeerRecord.DoesNotExist:
            # But if the exception is raised, it does not
            peer_record_exists = False

        # If the PeerRecord does not exist, create it
        if not peer_record_exists:
            PeerRecord.objects.create(network=network, network_ixlan=self)
github respawner / peering-manager / peeringdb / http.py View on Github external
def get_autonomous_system(self, asn):
        """
        Return an AS (and its details) given its ASN. The result can come from
        the local database (cache built with the peeringdb_sync command). If
        the AS details are not found in the local database, they will be
        fetched online which will take more time.
        """
        try:
            # Try to get from cached data
            network = Network.objects.get(asn=asn)
        except Network.DoesNotExist:
            # If no cached data found, query the API
            search = {"asn": asn}
            result = self.lookup(NAMESPACES["network"], search)

            if not result or not result["data"]:
                return None

            network = Object(result["data"][0])

        return network
github respawner / peering-manager / peering / models.py View on Github external
def get_peeringdb_network(self):
        try:
            return Network.objects.get(asn=self.asn)
        except Network.DoesNotExist:
            return None
github respawner / peering-manager / peeringdb / http.py View on Github external
# Build the cache
            for network_ixlan in NetworkIXLAN.objects.all():
                # Ignore if we have no IPv6 and no IPv4 to peer with
                if not network_ixlan.ipaddr6 and not network_ixlan.ipaddr4:
                    self.logger.debug(
                        "network ixlan with as%s and ixlan id %s"
                        " ignored, no ipv6 and no ipv4",
                        network_ixlan.asn,
                        network_ixlan.ixlan_id,
                    )
                    continue

                network = None
                try:
                    network = Network.objects.get(asn=network_ixlan.asn)
                except Network.DoesNotExist:
                    self.logger.debug("unable to find network as%s", network_ixlan.asn)

                if network:
                    PeerRecord.objects.create(
                        network=network, network_ixlan=network_ixlan
                    )
                    self.logger.debug(
                        "peer record with network as%s and ixlan" "id %s created",
                        network_ixlan.asn,
                        network_ixlan.ixlan_id,
                    )
                    indexed += 1
                else:
                    self.logger.debug(
                        "network ixlan with as%s and ixlan id %s" " ignored",