2018-03-23 11:46:46 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
2020-10-14 11:39:55 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2023-01-16 11:02:09 +00:00
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
2020-10-14 11:39:55 +00:00
|
|
|
from rest_framework.serializers import ValidationError
|
2023-01-16 11:02:09 +00:00
|
|
|
from rest_framework.views import APIView, Response
|
2023-02-20 05:31:56 +00:00
|
|
|
|
2023-02-13 11:52:49 +00:00
|
|
|
from assets.tasks import test_gateways_connectivity_manual
|
2023-02-20 05:31:56 +00:00
|
|
|
from common.utils import get_logger
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
2023-02-17 05:56:18 +00:00
|
|
|
from .asset import HostViewSet
|
2018-03-23 11:46:46 +00:00
|
|
|
from .. import serializers
|
2023-01-16 11:02:09 +00:00
|
|
|
from ..models import Domain, Gateway
|
2018-03-23 11:46:46 +00:00
|
|
|
|
|
|
|
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
|
2022-11-22 09:33:09 +00:00
|
|
|
filterset_fields = ("name",)
|
2021-01-07 02:53:10 +00:00
|
|
|
search_fields = filterset_fields
|
2022-11-22 09:33:09 +00:00
|
|
|
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
|
2023-02-20 05:40:42 +00:00
|
|
|
return serializers.DomainSerializer
|
2018-03-25 13:47:29 +00:00
|
|
|
|
2023-06-18 12:24:14 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
return super().get_queryset().prefetch_related('assets')
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2023-02-17 05:56:18 +00:00
|
|
|
class GatewayViewSet(HostViewSet):
|
2022-12-02 03:45:05 +00:00
|
|
|
perm_model = Gateway
|
2022-11-22 09:33:09 +00:00
|
|
|
filterset_fields = ("domain__name", "name", "domain")
|
|
|
|
search_fields = ("domain__name",)
|
2023-02-17 05:56:18 +00:00
|
|
|
|
|
|
|
def get_serializer_classes(self):
|
|
|
|
serializer_classes = super().get_serializer_classes()
|
|
|
|
serializer_classes['default'] = serializers.GatewaySerializer
|
|
|
|
return serializer_classes
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2022-11-22 09:33:09 +00:00
|
|
|
def get_queryset(self):
|
2022-12-01 07:21:53 +00:00
|
|
|
queryset = Domain.get_gateway_queryset()
|
2022-11-22 09:33:09 +00:00
|
|
|
return queryset
|
|
|
|
|
2018-03-23 11:46:46 +00:00
|
|
|
|
|
|
|
class GatewayTestConnectionApi(SingleObjectMixin, APIView):
|
2022-02-17 12:13:31 +00:00
|
|
|
rbac_perms = {
|
2023-01-16 11:02:09 +00:00
|
|
|
'POST': 'assets.test_assetconnectivity'
|
2022-02-17 12:13:31 +00:00
|
|
|
}
|
2018-03-23 11:46:46 +00:00
|
|
|
|
2022-11-22 09:33:09 +00:00
|
|
|
def get_queryset(self):
|
2022-12-01 07:21:53 +00:00
|
|
|
queryset = Domain.get_gateway_queryset()
|
2022-11-22 09:33:09 +00:00
|
|
|
return queryset
|
|
|
|
|
2019-03-19 11:09:09 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
2022-12-01 07:21:53 +00:00
|
|
|
gateway = self.get_object()
|
|
|
|
local_port = self.request.data.get('port') or gateway.port
|
2020-10-14 11:39:55 +00:00
|
|
|
try:
|
|
|
|
local_port = int(local_port)
|
|
|
|
except ValueError:
|
|
|
|
raise ValidationError({'port': _('Number required')})
|
2023-02-13 11:52:49 +00:00
|
|
|
task = test_gateways_connectivity_manual([gateway.id], local_port)
|
2023-01-16 11:02:09 +00:00
|
|
|
return Response({'task': task.id})
|