2018-02-06 10:32:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2018-07-10 08:03:05 +00:00
|
|
|
import random
|
|
|
|
|
2018-02-06 10:32:02 +00:00
|
|
|
from rest_framework.response import Response
|
2019-12-16 08:53:29 +00:00
|
|
|
from rest_framework.viewsets import ModelViewSet
|
|
|
|
from rest_framework.generics import RetrieveAPIView
|
2018-02-06 10:32:02 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
|
2019-05-21 08:24:01 +00:00
|
|
|
from common.utils import get_logger, get_object_or_none
|
2019-12-16 08:53:29 +00:00
|
|
|
from common.permissions import IsOrgAdmin, IsOrgAdminOrAppUser, IsSuperUser
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
2019-10-18 07:05:45 +00:00
|
|
|
from orgs.mixins import generics
|
2019-12-16 08:53:29 +00:00
|
|
|
from ..models import Asset, Node, Platform
|
2018-02-06 10:32:02 +00:00
|
|
|
from .. import serializers
|
2019-12-16 08:53:29 +00:00
|
|
|
from ..tasks import (
|
|
|
|
update_asset_hardware_info_manual, test_asset_connectivity_manual
|
|
|
|
)
|
2019-09-18 14:06:46 +00:00
|
|
|
from ..filters import AssetByNodeFilterBackend, LabelFilterBackend
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
__all__ = [
|
2019-12-16 08:53:29 +00:00
|
|
|
'AssetViewSet', 'AssetPlatformRetrieveApi',
|
2018-07-10 08:03:05 +00:00
|
|
|
'AssetRefreshHardwareApi', 'AssetAdminUserTestApi',
|
2019-12-16 08:53:29 +00:00
|
|
|
'AssetGatewayApi', 'AssetPlatformViewSet',
|
2018-02-06 10:32:02 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-09-18 14:06:46 +00:00
|
|
|
class AssetViewSet(OrgBulkModelViewSet):
|
2018-02-06 10:32:02 +00:00
|
|
|
"""
|
|
|
|
API endpoint that allows Asset to be viewed or edited.
|
|
|
|
"""
|
2019-10-18 07:05:45 +00:00
|
|
|
model = Asset
|
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-02-06 10:32:02 +00:00
|
|
|
serializer_class = serializers.AssetSerializer
|
2018-09-03 11:41:44 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2019-09-18 14:06:46 +00:00
|
|
|
extra_filter_backends = [AssetByNodeFilterBackend, LabelFilterBackend]
|
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-02-06 10:32:02 +00:00
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
class AssetPlatformRetrieveApi(RetrieveAPIView):
|
|
|
|
queryset = Platform.objects.all()
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
serializer_class = serializers.PlatformSerializer
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
asset_pk = self.kwargs.get('pk')
|
|
|
|
asset = get_object_or_404(Asset, pk=asset_pk)
|
|
|
|
return asset.platform
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPlatformViewSet(ModelViewSet):
|
|
|
|
queryset = Platform.objects.all()
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
serializer_class = serializers.PlatformSerializer
|
|
|
|
filterset_fields = ['name', 'base']
|
|
|
|
search_fields = ['name']
|
|
|
|
|
|
|
|
def check_object_permissions(self, request, obj):
|
|
|
|
if request.method.lower() in ['delete', 'put', 'patch'] and \
|
|
|
|
obj.internal:
|
|
|
|
self.permission_denied(
|
|
|
|
request, message={"detail": "Internal platform"}
|
|
|
|
)
|
|
|
|
|
|
|
|
return super().check_object_permissions(request, obj)
|
|
|
|
|
|
|
|
|
2018-02-06 10:32:02 +00:00
|
|
|
class AssetRefreshHardwareApi(generics.RetrieveAPIView):
|
|
|
|
"""
|
|
|
|
Refresh asset hardware info
|
|
|
|
"""
|
2019-10-18 07:05:45 +00:00
|
|
|
model = Asset
|
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
|
|
|
"""
|
2019-10-18 07:05:45 +00:00
|
|
|
model = Asset
|
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-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2019-01-15 02:23:30 +00:00
|
|
|
serializer_class = serializers.GatewayWithAuthSerializer
|
2019-10-18 07:05:45 +00:00
|
|
|
model = Asset
|
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:
|
2019-10-18 07:05:45 +00:00
|
|
|
return Response({"msg": "Not have gateway"}, status=404)
|