2018-02-06 10:32:02 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
# Copyright (C) 2014-2018 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.
|
|
|
|
|
2018-02-09 07:24:44 +00:00
|
|
|
from django.db import transaction
|
2018-12-18 09:28:45 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2018-02-06 10:32:02 +00:00
|
|
|
from rest_framework.response import Response
|
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
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
from common.utils import get_logger
|
2018-07-23 04:55:13 +00:00
|
|
|
from ..hands import IsOrgAdmin
|
2018-02-09 07:24:44 +00:00
|
|
|
from ..models import AdminUser, Asset
|
2018-02-06 10:32:02 +00:00
|
|
|
from .. import serializers
|
2018-12-18 09:28:45 +00:00
|
|
|
from ..tasks import test_admin_user_connectivity_manual
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
__all__ = [
|
2018-03-27 10:34:41 +00:00
|
|
|
'AdminUserViewSet', 'ReplaceNodesAdminUserApi',
|
|
|
|
'AdminUserTestConnectiveApi', 'AdminUserAuthApi',
|
2018-12-18 09:28:45 +00:00
|
|
|
'AdminUserAssetsListView',
|
2018-02-06 10:32:02 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
class AdminUserViewSet(OrgBulkModelViewSet):
|
2018-02-06 10:32:02 +00:00
|
|
|
"""
|
|
|
|
Admin user api set, for add,delete,update,list,retrieve resource
|
|
|
|
"""
|
2019-10-18 07:05:45 +00:00
|
|
|
model = AdminUser
|
2018-10-31 07:31:09 +00:00
|
|
|
filter_fields = ("name", "username")
|
|
|
|
search_fields = filter_fields
|
2018-02-06 10:32:02 +00:00
|
|
|
serializer_class = serializers.AdminUserSerializer
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
2018-03-27 10:34:41 +00:00
|
|
|
class AdminUserAuthApi(generics.UpdateAPIView):
|
2019-10-18 07:05:45 +00:00
|
|
|
model = AdminUser
|
2018-03-27 10:34:41 +00:00
|
|
|
serializer_class = serializers.AdminUserAuthSerializer
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-03-27 10:34:41 +00:00
|
|
|
|
|
|
|
|
2018-02-09 07:24:44 +00:00
|
|
|
class ReplaceNodesAdminUserApi(generics.UpdateAPIView):
|
2019-10-18 07:05:45 +00:00
|
|
|
model = AdminUser
|
2018-02-09 07:24:44 +00:00
|
|
|
serializer_class = serializers.ReplaceNodeAdminUserSerializer
|
2018-07-23 04:55:13 +00:00
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
admin_user = self.get_object()
|
|
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
if serializer.is_valid():
|
2018-02-09 07:24:44 +00:00
|
|
|
nodes = serializer.validated_data['nodes']
|
|
|
|
assets = []
|
|
|
|
for node in nodes:
|
|
|
|
assets.extend([asset.id for asset in node.get_all_assets()])
|
|
|
|
|
|
|
|
with transaction.atomic():
|
|
|
|
Asset.objects.filter(id__in=assets).update(admin_user=admin_user)
|
|
|
|
|
2018-02-06 10:32:02 +00:00
|
|
|
return Response({"msg": "ok"})
|
|
|
|
else:
|
|
|
|
return Response({'error': serializer.errors}, status=400)
|
|
|
|
|
|
|
|
|
|
|
|
class AdminUserTestConnectiveApi(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 = AdminUser
|
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):
|
|
|
|
admin_user = self.get_object()
|
2018-12-18 09:28:45 +00:00
|
|
|
task = test_admin_user_connectivity_manual.delay(admin_user)
|
2018-04-02 08:55:39 +00:00
|
|
|
return Response({"task": task.id})
|
2018-12-18 09:28:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AdminUserAssetsListView(generics.ListAPIView):
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
serializer_class = serializers.AssetSimpleSerializer
|
|
|
|
filter_fields = ("hostname", "ip")
|
|
|
|
http_method_names = ['get']
|
|
|
|
search_fields = filter_fields
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
pk = self.kwargs.get('pk')
|
|
|
|
return get_object_or_404(AdminUser, pk=pk)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
admin_user = self.get_object()
|
|
|
|
return admin_user.get_related_assets()
|