2018-02-06 10:32:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2019-04-15 06:33:16 +00:00
|
|
|
import uuid
|
2018-07-10 08:03:05 +00:00
|
|
|
import random
|
|
|
|
|
2018-09-03 11:41:44 +00:00
|
|
|
from rest_framework import generics
|
2019-04-15 06:33:16 +00:00
|
|
|
from rest_framework.views import APIView
|
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
|
2019-04-15 06:33:16 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-02-06 10:32:02 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2019-04-15 06:33:16 +00:00
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.core.cache import cache
|
2018-02-06 10:32:02 +00:00
|
|
|
from django.db.models import Q
|
|
|
|
|
2019-06-13 10:58:43 +00:00
|
|
|
from common.mixins import IDInCacheFilterMixin, ApiMessageMixin
|
2019-05-21 08:24:01 +00:00
|
|
|
|
|
|
|
from common.utils import get_logger, get_object_or_none
|
2018-09-03 11:41:44 +00:00
|
|
|
from common.permissions import IsOrgAdmin, IsOrgAdminOrAppUser
|
2019-04-15 06:33:16 +00:00
|
|
|
from ..const import CACHE_KEY_ASSET_BULK_UPDATE_ID_PREFIX
|
2018-09-03 11:41:44 +00:00
|
|
|
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',
|
2019-04-15 06:33:16 +00:00
|
|
|
'AssetGatewayApi', 'AssetBulkUpdateSelectAPI'
|
2018-02-06 10:32:02 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-06-13 10:58:43 +00:00
|
|
|
class AssetViewSet(IDInCacheFilterMixin, LabelFilter, ApiMessageMixin, BulkModelViewSet):
|
2018-02-06 10:32:02 +00:00
|
|
|
"""
|
|
|
|
API endpoint that allows Asset to be viewed or edited.
|
|
|
|
"""
|
2019-06-19 11:25:21 +00:00
|
|
|
filter_fields = ("hostname", "ip", "systemuser__id", "admin_user__id")
|
|
|
|
search_fields = ("hostname", "ip")
|
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,)
|
2019-06-13 10:58:43 +00:00
|
|
|
success_message = _("%(hostname)s was %(action)s successfully")
|
2018-02-06 10:32:02 +00:00
|
|
|
|
2019-05-21 08:24:01 +00:00
|
|
|
def set_assets_node(self, assets):
|
|
|
|
if not isinstance(assets, list):
|
|
|
|
assets = [assets]
|
|
|
|
node_id = self.request.query_params.get('node_id')
|
2019-06-12 09:49:30 +00:00
|
|
|
if not node_id:
|
|
|
|
return
|
|
|
|
node = get_object_or_none(Node, pk=node_id)
|
|
|
|
if not node:
|
|
|
|
return
|
2019-05-21 08:24:01 +00:00
|
|
|
node.assets.add(*assets)
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
assets = serializer.save()
|
|
|
|
self.set_assets_node(assets)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2019-05-21 08:24:01 +00:00
|
|
|
class AssetListUpdateApi(IDInCacheFilterMixin, ListBulkCreateUpdateDestroyAPIView):
|
2018-02-06 10:32:02 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
2019-04-15 06:33:16 +00:00
|
|
|
class AssetBulkUpdateSelectAPI(APIView):
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
assets_id = request.data.get('assets_id', '')
|
|
|
|
if assets_id:
|
|
|
|
spm = uuid.uuid4().hex
|
|
|
|
key = CACHE_KEY_ASSET_BULK_UPDATE_ID_PREFIX.format(spm)
|
|
|
|
cache.set(key, assets_id, 300)
|
|
|
|
url = reverse_lazy('assets:asset-bulk-update') + '?spm=%s' % spm
|
|
|
|
return Response({'url': url})
|
|
|
|
error = _('Please select assets that need to be updated')
|
|
|
|
return Response({'error': error}, status=400)
|
|
|
|
|
|
|
|
|
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,)
|
2019-01-15 02:23:30 +00:00
|
|
|
serializer_class = serializers.TaskIDSerializer
|
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,)
|
2019-01-15 02:23:30 +00:00
|
|
|
serializer_class = serializers.GatewayWithAuthSerializer
|
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 \
|
2019-06-13 10:58:43 +00:00
|
|
|
asset.domain.gateways.filter(protocol='ssh').exists():
|
|
|
|
gateway = random.choice(asset.domain.gateways.filter(protocol='ssh'))
|
2018-07-10 08:03:05 +00:00
|
|
|
serializer = serializers.GatewayWithAuthSerializer(instance=gateway)
|
|
|
|
return Response(serializer.data)
|
|
|
|
else:
|
|
|
|
return Response({"msg": "Not have gateway"}, status=404)
|