__init__.py 4.5 KB
Newer Older
1 2

from rest_framework import serializers
Lukas Burgey's avatar
Lukas Burgey committed
3 4
from rest_polymorphic.serializers import PolymorphicSerializer

5
from feudal.backend.auth.v1.models.serializers import VOSerializer
6

Lukas Burgey's avatar
Lukas Burgey committed
7 8 9 10
from ...models import Site, Service
from ..users import User, SSHPublicKey
from ..deployments import CredentialState, DeploymentState, Deployment, VODeployment, ServiceDeployment

11

12 13
class SSHPublicKeySerializer(serializers.ModelSerializer):
    class Meta:
Lukas Burgey's avatar
Lukas Burgey committed
14
        model = SSHPublicKey
15 16 17 18 19 20 21 22 23
        fields = [
            'id',
            'name',
            'key',
        ]


class SSHPublicKeyRefSerializer(serializers.ModelSerializer):
    class Meta:
Lukas Burgey's avatar
Lukas Burgey committed
24
        model = SSHPublicKey
25 26 27 28
        fields = [
            'id',
            'name',
        ]
Lukas Burgey's avatar
Lukas Burgey committed
29 30


31
class CredentialStateSerializer(serializers.ModelSerializer):
32 33 34
    credential = SSHPublicKeyRefSerializer()

    class Meta:
Lukas Burgey's avatar
Lukas Burgey committed
35
        model = CredentialState
36 37 38 39
        fields = [
            'state',
            'state_target',
            'credential',
Lukas Burgey's avatar
Lukas Burgey committed
40
            'is_pending',
41 42
        ]

43

Lukas Burgey's avatar
Lukas Burgey committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
class SiteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Site
        fields = ['id', 'name']


class ServiceSerializer(serializers.ModelSerializer):
    site = SiteSerializer()
    vos = VOSerializer(many=True)

    class Meta:
        model = Service
        fields = [
            'id',
            'name',
            'site',
            'vos',
            'description',
        ]


65 66 67 68 69 70 71 72 73 74
class CompactServiceSerializer(serializers.ModelSerializer):
    class Meta:
        model = Service
        fields = [
            'id',
            'name',
            'description',
        ]


Lukas Burgey's avatar
Lukas Burgey committed
75
class DeploymentStateSerializer(serializers.ModelSerializer):
76 77 78
    # why all the read_onlys: the rest interface exposes these fields, but the must not be changed
    credential_states = CredentialStateSerializer(many=True, read_only=True)
    credentials = serializers.JSONField(read_only=True)
Lukas Burgey's avatar
Lukas Burgey committed
79
    questionnaire = serializers.JSONField()
80 81
    service = CompactServiceSerializer(read_only=True)
    site = SiteSerializer(read_only=True)
Lukas Burgey's avatar
Lukas Burgey committed
82 83 84

    class Meta:
        model = DeploymentState
85
        fields = (
Lukas Burgey's avatar
Lukas Burgey committed
86
            'credential_states',
87 88
            'credentials',
            'id',
Lukas Burgey's avatar
Lukas Burgey committed
89 90
            'is_credential_pending',
            'is_pending',
91 92 93 94 95 96
            'message',
            'questionnaire',
            'service',
            'site',
            'state',
            'state_target',
97 98 99 100 101
        )
        read_only_fields = (
            'state',
            'message',
        )
Lukas Burgey's avatar
Lukas Burgey committed
102 103 104 105 106


DEPLOYMENT_FIELDS = (
    'state_target',
    'id',
107
    'states',
Lukas Burgey's avatar
Lukas Burgey committed
108 109 110 111
)


class AbstractDeploymentSerializer(serializers.ModelSerializer):
112
    states = DeploymentStateSerializer(many=True)
Lukas Burgey's avatar
Lukas Burgey committed
113 114 115 116 117 118 119

    class Meta:
        model = Deployment
        fields = DEPLOYMENT_FIELDS


class VODeploymentSerializer(serializers.ModelSerializer):
120
    states = DeploymentStateSerializer(many=True)
121 122 123
    vo = VOSerializer()
    services = ServiceSerializer(many=True)

Lukas Burgey's avatar
Lukas Burgey committed
124 125 126 127 128 129 130 131 132
    class Meta:
        model = VODeployment
        fields = DEPLOYMENT_FIELDS + (
            'vo',
            'services',
        )


class ServiceDeploymentSerializer(serializers.ModelSerializer):
133
    states = DeploymentStateSerializer(many=True)
134
    service = ServiceSerializer()
135

Lukas Burgey's avatar
Lukas Burgey committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    class Meta:
        model = ServiceDeployment
        fields = DEPLOYMENT_FIELDS + (
            'service',
        )


class DeploymentSerializer(PolymorphicSerializer):
    model_serializer_mapping = {
        Deployment: AbstractDeploymentSerializer,
        VODeployment: VODeploymentSerializer,
        ServiceDeployment: ServiceDeploymentSerializer,
    }


class UserStateSerializer(serializers.ModelSerializer):
    deployments = DeploymentSerializer(many=True)
    services = ServiceSerializer(many=True)
154 155 156
    ssh_keys = SSHPublicKeySerializer(many=True)
    states = DeploymentStateSerializer(many=True)
    vos = VOSerializer(many=True)
Lukas Burgey's avatar
Lukas Burgey committed
157 158 159

    class Meta:
        model = User
160
        fields = (
Lukas Burgey's avatar
Lukas Burgey committed
161 162
            'deployments',
            'id',
163 164
            'profile_name',
            'services',
Lukas Burgey's avatar
Lukas Burgey committed
165
            'ssh_keys',
166
            'states',
Lukas Burgey's avatar
Lukas Burgey committed
167
            'userinfo',
168
            'vos',
169
        )
170 171 172


class StateSerializer(serializers.Serializer):
Lukas Burgey's avatar
Lukas Burgey committed
173 174 175 176 177 178 179
    msg = serializers.CharField(
        allow_blank=True,
        required=False,
    )
    session = serializers.JSONField(
        required=False,
    )
180
    user = UserStateSerializer()
181 182 183 184 185 186


class UpdateSerializer(serializers.Serializer):
    error = serializers.CharField(allow_blank=True, required=False)
    deployment = DeploymentSerializer(required=False)
    deployment_state = DeploymentStateSerializer(required=False)