views.py 3.34 KB
Newer Older
1

2
import logging
3
from rest_framework import generics, views
4
from rest_framework.authentication import BasicAuthentication
5
from rest_framework.response import Response
Lukas Burgey's avatar
Lukas Burgey committed
6
from .serializers import DeploymentStateSerializer, ServiceSerializer, RabbitMQInstanceSerializer
7
from ..models import RabbitMQInstance
8

9
10
11
LOGGER = logging.getLogger(__name__)


12
# authentication class for the client api
13
AUTHENTICATION_CLASSES = (BasicAuthentication, )
14

15

16
# The client fetches pending tasks
17
class DeploymentsView(generics.ListAPIView):
Lukas Burgey's avatar
Lukas Burgey committed
18
    authentication_classes = AUTHENTICATION_CLASSES
Lukas Burgey's avatar
Lukas Burgey committed
19
    serializer_class = DeploymentStateSerializer
20

21
    def get_queryset(self):
Lukas Burgey's avatar
Lukas Burgey committed
22
        return self.request.user.site.pending_tasks
23
24


25
# the client has to fetch the configuration (like services etc.) here
26
class ConfigurationView(views.APIView):
Lukas Burgey's avatar
Lukas Burgey committed
27
    authentication_classes = AUTHENTICATION_CLASSES
28

29
30
31
32
33
34
35
36
37
    def get(self, request):
        response = {
            'services': ServiceSerializer(
                request.user.site.services.all(),
                many=True,
            ).data,
            'rabbitmq_config': RabbitMQInstanceSerializer(
                RabbitMQInstance.load(),
            ).data,
Lukas Burgey's avatar
Lukas Burgey committed
38
            'site': request.user.site.name,
39
40
        }
        return Response(response)
41
42
43


class AckView(views.APIView):
Lukas Burgey's avatar
Lukas Burgey committed
44
    authentication_classes = AUTHENTICATION_CLASSES
45

Lukas Burgey's avatar
Lukas Burgey committed
46
47
48
    def delete(self, request, state_id=None):
        # find the corresponding state for this item
        for item in request.user.site.state_items.all():
49
50
            if item.parent.id == int(state_id):
                if item.parent.state_target == 'deployed':
Lukas Burgey's avatar
Lukas Burgey committed
51
                    item.client_deployed({})
52
53
                else:
                    item.client_removed()
Lukas Burgey's avatar
Lukas Burgey committed
54
                return Response({})
55

Lukas Burgey's avatar
Lukas Burgey committed
56
        LOGGER.info('%s executed the obsolete state#%s', request.user, state_id)
Lukas Burgey's avatar
Lukas Burgey committed
57
58
59
60
        return Response(
            data={'error': 'obsolete_state'},
            status=500,
        )
61
62
63
64
65
66


class ResponseView(views.APIView):
    authentication_classes = AUTHENTICATION_CLASSES

    def post(self, request):
67
68
        output = request.data['output']
        status = output['status']
Lukas Burgey's avatar
Lukas Burgey committed
69
        state_id = request.data['id']
70

71
        # LOGGER.debug('%s responded to state %s:\n%s', request.user, state_id, request.data)
72

Lukas Burgey's avatar
Lukas Burgey committed
73
74
75
        # find the corresponding state for this item
        state_item = None
        for item in request.user.site.state_items.all():
76
            if item.parent.id == int(state_id):
Lukas Burgey's avatar
Lukas Burgey committed
77
                state_item = item
78

Lukas Burgey's avatar
Lukas Burgey committed
79
        if state_item is not None:
80
81
            if status == 'deployed':
                state_item.client_deployed(
Lukas Burgey's avatar
Lukas Burgey committed
82
                    credentials=output.get('credentials', {}),
83
84
                )
                return Response({})
85

86
87
            elif status == 'removed':
                state_item.client_removed()
88
                return Response({})
89

90
91
92
93
            elif status == 'questionnaire':
                state_item.client_questionnaire(
                    output['questionnaire'],
                )
94
                return Response({})
95

96
97
98
99
100
101
            LOGGER.error("Unrecognized response status from client: %s", status)
            return Response(
                data={'error': 'unknown status'},
                status=500,
            )

Lukas Burgey's avatar
Lukas Burgey committed
102
        LOGGER.info('%s executed the obsolete state#%s', request.user, state_id)
103
        return Response(
Lukas Burgey's avatar
Lukas Burgey committed
104
            data={'error': 'obsolete_state'},
105
106
            status=500,
        )