Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
)
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)
self.logger.debug(
"peer record created with as%s and ixlan id %s", self.asn, self.ixlan_id
existing_sessions = self.get_peering_sessions()
ipv4_sessions = []
ipv6_sessions = []
for session in existing_sessions:
ip = ipaddress.ip_address(session.ip_address)
if ip.version == 6:
ipv6_sessions.append(str(ip))
elif ip.version == 4:
ipv4_sessions.append(str(ip))
else:
self.logger.debug("peering session with strange ip: %s", ip)
# Find all peers belonging to the same IX and order them by ASN
# Exclude our own ASN and already existing sessions
return (
PeerRecord.objects.filter(
Q(network_ixlan__ixlan_id=network_ixlan.ixlan_id)
& ~Q(network__asn=settings.MY_ASN)
& (
~Q(network_ixlan__ipaddr6__in=ipv6_sessions)
| ~Q(network_ixlan__ipaddr4__in=ipv4_sessions)
)
)
.prefetch_related("network")
.prefetch_related("network_ixlan")
.order_by("network__asn")
)
def statistics(self, request):
return Response(
{
"contact-count": Contact.objects.count(),
"network-count": Network.objects.count(),
"network-ixlan-count": NetworkIXLAN.objects.count(),
"peer-record-count": PeerRecord.objects.count(),
}