__init__.py 4.67 KB
Newer Older
1

2
from rest_framework.serializers import ModelSerializer, Serializer, CharField, JSONField
Lukas Burgey's avatar
Lukas Burgey committed
3
4
from rest_polymorphic.serializers import PolymorphicSerializer

5
6
7
from feudal.backend.models import Site, Service
from feudal.backend.models.users import User, SSHPublicKey
from feudal.backend.models.deployments import CredentialState, DeploymentState, Deployment, VODeployment, ServiceDeployment
8

9
from feudal.backend.models.auth.serializers import VOSerializer
Lukas Burgey's avatar
Lukas Burgey committed
10

11

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


22
class SSHPublicKeyRefSerializer(ModelSerializer):
23
    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(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

44
class SiteSerializer(ModelSerializer):
Lukas Burgey's avatar
Lukas Burgey committed
45
46
    class Meta:
        model = Site
47
48
49
50
51
        fields = [
            'id',
            'name',
            'description',
        ]
Lukas Burgey's avatar
Lukas Burgey committed
52
53


54
class ServiceSerializer(ModelSerializer):
Lukas Burgey's avatar
Lukas Burgey committed
55
56
57
58
59
60
61
62
63
64
65
    site = SiteSerializer()
    vos = VOSerializer(many=True)

    class Meta:
        model = Service
        fields = [
            'id',
            'name',
            'site',
            'vos',
            'description',
66
67
            'contact_email',
            'contact_description',
Lukas Burgey's avatar
Lukas Burgey committed
68
69
70
        ]


71
class CompactServiceSerializer(ModelSerializer):
72
73
74
75
76
77
78
79
80
    class Meta:
        model = Service
        fields = [
            'id',
            'name',
            'description',
        ]


81
class DeploymentStateSerializer(ModelSerializer):
82
    # why all the read_onlys: the rest interface exposes these fields, but the must not be changed
83
    answers = JSONField() # not read_only: it is patched by the user
84
    credential_states = CredentialStateSerializer(many=True, read_only=True)
85
86
    credentials = JSONField(read_only=True)
    questionnaire = JSONField(read_only=True)
87
88
    service = CompactServiceSerializer(read_only=True)
    site = SiteSerializer(read_only=True)
Lukas Burgey's avatar
Lukas Burgey committed
89
90
91

    class Meta:
        model = DeploymentState
92
        fields = (
93
            'answers',
Lukas Burgey's avatar
Lukas Burgey committed
94
            'credential_states',
95
96
            'credentials',
            'id',
Lukas Burgey's avatar
Lukas Burgey committed
97
98
            'is_credential_pending',
            'is_pending',
99
100
101
102
103
104
            'message',
            'questionnaire',
            'service',
            'site',
            'state',
            'state_target',
105
106
107
108
109
        )
        read_only_fields = (
            'state',
            'message',
        )
Lukas Burgey's avatar
Lukas Burgey committed
110
111
112
113
114


DEPLOYMENT_FIELDS = (
    'state_target',
    'id',
115
    'states',
Lukas Burgey's avatar
Lukas Burgey committed
116
117
118
)


119
class AbstractDeploymentSerializer(ModelSerializer):
120
    states = DeploymentStateSerializer(many=True)
Lukas Burgey's avatar
Lukas Burgey committed
121
122
123
124
125
126

    class Meta:
        model = Deployment
        fields = DEPLOYMENT_FIELDS


127
class VODeploymentSerializer(ModelSerializer):
128
    states = DeploymentStateSerializer(many=True)
129
130
131
    vo = VOSerializer()
    services = ServiceSerializer(many=True)

Lukas Burgey's avatar
Lukas Burgey committed
132
133
134
135
136
137
138
139
    class Meta:
        model = VODeployment
        fields = DEPLOYMENT_FIELDS + (
            'vo',
            'services',
        )


140
class ServiceDeploymentSerializer(ModelSerializer):
141
    states = DeploymentStateSerializer(many=True, read_only=True)
142
    service = ServiceSerializer()
143

Lukas Burgey's avatar
Lukas Burgey committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    class Meta:
        model = ServiceDeployment
        fields = DEPLOYMENT_FIELDS + (
            'service',
        )


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


159
class UserStateSerializer(ModelSerializer):
Lukas Burgey's avatar
Lukas Burgey committed
160
161
    deployments = DeploymentSerializer(many=True)
    services = ServiceSerializer(many=True)
162
163
164
    ssh_keys = SSHPublicKeySerializer(many=True)
    states = DeploymentStateSerializer(many=True)
    vos = VOSerializer(many=True)
Lukas Burgey's avatar
Lukas Burgey committed
165
166
167

    class Meta:
        model = User
168
        fields = (
Lukas Burgey's avatar
Lukas Burgey committed
169
170
            'deployments',
            'id',
171
172
            'profile_name',
            'services',
Lukas Burgey's avatar
Lukas Burgey committed
173
            'ssh_keys',
174
            'states',
Lukas Burgey's avatar
Lukas Burgey committed
175
            'userinfo',
176
            'vos',
177
        )
178
179


Lukas Burgey's avatar
Lukas Burgey committed
180
# pylint: disable=abstract-method
181
182
class StateSerializer(Serializer):
    msg = CharField(
Lukas Burgey's avatar
Lukas Burgey committed
183
184
185
        allow_blank=True,
        required=False,
    )
186
    session = JSONField(
Lukas Burgey's avatar
Lukas Burgey committed
187
188
        required=False,
    )
189
    user = UserStateSerializer()
190
191


192
193
class UpdateSerializer(Serializer):
    error = CharField(allow_blank=True, required=False)
194
195
    deployment = DeploymentSerializer(required=False)
    deployment_state = DeploymentStateSerializer(required=False)