2019-12-05 07:09:25 +00:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
|
|
|
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django_filters import utils
|
2019-12-05 07:09:25 +00:00
|
|
|
from rest_framework import viewsets, generics, status
|
2021-02-22 10:35:53 +00:00
|
|
|
from rest_framework.decorators import action
|
2023-07-24 03:52:25 +00:00
|
|
|
from rest_framework.request import Request
|
|
|
|
from rest_framework.response import Response
|
2019-12-05 07:09:25 +00:00
|
|
|
|
2021-02-22 10:35:53 +00:00
|
|
|
from common.const.http import GET
|
2023-07-24 03:52:25 +00:00
|
|
|
from terminal import const
|
2021-02-22 10:35:53 +00:00
|
|
|
from terminal.filters import CommandStorageFilter, CommandFilter, CommandFilterForStorageTree
|
2022-10-25 04:57:34 +00:00
|
|
|
from terminal.models import CommandStorage, ReplayStorage
|
|
|
|
from terminal.serializers import CommandStorageSerializer, ReplayStorageSerializer
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'CommandStorageViewSet', 'CommandStorageTestConnectiveApi',
|
|
|
|
'ReplayStorageViewSet', 'ReplayStorageTestConnectiveApi'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class BaseStorageViewSetMixin:
|
|
|
|
|
|
|
|
def destroy(self, request, *args, **kwargs):
|
|
|
|
instance = self.get_object()
|
2022-06-30 04:34:23 +00:00
|
|
|
if instance.type_null_or_server or instance.is_default:
|
2019-12-05 07:09:25 +00:00
|
|
|
data = {'msg': _('Deleting the default storage is not allowed')}
|
|
|
|
return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
|
2021-01-12 10:06:42 +00:00
|
|
|
if instance.is_use():
|
2020-09-28 12:02:30 +00:00
|
|
|
data = {'msg': _('Cannot delete storage that is being used')}
|
|
|
|
return Response(data=data, status=status.HTTP_400_BAD_REQUEST)
|
2019-12-05 07:09:25 +00:00
|
|
|
return super().destroy(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class CommandStorageViewSet(BaseStorageViewSetMixin, viewsets.ModelViewSet):
|
2021-06-28 02:32:59 +00:00
|
|
|
search_fields = ('name', 'type')
|
2019-12-05 07:09:25 +00:00
|
|
|
queryset = CommandStorage.objects.all()
|
|
|
|
serializer_class = CommandStorageSerializer
|
2021-02-22 10:35:53 +00:00
|
|
|
filterset_class = CommandStorageFilter
|
2022-02-17 12:13:31 +00:00
|
|
|
rbac_perms = {
|
2022-03-08 07:11:26 +00:00
|
|
|
'tree': 'terminal.view_commandstorage | terminal.view_command'
|
2022-02-17 12:13:31 +00:00
|
|
|
}
|
2021-02-22 10:35:53 +00:00
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
@action(methods=[GET], detail=False, filterset_class=CommandFilterForStorageTree)
|
2021-02-22 10:35:53 +00:00
|
|
|
def tree(self, request: Request):
|
|
|
|
storage_qs = self.get_queryset().exclude(name='null')
|
|
|
|
storages_with_count = []
|
2021-03-17 07:26:47 +00:00
|
|
|
invalid_storages = []
|
|
|
|
|
2021-02-22 10:35:53 +00:00
|
|
|
for storage in storage_qs:
|
2021-03-17 07:26:47 +00:00
|
|
|
if not storage.is_valid():
|
|
|
|
invalid_storages.append(storage)
|
|
|
|
continue
|
|
|
|
|
2021-02-22 10:35:53 +00:00
|
|
|
command_qs = storage.get_command_queryset()
|
|
|
|
filterset = CommandFilter(
|
|
|
|
data=request.query_params, queryset=command_qs,
|
|
|
|
request=request
|
|
|
|
)
|
|
|
|
if not filterset.is_valid():
|
|
|
|
raise utils.translate_validation(filterset.errors)
|
|
|
|
command_qs = filterset.qs
|
2022-11-04 03:40:16 +00:00
|
|
|
if storage.type == const.CommandStorageType.es:
|
2021-02-22 10:35:53 +00:00
|
|
|
command_count = command_qs.count(limit_to_max_result_window=False)
|
|
|
|
else:
|
|
|
|
command_count = command_qs.count()
|
|
|
|
storages_with_count.append((storage, command_count))
|
|
|
|
|
|
|
|
root = {
|
|
|
|
'id': 'root',
|
|
|
|
'name': _('Command storages'),
|
|
|
|
'title': _('Command storages'),
|
|
|
|
'pId': '',
|
|
|
|
'isParent': True,
|
|
|
|
'open': True,
|
|
|
|
}
|
|
|
|
|
2021-03-17 07:26:47 +00:00
|
|
|
invalid = _('Invalid')
|
2021-02-22 10:35:53 +00:00
|
|
|
nodes = [
|
2023-07-24 03:52:25 +00:00
|
|
|
{
|
|
|
|
'id': storage.id,
|
|
|
|
'name': f'{storage.name}({storage.type})({command_count})',
|
|
|
|
'title': f'{storage.name}({storage.type})',
|
|
|
|
'pId': 'root',
|
|
|
|
'isParent': False,
|
|
|
|
'open': False,
|
|
|
|
'valid': True,
|
|
|
|
} for storage, command_count in storages_with_count
|
|
|
|
] + [
|
|
|
|
{
|
|
|
|
'id': storage.id,
|
|
|
|
'name': f'{storage.name}({storage.type}) *{invalid}',
|
|
|
|
'title': f'{storage.name}({storage.type})',
|
|
|
|
'pId': 'root',
|
|
|
|
'isParent': False,
|
|
|
|
'open': False,
|
|
|
|
'valid': False,
|
|
|
|
} for storage in invalid_storages
|
|
|
|
]
|
2021-02-22 10:35:53 +00:00
|
|
|
nodes.append(root)
|
|
|
|
return Response(data=nodes)
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ReplayStorageViewSet(BaseStorageViewSetMixin, viewsets.ModelViewSet):
|
2021-06-28 02:32:59 +00:00
|
|
|
filterset_fields = ('name', 'type', 'is_default')
|
2021-01-07 02:53:10 +00:00
|
|
|
search_fields = filterset_fields
|
2019-12-05 07:09:25 +00:00
|
|
|
queryset = ReplayStorage.objects.all()
|
|
|
|
serializer_class = ReplayStorageSerializer
|
|
|
|
|
|
|
|
|
|
|
|
class BaseStorageTestConnectiveMixin:
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
instance = self.get_object()
|
|
|
|
try:
|
|
|
|
is_valid = instance.is_valid()
|
|
|
|
except Exception as e:
|
|
|
|
is_valid = False
|
|
|
|
msg = _("Test failure: {}".format(str(e)))
|
|
|
|
else:
|
|
|
|
if is_valid:
|
|
|
|
msg = _("Test successful")
|
|
|
|
else:
|
|
|
|
msg = _("Test failure: Account invalid")
|
|
|
|
data = {
|
|
|
|
'is_valid': is_valid,
|
|
|
|
'msg': msg
|
|
|
|
}
|
|
|
|
return Response(data)
|
|
|
|
|
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
class CommandStorageTestConnectiveApi(BaseStorageTestConnectiveMixin, generics.RetrieveAPIView):
|
2019-12-05 07:09:25 +00:00
|
|
|
queryset = CommandStorage.objects.all()
|
2022-03-15 11:35:32 +00:00
|
|
|
rbac_perms = {
|
|
|
|
'retrieve': 'terminal.view_commandstorage'
|
|
|
|
}
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
|
2021-01-12 10:06:42 +00:00
|
|
|
class ReplayStorageTestConnectiveApi(BaseStorageTestConnectiveMixin, generics.RetrieveAPIView):
|
2019-12-05 07:09:25 +00:00
|
|
|
queryset = ReplayStorage.objects.all()
|
2022-03-15 11:35:32 +00:00
|
|
|
rbac_perms = {
|
|
|
|
'retrieve': 'terminal.view_replaystorage'
|
|
|
|
}
|