Exemplo n.º 1
0
class ConfigContextSerializer(ValidatedModelSerializer):
    regions = SerializedPKRelatedField(queryset=Region.objects.all(),
                                       serializer=NestedRegionSerializer,
                                       required=False,
                                       many=True)
    sites = SerializedPKRelatedField(queryset=Site.objects.all(),
                                     serializer=NestedSiteSerializer,
                                     required=False,
                                     many=True)
    roles = SerializedPKRelatedField(queryset=DeviceRole.objects.all(),
                                     serializer=NestedDeviceRoleSerializer,
                                     required=False,
                                     many=True)
    platforms = SerializedPKRelatedField(queryset=Platform.objects.all(),
                                         serializer=NestedPlatformSerializer,
                                         required=False,
                                         many=True)
    tenant_groups = SerializedPKRelatedField(
        queryset=TenantGroup.objects.all(),
        serializer=NestedTenantGroupSerializer,
        required=False,
        many=True)
    tenants = SerializedPKRelatedField(queryset=Tenant.objects.all(),
                                       serializer=NestedTenantSerializer,
                                       required=False,
                                       many=True)

    class Meta:
        model = ConfigContext
        fields = [
            'id',
            'name',
            'weight',
            'description',
            'is_active',
            'regions',
            'sites',
            'roles',
            'platforms',
            'tenant_groups',
            'tenants',
            'data',
        ]
Exemplo n.º 2
0
class InterfaceSerializer(TaggitSerializer, ValidatedModelSerializer):
    device = NestedDeviceSerializer()
    form_factor = ChoiceField(choices=IFACE_FF_CHOICES, required=False)
    lag = NestedInterfaceSerializer(required=False, allow_null=True)
    is_connected = serializers.SerializerMethodField(read_only=True)
    interface_connection = serializers.SerializerMethodField(read_only=True)
    circuit_termination = InterfaceCircuitTerminationSerializer(read_only=True)
    mode = ChoiceField(choices=IFACE_MODE_CHOICES, required=False)
    untagged_vlan = InterfaceVLANSerializer(required=False, allow_null=True)
    tagged_vlans = SerializedPKRelatedField(
        queryset=VLAN.objects.all(),
        serializer=InterfaceVLANSerializer,
        required=False,
        many=True
    )
    tags = TagListSerializerField(required=False)

    class Meta:
        model = Interface
        fields = [
            'id', 'device', 'name', 'form_factor', 'enabled', 'lag', 'mtu', 'mac_address', 'mgmt_only', 'description',
            'is_connected', 'interface_connection', 'circuit_termination', 'mode', 'untagged_vlan', 'tagged_vlans',
            'tags',
        ]

    def validate(self, data):

        # All associated VLANs be global or assigned to the parent device's site.
        device = self.instance.device if self.instance else data.get('device')
        untagged_vlan = data.get('untagged_vlan')
        if untagged_vlan and untagged_vlan.site not in [device.site, None]:
            raise serializers.ValidationError({
                'untagged_vlan': "VLAN {} must belong to the same site as the interface's parent device, or it must be "
                                 "global.".format(untagged_vlan)
            })
        for vlan in data.get('tagged_vlans', []):
            if vlan.site not in [device.site, None]:
                raise serializers.ValidationError({
                    'tagged_vlans': "VLAN {} must belong to the same site as the interface's parent device, or it must "
                                    "be global.".format(vlan)
                })

        return super(InterfaceSerializer, self).validate(data)

    def get_is_connected(self, obj):
        """
        Return True if the interface has a connected interface or circuit termination.
        """
        if obj.connection:
            return True
        try:
            circuit_termination = obj.circuit_termination
            return True
        except CircuitTermination.DoesNotExist:
            pass
        return False

    def get_interface_connection(self, obj):
        if obj.connection:
            context = {
                'request': self.context['request'],
                'interface': obj.connected_interface,
            }
            return ContextualInterfaceConnectionSerializer(obj.connection, context=context).data
        return None
Exemplo n.º 3
0
class ConfigContextSerializer(ValidatedModelSerializer):
    url = serializers.HyperlinkedIdentityField(
        view_name='extras-api:configcontext-detail')
    regions = SerializedPKRelatedField(queryset=Region.objects.all(),
                                       serializer=NestedRegionSerializer,
                                       required=False,
                                       many=True)
    sites = SerializedPKRelatedField(queryset=Site.objects.all(),
                                     serializer=NestedSiteSerializer,
                                     required=False,
                                     many=True)
    roles = SerializedPKRelatedField(queryset=DeviceRole.objects.all(),
                                     serializer=NestedDeviceRoleSerializer,
                                     required=False,
                                     many=True)
    platforms = SerializedPKRelatedField(queryset=Platform.objects.all(),
                                         serializer=NestedPlatformSerializer,
                                         required=False,
                                         many=True)
    cluster_groups = SerializedPKRelatedField(
        queryset=ClusterGroup.objects.all(),
        serializer=NestedClusterGroupSerializer,
        required=False,
        many=True)
    clusters = SerializedPKRelatedField(queryset=Cluster.objects.all(),
                                        serializer=NestedClusterSerializer,
                                        required=False,
                                        many=True)
    tenant_groups = SerializedPKRelatedField(
        queryset=TenantGroup.objects.all(),
        serializer=NestedTenantGroupSerializer,
        required=False,
        many=True)
    tenants = SerializedPKRelatedField(queryset=Tenant.objects.all(),
                                       serializer=NestedTenantSerializer,
                                       required=False,
                                       many=True)
    tags = serializers.SlugRelatedField(queryset=Tag.objects.all(),
                                        slug_field='slug',
                                        required=False,
                                        many=True)

    class Meta:
        model = ConfigContext
        fields = [
            'id',
            'url',
            'name',
            'weight',
            'description',
            'is_active',
            'regions',
            'sites',
            'roles',
            'platforms',
            'cluster_groups',
            'clusters',
            'tenant_groups',
            'tenants',
            'tags',
            'data',
            'created',
            'last_updated',
        ]
