2018-02-07 15:25:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from rest_framework import serializers
|
2019-05-21 08:24:01 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2022-08-24 08:36:42 +00:00
|
|
|
from django.db.transaction import atomic
|
2022-08-22 10:32:33 +00:00
|
|
|
from django.db.models import F
|
2019-05-21 08:24:01 +00:00
|
|
|
|
2022-08-08 06:34:57 +00:00
|
|
|
from common.drf.serializers import JMSWritableNestedModelSerializer
|
2022-08-18 09:58:59 +00:00
|
|
|
from common.drf.fields import ChoiceDisplayField
|
2022-07-17 06:28:55 +00:00
|
|
|
from ..account import AccountSerializer
|
2022-08-22 05:25:57 +00:00
|
|
|
from ...models import Asset, Node, Platform, Protocol, Label, Domain
|
2022-08-18 09:58:59 +00:00
|
|
|
from ...const import Category, AllTypes
|
2018-02-07 15:25:15 +00:00
|
|
|
|
2018-04-24 03:07:09 +00:00
|
|
|
__all__ = [
|
2021-08-27 08:38:23 +00:00
|
|
|
'AssetSerializer', 'AssetSimpleSerializer', 'MiniAssetSerializer',
|
2022-08-05 07:46:36 +00:00
|
|
|
'AssetTaskSerializer', 'AssetsTaskSerializer',
|
2018-04-24 03:07:09 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-08-05 07:46:36 +00:00
|
|
|
class AssetProtocolsSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Protocol
|
2022-08-09 08:53:43 +00:00
|
|
|
fields = ['id', 'name', 'port']
|
2019-07-05 10:07:10 +00:00
|
|
|
|
2019-06-19 11:25:21 +00:00
|
|
|
|
2022-08-05 07:46:36 +00:00
|
|
|
class AssetLabelSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Label
|
2022-08-09 08:53:43 +00:00
|
|
|
fields = ['id', 'name', 'value']
|
2022-08-08 03:39:55 +00:00
|
|
|
extra_kwargs = {
|
2022-08-09 08:53:43 +00:00
|
|
|
'name': {'required': False},
|
2022-08-08 03:39:55 +00:00
|
|
|
'value': {'required': False}
|
|
|
|
}
|
2022-05-05 08:18:05 +00:00
|
|
|
|
2019-06-19 11:25:21 +00:00
|
|
|
|
2022-08-08 03:39:55 +00:00
|
|
|
class AssetPlatformSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Platform
|
2022-08-09 08:53:43 +00:00
|
|
|
fields = ['id', 'name']
|
2022-08-08 03:39:55 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
'name': {'required': False}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class AssetDomainSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Domain
|
2022-08-09 08:53:43 +00:00
|
|
|
fields = ['id', 'name']
|
|
|
|
extra_kwargs = {
|
|
|
|
'name': {'required': False}
|
|
|
|
}
|
2022-08-08 03:39:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetNodesSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Node
|
2022-08-09 08:53:43 +00:00
|
|
|
fields = ['id', 'value']
|
2022-08-08 03:39:55 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
'value': {'required': False}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-08-09 08:53:43 +00:00
|
|
|
class AssetSerializer(JMSWritableNestedModelSerializer):
|
2022-08-18 09:58:59 +00:00
|
|
|
category = ChoiceDisplayField(choices=Category.choices, read_only=True, label=_('Category'))
|
|
|
|
type = ChoiceDisplayField(choices=AllTypes.choices, read_only=True, label=_('Type'))
|
2022-08-08 03:39:55 +00:00
|
|
|
domain = AssetDomainSerializer(required=False)
|
2022-08-09 08:53:43 +00:00
|
|
|
platform = AssetPlatformSerializer(required=False)
|
2022-08-05 07:46:36 +00:00
|
|
|
labels = AssetLabelSerializer(many=True, required=False)
|
2022-08-08 03:39:55 +00:00
|
|
|
nodes = AssetNodesSerializer(many=True, required=False)
|
|
|
|
accounts = AccountSerializer(many=True, required=False)
|
2022-08-09 08:53:43 +00:00
|
|
|
protocols = AssetProtocolsSerializer(many=True, required=False)
|
2020-07-07 03:09:43 +00:00
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
"""
|
|
|
|
资产的数据结构
|
|
|
|
"""
|
2022-08-24 08:36:42 +00:00
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
class Meta:
|
|
|
|
model = Asset
|
2022-04-06 10:14:51 +00:00
|
|
|
fields_mini = [
|
2022-08-11 07:45:03 +00:00
|
|
|
'id', 'name', 'ip',
|
2022-04-06 10:14:51 +00:00
|
|
|
]
|
2020-04-23 03:14:02 +00:00
|
|
|
fields_small = fields_mini + [
|
2022-08-11 07:45:03 +00:00
|
|
|
'is_active', 'comment',
|
2021-09-15 09:37:49 +00:00
|
|
|
]
|
2020-04-23 03:14:02 +00:00
|
|
|
fields_fk = [
|
2022-08-08 03:39:55 +00:00
|
|
|
'domain', 'platform', 'platform',
|
2020-04-23 03:14:02 +00:00
|
|
|
]
|
|
|
|
fields_m2m = [
|
2022-08-08 03:39:55 +00:00
|
|
|
'nodes', 'labels', 'accounts', 'protocols',
|
2022-08-08 06:34:57 +00:00
|
|
|
'nodes_display',
|
2019-05-21 08:24:01 +00:00
|
|
|
]
|
2020-04-23 03:14:02 +00:00
|
|
|
read_only_fields = [
|
2022-08-09 08:53:43 +00:00
|
|
|
'category', 'type', 'connectivity', 'date_verified',
|
|
|
|
'created_by', 'date_created',
|
2021-07-08 06:23:18 +00:00
|
|
|
]
|
2022-04-02 10:35:46 +00:00
|
|
|
fields = fields_small + fields_fk + fields_m2m + read_only_fields
|
2021-11-11 03:56:30 +00:00
|
|
|
extra_kwargs = {
|
2022-08-11 07:45:03 +00:00
|
|
|
'name': {'label': _("Name")},
|
2022-04-30 15:19:43 +00:00
|
|
|
'ip': {'label': _('IP/Host')},
|
2019-06-19 11:25:21 +00:00
|
|
|
'protocol': {'write_only': True},
|
|
|
|
'port': {'write_only': True},
|
2021-09-15 09:37:49 +00:00
|
|
|
'admin_user_display': {'label': _('Admin user display'), 'read_only': True},
|
2021-11-11 03:56:30 +00:00
|
|
|
}
|
2018-02-07 15:25:15 +00:00
|
|
|
|
2022-07-15 10:03:32 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
data = kwargs.get('data', {})
|
|
|
|
self.accounts_data = data.pop('accounts', [])
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
""" Perform necessary eager loading of data. """
|
2022-08-24 08:36:42 +00:00
|
|
|
queryset = queryset.prefetch_related('domain', 'platform', 'protocols') \
|
|
|
|
.annotate(category=F("platform__category")) \
|
2022-08-22 10:32:33 +00:00
|
|
|
.annotate(type=F("platform__type"))
|
2020-09-27 08:02:44 +00:00
|
|
|
queryset = queryset.prefetch_related('nodes', 'labels')
|
2018-12-17 10:20:44 +00:00
|
|
|
return queryset
|
|
|
|
|
2020-10-30 02:16:49 +00:00
|
|
|
def perform_nodes_display_create(self, instance, nodes_display):
|
|
|
|
if not nodes_display:
|
|
|
|
return
|
|
|
|
nodes_to_set = []
|
|
|
|
for full_value in nodes_display:
|
|
|
|
node = Node.objects.filter(full_value=full_value).first()
|
|
|
|
if node:
|
|
|
|
nodes_to_set.append(node)
|
|
|
|
else:
|
|
|
|
node = Node.create_node_by_full_value(full_value)
|
|
|
|
nodes_to_set.append(node)
|
|
|
|
instance.nodes.set(nodes_to_set)
|
|
|
|
|
2022-07-15 10:03:32 +00:00
|
|
|
@staticmethod
|
|
|
|
def add_accounts(instance, accounts_data):
|
|
|
|
for data in accounts_data:
|
|
|
|
data['asset'] = instance.id
|
|
|
|
serializer = AccountSerializer(data=accounts_data, many=True)
|
|
|
|
try:
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
except Exception as e:
|
|
|
|
raise serializers.ValidationError({'accounts': e})
|
|
|
|
serializer.save()
|
|
|
|
|
2022-08-24 08:36:42 +00:00
|
|
|
@atomic
|
2019-07-05 10:07:10 +00:00
|
|
|
def create(self, validated_data):
|
2020-10-31 04:52:24 +00:00
|
|
|
nodes_display = validated_data.pop('nodes_display', '')
|
2019-06-13 10:58:43 +00:00
|
|
|
instance = super().create(validated_data)
|
2022-07-15 10:03:32 +00:00
|
|
|
if self.accounts_data:
|
|
|
|
self.add_accounts(instance, self.accounts_data)
|
2020-10-30 02:16:49 +00:00
|
|
|
self.perform_nodes_display_create(instance, nodes_display)
|
2019-06-13 10:58:43 +00:00
|
|
|
return instance
|
|
|
|
|
2022-08-24 08:36:42 +00:00
|
|
|
@atomic
|
2019-06-13 10:58:43 +00:00
|
|
|
def update(self, instance, validated_data):
|
2020-10-31 04:52:24 +00:00
|
|
|
nodes_display = validated_data.pop('nodes_display', '')
|
2020-10-30 02:16:49 +00:00
|
|
|
instance = super().update(instance, validated_data)
|
|
|
|
self.perform_nodes_display_create(instance, nodes_display)
|
|
|
|
return instance
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
|
2021-08-27 08:38:23 +00:00
|
|
|
class MiniAssetSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
fields = AssetSerializer.Meta.fields_mini
|
|
|
|
|
|
|
|
|
2018-12-18 09:28:45 +00:00
|
|
|
class AssetSimpleSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
2022-04-29 10:28:12 +00:00
|
|
|
fields = [
|
2022-08-11 07:45:03 +00:00
|
|
|
'id', 'name', 'ip', 'port',
|
2022-04-29 10:28:12 +00:00
|
|
|
'connectivity', 'date_verified'
|
|
|
|
]
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
2021-07-20 06:48:38 +00:00
|
|
|
class AssetsTaskSerializer(serializers.Serializer):
|
2020-03-12 08:24:38 +00:00
|
|
|
ACTION_CHOICES = (
|
|
|
|
('refresh', 'refresh'),
|
|
|
|
('test', 'test'),
|
|
|
|
)
|
|
|
|
task = serializers.CharField(read_only=True)
|
|
|
|
action = serializers.ChoiceField(choices=ACTION_CHOICES, write_only=True)
|
2021-01-06 03:08:35 +00:00
|
|
|
assets = serializers.PrimaryKeyRelatedField(
|
|
|
|
queryset=Asset.objects, required=False, allow_empty=True, many=True
|
|
|
|
)
|
2021-07-20 06:48:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetTaskSerializer(AssetsTaskSerializer):
|
|
|
|
ACTION_CHOICES = tuple(list(AssetsTaskSerializer.ACTION_CHOICES) + [
|
|
|
|
('push_system_user', 'push_system_user'),
|
|
|
|
('test_system_user', 'test_system_user')
|
|
|
|
])
|
2021-08-17 06:02:13 +00:00
|
|
|
action = serializers.ChoiceField(choices=ACTION_CHOICES, write_only=True)
|
2021-07-20 06:48:38 +00:00
|
|
|
asset = serializers.PrimaryKeyRelatedField(
|
|
|
|
queryset=Asset.objects, required=False, allow_empty=True, many=False
|
|
|
|
)
|