2016-08-09 09:27:37 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
2017-06-19 09:59:13 +00:00
|
|
|
# Copyright (C) 2014-2017 Beijing DuiZhan Technology Co.,Ltd. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the GNU General Public License v2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2017-09-26 23:20:16 +00:00
|
|
|
from rest_framework import generics
|
2016-11-01 11:31:35 +00:00
|
|
|
from rest_framework.response import Response
|
2017-09-26 23:20:16 +00:00
|
|
|
from rest_framework_bulk import BulkModelViewSet
|
|
|
|
from rest_framework_bulk import ListBulkCreateUpdateDestroyAPIView
|
2016-11-06 13:29:04 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2017-12-19 04:41:00 +00:00
|
|
|
from django.db.models import Q
|
2016-11-01 09:21:16 +00:00
|
|
|
|
2016-11-25 03:00:51 +00:00
|
|
|
from common.mixins import IDInFilterMixin
|
2017-12-18 10:38:30 +00:00
|
|
|
from .hands import IsSuperUser, IsValidUser, IsSuperUserOrAppUser, \
|
2017-12-19 04:41:00 +00:00
|
|
|
get_user_granted_assets
|
2017-12-07 08:25:50 +00:00
|
|
|
from .models import AssetGroup, Asset, Cluster, SystemUser, AdminUser
|
2016-11-06 13:29:04 +00:00
|
|
|
from . import serializers
|
2017-12-19 04:41:00 +00:00
|
|
|
from .tasks import update_assets_hardware_info, test_admin_user_connectability, \
|
|
|
|
test_admin_user_connectability_manual, push_system_user_to_cluster_assets, \
|
|
|
|
test_system_user_connectability
|
2016-09-04 09:43:03 +00:00
|
|
|
|
|
|
|
|
2017-01-06 12:34:24 +00:00
|
|
|
class AssetViewSet(IDInFilterMixin, BulkModelViewSet):
|
2017-12-10 16:29:25 +00:00
|
|
|
"""
|
|
|
|
API endpoint that allows Asset to be viewed or edited.
|
|
|
|
"""
|
2016-09-03 11:05:50 +00:00
|
|
|
queryset = Asset.objects.all()
|
2016-11-06 13:29:04 +00:00
|
|
|
serializer_class = serializers.AssetSerializer
|
2017-03-29 07:26:32 +00:00
|
|
|
permission_classes = (IsValidUser,)
|
2016-09-03 11:05:50 +00:00
|
|
|
|
2016-11-06 14:45:26 +00:00
|
|
|
def get_queryset(self):
|
2017-12-16 09:16:40 +00:00
|
|
|
if self.request.user.is_superuser or self.request.user.is_app:
|
2017-12-11 09:08:43 +00:00
|
|
|
queryset = super().get_queryset()
|
2017-03-29 07:26:32 +00:00
|
|
|
else:
|
2017-12-16 09:16:40 +00:00
|
|
|
assets_granted = get_user_granted_assets(self.request.user)
|
|
|
|
queryset = self.queryset.filter(id__in=[asset.id for asset in assets_granted])
|
2017-12-11 09:08:43 +00:00
|
|
|
cluster_id = self.request.query_params.get('cluster_id')
|
|
|
|
asset_group_id = self.request.query_params.get('asset_group_id')
|
|
|
|
admin_user_id = self.request.query_params.get('admin_user_id')
|
2017-12-07 08:25:50 +00:00
|
|
|
if cluster_id:
|
|
|
|
queryset = queryset.filter(cluster__id=cluster_id)
|
2016-11-17 11:28:45 +00:00
|
|
|
if asset_group_id:
|
|
|
|
queryset = queryset.filter(groups__id=asset_group_id)
|
2017-12-11 09:08:43 +00:00
|
|
|
if admin_user_id:
|
|
|
|
admin_user = get_object_or_404(AdminUser, id=admin_user_id)
|
2017-12-19 04:41:00 +00:00
|
|
|
assets_direct = [asset.id for asset in admin_user.asset_set.all()]
|
2017-12-11 09:08:43 +00:00
|
|
|
clusters = [cluster.id for cluster in admin_user.cluster_set.all()]
|
2017-12-19 04:41:00 +00:00
|
|
|
queryset = queryset.filter(Q(cluster__id__in=clusters)|Q(id__in=assets_direct))
|
2016-11-06 14:45:26 +00:00
|
|
|
return queryset
|
|
|
|
|
2016-09-04 09:43:03 +00:00
|
|
|
|
2017-01-06 12:34:24 +00:00
|
|
|
class AssetGroupViewSet(IDInFilterMixin, BulkModelViewSet):
|
2017-12-18 10:38:30 +00:00
|
|
|
"""
|
|
|
|
Asset group api set, for add,delete,update,list,retrieve resource
|
|
|
|
"""
|
2016-11-09 16:18:57 +00:00
|
|
|
queryset = AssetGroup.objects.all()
|
|
|
|
serializer_class = serializers.AssetGroupSerializer
|
2017-01-07 14:34:12 +00:00
|
|
|
permission_classes = (IsSuperUser,)
|
2016-11-09 16:18:57 +00:00
|
|
|
|
2016-12-19 15:10:16 +00:00
|
|
|
|
2017-12-18 14:48:19 +00:00
|
|
|
class GroupUpdateAssetsApi(generics.RetrieveUpdateAPIView):
|
2017-12-18 10:38:30 +00:00
|
|
|
"""
|
|
|
|
Asset group, update it's asset member
|
|
|
|
"""
|
2017-01-07 14:34:12 +00:00
|
|
|
queryset = AssetGroup.objects.all()
|
2017-12-18 14:48:19 +00:00
|
|
|
serializer_class = serializers.GroupUpdateAssetsSerializer
|
2017-01-07 14:34:12 +00:00
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
|
2017-01-06 12:34:24 +00:00
|
|
|
|
2017-12-18 14:48:19 +00:00
|
|
|
class GroupAddAssetsApi(generics.UpdateAPIView):
|
2017-01-07 14:34:12 +00:00
|
|
|
queryset = AssetGroup.objects.all()
|
2017-12-18 14:48:19 +00:00
|
|
|
serializer_class = serializers.GroupUpdateAssetsSerializer
|
2017-01-07 14:34:12 +00:00
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
|
2017-12-18 14:48:19 +00:00
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
group = self.get_object()
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
|
|
assets = serializer.validated_data['assets']
|
|
|
|
group.assets.add(*tuple(assets))
|
|
|
|
return Response({"msg": "ok"})
|
|
|
|
else:
|
|
|
|
return Response({'error': serializer.errors}, status=400)
|
|
|
|
|
2017-01-06 12:34:24 +00:00
|
|
|
|
2017-12-07 08:25:50 +00:00
|
|
|
class ClusterUpdateAssetsApi(generics.RetrieveUpdateAPIView):
|
2017-12-18 10:38:30 +00:00
|
|
|
"""
|
|
|
|
Cluster update asset member
|
|
|
|
"""
|
2017-12-07 08:25:50 +00:00
|
|
|
queryset = Cluster.objects.all()
|
|
|
|
serializer_class = serializers.ClusterUpdateAssetsSerializer
|
2017-01-07 14:34:12 +00:00
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
|
2017-01-06 12:34:24 +00:00
|
|
|
|
2017-12-07 08:25:50 +00:00
|
|
|
class ClusterViewSet(IDInFilterMixin, BulkModelViewSet):
|
2017-12-18 10:38:30 +00:00
|
|
|
"""
|
|
|
|
Cluster api set, for add,delete,update,list,retrieve resource
|
|
|
|
"""
|
2017-12-07 08:25:50 +00:00
|
|
|
queryset = Cluster.objects.all()
|
|
|
|
serializer_class = serializers.ClusterSerializer
|
2016-11-01 11:31:35 +00:00
|
|
|
permission_classes = (IsSuperUser,)
|
2016-09-22 10:31:04 +00:00
|
|
|
|
2016-12-19 15:10:16 +00:00
|
|
|
|
2017-12-19 04:41:00 +00:00
|
|
|
# TOdo
|
|
|
|
class ClusterTestAssetsAliveApi(generics.RetrieveAPIView):
|
|
|
|
"""
|
|
|
|
Test cluster asset can connect using admin user or not
|
|
|
|
"""
|
|
|
|
queryset = Cluster.objects.all()
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
cluster = self.get_object()
|
|
|
|
|
|
|
|
|
2017-12-18 14:48:19 +00:00
|
|
|
class ClusterAddAssetsApi(generics.UpdateAPIView):
|
|
|
|
queryset = Cluster.objects.all()
|
|
|
|
serializer_class = serializers.ClusterUpdateAssetsSerializer
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
|
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
cluster = self.get_object()
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
|
|
assets = serializer.validated_data['assets']
|
|
|
|
for asset in assets:
|
|
|
|
asset.cluster = cluster
|
|
|
|
asset.save()
|
|
|
|
return Response({"msg": "ok"})
|
|
|
|
else:
|
|
|
|
return Response({'error': serializer.errors}, status=400)
|
|
|
|
|
|
|
|
|
2017-01-06 12:34:24 +00:00
|
|
|
class AdminUserViewSet(IDInFilterMixin, BulkModelViewSet):
|
2017-12-18 10:38:30 +00:00
|
|
|
"""
|
|
|
|
Admin user api set, for add,delete,update,list,retrieve resource
|
|
|
|
"""
|
2016-11-06 13:29:04 +00:00
|
|
|
queryset = AdminUser.objects.all()
|
|
|
|
serializer_class = serializers.AdminUserSerializer
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
|
2016-12-19 15:10:16 +00:00
|
|
|
|
2017-12-18 14:48:19 +00:00
|
|
|
class AdminUserAddClustersApi(generics.UpdateAPIView):
|
|
|
|
queryset = AdminUser.objects.all()
|
|
|
|
serializer_class = serializers.AdminUserUpdateClusterSerializer
|
2016-12-15 11:55:15 +00:00
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
|
2017-12-18 14:48:19 +00:00
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
admin_user = self.get_object()
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
|
|
clusters = serializer.validated_data['clusters']
|
|
|
|
for cluster in clusters:
|
|
|
|
cluster.admin_user = admin_user
|
|
|
|
cluster.save()
|
|
|
|
return Response({"msg": "ok"})
|
|
|
|
else:
|
|
|
|
return Response({'error': serializer.errors}, status=400)
|
2017-01-07 14:34:12 +00:00
|
|
|
|
2016-11-06 13:29:04 +00:00
|
|
|
|
2017-12-19 04:41:00 +00:00
|
|
|
class SystemUserViewSet(BulkModelViewSet):
|
2017-12-18 10:38:30 +00:00
|
|
|
"""
|
2017-12-18 14:48:19 +00:00
|
|
|
System user api set, for add,delete,update,list,retrieve resource
|
2017-12-18 10:38:30 +00:00
|
|
|
"""
|
2017-01-07 14:34:12 +00:00
|
|
|
queryset = SystemUser.objects.all()
|
2017-12-18 14:48:19 +00:00
|
|
|
serializer_class = serializers.SystemUserSerializer
|
|
|
|
permission_classes = (IsSuperUserOrAppUser,)
|
2016-12-15 11:55:15 +00:00
|
|
|
|
2017-12-19 04:41:00 +00:00
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
print(request.data)
|
|
|
|
return super().update(request, *args, **kwargs)
|
|
|
|
|
2016-11-06 13:29:04 +00:00
|
|
|
|
2016-11-25 03:00:51 +00:00
|
|
|
class AssetListUpdateApi(IDInFilterMixin, ListBulkCreateUpdateDestroyAPIView):
|
2017-12-18 10:38:30 +00:00
|
|
|
"""
|
|
|
|
Asset bulk update api
|
|
|
|
"""
|
2016-10-21 13:14:49 +00:00
|
|
|
queryset = Asset.objects.all()
|
2016-11-09 16:18:57 +00:00
|
|
|
serializer_class = serializers.AssetSerializer
|
2016-11-01 11:31:35 +00:00
|
|
|
permission_classes = (IsSuperUser,)
|
2016-11-01 09:21:16 +00:00
|
|
|
|
|
|
|
|
2016-12-29 11:17:00 +00:00
|
|
|
class SystemUserAuthInfoApi(generics.RetrieveAPIView):
|
2017-12-18 10:38:30 +00:00
|
|
|
"""
|
|
|
|
Get system user auth info
|
|
|
|
"""
|
2016-12-29 11:17:00 +00:00
|
|
|
queryset = SystemUser.objects.all()
|
2017-12-13 09:21:08 +00:00
|
|
|
permission_classes = (IsSuperUserOrAppUser,)
|
2016-11-01 11:31:35 +00:00
|
|
|
|
2016-12-29 11:17:00 +00:00
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
system_user = self.get_object()
|
|
|
|
data = {
|
|
|
|
'id': system_user.id,
|
|
|
|
'name': system_user.name,
|
|
|
|
'username': system_user.username,
|
|
|
|
'password': system_user.password,
|
|
|
|
'private_key': system_user.private_key,
|
|
|
|
}
|
|
|
|
return Response(data)
|
2016-10-28 13:19:37 +00:00
|
|
|
|
2017-04-04 11:16:34 +00:00
|
|
|
|
2017-12-19 04:41:00 +00:00
|
|
|
class AssetRefreshHardwareApi(generics.RetrieveAPIView):
|
2017-12-18 10:38:30 +00:00
|
|
|
"""
|
|
|
|
Refresh asset hardware info
|
|
|
|
"""
|
2017-04-04 11:16:34 +00:00
|
|
|
queryset = Asset.objects.all()
|
|
|
|
serializer_class = serializers.AssetSerializer
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
asset_id = kwargs.get('pk')
|
|
|
|
asset = get_object_or_404(Asset, pk=asset_id)
|
2017-04-05 11:09:51 +00:00
|
|
|
summary = update_assets_hardware_info([asset])
|
2017-12-12 04:19:45 +00:00
|
|
|
if summary.get('dark'):
|
|
|
|
return Response(summary['dark'].values(), status=501)
|
2017-04-05 11:09:51 +00:00
|
|
|
else:
|
2017-12-12 04:19:45 +00:00
|
|
|
return Response({"msg": "ok"})
|
2017-04-05 11:09:51 +00:00
|
|
|
|
|
|
|
|
2017-12-19 04:41:00 +00:00
|
|
|
class AssetAdminUserTestApi(generics.RetrieveAPIView):
|
2017-12-18 10:38:30 +00:00
|
|
|
"""
|
|
|
|
Test asset admin user connectivity
|
|
|
|
"""
|
2017-04-05 11:09:51 +00:00
|
|
|
queryset = Asset.objects.all()
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
asset_id = kwargs.get('pk')
|
|
|
|
asset = get_object_or_404(Asset, pk=asset_id)
|
2017-12-19 04:41:00 +00:00
|
|
|
ok, msg = test_admin_user_connectability_manual(asset)
|
|
|
|
if ok:
|
|
|
|
return Response({"msg": "pong"})
|
2017-04-05 11:09:51 +00:00
|
|
|
else:
|
2017-12-19 04:41:00 +00:00
|
|
|
return Response({"error": msg}, status=502)
|
|
|
|
|
|
|
|
|
|
|
|
class AdminUserTestConnectiveApi(generics.RetrieveAPIView):
|
|
|
|
"""
|
|
|
|
Test asset admin user connectivity
|
|
|
|
"""
|
|
|
|
queryset = AdminUser.objects.all()
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
admin_user = self.get_object()
|
|
|
|
test_admin_user_connectability.delay(admin_user, force=True)
|
|
|
|
return Response({"msg": "Task created"})
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserPushApi(generics.RetrieveAPIView):
|
|
|
|
"""
|
|
|
|
Push system user to cluster assets api
|
|
|
|
"""
|
|
|
|
queryset = SystemUser.objects.all()
|
|
|
|
permission_classes = (IsSuperUser,)
|
2017-04-12 10:06:32 +00:00
|
|
|
|
2017-12-19 04:41:00 +00:00
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
system_user = self.get_object()
|
|
|
|
push_system_user_to_cluster_assets.delay(system_user, force=True)
|
|
|
|
return Response({"msg": "Task created"})
|
|
|
|
|
|
|
|
|
|
|
|
class SystemUserTestConnectiveApi(generics.RetrieveAPIView):
|
|
|
|
"""
|
|
|
|
Push system user to cluster assets api
|
|
|
|
"""
|
|
|
|
queryset = SystemUser.objects.all()
|
|
|
|
permission_classes = (IsSuperUser,)
|
|
|
|
|
|
|
|
def retrieve(self, request, *args, **kwargs):
|
|
|
|
system_user = self.get_object()
|
|
|
|
test_system_user_connectability.delay(system_user, force=True)
|
|
|
|
return Response({"msg": "Task created"})
|