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

64 lines
2.5 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-03 08:55:38 +00:00
from common.drf.api import JMSModelViewSet
2022-11-14 10:48:21 +00:00
from common.permissions import IsServiceAccount
2022-11-08 05:55:06 +00:00
from orgs.utils import tmp_to_builtin_org
2022-11-14 10:48:21 +00:00
from terminal.models import AppletHost, AppletHostDeployment
2022-11-07 12:41:18 +00:00
from terminal.serializers import (
AppletHostSerializer, AppletHostDeploymentSerializer,
2022-11-14 10:48:21 +00:00
AppletHostStartupSerializer, AppletHostDeployAppletSerializer
2022-11-07 12:41:18 +00:00
)
2022-11-14 10:48:21 +00:00
from terminal.tasks import run_applet_host_deployment, run_applet_host_deployment_install_applet
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()
2022-11-14 10:48:21 +00:00
rbac_perms = (
('applets', 'terminal.view_AppletHostDeployment'),
)
2022-11-01 03:52:51 +00:00
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)
2022-11-15 08:01:01 +00:00
instance.save_task(task.id)
2022-11-01 03:52:51 +00:00
return Response({'task': str(task.id)}, status=201)
2022-11-14 10:48:21 +00:00
@action(methods=['post'], detail=False, serializer_class=AppletHostDeployAppletSerializer)
def applets(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
applet_id = serializer.validated_data.get('applet_id')
instance = serializer.save()
task = run_applet_host_deployment_install_applet.delay(instance.id, applet_id)
2022-11-15 08:01:01 +00:00
instance.save_task(task.id)
2022-11-14 10:48:21 +00:00
return Response({'task': str(task.id)}, status=201)