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
|
2018-12-10 02:11:54 +00:00
|
|
|
from orgs.utils import current_org
|
|
|
|
from ..models import Task, AdHoc, AdHocRunHistory
|
|
|
|
from ..serializers import TaskSerializer, AdHocSerializer, \
|
2018-04-02 07:54:49 +00:00
|
|
|
AdHocRunHistorySerializer
|
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()
|
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
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
2018-12-18 09:28:45 +00:00
|
|
|
if current_org.is_real():
|
2018-12-10 02:11:54 +00:00
|
|
|
queryset = queryset.filter(created_by=current_org.id)
|
2018-12-18 09:28:45 +00:00
|
|
|
else:
|
|
|
|
queryset = queryset.filter(created_by='')
|
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
|
|
|
|
|
|
|
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):
|
2018-07-14 16:55:05 +00:00
|
|
|
queryset = AdHocRunHistory.objects.all()
|
2017-12-10 16:29:25 +00:00
|
|
|
serializer_class = AdHocRunHistorySerializer
|
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
|
|
|
|