more child and parent fields for prefixes and addresses

This commit is contained in:
Clint Armstrong 2025-04-09 09:23:26 -04:00
parent 006c22a54f
commit 052f1dbb7c
No known key found for this signature in database

View File

@ -185,6 +185,43 @@ class PrefixType(NetBoxObjectType, ContactsMixin, BaseIPAddressFamilyType):
], strawberry.union("PrefixScopeType")] | None: ], strawberry.union("PrefixScopeType")] | None:
return self.scope return self.scope
@strawberry_django.field
def parent_prefixes(self) -> List[Annotated["PrefixType", strawberry.lazy('ipam.graphql.types')]]:
"""
Return all parent prefixes containing this prefix.
"""
from ipam.models import Prefix
return Prefix.objects.filter(
vrf=self.vrf,
prefix__net_contains=str(self.prefix)
)
@strawberry_django.field
def child_prefixes(self) -> List[Annotated["PrefixType", strawberry.lazy('ipam.graphql.types')]]:
"""
Return all child prefixes contained within this prefix.
"""
from ipam.models import Prefix
return Prefix.objects.filter(
vrf=self.vrf,
prefix__net_contained=str(self.prefix)
)
@strawberry_django.field
def child_ip_addresses(self) -> List[Annotated["IPAddressType", strawberry.lazy('ipam.graphql.types')]]:
"""
Return all IP addresses within this prefix.
"""
if self.vrf is None and self.status == 'container':
return models.IPAddress.objects.filter(
address__net_host_contained=str(self.prefix)
)
else:
return models.IPAddress.objects.filter(
address__net_host_contained=str(self.prefix),
vrf=self.vrf
)
@strawberry_django.type( @strawberry_django.type(
models.RIR, models.RIR,