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-07-19 11:24:29 +00:00
|
|
|
|
from rest_framework import generics, mixins, viewsets
|
2018-05-11 03:27:48 +00:00
|
|
|
|
from rest_framework.serializers import ValidationError
|
2018-02-07 05:39:45 +00:00
|
|
|
|
from rest_framework.views import APIView
|
2018-02-06 10:32:02 +00:00
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-03-12 03:41:12 +00:00
|
|
|
|
from django.shortcuts import get_object_or_404
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
2018-03-01 04:40:41 +00:00
|
|
|
|
from common.utils import get_logger, get_object_or_none
|
2018-12-17 10:20:44 +00:00
|
|
|
|
from common.tree import TreeNodeSerializer
|
2018-07-23 04:55:13 +00:00
|
|
|
|
from ..hands import IsOrgAdmin
|
2018-12-17 10:20:44 +00:00
|
|
|
|
from ..models import Node
|
2018-12-18 09:28:45 +00:00
|
|
|
|
from ..tasks import update_assets_hardware_info_util, test_asset_connectivity_util
|
2018-02-06 10:32:02 +00:00
|
|
|
|
from .. import serializers
|
2019-06-27 13:43:10 +00:00
|
|
|
|
from ..utils import NodeUtil
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
2018-02-07 05:39:45 +00:00
|
|
|
|
__all__ = [
|
2018-07-16 06:47:06 +00:00
|
|
|
|
'NodeViewSet', 'NodeChildrenApi', 'NodeAssetsApi',
|
|
|
|
|
'NodeAddAssetsApi', 'NodeRemoveAssetsApi', 'NodeReplaceAssetsApi',
|
2018-03-12 03:41:12 +00:00
|
|
|
|
'NodeAddChildrenApi', 'RefreshNodeHardwareInfoApi',
|
2018-12-17 10:20:44 +00:00
|
|
|
|
'TestNodeConnectiveApi', 'NodeListAsTreeApi',
|
2018-12-26 05:54:42 +00:00
|
|
|
|
'NodeChildrenAsTreeApi', 'RefreshAssetsAmount',
|
2018-02-07 05:39:45 +00:00
|
|
|
|
]
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
2018-07-19 11:24:29 +00:00
|
|
|
|
class NodeViewSet(viewsets.ModelViewSet):
|
2019-05-16 02:36:47 +00:00
|
|
|
|
filter_fields = ('value', 'key', )
|
|
|
|
|
search_fields = filter_fields
|
2018-07-14 16:55:05 +00:00
|
|
|
|
queryset = Node.objects.all()
|
2018-07-23 04:55:13 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-05-29 03:50:09 +00:00
|
|
|
|
serializer_class = serializers.NodeSerializer
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
2019-01-22 04:44:01 +00:00
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
|
child_key = Node.root().get_next_child_key()
|
|
|
|
|
serializer.validated_data["key"] = child_key
|
|
|
|
|
serializer.save()
|
|
|
|
|
|
|
|
|
|
def update(self, request, *args, **kwargs):
|
|
|
|
|
node = self.get_object()
|
|
|
|
|
if node.is_root():
|
|
|
|
|
node_value = node.value
|
|
|
|
|
post_value = request.data.get('value')
|
|
|
|
|
if node_value != post_value:
|
|
|
|
|
return Response(
|
|
|
|
|
{"msg": _("You can't update the root node name")},
|
|
|
|
|
status=400
|
|
|
|
|
)
|
|
|
|
|
return super().update(request, *args, **kwargs)
|
|
|
|
|
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
|
class NodeListAsTreeApi(generics.ListAPIView):
|
|
|
|
|
"""
|
|
|
|
|
获取节点列表树
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
"id": "",
|
|
|
|
|
"name": "",
|
|
|
|
|
"pId": "",
|
|
|
|
|
"meta": ""
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
"""
|
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
|
serializer_class = TreeNodeSerializer
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2019-06-27 13:43:10 +00:00
|
|
|
|
queryset = Node.objects.all()
|
|
|
|
|
util = NodeUtil()
|
|
|
|
|
nodes = util.get_nodes_by_queryset(queryset)
|
|
|
|
|
queryset = [node.as_tree_node() for node in nodes]
|
2018-12-17 10:20:44 +00:00
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def refresh_nodes(queryset):
|
|
|
|
|
Node.expire_nodes_assets_amount()
|
|
|
|
|
Node.expire_nodes_full_value()
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NodeChildrenAsTreeApi(generics.ListAPIView):
|
|
|
|
|
"""
|
|
|
|
|
节点子节点作为树返回,
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
"id": "",
|
|
|
|
|
"name": "",
|
|
|
|
|
"pId": "",
|
|
|
|
|
"meta": ""
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
|
serializer_class = TreeNodeSerializer
|
|
|
|
|
node = None
|
|
|
|
|
is_root = False
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2019-06-28 14:07:22 +00:00
|
|
|
|
self.check_need_refresh_nodes()
|
2018-12-17 10:20:44 +00:00
|
|
|
|
node_key = self.request.query_params.get('key')
|
2019-06-27 13:43:10 +00:00
|
|
|
|
util = NodeUtil()
|
2019-06-28 14:07:22 +00:00
|
|
|
|
# 是否包含自己
|
|
|
|
|
with_self = False
|
2019-06-27 13:43:10 +00:00
|
|
|
|
if not node_key:
|
|
|
|
|
node_key = Node.root().key
|
2019-06-28 14:07:22 +00:00
|
|
|
|
with_self = True
|
2019-06-27 13:43:10 +00:00
|
|
|
|
self.node = util.get_node_by_key(node_key)
|
2019-06-28 14:07:22 +00:00
|
|
|
|
queryset = self.node.get_children(with_self=with_self)
|
2018-12-17 10:20:44 +00:00
|
|
|
|
queryset = [node.as_tree_node() for node in queryset]
|
2019-05-10 03:29:38 +00:00
|
|
|
|
queryset = sorted(queryset)
|
2018-12-17 10:20:44 +00:00
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
def filter_assets(self, queryset):
|
|
|
|
|
include_assets = self.request.query_params.get('assets', '0') == '1'
|
|
|
|
|
if not include_assets:
|
|
|
|
|
return queryset
|
2019-07-05 10:07:10 +00:00
|
|
|
|
assets = self.node.get_assets().only(
|
2019-07-01 12:01:47 +00:00
|
|
|
|
"id", "hostname", "ip", 'platform', "os", "org_id",
|
|
|
|
|
)
|
2018-12-17 10:20:44 +00:00
|
|
|
|
for asset in assets:
|
|
|
|
|
queryset.append(asset.as_tree_node(self.node))
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
|
queryset = self.filter_assets(queryset)
|
|
|
|
|
return queryset
|
|
|
|
|
|
2019-06-28 14:07:22 +00:00
|
|
|
|
def check_need_refresh_nodes(self):
|
2018-12-17 10:20:44 +00:00
|
|
|
|
if self.request.query_params.get('refresh', '0') == '1':
|
2019-06-28 14:07:22 +00:00
|
|
|
|
Node.refresh_nodes()
|
2018-08-10 08:11:40 +00:00
|
|
|
|
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
|
class NodeChildrenApi(mixins.ListModelMixin, generics.CreateAPIView):
|
2018-07-14 16:55:05 +00:00
|
|
|
|
queryset = Node.objects.all()
|
2018-07-23 04:55:13 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-02-06 10:32:02 +00:00
|
|
|
|
serializer_class = serializers.NodeSerializer
|
|
|
|
|
instance = None
|
|
|
|
|
|
2019-01-02 07:33:35 +00:00
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
return self.list(request, *args, **kwargs)
|
|
|
|
|
|
2018-02-06 10:32:02 +00:00
|
|
|
|
def post(self, request, *args, **kwargs):
|
2018-12-17 10:20:44 +00:00
|
|
|
|
instance = self.get_object()
|
2018-02-06 10:32:02 +00:00
|
|
|
|
if not request.data.get("value"):
|
2018-12-17 10:20:44 +00:00
|
|
|
|
request.data["value"] = instance.get_next_child_preset_name()
|
2018-02-06 10:32:02 +00:00
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
|
|
|
instance = self.get_object()
|
|
|
|
|
value = request.data.get("value")
|
2019-02-18 04:59:36 +00:00
|
|
|
|
_id = request.data.get('id') or None
|
2018-05-10 12:48:16 +00:00
|
|
|
|
values = [child.value for child in instance.get_children()]
|
|
|
|
|
if value in values:
|
2018-05-11 03:27:48 +00:00
|
|
|
|
raise ValidationError(
|
|
|
|
|
'The same level node name cannot be the same'
|
|
|
|
|
)
|
2019-02-18 04:59:36 +00:00
|
|
|
|
node = instance.create_child(value=value, _id=_id)
|
2018-12-17 10:20:44 +00:00
|
|
|
|
return Response(self.serializer_class(instance=node).data, status=201)
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
2018-04-10 12:29:06 +00:00
|
|
|
|
def get_object(self):
|
|
|
|
|
pk = self.kwargs.get('pk') or self.request.query_params.get('id')
|
|
|
|
|
if not pk:
|
2019-01-02 07:33:35 +00:00
|
|
|
|
node = Node.root()
|
2018-04-10 12:29:06 +00:00
|
|
|
|
else:
|
|
|
|
|
node = get_object_or_404(Node, pk=pk)
|
|
|
|
|
return node
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2018-04-26 12:48:32 +00:00
|
|
|
|
queryset = []
|
2018-04-10 12:29:06 +00:00
|
|
|
|
query_all = self.request.query_params.get("all")
|
|
|
|
|
node = self.get_object()
|
2018-08-06 09:16:52 +00:00
|
|
|
|
|
2018-05-16 08:12:27 +00:00
|
|
|
|
if node is None:
|
|
|
|
|
node = Node.root()
|
2018-08-06 09:16:52 +00:00
|
|
|
|
node.assets__count = node.get_all_assets().count()
|
2018-04-26 12:48:32 +00:00
|
|
|
|
queryset.append(node)
|
2018-08-06 09:16:52 +00:00
|
|
|
|
|
2018-04-10 12:29:06 +00:00
|
|
|
|
if query_all:
|
2018-08-10 06:46:04 +00:00
|
|
|
|
children = node.get_all_children()
|
2018-04-10 12:29:06 +00:00
|
|
|
|
else:
|
2018-08-10 06:46:04 +00:00
|
|
|
|
children = node.get_children()
|
2018-04-26 12:48:32 +00:00
|
|
|
|
queryset.extend(list(children))
|
2018-04-10 12:29:06 +00:00
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NodeAssetsApi(generics.ListAPIView):
|
2018-07-23 04:55:13 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-04-10 12:29:06 +00:00
|
|
|
|
serializer_class = serializers.AssetSerializer
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
node_id = self.kwargs.get('pk')
|
|
|
|
|
query_all = self.request.query_params.get('all')
|
|
|
|
|
instance = get_object_or_404(Node, pk=node_id)
|
|
|
|
|
if query_all:
|
|
|
|
|
return instance.get_all_assets()
|
2018-02-06 10:32:02 +00:00
|
|
|
|
else:
|
2018-04-10 12:29:06 +00:00
|
|
|
|
return instance.get_assets()
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
2018-02-07 05:39:45 +00:00
|
|
|
|
|
2018-03-01 04:40:41 +00:00
|
|
|
|
class NodeAddChildrenApi(generics.UpdateAPIView):
|
2018-07-14 16:55:05 +00:00
|
|
|
|
queryset = Node.objects.all()
|
2018-07-23 04:55:13 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-03-01 04:40:41 +00:00
|
|
|
|
serializer_class = serializers.NodeAddChildrenSerializer
|
|
|
|
|
instance = None
|
|
|
|
|
|
|
|
|
|
def put(self, request, *args, **kwargs):
|
|
|
|
|
instance = self.get_object()
|
|
|
|
|
nodes_id = request.data.get("nodes")
|
|
|
|
|
children = [get_object_or_none(Node, id=pk) for pk in nodes_id]
|
|
|
|
|
for node in children:
|
|
|
|
|
if not node:
|
|
|
|
|
continue
|
2018-05-29 11:19:35 +00:00
|
|
|
|
node.parent = instance
|
2018-03-01 04:40:41 +00:00
|
|
|
|
return Response("OK")
|
|
|
|
|
|
|
|
|
|
|
2018-02-07 05:39:45 +00:00
|
|
|
|
class NodeAddAssetsApi(generics.UpdateAPIView):
|
|
|
|
|
serializer_class = serializers.NodeAssetsSerializer
|
2018-07-14 16:55:05 +00:00
|
|
|
|
queryset = Node.objects.all()
|
2018-07-23 04:55:13 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-02-07 05:39:45 +00:00
|
|
|
|
instance = None
|
|
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
|
assets = serializer.validated_data.get('assets')
|
|
|
|
|
instance = self.get_object()
|
|
|
|
|
instance.assets.add(*tuple(assets))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NodeRemoveAssetsApi(generics.UpdateAPIView):
|
|
|
|
|
serializer_class = serializers.NodeAssetsSerializer
|
2018-07-14 16:55:05 +00:00
|
|
|
|
queryset = Node.objects.all()
|
2018-07-23 04:55:13 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-02-07 05:39:45 +00:00
|
|
|
|
instance = None
|
|
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
|
assets = serializer.validated_data.get('assets')
|
|
|
|
|
instance = self.get_object()
|
2018-02-25 10:08:00 +00:00
|
|
|
|
if instance != Node.root():
|
|
|
|
|
instance.assets.remove(*tuple(assets))
|
2018-05-10 12:48:16 +00:00
|
|
|
|
else:
|
|
|
|
|
assets = [asset for asset in assets if asset.nodes.count() > 1]
|
|
|
|
|
instance.assets.remove(*tuple(assets))
|
2018-03-12 03:41:12 +00:00
|
|
|
|
|
|
|
|
|
|
2018-04-13 07:48:10 +00:00
|
|
|
|
class NodeReplaceAssetsApi(generics.UpdateAPIView):
|
|
|
|
|
serializer_class = serializers.NodeAssetsSerializer
|
2018-07-14 16:55:05 +00:00
|
|
|
|
queryset = Node.objects.all()
|
2018-07-23 04:55:13 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-04-13 07:48:10 +00:00
|
|
|
|
instance = None
|
|
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
|
assets = serializer.validated_data.get('assets')
|
|
|
|
|
instance = self.get_object()
|
|
|
|
|
for asset in assets:
|
|
|
|
|
asset.nodes.set([instance])
|
|
|
|
|
|
|
|
|
|
|
2018-03-12 03:41:12 +00:00
|
|
|
|
class RefreshNodeHardwareInfoApi(APIView):
|
2018-07-23 04:55:13 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-03-12 03:41:12 +00:00
|
|
|
|
model = Node
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
node_id = kwargs.get('pk')
|
|
|
|
|
node = get_object_or_404(self.model, id=node_id)
|
2019-01-21 03:58:51 +00:00
|
|
|
|
assets = node.get_all_assets()
|
2018-09-03 03:24:25 +00:00
|
|
|
|
# task_name = _("更新节点资产硬件信息: {}".format(node.name))
|
|
|
|
|
task_name = _("Update node asset hardware information: {}").format(node.name)
|
2018-04-02 08:55:39 +00:00
|
|
|
|
task = update_assets_hardware_info_util.delay(assets, task_name=task_name)
|
|
|
|
|
return Response({"task": task.id})
|
2018-03-12 03:41:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestNodeConnectiveApi(APIView):
|
2018-07-23 04:55:13 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2018-03-12 03:41:12 +00:00
|
|
|
|
model = Node
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
node_id = kwargs.get('pk')
|
|
|
|
|
node = get_object_or_404(self.model, id=node_id)
|
2019-01-21 03:58:51 +00:00
|
|
|
|
assets = node.get_all_assets()
|
2018-09-03 03:24:25 +00:00
|
|
|
|
# task_name = _("测试节点下资产是否可连接: {}".format(node.name))
|
|
|
|
|
task_name = _("Test if the assets under the node are connectable: {}".format(node.name))
|
2018-12-18 09:28:45 +00:00
|
|
|
|
task = test_asset_connectivity_util.delay(assets, task_name=task_name)
|
2018-04-02 08:55:39 +00:00
|
|
|
|
return Response({"task": task.id})
|
2018-12-26 05:54:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RefreshAssetsAmount(APIView):
|
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
|
model = Node
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
self.model.expire_nodes_assets_amount()
|
|
|
|
|
return Response("Ok")
|