views.py 3.6 KB
Newer Older
Elia Ponzio's avatar
Elia Ponzio committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from rest_framework import permissions, authentication
from rest_framework.response import Response
from rest_framework.decorators import api_view, permission_classes, authentication_classes, action
from api.serializers import UserSerializer, DeltaSerializer, PeriodicTaskSerializer, IntervalScheduleSerializer

from cynomys.models import Delta
from django_celery_beat.models import PeriodicTask, IntervalSchedule

class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    permission_classes = [permissions.IsAuthenticated]

class DeltaViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = Delta.objects.all()
    serializer_class = DeltaSerializer
    permission_classes = [permissions.IsAuthenticated]

class TaskViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = PeriodicTask.objects.all()
    serializer_class = PeriodicTaskSerializer
    permission_classes = [permissions.IsAuthenticated]

    @action(detail=True, methods=['GET'])
    def get_all_deltas(self, request, pk=None):

        deltas = Delta.objects.all().filter(task=pk).order_by('-latest_check')

        serializer = DeltaSerializer(deltas, many=True, context={'request': request})
        return Response({'Message': "All deltas of this task.", 'Deltas': serializer.data})

    @action(detail=True, methods=['GET'])
    def latest_delta(self, request, pk=None):

        deltas = Delta.objects.filter(task=pk).latest('pk')

        serializer = DeltaSerializer(deltas, many=False, context={'request': request})
        return Response({'Message': "Latest Delta, current status.", 'Deltas': serializer.data})

    @action(detail=False, methods=['POST'])
    def create_ping_check(self, request):
        task = PeriodicTask()
        task.name = request.data['name']
        task.task = "cynomys.tasks.check_ping"
        task.interval = IntervalSchedule.objects.get(pk=request.data['interval_id'])
Elia Ponzio's avatar
Updates    
Elia Ponzio committed
57
        task.args = '["'+ request.data['host'] +'","'+  request.data['timeout'] + '"]'
Elia Ponzio's avatar
Elia Ponzio committed
58
59
60
        task.expire_seconds = 30
        task.save()

Elia Ponzio's avatar
Updates    
Elia Ponzio committed
61
        task.kwargs = '{"task_id": "' + str(task.pk) + '", "notifier": "email"}'
Elia Ponzio's avatar
Elia Ponzio committed
62
63
64
65
66
67
68
69
70
71
72
        task.save()

        serializer = PeriodicTaskSerializer(task, many=False, context={'request': request})
        return Response({'Message': "Task Created.", 'Task': serializer.data})

    @action(detail=False, methods=['POST'])
    def create_http_check(self, request):
        task = PeriodicTask()
        task.name = request.data['name']
        task.task = "cynomys.tasks.check_http"
        task.interval = IntervalSchedule.objects.get(pk=request.data['interval_id'])
Elia Ponzio's avatar
Updates    
Elia Ponzio committed
73
        task.args = '["'+ request.data['host'] +'","'+  request.data['timeout'] + '","'+  request.data['httpcode'] + '"]'
Elia Ponzio's avatar
Elia Ponzio committed
74
75
76
        task.expire_seconds = 30
        task.save()

Elia Ponzio's avatar
Updates    
Elia Ponzio committed
77
        task.kwargs = '{"task_id": "' + str(task.pk) + '", "notifier": "email"}'
Elia Ponzio's avatar
Elia Ponzio committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        task.save()

        serializer = PeriodicTaskSerializer(task, many=False, context={'request': request})
        return Response({'Message': "Task Created.", 'Task': serializer.data})


class IntervalViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = IntervalSchedule.objects.all()
    serializer_class = IntervalScheduleSerializer
    permission_classes = [permissions.IsAuthenticated]