jumpserver/apps/assets/api/domain.py

55 lines
1.7 KiB
Python
Raw Normal View History

2018-03-23 11:46:46 +00:00
# ~*~ coding: utf-8 ~*~
from rest_framework_bulk import BulkModelViewSet
2018-03-25 13:47:29 +00:00
from rest_framework.views import APIView, Response
2018-03-23 11:46:46 +00:00
from django.views.generic.detail import SingleObjectMixin
from common.utils import get_logger
from common.permissions import IsOrgAdmin, IsAppUser, IsOrgAdminOrAppUser
2018-03-23 11:46:46 +00:00
from ..models import Domain, Gateway
from ..utils import test_gateway_connectability
from .. import serializers
logger = get_logger(__file__)
__all__ = ['DomainViewSet', 'GatewayViewSet', "GatewayTestConnectionApi"]
class DomainViewSet(BulkModelViewSet):
2018-07-14 16:55:05 +00:00
queryset = Domain.objects.all()
2018-07-23 04:55:13 +00:00
permission_classes = (IsOrgAdmin,)
2018-03-23 11:46:46 +00:00
serializer_class = serializers.DomainSerializer
2018-03-25 13:47:29 +00:00
def get_serializer_class(self):
if self.request.query_params.get('gateway'):
return serializers.DomainWithGatewaySerializer
return super().get_serializer_class()
def get_permissions(self):
if self.request.query_params.get('gateway'):
2018-07-23 04:55:13 +00:00
self.permission_classes = (IsOrgAdminOrAppUser,)
2018-03-25 13:47:29 +00:00
return super().get_permissions()
2018-03-23 11:46:46 +00:00
class GatewayViewSet(BulkModelViewSet):
filter_fields = ("domain",)
search_fields = filter_fields
2018-07-14 16:55:05 +00:00
queryset = Gateway.objects.all()
2018-07-23 04:55:13 +00:00
permission_classes = (IsOrgAdmin,)
2018-03-23 11:46:46 +00:00
serializer_class = serializers.GatewaySerializer
class GatewayTestConnectionApi(SingleObjectMixin, APIView):
2018-07-23 04:55:13 +00:00
permission_classes = (IsOrgAdmin,)
2018-03-23 11:46:46 +00:00
model = Gateway
object = None
def get(self, request, *args, **kwargs):
self.object = self.get_object(Gateway.objects.all())
ok, e = test_gateway_connectability(self.object)
if ok:
return Response("ok")
else:
return Response({"failed": e}, status=404)