jumpserver/apps/assets/api/platform.py

57 lines
2.2 KiB
Python
Raw Normal View History

2022-04-28 14:54:18 +00:00
from rest_framework.decorators import action
from rest_framework.response import Response
2022-04-02 10:35:46 +00:00
2022-04-28 14:54:18 +00:00
from common.drf.api import JMSModelViewSet
from common.drf.serializers import GroupedChoiceSerailizer
2022-04-02 10:35:46 +00:00
from assets.models import Platform
2022-08-30 06:13:33 +00:00
from assets.serializers import PlatformSerializer, PlatformOpsMethodSerializer
2022-04-28 14:54:18 +00:00
from assets.const import AllTypes, Category
2022-08-30 03:56:56 +00:00
from assets.playbooks.platform import filter_platform_methods
2022-04-02 10:35:46 +00:00
__all__ = ['AssetPlatformViewSet']
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,
'categories': GroupedChoiceSerailizer
}
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'
2022-04-28 14:54:18 +00:00
}
@action(methods=['GET'], detail=False)
def categories(self, request, *args, **kwargs):
data = AllTypes.grouped_choices_to_objs()
serializer = self.get_serializer(data, many=True)
return Response(serializer.data)
2022-04-02 10:35:46 +00:00
2022-08-10 11:27:08 +00:00
@action(methods=['GET'], detail=False, url_path='type-constraints')
def type_constraints(self, request, *args, **kwargs):
2022-05-05 08:18:05 +00:00
category = request.query_params.get('category')
tp = request.query_params.get('type')
2022-08-10 11:27:08 +00:00
limits = AllTypes.get_constraints(category, tp)
2022-05-05 08:18:05 +00:00
return Response(limits)
2022-08-29 07:50:25 +00:00
@action(methods=['GET'], detail=False, url_path='ops-methods')
def ops_methods(self, request, *args, **kwargs):
category = request.query_params.get('category')
tp = request.query_params.get('type')
2022-08-30 03:56:56 +00:00
method = request.query_params.get('method')
methods = filter_platform_methods(category, tp, method)
2022-08-30 06:13:33 +00:00
serializer = PlatformOpsMethodSerializer(methods, many=True)
return Response(serializer.data)
2022-08-29 07:50:25 +00:00
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)