views.py 4.35 KB
Newer Older
Lukas Burgey's avatar
Lukas Burgey committed
1
2
from django.db import connections
from django.db.utils import OperationalError
3
from django.shortcuts import get_object_or_404
Lukas Burgey's avatar
Lukas Burgey committed
4
5
6
7
8
from rest_framework import views, viewsets
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework import status

9
from . import serializers, models as frontend_models
10
from .. import models
Lukas Burgey's avatar
Lukas Burgey committed
11
12


13
14
15
16
17
18
19
20
21
22
23
# Response for StateView, LogoutView, and all post requests
def state_response(request):
    response = {
            'logged_in': request.user.is_authenticated()
            }
    if request.user.is_authenticated():
        response['user'] = serializers.UserSerializer(request.user).data

    response['state'] = serializers.StateSerializer(
            frontend_models.State(request.user)).data

24
25
26
27
28
    if 'error' in request.session:
        response['error'] = request.session['error']
        # we display errors only once
        del request.session['error']

29
30
31
    return Response(response)


Lukas Burgey's avatar
Lukas Burgey committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class OperationalView(views.APIView):
    authentication_classes = []
    permission_classes = (AllowAny,)

    def get(self, request, format=None):
        try:
            db_conn = connections['default']
            db_conn.cursor()
            # we check if we can access the state
            request.session
        except OperationalError:
            op = False
        else:
            op = True
        response = {'operational': op}
        return Response(response)


class StateView(views.APIView):
    permission_classes = (AllowAny,)

    def get(self, request, format=None):
54
        return state_response(request)
Lukas Burgey's avatar
Lukas Burgey committed
55
56
57
58
59
60
61
62
63


class ServiceViewSet(viewsets.ModelViewSet):
    serializer_class = serializers.ServiceSerializer
    queryset = models.Service.objects.all()


class SSHPublicKeyView(views.APIView):
    def post(self, request, format=None):
Lukas Burgey's avatar
Lukas Burgey committed
64
65
66
67
68
69
70
71
72
73
        if 'type' not in request.data:
            return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST)

        request_type = request.data['type']

        if request_type == 'remove':
            if 'name' in request.data:
                key = get_object_or_404(
                        models.SSHPublicKey,
                        name=request.data['name'])
Lukas Burgey's avatar
Lukas Burgey committed
74
75
                # we do not delete ssh keys directly, as we need to keep track
                # of them until all clients have also deleted them
76
77
78

                # key state: -> (1), if there are no deployments
                # key state: -> (5), if there are deployments
79
                key.delete_key()
80
                return state_response(request)
Lukas Burgey's avatar
Lukas Burgey committed
81
82
83
84
85
86
87
        elif request_type == 'add':
            if 'key' in request.data:
                key = models.SSHPublicKey(
                        user=request.user,
                        name=request.data['key']['name'],
                        key=request.data['key']['key'],
                        )
88
                # key state: -> (2)
Lukas Burgey's avatar
Lukas Burgey committed
89
                key.save()
90
                return state_response(request)
Lukas Burgey's avatar
Lukas Burgey committed
91
92

        return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST)
Lukas Burgey's avatar
Lukas Burgey committed
93

Lukas Burgey's avatar
Lukas Burgey committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

class DeploymentView(views.APIView):
    def post(self, request, format=None):
        if (
                'type' not in request.data or
                'key' not in request.data or
                'service' not in request.data
                ):
            return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST)

        request_type = request.data['type']
        request_service = get_object_or_404(
                models.Service, name=request.data['service'])
        request_key = get_object_or_404(
                models.SSHPublicKey,
                name=request.data['key'])

        # check if there is already an deployment
        try:
            deployment = request.user.deployments.get(service=request_service)
        except Exception:
Lukas Burgey's avatar
Lukas Burgey committed
115
            if request_type == 'remove':
Lukas Burgey's avatar
Lukas Burgey committed
116
117
118
119
120
121
122
123
124
                return Response({'ok': False},
                                status=status.HTTP_400_BAD_REQUEST)

            deployment = models.Deployment(
                    user=request.user,
                    service=request_service)
            deployment.save()

        if request_type == 'add':
125
            deployment.deploy_key(request_key)
Lukas Burgey's avatar
Lukas Burgey committed
126
        elif request_type == 'remove':
127
128
129
            deployment.withdraw_key(request_key)
        else:
            return Response({'ok': False}, status=status.HTTP_400_BAD_REQUEST)
Lukas Burgey's avatar
Lukas Burgey committed
130

131
132
        deployment.save()
        return state_response(request)