clients.py 3.11 KB
Newer Older
1
2
# we don't need to deserialize, so we do not implement the abstract methods
# pylint: disable=abstract-method
3

Lukas Burgey's avatar
Lukas Burgey committed
4
from django_mysql.models import JSONField
5
from rest_framework.serializers import ModelSerializer, DictField, ListField
6
from rest_polymorphic.serializers import PolymorphicSerializer
Lukas Burgey's avatar
Lukas Burgey committed
7

8
9
10
11
12
from feudal.backend.models import Service
from feudal.backend.models.brokers import RabbitMQInstance
from feudal.backend.models.users import User, SSHPublicKey
from feudal.backend.models.deployments import Deployment, VODeployment, ServiceDeployment, DeploymentState, CredentialState
from feudal.backend.models.auth.serializers.clients import VOSerializer
13

14

15
class ServiceSerializer(ModelSerializer):
Lukas Burgey's avatar
Lukas Burgey committed
16
    class Meta:
17
        model = Service
Lukas Burgey's avatar
Lukas Burgey committed
18
19
20
        fields = [
            'name',
        ]
Lukas Burgey's avatar
Lukas Burgey committed
21
22


23
class CredentialSerializer(ModelSerializer):
24
25
26
27
28
29
30
31
32
    class Meta:
        model = SSHPublicKey
        fields = [
            'id',
            'name',
            'value',
        ]


33
class UserSerializer(ModelSerializer):
34
    vos = VOSerializer(many=True)
Lukas Burgey's avatar
Lukas Burgey committed
35
    userinfo = JSONField()
36
37
    credentials = DictField(
        child=ListField(
38
39
40
            child=CredentialSerializer()
        )
    )
41
42

    class Meta:
43
        model = User
44
        fields = [
45
            'credentials',
46
47
            'email',
            'userinfo',
48
            'vos',
49
        ]
50
51


52
class RabbitMQInstanceSerializer(ModelSerializer):
53
    class Meta:
54
        model = RabbitMQInstance
55
56
57
        fields = [
            'vhost',
        ]
58
59
60
61
62
63
64
65
66


DEPLOYMENT_FIELDS = (
    'id',
    'state_target',
    'user',
)


67
class AbstractDeploymentSerializer(ModelSerializer):
68
69
70
    user = UserSerializer()

    class Meta:
71
        model = Deployment
72
73
74
        fields = DEPLOYMENT_FIELDS


75
class VODeploymentSerializer(ModelSerializer):
76
77
78
79
80
    user = UserSerializer()
    vo = VOSerializer()
    services = ServiceSerializer(many=True)

    class Meta:
81
        model = VODeployment
82
83
84
85
86
87
        fields = DEPLOYMENT_FIELDS + (
            'vo',
            'services',
        )


88
class ServiceDeploymentSerializer(ModelSerializer):
89
90
91
92
    user = UserSerializer()
    service = ServiceSerializer()

    class Meta:
93
        model = ServiceDeployment
94
95
96
97
98
99
100
        fields = DEPLOYMENT_FIELDS + (
            'service',
        )


class DeploymentSerializer(PolymorphicSerializer):
    model_serializer_mapping = {
101
102
103
        Deployment: AbstractDeploymentSerializer,
        VODeployment: VODeploymentSerializer,
        ServiceDeployment: ServiceDeploymentSerializer,
104
    }
105
106


107
class CredentialStateSerializer(ModelSerializer):
108
109

    class Meta:
110
        model = CredentialState
111
112
113
114
115
116
        fields = (
            'state',
            'state_target',
        )


117
class DeploymentStateSerializer(ModelSerializer):
118
119
    user = UserSerializer()
    service = ServiceSerializer()
120
    answers = JSONField()
121
    questionnaire = JSONField()
122
123

    class Meta:
124
        model = DeploymentState
125
126
127
128
129
130
        fields = (
            'id',
            'user',
            'state',
            'state_target',
            'credentials',
131
            'questionnaire',
132
            'service',
133
            'answers',
134
            'message',
135
        )
136
137
138
        read_only_fields = (
            'answers',
        )