jumpserver/apps/terminal/api/applet/host.py

52 lines
1.8 KiB
Python
Raw Normal View History

2022-10-25 04:57:34 +00:00
from rest_framework import viewsets
2022-10-28 10:19:44 +00:00
from rest_framework.decorators import action
from rest_framework.response import Response
2022-10-25 04:57:34 +00:00
2022-11-07 12:41:18 +00:00
from common.permissions import IsServiceAccount
2022-11-03 08:55:38 +00:00
from common.drf.api import JMSModelViewSet
2022-11-08 05:55:06 +00:00
from orgs.utils import tmp_to_builtin_org
2022-11-07 12:41:18 +00:00
from terminal.serializers import (
AppletHostSerializer, AppletHostDeploymentSerializer,
AppletHostStartupSerializer
)
from terminal.models import AppletHost, AppletHostDeployment
2022-10-28 10:19:44 +00:00
from terminal.tasks import run_applet_host_deployment
2022-10-25 04:57:34 +00:00
2022-11-01 03:52:51 +00:00
__all__ = ['AppletHostViewSet', 'AppletHostDeploymentViewSet']
2022-10-25 04:57:34 +00:00
2022-11-03 08:55:38 +00:00
class AppletHostViewSet(JMSModelViewSet):
2022-11-07 12:41:18 +00:00
serializer_class = AppletHostSerializer
2022-11-01 03:52:51 +00:00
queryset = AppletHost.objects.all()
2022-11-07 12:41:18 +00:00
2022-11-08 05:55:06 +00:00
def dispatch(self, request, *args, **kwargs):
with tmp_to_builtin_org(system=1):
return super().dispatch(request, *args, **kwargs)
2022-11-07 12:41:18 +00:00
def get_permissions(self):
if self.action == 'startup':
return [IsServiceAccount()]
return super().get_permissions()
@action(methods=['post'], detail=True, serializer_class=AppletHostStartupSerializer)
def startup(self, request, *args, **kwargs):
2022-11-02 12:33:27 +00:00
instance = self.get_object()
2022-11-03 08:55:38 +00:00
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
instance.check_terminal_binding(request)
2022-11-02 12:33:27 +00:00
return Response({'msg': 'ok'})
2022-11-01 03:52:51 +00:00
class AppletHostDeploymentViewSet(viewsets.ModelViewSet):
2022-11-07 12:41:18 +00:00
serializer_class = AppletHostDeploymentSerializer
2022-11-01 03:52:51 +00:00
queryset = AppletHostDeployment.objects.all()
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
instance = serializer.save()
task = run_applet_host_deployment.delay(instance.id)
return Response({'task': str(task.id)}, status=201)