コード例 #1
0
ファイル: serializers.py プロジェクト: xonacs/netbox
class SecretSerializer(TaggitSerializer, CustomFieldModelSerializer):
    device = NestedDeviceSerializer()
    role = NestedSecretRoleSerializer()
    plaintext = serializers.CharField()
    tags = TagListSerializerField(required=False)

    class Meta:
        model = Secret
        fields = [
            'id', 'device', 'role', 'name', 'plaintext', 'hash', 'tags', 'custom_fields', 'created', 'last_updated',
        ]
        validators = []

    def validate(self, data):

        # Encrypt plaintext data using the master key provided from the view context
        if data.get('plaintext'):
            s = Secret(plaintext=data['plaintext'])
            s.encrypt(self.context['master_key'])
            data['ciphertext'] = s.ciphertext
            data['hash'] = s.hash

        # Validate uniqueness of name if one has been provided.
        if data.get('name'):
            validator = UniqueTogetherValidator(queryset=Secret.objects.all(), fields=('device', 'role', 'name'))
            validator.set_context(self)
            validator(data)

        # Enforce model validation
        super(SecretSerializer, self).validate(data)

        return data
コード例 #2
0
ファイル: serializers.py プロジェクト: XantaroDE/netbox
class IPAddressInterfaceSerializer(serializers.ModelSerializer):
    url = serializers.SerializerMethodField(
    )  # We're imitating a HyperlinkedIdentityField here
    device = NestedDeviceSerializer()
    virtual_machine = NestedVirtualMachineSerializer()

    class Meta(InterfaceSerializer.Meta):
        model = Interface
        fields = [
            'id',
            'url',
            'device',
            'virtual_machine',
            'name',
        ]

    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'])
コード例 #3
0
ファイル: serializers.py プロジェクト: waltermeyer/netbox
class SecretSerializer(serializers.ModelSerializer):
    device = NestedDeviceSerializer()
    role = NestedSecretRoleSerializer()

    class Meta:
        model = Secret
        fields = ['id', 'device', 'role', 'name', 'plaintext', 'hash', 'created', 'last_updated']
コード例 #4
0
class ServiceSerializer(serializers.ModelSerializer):
    device = NestedDeviceSerializer()
    protocol = ChoiceFieldSerializer(choices=IP_PROTOCOL_CHOICES)
    ipaddresses = NestedIPAddressSerializer(many=True)

    class Meta:
        model = Service
        fields = ['id', 'device', 'name', 'port', 'protocol', 'ipaddresses', 'description']
コード例 #5
0
class VRFSerializer(TaggitSerializer, CustomFieldModelSerializer):
    device = NestedDeviceSerializer(required=False, allow_null=True)
    tags = TagListSerializerField(required=False)

    class Meta:
        model = VRF
        fields = [
            'id', 'name', 'rd', 'device', 'enforce_unique', 'description', 'tags', 'display_name', 'custom_fields',
            'created', 'last_updated',
        ]
コード例 #6
0
ファイル: serializers.py プロジェクト: eqvinox/netbox
class ServiceSerializer(serializers.ModelSerializer):
    device = NestedDeviceSerializer()
    virtual_machine = NestedVirtualMachineSerializer()
    protocol = ChoiceFieldSerializer(choices=IP_PROTOCOL_CHOICES)
    ipaddresses = NestedIPAddressSerializer(many=True)

    class Meta:
        model = Service
        fields = [
            'id', 'device', 'virtual_machine', 'name', 'port', 'protocol', 'ipaddresses', 'description', 'created',
            'last_updated',
        ]
コード例 #7
0
class ServiceSerializer(CustomFieldModelSerializer):
    device = NestedDeviceSerializer(required=False, allow_null=True)
    virtual_machine = NestedVirtualMachineSerializer(required=False, allow_null=True)
    protocol = ChoiceField(choices=IP_PROTOCOL_CHOICES)
    ipaddresses = SerializedPKRelatedField(
        queryset=IPAddress.objects.all(),
        serializer=NestedIPAddressSerializer,
        required=False,
        many=True
    )

    class Meta:
        model = Service
        fields = [
            'id', 'device', 'virtual_machine', 'name', 'port', 'protocol', 'ipaddresses', 'description',
            'custom_fields', 'created', 'last_updated',
        ]