2018-02-06 10:32:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2018-07-10 08:03:05 +00:00
|
|
|
import random
|
|
|
|
|
2018-09-03 11:41:44 +00:00
|
|
|
from rest_framework import generics
|
2018-02-06 10:32:02 +00:00
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework_bulk import BulkModelViewSet
|
|
|
|
from rest_framework_bulk import ListBulkCreateUpdateDestroyAPIView
|
|
|
|
from rest_framework.pagination import LimitOffsetPagination
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from django.db.models import Q
|
|
|
|
|
|
|
|
from common.mixins import IDInFilterMixin
|
|
|
|
from common.utils import get_logger
|
2018-09-03 11:41:44 +00:00
|
|
|
from common.permissions import IsOrgAdmin, IsOrgAdminOrAppUser
|
|
|
|
from ..models import Asset, AdminUser, Node
|
2018-02-06 10:32:02 +00:00
|
|
|
from .. import serializers
|
|
|
|
from ..tasks import update_asset_hardware_info_manual, \
|
2018-12-18 09:28:45 +00:00
|
|
|
test_asset_connectivity_manual
|
2018-02-06 10:32:02 +00:00
|
|
|
from ..utils import LabelFilter
|
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
__all__ = [
|
2018-05-31 11:47:57 +00:00
|
|
|
'AssetViewSet', 'AssetListUpdateApi',
|
2018-07-10 08:03:05 +00:00
|
|
|
'AssetRefreshHardwareApi', 'AssetAdminUserTestApi',
|
|
|
|
'AssetGatewayApi'
|
2018-02-06 10:32:02 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class AssetViewSet(IDInFilterMixin, LabelFilter, BulkModelViewSet):
|
|
|
|
"""
|
|
|
|
API endpoint that allows Asset to be viewed or edited.
|
|
|
|
"""
|
|
|
|
filter_fields = ("hostname", "ip")
|
|
|
|
search_fields = filter_fields
|
2018-02-09 03:12:40 +00:00
|
|
|
ordering_fields = ("hostname", "ip", "port", "cpu_cores")
|
2018-07-14 16:55:05 +00:00
|
|
|
queryset = Asset.objects.all()
|
2018-02-06 10:32:02 +00:00
|
|
|
serializer_class = serializers.AssetSerializer
|
|
|
|
pagination_class = LimitOffsetPagination
|
2018-09-03 11:41:44 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2018-02-06 10:32:02 +00:00
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
def filter_node(self, queryset):
|
2018-02-06 10:32:02 +00:00
|
|
|
node_id = self.request.query_params.get("node_id")
|
2018-07-25 07:05:28 +00:00
|
|
|
if not node_id:
|
2018-12-17 10:20:44 +00:00
|
|
|
return queryset
|
2018-07-25 07:05:28 +00:00
|
|
|
|
|
|
|
node = get_object_or_404(Node, id=node_id)
|
2018-08-16 04:44:39 +00:00
|
|
|
show_current_asset = self.request.query_params.get("show_current_asset") in ('1', 'true')
|
2018-02-06 10:32:02 +00:00
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
if node.is_root() and show_current_asset:
|
|
|
|
queryset = queryset.filter(
|
|
|
|
Q(nodes=node_id) | Q(nodes__isnull=True)
|
|
|
|
)
|
|
|
|
elif node.is_root() and not show_current_asset:
|
|
|
|
pass
|
|
|
|
elif not node.is_root() and show_current_asset:
|
|
|
|
queryset = queryset.filter(nodes=node)
|
2018-07-25 07:05:28 +00:00
|
|
|
else:
|
2018-12-17 10:20:44 +00:00
|
|
|
queryset = queryset.filter(
|
2018-07-25 07:05:28 +00:00
|
|
|
nodes__key__regex='^{}(:[0-9]+)*$'.format(node.key),
|
2018-10-24 05:05:32 +00:00
|
|
|
)
|
2018-12-17 10:20:44 +00:00
|
|
|
return queryset
|
2018-07-25 07:05:28 +00:00
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
def filter_admin_user_id(self, queryset):
|
2018-07-25 07:05:28 +00:00
|
|
|
admin_user_id = self.request.query_params.get('admin_user_id')
|
2018-12-17 10:20:44 +00:00
|
|
|
if not admin_user_id:
|
|
|
|
return queryset
|
|
|
|
admin_user = get_object_or_404(AdminUser, id=admin_user_id)
|
|
|
|
queryset = queryset.filter(admin_user=admin_user)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
queryset = super().filter_queryset(queryset)
|
|
|
|
queryset = self.filter_node(queryset)
|
|
|
|
queryset = self.filter_admin_user_id(queryset)
|
|
|
|
return queryset
|
2018-05-28 07:00:06 +00:00
|
|
|
|
2018-07-25 07:05:28 +00:00
|
|
|
def get_queryset(self):
|
2018-12-17 10:20:44 +00:00
|
|
|
queryset = super().get_queryset().distinct()
|
|
|
|
queryset = self.get_serializer_class().setup_eager_loading(queryset)
|
|
|
|
return queryset
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetListUpdateApi(IDInFilterMixin, ListBulkCreateUpdateDestroyAPIView):
|
|
|
|
"""
|
|
|
|
Asset bulk update api
|
|
|
|
"""
|
2018-07-14 16:55:05 +00:00
|
|
|
queryset = Asset.objects.all()
|
2018-02-06 10:32:02 +00:00
|
|
|
serializer_class = serializers.AssetSerializer
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetRefreshHardwareApi(generics.RetrieveAPIView):
|
|
|
|
"""
|
|
|
|
Refresh asset hardware info
|
|
|
|
"""
|
2018-07-14 16:55:05 +00:00
|
|
|
queryset = Asset.objects.all()
|
2018-02-06 10:32:02 +00:00
|
|
|
serializer_class = serializers.AssetSerializer
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
asset_id = kwargs.get('pk')
|
|
|
|
asset = get_object_or_404(Asset, pk=asset_id)
|
2018-04-02 08:55:39 +00:00
|
|
|
task = update_asset_hardware_info_manual.delay(asset)
|
|
|
|
return Response({"task": task.id})
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetAdminUserTestApi(generics.RetrieveAPIView):
|
|
|
|
"""
|
2018-12-18 09:28:45 +00:00
|
|
|
Test asset admin user assets_connectivity
|
2018-02-06 10:32:02 +00:00
|
|
|
"""
|
2018-07-14 16:55:05 +00:00
|
|
|
queryset = Asset.objects.all()
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
asset_id = kwargs.get('pk')
|
|
|
|
asset = get_object_or_404(Asset, pk=asset_id)
|
2018-12-18 09:28:45 +00:00
|
|
|
task = test_asset_connectivity_manual.delay(asset)
|
2018-04-02 08:55:39 +00:00
|
|
|
return Response({"task": task.id})
|
2018-07-10 08:03:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetGatewayApi(generics.RetrieveAPIView):
|
2018-07-14 16:55:05 +00:00
|
|
|
queryset = Asset.objects.all()
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2018-07-10 08:03:05 +00:00
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
asset_id = kwargs.get('pk')
|
|
|
|
asset = get_object_or_404(Asset, pk=asset_id)
|
|
|
|
|
|
|
|
if asset.domain and \
|
|
|
|
asset.domain.gateways.filter(protocol=asset.protocol).exists():
|
|
|
|
gateway = random.choice(asset.domain.gateways.filter(protocol=asset.protocol))
|
|
|
|
serializer = serializers.GatewayWithAuthSerializer(instance=gateway)
|
|
|
|
return Response(serializer.data)
|
|
|
|
else:
|
|
|
|
return Response({"msg": "Not have gateway"}, status=404)
|