class BGPSessionSerializer(TaggedObjectSerializer, CustomFieldModelSerializer): status = ChoiceField(choices=SessionStatusChoices, required=False) site = NestedSiteSerializer(required=False, allow_null=True) tenant = NestedTenantSerializer(required=False, allow_null=True) device = NestedDeviceSerializer(required=False, allow_null=True) local_address = NestedIPAddressSerializer(required=True, allow_null=False) remote_address = NestedIPAddressSerializer(required=True, allow_null=False) local_as = ASNSerializer(required=True, allow_null=False) remote_as = ASNSerializer(required=True, allow_null=False) class Meta: model = BGPSession fields = '__all__'
class DeviceSerializer(TaggitSerializer, CustomFieldModelSerializer): device_type = NestedDeviceTypeSerializer() device_role = NestedDeviceRoleSerializer() tenant = NestedTenantSerializer(required=False, allow_null=True) platform = NestedPlatformSerializer(required=False, allow_null=True) site = NestedSiteSerializer() rack = NestedRackSerializer(required=False, allow_null=True) face = ChoiceField(choices=RACK_FACE_CHOICES, required=False, allow_null=True) status = ChoiceField(choices=DEVICE_STATUS_CHOICES, required=False) primary_ip = NestedIPAddressSerializer(read_only=True) primary_ip4 = NestedIPAddressSerializer(required=False, allow_null=True) primary_ip6 = NestedIPAddressSerializer(required=False, allow_null=True) parent_device = serializers.SerializerMethodField() cluster = NestedClusterSerializer(required=False, allow_null=True) virtual_chassis = NestedVirtualChassisSerializer(required=False, allow_null=True) tags = TagListSerializerField(required=False) class Meta: model = Device fields = [ 'id', 'name', 'display_name', 'device_type', 'device_role', 'tenant', 'platform', 'serial', 'asset_tag', 'site', 'rack', 'position', 'face', 'parent_device', 'status', 'primary_ip', 'primary_ip4', 'primary_ip6', 'cluster', 'virtual_chassis', 'vc_position', 'vc_priority', 'comments', 'local_context_data', 'tags', 'custom_fields', 'created', 'last_updated', ] validators = [] def validate(self, data): # Validate uniqueness of (rack, position, face) since we omitted the automatically-created validator from Meta. if data.get('rack') and data.get('position') and data.get('face'): validator = UniqueTogetherValidator(queryset=Device.objects.all(), fields=('rack', 'position', 'face')) validator.set_context(self) validator(data) # Enforce model validation super().validate(data) return data @swagger_serializer_method(serializer_or_field=NestedDeviceSerializer) def get_parent_device(self, obj): try: device_bay = obj.parent_bay except DeviceBay.DoesNotExist: return None context = {'request': self.context['request']} data = NestedDeviceSerializer(instance=device_bay.device, context=context).data data['device_bay'] = NestedDeviceBaySerializer(instance=device_bay, context=context).data return data
class BGPSessionSerializer(TaggedObjectSerializer, CustomFieldModelSerializer): status = ChoiceField(choices=SessionStatusChoices, required=False) site = NestedSiteSerializer(required=False, allow_null=True) tenant = NestedTenantSerializer(required=False, allow_null=True) device = NestedDeviceSerializer(required=False, allow_null=True) local_address = NestedIPAddressSerializer(required=True, allow_null=False) remote_address = NestedIPAddressSerializer(required=True, allow_null=False) local_as = NestedASNSerializer(required=True, allow_null=False) remote_as = NestedASNSerializer(required=True, allow_null=False) peer_group = NestedBGPPeerGroupSerializer(required=False, allow_null=True) import_policies = SerializedPKRelatedField( queryset=RoutingPolicy.objects.all(), serializer=NestedRoutingPolicySerializer, required=False, allow_null=True, many=True ) export_policies = SerializedPKRelatedField( queryset=RoutingPolicy.objects.all(), serializer=NestedRoutingPolicySerializer, required=False, allow_null=True, many=True ) class Meta: model = BGPSession fields = '__all__' def to_representation(self, instance): ret = super().to_representation(instance) if instance is not None: if instance.peer_group: for pol in instance.peer_group.import_policies.difference(instance.import_policies.all()): ret['import_policies'].append( NestedRoutingPolicySerializer(pol, context={'request': self.context['request']}).data ) for pol in instance.peer_group.export_policies.difference(instance.export_policies.all()): ret['export_policies'].append( NestedRoutingPolicySerializer(pol, context={'request': self.context['request']}).data ) return ret
class VirtualMachineSerializer(TaggitSerializer, CustomFieldModelSerializer): status = ChoiceField(choices=VirtualMachineStatusChoices, required=False) site = NestedSiteSerializer(read_only=True) cluster = NestedClusterSerializer() role = NestedDeviceRoleSerializer(required=False, allow_null=True) tenant = NestedTenantSerializer(required=False, allow_null=True) platform = NestedPlatformSerializer(required=False, allow_null=True) primary_ip = NestedIPAddressSerializer(read_only=True) primary_ip4 = NestedIPAddressSerializer(required=False, allow_null=True) primary_ip6 = NestedIPAddressSerializer(required=False, allow_null=True) tags = TagListSerializerField(required=False) class Meta: model = VirtualMachine fields = [ 'id', 'name', 'status', 'site', 'cluster', 'role', 'tenant', 'platform', 'primary_ip', 'primary_ip4', 'primary_ip6', 'vcpus', 'memory', 'disk', 'comments', 'local_context_data', 'tags', 'custom_fields', 'created', 'last_updated', ] validators = []
class DeviceSerializer(PrimaryModelSerializer): url = serializers.HyperlinkedIdentityField(view_name='dcim-api:device-detail') device_type = NestedDeviceTypeSerializer() device_role = NestedDeviceRoleSerializer() tenant = NestedTenantSerializer(required=False, allow_null=True, default=None) platform = NestedPlatformSerializer(required=False, allow_null=True) site = NestedSiteSerializer() location = NestedLocationSerializer(required=False, allow_null=True, default=None) rack = NestedRackSerializer(required=False, allow_null=True, default=None) face = ChoiceField(choices=DeviceFaceChoices, allow_blank=True, default='') position = serializers.IntegerField(allow_null=True, label='Position (U)', min_value=1, default=None) status = ChoiceField(choices=DeviceStatusChoices, required=False) airflow = ChoiceField(choices=DeviceAirflowChoices, allow_blank=True, required=False) primary_ip = NestedIPAddressSerializer(read_only=True) primary_ip4 = NestedIPAddressSerializer(required=False, allow_null=True) primary_ip6 = NestedIPAddressSerializer(required=False, allow_null=True) parent_device = serializers.SerializerMethodField() cluster = NestedClusterSerializer(required=False, allow_null=True) virtual_chassis = NestedVirtualChassisSerializer(required=False, allow_null=True, default=None) vc_position = serializers.IntegerField(allow_null=True, max_value=255, min_value=0, default=None) class Meta: model = Device fields = [ 'id', 'url', 'display', 'name', 'device_type', 'device_role', 'tenant', 'platform', 'serial', 'asset_tag', 'site', 'location', 'rack', 'position', 'face', 'parent_device', 'status', 'airflow', 'primary_ip', 'primary_ip4', 'primary_ip6', 'cluster', 'virtual_chassis', 'vc_position', 'vc_priority', 'comments', 'local_context_data', 'tags', 'custom_fields', 'created', 'last_updated', ] @swagger_serializer_method(serializer_or_field=NestedDeviceSerializer) def get_parent_device(self, obj): try: device_bay = obj.parent_bay except DeviceBay.DoesNotExist: return None context = {'request': self.context['request']} data = NestedDeviceSerializer(instance=device_bay.device, context=context).data data['device_bay'] = NestedDeviceBaySerializer(instance=device_bay, context=context).data return data
class VirtualMachineSerializer(PrimaryModelSerializer): url = serializers.HyperlinkedIdentityField( view_name='virtualization-api:virtualmachine-detail') status = ChoiceField(choices=VirtualMachineStatusChoices, required=False) site = NestedSiteSerializer(read_only=True) cluster = NestedClusterSerializer() role = NestedDeviceRoleSerializer(required=False, allow_null=True) tenant = NestedTenantSerializer(required=False, allow_null=True) platform = NestedPlatformSerializer(required=False, allow_null=True) primary_ip = NestedIPAddressSerializer(read_only=True) primary_ip4 = NestedIPAddressSerializer(required=False, allow_null=True) primary_ip6 = NestedIPAddressSerializer(required=False, allow_null=True) class Meta: model = VirtualMachine fields = [ 'id', 'url', 'display', 'name', 'status', 'site', 'cluster', 'role', 'tenant', 'platform', 'primary_ip', 'primary_ip4', 'primary_ip6', 'vcpus', 'memory', 'disk', 'comments', 'local_context_data', 'tags', 'custom_fields', 'created', 'last_updated', ] validators = []
class BgpPeeringSerializer(serializers.ModelSerializer): """Serializer for the BgpPeering model.""" site = NestedSiteSerializer( many=False, read_only=False, required=False, help_text="BgpPeering Site", ) device = NestedDeviceSerializer( many=False, read_only=False, required=True, help_text="BgpPeering Device", ) local_ip = NestedIPAddressSerializer( many=False, read_only=False, required=True, help_text="Local peering IP", ) class Meta: model = BgpPeering fields = [ "id", "site", "device", "local_ip", "local_as", "remote_ip", "remote_as", "peer_name", "description", ]
class BGPSessionSerializer(NetBoxModelSerializer): status = ChoiceField(choices=SessionStatusChoices, required=False) site = NestedSiteSerializer(required=False, allow_null=True) tenant = NestedTenantSerializer(required=False, allow_null=True) device = NestedDeviceSerializer(required=False, allow_null=True) local_address = NestedIPAddressSerializer(required=True, allow_null=False) remote_address = NestedIPAddressSerializer(required=True, allow_null=False) local_as = NestedASNSerializer(required=True, allow_null=False) remote_as = NestedASNSerializer(required=True, allow_null=False) peer_group = NestedBGPPeerGroupSerializer(required=False, allow_null=True) import_policies = SerializedPKRelatedField( queryset=RoutingPolicy.objects.all(), serializer=NestedRoutingPolicySerializer, required=False, allow_null=True, many=True) export_policies = SerializedPKRelatedField( queryset=RoutingPolicy.objects.all(), serializer=NestedRoutingPolicySerializer, required=False, allow_null=True, many=True) class Meta: model = BGPSession fields = '__all__' validators = [] def validate(self, attrs): qs = BGPSession.objects.filter( device=attrs.get('device'), local_as=attrs.get('local_as'), local_address=attrs.get('local_address'), remote_as=attrs.get('remote_as'), remote_address=attrs.get('remote_address'), ) if qs.exists(): raise ValidationError({ 'error': 'BGP Session with this Device, Local address, Local AS, Remote address and Remote AS already exists.' }) return attrs def to_representation(self, instance): ret = super().to_representation(instance) if instance is not None: if instance.peer_group: for pol in instance.peer_group.import_policies.difference( instance.import_policies.all()): ret['import_policies'].append( NestedRoutingPolicySerializer( pol, context={ 'request': self.context['request'] }).data) for pol in instance.peer_group.export_policies.difference( instance.export_policies.all()): ret['export_policies'].append( NestedRoutingPolicySerializer( pol, context={ 'request': self.context['request'] }).data) return ret