2017-06-28 16:23:17 -04:00
|
|
|
from collections import OrderedDict
|
2017-05-24 11:33:11 -04:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
from rest_framework import serializers
|
2017-11-15 12:21:52 -05:00
|
|
|
from rest_framework.reverse import reverse
|
2017-03-17 14:36:59 -04:00
|
|
|
from rest_framework.validators import UniqueTogetherValidator
|
2018-08-03 09:43:03 -04:00
|
|
|
from taggit_serializer.serializers import TaggitSerializer, TagListSerializerField
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2018-11-12 11:36:44 -05:00
|
|
|
from dcim.api.nested_serializers import NestedDeviceSerializer, NestedSiteSerializer
|
2017-11-15 12:21:52 -05:00
|
|
|
from dcim.models import Interface
|
2017-03-08 16:18:41 -05:00
|
|
|
from extras.api.customfields import CustomFieldModelSerializer
|
2019-11-27 21:46:53 -05:00
|
|
|
from ipam.choices import *
|
2020-01-15 13:53:17 -05:00
|
|
|
from ipam.models import Aggregate, IPAddress, Prefix, RIR, Role, Service, VLAN, VLANGroup, VRF
|
2018-11-12 11:36:44 -05:00
|
|
|
from tenancy.api.nested_serializers import NestedTenantSerializer
|
2018-05-10 12:53:11 -04:00
|
|
|
from utilities.api import (
|
2018-08-03 09:43:03 -04:00
|
|
|
ChoiceField, SerializedPKRelatedField, ValidatedModelSerializer, WritableNestedSerializer,
|
2018-05-10 12:53:11 -04:00
|
|
|
)
|
2018-11-12 11:36:44 -05:00
|
|
|
from virtualization.api.nested_serializers import NestedVirtualMachineSerializer
|
|
|
|
from .nested_serializers import *
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# VRFs
|
|
|
|
#
|
|
|
|
|
2018-08-03 09:43:03 -04:00
|
|
|
class VRFSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
2018-04-05 14:12:43 -04:00
|
|
|
tenant = NestedTenantSerializer(required=False, allow_null=True)
|
2018-08-03 09:43:03 -04:00
|
|
|
tags = TagListSerializerField(required=False)
|
2019-04-19 16:50:42 -04:00
|
|
|
ipaddress_count = serializers.IntegerField(read_only=True)
|
2019-04-12 17:07:56 -04:00
|
|
|
prefix_count = serializers.IntegerField(read_only=True)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = VRF
|
2017-12-21 10:49:40 -05:00
|
|
|
fields = [
|
2018-05-10 12:53:11 -04:00
|
|
|
'id', 'name', 'rd', 'tenant', 'enforce_unique', 'description', 'tags', 'display_name', 'custom_fields',
|
2019-04-19 16:50:42 -04:00
|
|
|
'created', 'last_updated', 'ipaddress_count', 'prefix_count',
|
2017-12-21 10:49:40 -05:00
|
|
|
]
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
|
|
|
#
|
2018-11-12 11:36:44 -05:00
|
|
|
# RIRs/aggregates
|
2016-03-01 11:23:03 -05:00
|
|
|
#
|
|
|
|
|
2017-08-15 13:54:04 -04:00
|
|
|
class RIRSerializer(ValidatedModelSerializer):
|
2019-04-12 17:07:56 -04:00
|
|
|
aggregate_count = serializers.IntegerField(read_only=True)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = RIR
|
2019-04-12 17:07:56 -04:00
|
|
|
fields = ['id', 'name', 'slug', 'is_private', 'aggregate_count']
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2018-08-03 09:43:03 -04:00
|
|
|
class AggregateSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
2020-01-15 13:53:17 -05:00
|
|
|
family = ChoiceField(choices=IPAddressFamilyChoices, read_only=True)
|
2017-01-27 12:22:29 -05:00
|
|
|
rir = NestedRIRSerializer()
|
2018-08-03 09:43:03 -04:00
|
|
|
tags = TagListSerializerField(required=False)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Aggregate
|
2017-12-21 10:49:40 -05:00
|
|
|
fields = [
|
2018-05-10 12:53:11 -04:00
|
|
|
'id', 'family', 'prefix', 'rir', 'date_added', 'description', 'tags', 'custom_fields', 'created',
|
|
|
|
'last_updated',
|
2017-12-21 10:49:40 -05:00
|
|
|
]
|
2018-04-05 14:12:43 -04:00
|
|
|
read_only_fields = ['family']
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2018-11-12 11:36:44 -05:00
|
|
|
#
|
|
|
|
# VLANs
|
|
|
|
#
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2018-11-12 11:36:44 -05:00
|
|
|
class RoleSerializer(ValidatedModelSerializer):
|
2019-04-12 17:07:56 -04:00
|
|
|
prefix_count = serializers.IntegerField(read_only=True)
|
|
|
|
vlan_count = serializers.IntegerField(read_only=True)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2018-11-12 11:36:44 -05:00
|
|
|
class Meta:
|
|
|
|
model = Role
|
2019-12-10 12:59:10 -05:00
|
|
|
fields = ['id', 'name', 'slug', 'weight', 'description', 'prefix_count', 'vlan_count']
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2016-07-15 13:26:54 -04:00
|
|
|
|
2018-04-05 14:12:43 -04:00
|
|
|
class VLANGroupSerializer(ValidatedModelSerializer):
|
|
|
|
site = NestedSiteSerializer(required=False, allow_null=True)
|
2019-04-12 17:07:56 -04:00
|
|
|
vlan_count = serializers.IntegerField(read_only=True)
|
2017-01-31 15:35:09 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = VLANGroup
|
2019-04-12 17:07:56 -04:00
|
|
|
fields = ['id', 'name', 'slug', 'site', 'vlan_count']
|
2017-03-17 14:36:59 -04:00
|
|
|
validators = []
|
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
|
|
|
|
# Validate uniqueness of name and slug if a site has been assigned.
|
|
|
|
if data.get('site', None):
|
|
|
|
for field in ['name', 'slug']:
|
2017-05-31 11:50:03 -04:00
|
|
|
validator = UniqueTogetherValidator(queryset=VLANGroup.objects.all(), fields=('site', field))
|
2017-03-17 14:36:59 -04:00
|
|
|
validator.set_context(self)
|
|
|
|
validator(data)
|
|
|
|
|
2017-07-06 17:37:24 -04:00
|
|
|
# Enforce model validation
|
2018-11-27 10:52:24 -05:00
|
|
|
super().validate(data)
|
2017-07-06 17:37:24 -04:00
|
|
|
|
2017-03-17 14:36:59 -04:00
|
|
|
return data
|
2017-01-31 15:35:09 -05:00
|
|
|
|
|
|
|
|
2018-08-03 09:43:03 -04:00
|
|
|
class VLANSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
2018-04-05 14:12:43 -04:00
|
|
|
site = NestedSiteSerializer(required=False, allow_null=True)
|
|
|
|
group = NestedVLANGroupSerializer(required=False, allow_null=True)
|
|
|
|
tenant = NestedTenantSerializer(required=False, allow_null=True)
|
2019-11-27 22:15:59 -05:00
|
|
|
status = ChoiceField(choices=VLANStatusChoices, required=False)
|
2018-04-05 14:12:43 -04:00
|
|
|
role = NestedRoleSerializer(required=False, allow_null=True)
|
2018-08-03 09:43:03 -04:00
|
|
|
tags = TagListSerializerField(required=False)
|
2019-04-19 16:50:42 -04:00
|
|
|
prefix_count = serializers.IntegerField(read_only=True)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = VLAN
|
2017-01-27 12:22:29 -05:00
|
|
|
fields = [
|
2018-05-10 12:53:11 -04:00
|
|
|
'id', 'site', 'group', 'vid', 'name', 'tenant', 'status', 'role', 'description', 'tags', 'display_name',
|
2019-04-19 16:50:42 -04:00
|
|
|
'custom_fields', 'created', 'last_updated', 'prefix_count',
|
2017-01-27 12:22:29 -05:00
|
|
|
]
|
2017-03-17 14:36:59 -04:00
|
|
|
validators = []
|
|
|
|
|
|
|
|
def validate(self, data):
|
|
|
|
|
|
|
|
# Validate uniqueness of vid and name if a group has been assigned.
|
|
|
|
if data.get('group', None):
|
|
|
|
for field in ['vid', 'name']:
|
|
|
|
validator = UniqueTogetherValidator(queryset=VLAN.objects.all(), fields=('group', field))
|
|
|
|
validator.set_context(self)
|
|
|
|
validator(data)
|
|
|
|
|
2017-07-06 17:37:24 -04:00
|
|
|
# Enforce model validation
|
2018-11-27 10:52:24 -05:00
|
|
|
super().validate(data)
|
2017-07-06 17:37:24 -04:00
|
|
|
|
2017-03-17 14:36:59 -04:00
|
|
|
return data
|
2017-01-31 15:35:09 -05:00
|
|
|
|
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
#
|
|
|
|
# Prefixes
|
|
|
|
#
|
|
|
|
|
2018-08-03 09:43:03 -04:00
|
|
|
class PrefixSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
2020-01-15 13:53:17 -05:00
|
|
|
family = ChoiceField(choices=IPAddressFamilyChoices, read_only=True)
|
2018-04-05 14:12:43 -04:00
|
|
|
site = NestedSiteSerializer(required=False, allow_null=True)
|
|
|
|
vrf = NestedVRFSerializer(required=False, allow_null=True)
|
|
|
|
tenant = NestedTenantSerializer(required=False, allow_null=True)
|
|
|
|
vlan = NestedVLANSerializer(required=False, allow_null=True)
|
2019-11-27 21:46:53 -05:00
|
|
|
status = ChoiceField(choices=PrefixStatusChoices, required=False)
|
2018-04-05 14:12:43 -04:00
|
|
|
role = NestedRoleSerializer(required=False, allow_null=True)
|
2018-08-03 09:43:03 -04:00
|
|
|
tags = TagListSerializerField(required=False)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Prefix
|
2017-01-27 12:22:29 -05:00
|
|
|
fields = [
|
|
|
|
'id', 'family', 'prefix', 'site', 'vrf', 'tenant', 'vlan', 'status', 'role', 'is_pool', 'description',
|
2018-05-10 12:53:11 -04:00
|
|
|
'tags', 'custom_fields', 'created', 'last_updated',
|
2017-01-27 12:22:29 -05:00
|
|
|
]
|
2018-04-05 14:12:43 -04:00
|
|
|
read_only_fields = ['family']
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2017-11-09 16:59:50 -05:00
|
|
|
class AvailablePrefixSerializer(serializers.Serializer):
|
2018-11-12 11:36:44 -05:00
|
|
|
"""
|
|
|
|
Representation of a prefix which does not exist in the database.
|
|
|
|
"""
|
2017-11-09 16:59:50 -05:00
|
|
|
def to_representation(self, instance):
|
|
|
|
if self.context.get('vrf'):
|
|
|
|
vrf = NestedVRFSerializer(self.context['vrf'], context={'request': self.context['request']}).data
|
|
|
|
else:
|
|
|
|
vrf = None
|
|
|
|
return OrderedDict([
|
|
|
|
('family', instance.version),
|
|
|
|
('prefix', str(instance)),
|
|
|
|
('vrf', vrf),
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
#
|
|
|
|
# IP addresses
|
|
|
|
#
|
|
|
|
|
2018-08-02 18:58:14 -04:00
|
|
|
class IPAddressInterfaceSerializer(WritableNestedSerializer):
|
2018-11-12 11:36:44 -05:00
|
|
|
"""
|
|
|
|
Nested representation of an Interface which may belong to a Device *or* a VirtualMachine.
|
|
|
|
"""
|
2017-11-15 12:21:52 -05:00
|
|
|
url = serializers.SerializerMethodField() # We're imitating a HyperlinkedIdentityField here
|
2018-08-02 18:58:14 -04:00
|
|
|
device = NestedDeviceSerializer(read_only=True)
|
|
|
|
virtual_machine = NestedVirtualMachineSerializer(read_only=True)
|
2017-10-17 09:23:53 -04:00
|
|
|
|
2018-11-12 11:36:44 -05:00
|
|
|
class Meta:
|
2017-11-15 12:21:52 -05:00
|
|
|
model = Interface
|
2017-10-17 09:23:53 -04:00
|
|
|
fields = [
|
2017-11-15 12:21:52 -05:00
|
|
|
'id', 'url', 'device', 'virtual_machine', 'name',
|
2017-10-17 09:23:53 -04:00
|
|
|
]
|
|
|
|
|
2017-11-15 12:21:52 -05:00
|
|
|
def get_url(self, obj):
|
|
|
|
"""
|
|
|
|
Return a link to the Interface via either the DCIM API if the parent is a Device, or via the virtualization API
|
|
|
|
if the parent is a VirtualMachine.
|
|
|
|
"""
|
|
|
|
url_name = 'dcim-api:interface-detail' if obj.device else 'virtualization-api:interface-detail'
|
|
|
|
return reverse(url_name, kwargs={'pk': obj.pk}, request=self.context['request'])
|
|
|
|
|
2017-10-17 09:23:53 -04:00
|
|
|
|
2018-08-03 09:43:03 -04:00
|
|
|
class IPAddressSerializer(TaggitSerializer, CustomFieldModelSerializer):
|
2020-01-15 13:53:17 -05:00
|
|
|
family = ChoiceField(choices=IPAddressFamilyChoices, read_only=True)
|
2018-04-05 14:12:43 -04:00
|
|
|
vrf = NestedVRFSerializer(required=False, allow_null=True)
|
|
|
|
tenant = NestedTenantSerializer(required=False, allow_null=True)
|
2019-11-27 21:54:01 -05:00
|
|
|
status = ChoiceField(choices=IPAddressStatusChoices, required=False)
|
2019-11-27 22:09:16 -05:00
|
|
|
role = ChoiceField(choices=IPAddressRoleChoices, required=False, allow_null=True)
|
2018-04-05 14:12:43 -04:00
|
|
|
interface = IPAddressInterfaceSerializer(required=False, allow_null=True)
|
2018-11-12 11:36:44 -05:00
|
|
|
nat_inside = NestedIPAddressSerializer(required=False, allow_null=True)
|
|
|
|
nat_outside = NestedIPAddressSerializer(read_only=True)
|
2018-08-03 09:43:03 -04:00
|
|
|
tags = TagListSerializerField(required=False)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = IPAddress
|
2017-01-27 12:22:29 -05:00
|
|
|
fields = [
|
2019-04-22 18:10:28 -04:00
|
|
|
'id', 'family', 'address', 'vrf', 'tenant', 'status', 'role', 'interface', 'nat_inside',
|
|
|
|
'nat_outside', 'dns_name', 'description', 'tags', 'custom_fields', 'created', 'last_updated',
|
2017-01-27 12:22:29 -05:00
|
|
|
]
|
2018-04-05 14:12:43 -04:00
|
|
|
read_only_fields = ['family']
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2017-06-28 16:23:17 -04:00
|
|
|
class AvailableIPSerializer(serializers.Serializer):
|
2018-11-12 11:36:44 -05:00
|
|
|
"""
|
|
|
|
Representation of an IP address which does not exist in the database.
|
|
|
|
"""
|
2017-06-28 16:23:17 -04:00
|
|
|
def to_representation(self, instance):
|
|
|
|
if self.context.get('vrf'):
|
|
|
|
vrf = NestedVRFSerializer(self.context['vrf'], context={'request': self.context['request']}).data
|
|
|
|
else:
|
|
|
|
vrf = None
|
|
|
|
return OrderedDict([
|
|
|
|
('family', self.context['prefix'].version),
|
|
|
|
('address', '{}/{}'.format(instance, self.context['prefix'].prefixlen)),
|
|
|
|
('vrf', vrf),
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2016-12-15 15:32:58 -05:00
|
|
|
#
|
|
|
|
# Services
|
|
|
|
#
|
|
|
|
|
2018-06-21 16:17:18 -04:00
|
|
|
class ServiceSerializer(CustomFieldModelSerializer):
|
2018-04-05 14:12:43 -04:00
|
|
|
device = NestedDeviceSerializer(required=False, allow_null=True)
|
|
|
|
virtual_machine = NestedVirtualMachineSerializer(required=False, allow_null=True)
|
2019-11-27 22:27:06 -05:00
|
|
|
protocol = ChoiceField(choices=ServiceProtocolChoices)
|
2018-04-06 12:42:25 -04:00
|
|
|
ipaddresses = SerializedPKRelatedField(
|
|
|
|
queryset=IPAddress.objects.all(),
|
|
|
|
serializer=NestedIPAddressSerializer,
|
|
|
|
required=False,
|
|
|
|
many=True
|
|
|
|
)
|
2017-01-27 16:19:38 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Service
|
2017-12-21 10:49:40 -05:00
|
|
|
fields = [
|
2018-06-21 16:17:18 -04:00
|
|
|
'id', 'device', 'virtual_machine', 'name', 'port', 'protocol', 'ipaddresses', 'description',
|
|
|
|
'custom_fields', 'created', 'last_updated',
|
2017-12-21 10:49:40 -05:00
|
|
|
]
|