Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes: #11507 - Show Prefixes Aggregate and RIR on API view #18935

Draft
wants to merge 1 commit into
base: feature
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions netbox/ipam/api/serializers_/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Meta:


class PrefixSerializer(NetBoxModelSerializer):
aggregate = AggregateSerializer(nested=True, read_only=True, allow_null=True)
rir = RIRSerializer(nested=True, read_only=True, allow_null=True)
family = ChoiceField(choices=IPAddressFamilyChoices, read_only=True)
vrf = VRFSerializer(nested=True, required=False, allow_null=True)
scope_type = ContentTypeField(
Expand All @@ -67,9 +69,9 @@ class PrefixSerializer(NetBoxModelSerializer):
class Meta:
model = Prefix
fields = [
'id', 'url', 'display_url', 'display', 'family', 'prefix', 'vrf', 'scope_type', 'scope_id', 'scope',
'tenant', 'vlan', 'status', 'role', 'is_pool', 'mark_utilized', 'description', 'comments', 'tags',
'custom_fields', 'created', 'last_updated', 'children', '_depth',
'id', 'url', 'display_url', 'display', 'aggregate', 'rir', 'family', 'prefix', 'vrf', 'scope_type',
'scope_id', 'scope', 'tenant', 'vlan', 'status', 'role', 'is_pool', 'mark_utilized', 'description',
'comments', 'tags', 'custom_fields', 'created', 'last_updated', 'children', '_depth',
]
brief_fields = ('id', 'url', 'display', 'family', 'prefix', 'description', '_depth')

Expand Down
9 changes: 9 additions & 0 deletions netbox/ipam/models/ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ def save(self, *args, **kwargs):

super().save(*args, **kwargs)

@property
def aggregate(self):
return Aggregate.objects.filter(prefix__net_contains_or_equals=self.prefix).first()

@property
def rir(self):
aggregate = self.aggregate
return aggregate.rir if aggregate else None

@property
def family(self):
return self.prefix.version if self.prefix else None
Expand Down
27 changes: 27 additions & 0 deletions netbox/ipam/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,33 @@ def test_create_multiple_available_ips(self):
self.assertHttpStatus(response, status.HTTP_201_CREATED)
self.assertEqual(len(response.data), 8)

def test_get_prefix_with_aggregate_and_rir(self):
self.add_permissions('ipam.view_prefix')
rir = RIR.objects.create(name='RFC 1918', slug='rfc-1918')
aggregate = Aggregate.objects.create(prefix=IPNetwork('192.168.0.0/16'), rir=rir)
prefixes = [
Prefix.objects.filter(prefix=IPNetwork('192.168.2.0/24')).first(),
Prefix.objects.create(prefix=IPNetwork('10.0.0.0/24'))
]

self.assertIsNotNone(prefixes[0])

url = self._get_detail_url(prefixes[0])
response = self.client.get(url, **self.header)
self.assertIsNotNone(prefixes[0].aggregate)
self.assertHttpStatus(response, status.HTTP_200_OK)
self.assertIsNotNone(response.data.get('aggregate'))
self.assertIsNotNone(response.data.get('rir'))
self.assertEqual(response.data.get('aggregate').get('id', None), aggregate.pk)
self.assertEqual(response.data.get('rir').get('id', None), rir.pk)

url = self._get_detail_url(prefixes[1])
response = self.client.get(url, **self.header)
self.assertIsNone(prefixes[1].aggregate)
self.assertHttpStatus(response, status.HTTP_200_OK)
self.assertIsNone(response.data.get('aggregate'))
self.assertIsNone(response.data.get('rir'))


class IPRangeTest(APIViewTestCases.APIViewTestCase):
model = IPRange
Expand Down