jumpserver/apps/assets/api/platform.py

89 lines
3.3 KiB
Python
Raw Normal View History

from rest_framework import generics
from rest_framework import serializers
from rest_framework.decorators import action
from rest_framework.response import Response
from assets.const import AllTypes
from assets.models import Platform, Node, Asset
2022-09-29 12:44:45 +00:00
from assets.serializers import PlatformSerializer
2023-02-22 12:30:43 +00:00
from common.api import JMSModelViewSet
from common.permissions import IsValidUser
2023-02-22 12:30:43 +00:00
from common.serializers import GroupedChoiceSerializer
2022-04-02 10:35:46 +00:00
__all__ = ['AssetPlatformViewSet', 'PlatformAutomationMethodsApi']
2022-04-02 10:35:46 +00:00
2022-04-28 14:54:18 +00:00
class AssetPlatformViewSet(JMSModelViewSet):
2022-04-02 10:35:46 +00:00
queryset = Platform.objects.all()
2022-04-28 14:54:18 +00:00
serializer_classes = {
'default': PlatformSerializer,
2022-09-13 02:41:49 +00:00
'categories': GroupedChoiceSerializer
2022-04-28 14:54:18 +00:00
}
2022-08-22 05:25:57 +00:00
filterset_fields = ['name', 'category', 'type']
2022-04-02 10:35:46 +00:00
search_fields = ['name']
2022-04-28 14:54:18 +00:00
rbac_perms = {
2022-05-05 08:18:05 +00:00
'categories': 'assets.view_platform',
2022-08-29 07:50:25 +00:00
'type_constraints': 'assets.view_platform',
'ops_methods': 'assets.view_platform',
'filter_nodes_assets': 'assets.view_platform'
2022-04-28 14:54:18 +00:00
}
def get_queryset(self):
queryset = super().get_queryset()
queryset = queryset.filter(type__in=AllTypes.get_types_values())
return queryset
2022-10-27 10:34:25 +00:00
def get_object(self):
pk = self.kwargs.get('pk', '')
if pk.isnumeric():
return super().get_object()
return self.get_queryset().get(name=pk)
2022-04-02 10:35:46 +00:00
def check_object_permissions(self, request, obj):
if request.method.lower() in ['delete', 'put', 'patch'] and obj.internal:
self.permission_denied(
request, message={"detail": "Internal platform"}
)
return super().check_object_permissions(request, obj)
@action(methods=['post'], detail=False, url_path='filter-nodes-assets')
def filter_nodes_assets(self, request, *args, **kwargs):
node_ids = request.data.get('node_ids', [])
asset_ids = request.data.get('asset_ids', [])
nodes = Node.objects.filter(id__in=node_ids)
node_asset_ids = Node.get_nodes_all_assets(*nodes).values_list('id', flat=True)
direct_asset_ids = Asset.objects.filter(id__in=asset_ids).values_list('id', flat=True)
platform_ids = Asset.objects.filter(
id__in=set(list(direct_asset_ids) + list(node_asset_ids))
).values_list('platform_id', flat=True)
platforms = Platform.objects.filter(id__in=platform_ids)
serializer = self.get_serializer(platforms, many=True)
return Response(serializer.data)
class PlatformAutomationMethodsApi(generics.ListAPIView):
permission_classes = (IsValidUser,)
@staticmethod
def automation_methods():
return AllTypes.get_automation_methods()
def generate_serializer_fields(self):
data = self.automation_methods()
fields = {
2023-04-20 06:05:00 +00:00
i['id']: i['params_serializer'](label=i['name'])
if i['params_serializer'] else None
for i in data
}
return fields
def get_serializer_class(self):
fields = self.generate_serializer_fields()
serializer_name = 'AutomationMethodsSerializer'
return type(serializer_name, (serializers.Serializer,), fields)
def list(self, request, *args, **kwargs):
data = self.generate_serializer_fields()
serializer = self.get_serializer(data)
return Response(serializer.data)