2018-03-23 11:46:46 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
2020-10-14 11:39:55 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from rest_framework.views import APIView, Response
|
|
|
|
from rest_framework.serializers import ValidationError
|
2018-03-23 11:46:46 +00:00
|
|
|
|
|
|
|
from common.utils import get_logger
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
2018-03-23 11:46:46 +00:00
|
|
|
from ..models import Domain, Gateway
|
|
|
|
from .. import serializers
|
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
__all__ = ['DomainViewSet', 'GatewayViewSet', "GatewayTestConnectionApi"]
|
|
|
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
class DomainViewSet(OrgBulkModelViewSet):
|
2019-10-18 07:05:45 +00:00
|
|
|
model = Domain
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ("name", )
|
|
|
|
search_fields = filterset_fields
|
2018-03-23 11:46:46 +00:00
|
|
|
serializer_class = serializers.DomainSerializer
|
2021-10-22 06:27:54 +00:00
|
|
|
ordering_fields = ('name',)
|
|
|
|
ordering = ('name', )
|
2018-10-31 07:31:09 +00:00
|
|
|
|
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()
|
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
class GatewayViewSet(OrgBulkModelViewSet):
|
2019-10-18 07:05:45 +00:00
|
|
|
model = Gateway
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ("domain__name", "name", "username", "ip", "domain")
|
2019-12-04 09:07:57 +00:00
|
|
|
search_fields = ("domain__name", "name", "username", "ip")
|
2018-03-23 11:46:46 +00:00
|
|
|
serializer_class = serializers.GatewaySerializer
|
|
|
|
|
|
|
|
|
|
|
|
class GatewayTestConnectionApi(SingleObjectMixin, APIView):
|
2022-02-21 10:51:11 +00:00
|
|
|
queryset = Gateway.objects.all()
|
2018-03-23 11:46:46 +00:00
|
|
|
object = None
|
2022-02-17 12:13:31 +00:00
|
|
|
rbac_perms = {
|
2022-03-17 04:31:14 +00:00
|
|
|
'POST': 'assets.test_gateway'
|
2022-02-17 12:13:31 +00:00
|
|
|
}
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2019-03-19 11:09:09 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
2018-03-23 11:46:46 +00:00
|
|
|
self.object = self.get_object(Gateway.objects.all())
|
2019-03-19 11:09:09 +00:00
|
|
|
local_port = self.request.data.get('port') or self.object.port
|
2020-10-14 11:39:55 +00:00
|
|
|
try:
|
|
|
|
local_port = int(local_port)
|
|
|
|
except ValueError:
|
|
|
|
raise ValidationError({'port': _('Number required')})
|
2019-03-19 11:09:09 +00:00
|
|
|
ok, e = self.object.test_connective(local_port=local_port)
|
2018-03-23 11:46:46 +00:00
|
|
|
if ok:
|
|
|
|
return Response("ok")
|
|
|
|
else:
|
2019-11-13 08:40:34 +00:00
|
|
|
return Response({"error": e}, status=400)
|