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
|
|
|
|
from orgs.utils import tmp_to_builtin_org
|
2022-10-28 10:19:44 +00:00
|
|
|
from terminal import serializers
|
2022-11-01 03:52:51 +00:00
|
|
|
from terminal.models import AppletHost, Applet, 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-10-25 04:57:34 +00:00
|
|
|
serializer_class = serializers.AppletHostSerializer
|
2022-11-01 03:52:51 +00:00
|
|
|
queryset = AppletHost.objects.all()
|
2022-11-03 08:55:38 +00:00
|
|
|
rbac_perms = {
|
|
|
|
'accounts': 'terminal.view_applethost',
|
|
|
|
'reports': '*'
|
|
|
|
}
|
|
|
|
|
|
|
|
@action(methods=['post'], detail=True, serializer_class=serializers.AppletHostReportSerializer)
|
|
|
|
def reports(self, request, *args, **kwargs):
|
|
|
|
# 1. Host 和 Terminal 关联
|
|
|
|
# 2. 上报 安装的 Applets 每小时
|
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)
|
|
|
|
|
|
|
|
data = serializer.validated_data
|
|
|
|
instance.check_terminal_binding(request)
|
|
|
|
instance.check_applets_state(data['applets'])
|
2022-11-02 12:33:27 +00:00
|
|
|
return Response({'msg': 'ok'})
|
|
|
|
|
2022-11-03 08:55:38 +00:00
|
|
|
@action(methods=['get'], detail=True, serializer_class=serializers.AppletHostAccountSerializer)
|
2022-11-02 12:33:27 +00:00
|
|
|
def accounts(self, request, *args, **kwargs):
|
2022-11-03 08:55:38 +00:00
|
|
|
host = self.get_object()
|
|
|
|
with tmp_to_builtin_org(system=1):
|
|
|
|
accounts = host.accounts.all().filter(privileged=False)
|
|
|
|
response = self.get_paginated_response_from_queryset(accounts)
|
|
|
|
return response
|
2022-11-02 12:33:27 +00:00
|
|
|
|
2022-11-01 03:52:51 +00:00
|
|
|
|
|
|
|
class AppletHostDeploymentViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = serializers.AppletHostDeploymentSerializer
|
|
|
|
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)
|
|
|
|
|