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
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.api import JMSBulkModelViewSet
|
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
|
|
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
class AppletHostViewSet(JMSBulkModelViewSet):
|
2022-11-07 12:41:18 +00:00
|
|
|
serializer_class = AppletHostSerializer
|
2022-11-01 03:52:51 +00:00
|
|
|
queryset = AppletHost.objects.all()
|
2023-01-16 11:02:09 +00:00
|
|
|
search_fields = ['asset_ptr__name', 'asset_ptr__address', ]
|
2023-05-10 02:47:50 +00:00
|
|
|
rbac_perms = {
|
|
|
|
'generate_accounts': 'terminal.change_applethost',
|
|
|
|
}
|
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'})
|
|
|
|
|
2023-05-10 02:47:50 +00:00
|
|
|
@action(methods=['put'], detail=True, url_path='generate-accounts')
|
|
|
|
def generate_accounts(self, request, *args, **kwargs):
|
|
|
|
instance = self.get_object()
|
|
|
|
instance.generate_accounts()
|
|
|
|
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()
|
2023-01-16 11:02:09 +00:00
|
|
|
filterset_fields = ['host', ]
|
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)
|
2023-02-22 06:10:13 +00:00
|
|
|
applet_id = serializer.validated_data.pop('applet_id', '')
|
2022-11-14 10:48:21 +00:00
|
|
|
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)
|