2018-02-06 10:32:02 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-08-16 15:08:58 +00:00
|
|
|
from assets.api import FilterAssetByNodeMixin
|
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 (
|
2021-01-06 03:08:35 +00:00
|
|
|
update_assets_hardware_info_manual, test_assets_connectivity_manual
|
2019-12-16 08:53:29 +00:00
|
|
|
)
|
2020-08-16 15:08:58 +00:00
|
|
|
from ..filters import FilterAssetByNodeFilterBackend, LabelFilterBackend, IpInFilterBackend
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
__all__ = [
|
2019-12-16 08:53:29 +00:00
|
|
|
'AssetViewSet', 'AssetPlatformRetrieveApi',
|
2020-03-12 08:24:38 +00:00
|
|
|
'AssetGatewayListApi', 'AssetPlatformViewSet',
|
2021-01-06 03:08:35 +00:00
|
|
|
'AssetTaskCreateApi', 'AssetsTaskCreateApi',
|
2018-02-06 10:32:02 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
class AssetViewSet(FilterAssetByNodeMixin, 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
|
2021-01-07 11:05:55 +00:00
|
|
|
filterset_fields = {
|
|
|
|
'hostname': ['exact'],
|
|
|
|
'ip': ['exact'],
|
|
|
|
'systemuser__id': ['exact'],
|
|
|
|
'admin_user__id': ['exact'],
|
|
|
|
'platform__base': ['exact'],
|
|
|
|
'is_active': ['exact'],
|
|
|
|
'protocols': ['exact', 'icontains']
|
|
|
|
}
|
2019-06-19 11:25:21 +00:00
|
|
|
search_fields = ("hostname", "ip")
|
2018-02-09 03:12:40 +00:00
|
|
|
ordering_fields = ("hostname", "ip", "port", "cpu_cores")
|
2020-03-12 08:24:38 +00:00
|
|
|
serializer_classes = {
|
|
|
|
'default': serializers.AssetSerializer,
|
|
|
|
'display': serializers.AssetDisplaySerializer,
|
|
|
|
}
|
2018-09-03 11:41:44 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2020-08-16 15:08:58 +00:00
|
|
|
extra_filter_backends = [FilterAssetByNodeFilterBackend, LabelFilterBackend, IpInFilterBackend]
|
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
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ['name', 'base']
|
2019-12-16 08:53:29 +00:00
|
|
|
search_fields = ['name']
|
|
|
|
|
2020-06-10 04:58:18 +00:00
|
|
|
def get_permissions(self):
|
|
|
|
if self.request.method.lower() in ['get', 'options']:
|
|
|
|
self.permission_classes = (IsOrgAdmin,)
|
|
|
|
return super().get_permissions()
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
def check_object_permissions(self, request, obj):
|
2020-06-10 04:58:18 +00:00
|
|
|
if request.method.lower() in ['delete', 'put', 'patch'] and obj.internal:
|
2019-12-16 08:53:29 +00:00
|
|
|
self.permission_denied(
|
|
|
|
request, message={"detail": "Internal platform"}
|
|
|
|
)
|
|
|
|
return super().check_object_permissions(request, obj)
|
|
|
|
|
|
|
|
|
2021-01-06 03:08:35 +00:00
|
|
|
class AssetsTaskMixin:
|
|
|
|
def perform_assets_task(self, serializer):
|
|
|
|
data = serializer.validated_data
|
|
|
|
assets = data['assets']
|
|
|
|
action = data['action']
|
2020-03-12 08:24:38 +00:00
|
|
|
if action == "refresh":
|
2021-01-06 03:08:35 +00:00
|
|
|
task = update_assets_hardware_info_manual.delay(assets)
|
2020-03-12 08:24:38 +00:00
|
|
|
else:
|
2021-01-06 03:08:35 +00:00
|
|
|
task = test_assets_connectivity_manual.delay(assets)
|
2020-03-12 08:24:38 +00:00
|
|
|
data = getattr(serializer, '_data', {})
|
|
|
|
data["task"] = task.id
|
|
|
|
setattr(serializer, '_data', data)
|
2018-07-10 08:03:05 +00:00
|
|
|
|
2021-01-06 03:08:35 +00:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
self.perform_assets_task(serializer)
|
|
|
|
|
|
|
|
|
|
|
|
class AssetTaskCreateApi(AssetsTaskMixin, generics.CreateAPIView):
|
|
|
|
model = Asset
|
|
|
|
serializer_class = serializers.AssetTaskSerializer
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
pk = self.kwargs.get('pk')
|
|
|
|
request.data['assets'] = [pk]
|
|
|
|
return super().create(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class AssetsTaskCreateApi(AssetsTaskMixin, generics.CreateAPIView):
|
|
|
|
model = Asset
|
|
|
|
serializer_class = serializers.AssetTaskSerializer
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
|
2018-07-10 08:03:05 +00:00
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class AssetGatewayListApi(generics.ListAPIView):
|
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
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
def get_queryset(self):
|
|
|
|
asset_id = self.kwargs.get('pk')
|
2018-07-10 08:03:05 +00:00
|
|
|
asset = get_object_or_404(Asset, pk=asset_id)
|
2020-03-12 08:24:38 +00:00
|
|
|
if not asset.domain:
|
|
|
|
return []
|
|
|
|
queryset = asset.domain.gateways.filter(protocol='ssh')
|
|
|
|
return queryset
|