2018-12-10 02:11:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2017-03-22 16:15:25 +00:00
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2018-01-08 11:16:28 +00:00
|
|
|
from rest_framework import viewsets, generics
|
|
|
|
from rest_framework.views import Response
|
2017-03-22 16:15:25 +00:00
|
|
|
|
2018-07-23 04:55:13 +00:00
|
|
|
from common.permissions import IsOrgAdmin
|
2019-08-21 12:27:21 +00:00
|
|
|
from common.serializers import CeleryTaskSerializer
|
2020-03-12 08:24:38 +00:00
|
|
|
from ..models import Task, AdHoc, AdHocExecution
|
2020-06-15 11:42:36 +00:00
|
|
|
from ..serializers import (
|
|
|
|
TaskSerializer,
|
|
|
|
AdHocSerializer,
|
|
|
|
AdHocExecutionSerializer,
|
|
|
|
TaskDetailSerializer,
|
|
|
|
AdHocDetailSerializer,
|
|
|
|
)
|
2018-12-10 02:11:54 +00:00
|
|
|
from ..tasks import run_ansible_task
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'TaskViewSet', 'TaskRun', 'AdHocViewSet', 'AdHocRunHistoryViewSet'
|
|
|
|
]
|
2017-03-22 16:15:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TaskViewSet(viewsets.ModelViewSet):
|
2018-07-14 16:55:05 +00:00
|
|
|
queryset = Task.objects.all()
|
2019-12-03 07:31:42 +00:00
|
|
|
filter_fields = ("name",)
|
|
|
|
search_fields = filter_fields
|
2017-03-22 16:15:25 +00:00
|
|
|
serializer_class = TaskSerializer
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-12-10 02:11:54 +00:00
|
|
|
|
2020-06-09 12:26:23 +00:00
|
|
|
def get_serializer_class(self):
|
|
|
|
if self.action == 'retrieve':
|
|
|
|
return TaskDetailSerializer
|
|
|
|
return super().get_serializer_class()
|
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
2020-03-12 08:24:38 +00:00
|
|
|
queryset = queryset.select_related('latest_execution')
|
2018-12-10 02:11:54 +00:00
|
|
|
return queryset
|
2017-03-22 16:15:25 +00:00
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2018-01-08 11:16:28 +00:00
|
|
|
class TaskRun(generics.RetrieveAPIView):
|
2018-07-14 16:55:05 +00:00
|
|
|
queryset = Task.objects.all()
|
2019-08-21 12:27:21 +00:00
|
|
|
serializer_class = CeleryTaskSerializer
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-01-08 11:16:28 +00:00
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
task = self.get_object()
|
2018-04-01 15:45:37 +00:00
|
|
|
t = run_ansible_task.delay(str(task.id))
|
|
|
|
return Response({"task": t.id})
|
2018-01-08 11:16:28 +00:00
|
|
|
|
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
class AdHocViewSet(viewsets.ModelViewSet):
|
2018-07-14 16:55:05 +00:00
|
|
|
queryset = AdHoc.objects.all()
|
2017-12-10 16:29:25 +00:00
|
|
|
serializer_class = AdHocSerializer
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2017-12-10 16:29:25 +00:00
|
|
|
|
2020-06-15 11:42:36 +00:00
|
|
|
def get_serializer_class(self):
|
|
|
|
if self.action == 'retrieve':
|
|
|
|
return AdHocDetailSerializer
|
|
|
|
return super().get_serializer_class()
|
|
|
|
|
2017-12-10 16:29:25 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
task_id = self.request.query_params.get('task')
|
|
|
|
if task_id:
|
|
|
|
task = get_object_or_404(Task, id=task_id)
|
|
|
|
self.queryset = self.queryset.filter(task=task)
|
|
|
|
return self.queryset
|
|
|
|
|
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
class AdHocRunHistoryViewSet(viewsets.ModelViewSet):
|
2020-03-12 08:24:38 +00:00
|
|
|
queryset = AdHocExecution.objects.all()
|
|
|
|
serializer_class = AdHocExecutionSerializer
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2017-12-10 16:29:25 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
task_id = self.request.query_params.get('task')
|
2017-12-20 03:30:15 +00:00
|
|
|
adhoc_id = self.request.query_params.get('adhoc')
|
2017-12-10 16:29:25 +00:00
|
|
|
if task_id:
|
|
|
|
task = get_object_or_404(Task, id=task_id)
|
|
|
|
adhocs = task.adhoc.all()
|
|
|
|
self.queryset = self.queryset.filter(adhoc__in=adhocs)
|
2017-12-20 03:30:15 +00:00
|
|
|
|
|
|
|
if adhoc_id:
|
|
|
|
adhoc = get_object_or_404(AdHoc, id=adhoc_id)
|
|
|
|
self.queryset = self.queryset.filter(adhoc=adhoc)
|
2017-12-10 16:29:25 +00:00
|
|
|
return self.queryset
|
2018-03-30 14:03:43 +00:00
|
|
|
|
|
|
|
|
2018-04-01 15:45:37 +00:00
|
|
|
|
2018-03-30 14:03:43 +00:00
|
|
|
|
2018-04-02 07:54:49 +00:00
|
|
|
|