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.
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
|
from rest_framework import generics
|
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
|
2019-08-21 12:27:21 +00:00
|
|
|
|
from orgs.mixins.api import OrgModelViewSet
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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',
|
2019-09-11 13:22:25 +00:00
|
|
|
|
'NodeChildrenAsTreeApi', 'RefreshNodesCacheApi',
|
2018-02-07 05:39:45 +00:00
|
|
|
|
]
|
2018-02-06 10:32:02 +00:00
|
|
|
|
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
|
class NodeViewSet(OrgModelViewSet):
|
|
|
|
|
filter_fields = ('value', 'key', 'id')
|
|
|
|
|
search_fields = ('value', )
|
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-08-21 12:27:21 +00:00
|
|
|
|
# 仅支持根节点指直接创建,子节点下的节点需要通过children接口创建
|
2019-01-22 04:44:01 +00:00
|
|
|
|
def perform_create(self, serializer):
|
2019-08-23 10:23:07 +00:00
|
|
|
|
child_key = Node.org_root().get_next_child_key()
|
2019-01-22 04:44:01 +00:00
|
|
|
|
serializer.validated_data["key"] = child_key
|
|
|
|
|
serializer.save()
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
|
def perform_update(self, serializer):
|
2019-01-22 04:44:01 +00:00
|
|
|
|
node = self.get_object()
|
2019-08-23 10:23:07 +00:00
|
|
|
|
if node.is_org_root() and node.value != serializer.validated_data['value']:
|
2019-08-21 12:27:21 +00:00
|
|
|
|
msg = _("You can't update the root node name")
|
|
|
|
|
raise ValidationError({"error": msg})
|
|
|
|
|
return super().perform_update(serializer)
|
2019-01-22 04:44:01 +00:00
|
|
|
|
|
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
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def to_tree_queryset(queryset):
|
|
|
|
|
queryset = [node.as_tree_node() for node in queryset]
|
|
|
|
|
return queryset
|
|
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
|
def get_queryset(self):
|
2019-06-27 13:43:10 +00:00
|
|
|
|
queryset = Node.objects.all()
|
2018-12-17 10:20:44 +00:00
|
|
|
|
return queryset
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
|
queryset = super().filter_queryset(queryset)
|
|
|
|
|
queryset = self.to_tree_queryset(queryset)
|
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NodeChildrenApi(generics.ListCreateAPIView):
|
|
|
|
|
queryset = Node.objects.all()
|
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
|
serializer_class = serializers.NodeSerializer
|
|
|
|
|
instance = None
|
2019-08-23 10:23:07 +00:00
|
|
|
|
is_initial = False
|
2019-08-21 12:27:21 +00:00
|
|
|
|
|
|
|
|
|
def initial(self, request, *args, **kwargs):
|
|
|
|
|
self.instance = self.get_object()
|
|
|
|
|
return super().initial(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
|
data = serializer.validated_data
|
|
|
|
|
_id = data.get("id")
|
|
|
|
|
value = data.get("value")
|
|
|
|
|
if not value:
|
|
|
|
|
value = self.instance.get_next_child_preset_name()
|
|
|
|
|
node = self.instance.create_child(value=value, _id=_id)
|
|
|
|
|
# 避免查询 full value
|
|
|
|
|
node._full_value = node.value
|
|
|
|
|
serializer.instance = node
|
|
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
|
pk = self.kwargs.get('pk') or self.request.query_params.get('id')
|
|
|
|
|
key = self.request.query_params.get("key")
|
|
|
|
|
if not pk and not key:
|
2019-08-23 10:23:07 +00:00
|
|
|
|
node = Node.org_root()
|
|
|
|
|
self.is_initial = True
|
2019-08-21 12:27:21 +00:00
|
|
|
|
return node
|
|
|
|
|
if pk:
|
|
|
|
|
node = get_object_or_404(Node, pk=pk)
|
|
|
|
|
else:
|
|
|
|
|
node = get_object_or_404(Node, key=key)
|
|
|
|
|
return node
|
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
|
query_all = self.request.query_params.get("all", "0") == "all"
|
|
|
|
|
if not self.instance:
|
|
|
|
|
return Node.objects.none()
|
|
|
|
|
|
2019-08-23 10:23:07 +00:00
|
|
|
|
if self.is_initial:
|
2019-08-21 12:27:21 +00:00
|
|
|
|
with_self = True
|
|
|
|
|
else:
|
|
|
|
|
with_self = False
|
|
|
|
|
|
|
|
|
|
if query_all:
|
|
|
|
|
queryset = self.instance.get_all_children(with_self=with_self)
|
|
|
|
|
else:
|
|
|
|
|
queryset = self.instance.get_children(with_self=with_self)
|
2018-12-17 10:20:44 +00:00
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
|
class NodeChildrenAsTreeApi(NodeChildrenApi):
|
2018-12-17 10:20:44 +00:00
|
|
|
|
"""
|
|
|
|
|
节点子节点作为树返回,
|
|
|
|
|
[
|
|
|
|
|
{
|
|
|
|
|
"id": "",
|
|
|
|
|
"name": "",
|
|
|
|
|
"pId": "",
|
|
|
|
|
"meta": ""
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
serializer_class = TreeNodeSerializer
|
2019-08-21 12:27:21 +00:00
|
|
|
|
http_method_names = ['get']
|
2018-12-17 10:20:44 +00:00
|
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2019-08-21 12:27:21 +00:00
|
|
|
|
queryset = super().get_queryset()
|
2018-12-17 10:20:44 +00:00
|
|
|
|
queryset = [node.as_tree_node() for node in queryset]
|
2019-08-21 12:27:21 +00:00
|
|
|
|
queryset = self.add_assets_if_need(queryset)
|
2019-05-10 03:29:38 +00:00
|
|
|
|
queryset = sorted(queryset)
|
2018-12-17 10:20:44 +00:00
|
|
|
|
return queryset
|
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
|
def add_assets_if_need(self, queryset):
|
2018-12-17 10:20:44 +00:00
|
|
|
|
include_assets = self.request.query_params.get('assets', '0') == '1'
|
|
|
|
|
if not include_assets:
|
|
|
|
|
return queryset
|
2019-08-21 12:27:21 +00:00
|
|
|
|
assets = self.instance.get_assets().only(
|
|
|
|
|
"id", "hostname", "ip", 'platform', "os",
|
|
|
|
|
"org_id", "protocols",
|
2019-07-01 12:01:47 +00:00
|
|
|
|
)
|
2018-12-17 10:20:44 +00:00
|
|
|
|
for asset in assets:
|
2019-08-21 12:27:21 +00:00
|
|
|
|
queryset.append(asset.as_tree_node(self.instance))
|
2018-12-17 10:20:44 +00:00
|
|
|
|
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
|
|
|
|
|
2018-04-10 12:29:06 +00:00
|
|
|
|
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()
|
2019-08-23 10:23:07 +00:00
|
|
|
|
if instance != Node.org_root():
|
2018-02-25 10:08:00 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
2019-09-11 13:22:25 +00:00
|
|
|
|
class RefreshNodesCacheApi(APIView):
|
2018-12-26 05:54:42 +00:00
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
2019-09-11 13:22:25 +00:00
|
|
|
|
Node.refresh_nodes()
|
2018-12-26 05:54:42 +00:00
|
|
|
|
return Response("Ok")
|
2019-09-11 13:22:25 +00:00
|
|
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
|
|
|
|
self.get(*args, **kwargs)
|
|
|
|
|
return Response(status=204)
|