Exemplo n.º 4
0
class InterfaceSerializer(TaggitSerializer, ConnectedEndpointSerializer):
    device = NestedDeviceSerializer()
    type = ChoiceField(choices=IFACE_TYPE_CHOICES, required=False)
    # TODO: Remove in v2.7 (backward-compatibility for form_factor)
    form_factor = ChoiceField(choices=IFACE_TYPE_CHOICES, required=False)
    lag = NestedInterfaceSerializer(required=False, allow_null=True)
    mode = ChoiceField(choices=IFACE_MODE_CHOICES,
                       required=False,
                       allow_null=True)
    untagged_vlan = NestedVLANSerializer(required=False, allow_null=True)
    tagged_vlans = SerializedPKRelatedField(queryset=VLAN.objects.all(),
                                            serializer=NestedVLANSerializer,
                                            required=False,
                                            many=True)
    cable = NestedCableSerializer(read_only=True)
    tags = TagListSerializerField(required=False)

    class Meta:
        model = Interface
        fields = [
            'id',
            'device',
            'name',
            'type',
            'form_factor',
            'enabled',
            'lag',
            'mtu',
            'mac_address',
            'mgmt_only',
            'description',
            'connected_endpoint_type',
            'connected_endpoint',
            'connection_status',
            'cable',
            'mode',
            'untagged_vlan',
            'tagged_vlans',
            'tags',
            'count_ipaddresses',
        ]

    # TODO: This validation should be handled by Interface.clean()
    def validate(self, data):

        # All associated VLANs be global or assigned to the parent device's site.
        device = self.instance.device if self.instance else data.get('device')
        untagged_vlan = data.get('untagged_vlan')
        if untagged_vlan and untagged_vlan.site not in [device.site, None]:
            raise serializers.ValidationError({
                'untagged_vlan':
                "VLAN {} must belong to the same site as the interface's parent device, or it must be "
                "global.".format(untagged_vlan)
            })
        for vlan in data.get('tagged_vlans', []):
            if vlan.site not in [device.site, None]:
                raise serializers.ValidationError({
                    'tagged_vlans':
                    "VLAN {} must belong to the same site as the interface's parent device, or it must "
                    "be global.".format(vlan)
                })

        return super().validate(data)
Exemplo n.º 5
0
class InterfaceSerializer(TaggedObjectSerializer, ConnectedEndpointSerializer):
    url = serializers.HyperlinkedIdentityField(
        view_name='dcim-api:interface-detail')
    device = NestedDeviceSerializer()
    type = ChoiceField(choices=InterfaceTypeChoices)
    lag = NestedInterfaceSerializer(required=False, allow_null=True)
    mode = ChoiceField(choices=InterfaceModeChoices,
                       allow_blank=True,
                       required=False)
    untagged_vlan = NestedVLANSerializer(required=False, allow_null=True)
    tagged_vlans = SerializedPKRelatedField(queryset=VLAN.objects.all(),
                                            serializer=NestedVLANSerializer,
                                            required=False,
                                            many=True)
    cable = NestedCableSerializer(read_only=True)
    count_ipaddresses = serializers.IntegerField(read_only=True)

    class Meta:
        model = Interface
        fields = [
            'id',
            'url',
            'device',
            'name',
            'label',
            'type',
            'enabled',
            'lag',
            'mtu',
            'mac_address',
            'mgmt_only',
            'description',
            'connected_endpoint_type',
            'connected_endpoint',
            'connection_status',
            'cable',
            'mode',
            'untagged_vlan',
            'tagged_vlans',
            'tags',
            'count_ipaddresses',
        ]

    # TODO: This validation should be handled by Interface.clean()
    def validate(self, data):

        # All associated VLANs be global or assigned to the parent device's site.
        device = self.instance.device if self.instance else data.get('device')
        untagged_vlan = data.get('untagged_vlan')
        if untagged_vlan and untagged_vlan.site not in [device.site, None]:
            raise serializers.ValidationError({
                'untagged_vlan':
                "VLAN {} must belong to the same site as the interface's parent device, or it must be "
                "global.".format(untagged_vlan)
            })
        for vlan in data.get('tagged_vlans', []):
            if vlan.site not in [device.site, None]:
                raise serializers.ValidationError({
                    'tagged_vlans':
                    "VLAN {} must belong to the same site as the interface's parent device, or it must "
                    "be global.".format(vlan)
                })

        return super().validate(data)