2018-02-07 15:25:15 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
2022-09-23 10:59:19 +00:00
|
|
|
|
|
2023-04-18 09:07:01 +00:00
|
|
|
|
from django.db.models import F
|
2022-12-27 08:53:23 +00:00
|
|
|
|
from django.db.transaction import atomic
|
2023-07-24 03:52:25 +00:00
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2022-12-27 08:53:23 +00:00
|
|
|
|
from rest_framework import serializers
|
2019-05-21 08:24:01 +00:00
|
|
|
|
|
2023-02-01 10:45:51 +00:00
|
|
|
|
from accounts.models import Account
|
2023-04-03 10:18:31 +00:00
|
|
|
|
from accounts.serializers import AccountSerializer
|
2023-04-18 09:07:01 +00:00
|
|
|
|
from common.const import UUID_PATTERN
|
|
|
|
|
from common.serializers import (
|
|
|
|
|
WritableNestedModelSerializer, SecretReadableMixin,
|
|
|
|
|
CommonModelSerializer, MethodSerializer
|
|
|
|
|
)
|
2023-04-20 07:23:00 +00:00
|
|
|
|
from common.serializers.common import DictSerializer
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from common.serializers.fields import LabeledChoiceField
|
|
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2022-08-18 09:58:59 +00:00
|
|
|
|
from ...const import Category, AllTypes
|
2023-01-16 11:02:09 +00:00
|
|
|
|
from ...models import Asset, Node, Platform, Label, Protocol
|
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-11-16 07:07:35 +00:00
|
|
|
|
'AssetTaskSerializer', 'AssetsTaskSerializer', 'AssetProtocolsSerializer',
|
2023-01-16 11:02:09 +00:00
|
|
|
|
'AssetDetailSerializer', 'DetailMixin', 'AssetAccountSerializer',
|
2023-04-25 06:48:09 +00:00
|
|
|
|
'AccountSecretSerializer', 'AssetProtocolsPermsSerializer', 'AssetLabelSerializer'
|
2018-04-24 03:07:09 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
2022-08-05 07:46:36 +00:00
|
|
|
|
class AssetProtocolsSerializer(serializers.ModelSerializer):
|
2023-05-31 08:21:24 +00:00
|
|
|
|
port = serializers.IntegerField(required=False, allow_null=True, max_value=65535, min_value=0)
|
2023-02-17 13:11:06 +00:00
|
|
|
|
|
2023-03-10 07:52:07 +00:00
|
|
|
|
def to_file_representation(self, data):
|
|
|
|
|
return '{name}/{port}'.format(**data)
|
|
|
|
|
|
|
|
|
|
def to_file_internal_value(self, data):
|
|
|
|
|
name, port = data.split('/')
|
|
|
|
|
return {'name': name, 'port': port}
|
|
|
|
|
|
2022-08-05 07:46:36 +00:00
|
|
|
|
class Meta:
|
|
|
|
|
model = Protocol
|
2023-01-16 11:02:09 +00:00
|
|
|
|
fields = ['name', 'port']
|
2019-07-05 10:07:10 +00:00
|
|
|
|
|
2019-06-19 11:25:21 +00:00
|
|
|
|
|
2023-04-13 09:26:24 +00:00
|
|
|
|
class AssetProtocolsPermsSerializer(AssetProtocolsSerializer):
|
|
|
|
|
class Meta(AssetProtocolsSerializer.Meta):
|
|
|
|
|
fields = AssetProtocolsSerializer.Meta.fields + ['public', 'setting']
|
|
|
|
|
|
|
|
|
|
|
2022-08-05 07:46:36 +00:00
|
|
|
|
class AssetLabelSerializer(serializers.ModelSerializer):
|
|
|
|
|
class Meta:
|
|
|
|
|
model = Label
|
2022-09-28 04:10:39 +00:00
|
|
|
|
fields = ['id', 'name', 'value']
|
2022-08-08 03:39:55 +00:00
|
|
|
|
extra_kwargs = {
|
2023-02-07 07:56:59 +00:00
|
|
|
|
# 取消默认唯一键的校验
|
|
|
|
|
'id': {'validators': []},
|
2022-08-09 08:53:43 +00:00
|
|
|
|
'name': {'required': False},
|
2023-02-07 07:56:59 +00:00
|
|
|
|
'value': {'required': False},
|
2022-08-08 03:39:55 +00:00
|
|
|
|
}
|
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}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-04-03 10:18:31 +00:00
|
|
|
|
class AssetAccountSerializer(AccountSerializer):
|
2022-09-07 12:24:48 +00:00
|
|
|
|
add_org_fields = False
|
2023-04-03 10:18:31 +00:00
|
|
|
|
asset = serializers.PrimaryKeyRelatedField(queryset=Asset.objects, required=False, write_only=True)
|
2023-04-19 02:57:38 +00:00
|
|
|
|
clone_id = None
|
2023-04-13 06:20:07 +00:00
|
|
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
2023-04-19 02:57:38 +00:00
|
|
|
|
# 导入时,data有时为str
|
|
|
|
|
if isinstance(data, str):
|
|
|
|
|
return super().to_internal_value(data)
|
|
|
|
|
|
2023-04-13 06:20:07 +00:00
|
|
|
|
clone_id = data.pop('id', None)
|
|
|
|
|
ret = super().to_internal_value(data)
|
|
|
|
|
self.clone_id = clone_id
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
def set_secret(self, attrs):
|
|
|
|
|
_id = self.clone_id
|
|
|
|
|
if not _id:
|
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
|
|
account = Account.objects.get(id=_id)
|
|
|
|
|
attrs['secret'] = account.secret
|
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
|
attrs = super().validate(attrs)
|
|
|
|
|
return self.set_secret(attrs)
|
2022-09-07 12:24:48 +00:00
|
|
|
|
|
2023-04-03 10:18:31 +00:00
|
|
|
|
class Meta(AccountSerializer.Meta):
|
|
|
|
|
fields = [
|
|
|
|
|
f for f in AccountSerializer.Meta.fields
|
|
|
|
|
if f not in ['spec_info']
|
2022-09-07 12:24:48 +00:00
|
|
|
|
]
|
2023-01-16 11:02:09 +00:00
|
|
|
|
extra_kwargs = {
|
2023-04-03 10:18:31 +00:00
|
|
|
|
**AccountSerializer.Meta.extra_kwargs,
|
2023-01-16 11:02:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AccountSecretSerializer(SecretReadableMixin, CommonModelSerializer):
|
|
|
|
|
class Meta:
|
|
|
|
|
model = Account
|
|
|
|
|
fields = [
|
|
|
|
|
'name', 'username', 'privileged', 'secret_type', 'secret',
|
|
|
|
|
]
|
|
|
|
|
extra_kwargs = {
|
|
|
|
|
'secret': {'write_only': False},
|
|
|
|
|
}
|
2022-09-07 12:24:48 +00:00
|
|
|
|
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
|
class AssetSerializer(BulkOrgResourceModelSerializer, WritableNestedModelSerializer):
|
2022-09-01 06:46:31 +00:00
|
|
|
|
category = LabeledChoiceField(choices=Category.choices, read_only=True, label=_('Category'))
|
2022-09-26 10:03:48 +00:00
|
|
|
|
type = LabeledChoiceField(choices=AllTypes.choices(), read_only=True, label=_('Type'))
|
2023-01-16 11:02:09 +00:00
|
|
|
|
labels = AssetLabelSerializer(many=True, required=False, label=_('Label'))
|
2023-02-03 11:44:59 +00:00
|
|
|
|
protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols'), default=())
|
2023-04-10 02:57:44 +00:00
|
|
|
|
accounts = AssetAccountSerializer(many=True, required=False, allow_null=True, write_only=True, label=_('Account'))
|
2023-03-24 09:08:41 +00:00
|
|
|
|
nodes_display = serializers.ListField(read_only=False, required=False, label=_("Node path"))
|
2023-07-20 06:57:51 +00:00
|
|
|
|
_accounts = None
|
2022-09-09 03:00:09 +00:00
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
class Meta:
|
|
|
|
|
model = Asset
|
2022-12-27 08:53:23 +00:00
|
|
|
|
fields_mini = ['id', 'name', 'address']
|
2023-04-18 09:07:01 +00:00
|
|
|
|
fields_small = fields_mini + ['is_active', 'comment']
|
2023-01-16 11:02:09 +00:00
|
|
|
|
fields_fk = ['domain', 'platform']
|
2020-04-23 03:14:02 +00:00
|
|
|
|
fields_m2m = [
|
2023-01-31 09:46:56 +00:00
|
|
|
|
'nodes', 'labels', 'protocols',
|
2023-04-10 02:57:44 +00:00
|
|
|
|
'nodes_display', 'accounts',
|
2019-05-21 08:24:01 +00:00
|
|
|
|
]
|
2020-04-23 03:14:02 +00:00
|
|
|
|
read_only_fields = [
|
2023-04-10 02:57:44 +00:00
|
|
|
|
'category', 'type', 'connectivity', 'auto_config',
|
2023-02-09 12:48:25 +00:00
|
|
|
|
'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
|
2023-04-10 02:57:44 +00:00
|
|
|
|
fields_unexport = ['auto_config']
|
2021-11-11 03:56:30 +00:00
|
|
|
|
extra_kwargs = {
|
2023-04-10 02:57:44 +00:00
|
|
|
|
'auto_config': {'label': _('Auto info')},
|
2022-08-11 07:45:03 +00:00
|
|
|
|
'name': {'label': _("Name")},
|
2022-09-21 03:17:14 +00:00
|
|
|
|
'address': {'label': _('Address')},
|
2022-12-27 08:21:32 +00:00
|
|
|
|
'nodes_display': {'label': _('Node path')},
|
2023-04-21 06:53:24 +00:00
|
|
|
|
'nodes': {'allow_empty': True},
|
2021-11-11 03:56:30 +00:00
|
|
|
|
}
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
self._init_field_choices()
|
2023-07-20 06:57:51 +00:00
|
|
|
|
self._extract_accounts()
|
|
|
|
|
|
|
|
|
|
def _extract_accounts(self):
|
|
|
|
|
if not getattr(self, 'initial_data', None):
|
|
|
|
|
return
|
2023-07-25 06:45:24 +00:00
|
|
|
|
if isinstance(self.initial_data, list):
|
|
|
|
|
return
|
2023-07-20 06:57:51 +00:00
|
|
|
|
accounts = self.initial_data.pop('accounts', None)
|
|
|
|
|
self._accounts = accounts
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
2023-02-03 11:44:59 +00:00
|
|
|
|
def _get_protocols_required_default(self):
|
2023-03-15 06:14:48 +00:00
|
|
|
|
platform = self._asset_platform
|
2023-02-03 11:44:59 +00:00
|
|
|
|
platform_protocols = platform.protocols.all()
|
|
|
|
|
protocols_default = [p for p in platform_protocols if p.default]
|
|
|
|
|
protocols_required = [p for p in platform_protocols if p.required or p.primary]
|
|
|
|
|
return protocols_required, protocols_default
|
|
|
|
|
|
|
|
|
|
def _set_protocols_default(self):
|
|
|
|
|
if not hasattr(self, 'initial_data'):
|
|
|
|
|
return
|
|
|
|
|
protocols = self.initial_data.get('protocols')
|
|
|
|
|
if protocols is not None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
protocols_required, protocols_default = self._get_protocols_required_default()
|
2023-07-19 11:00:15 +00:00
|
|
|
|
protocol_map = {str(protocol.id): protocol for protocol in protocols_required + protocols_default}
|
|
|
|
|
protocols = list(protocol_map.values())
|
|
|
|
|
protocols_data = [{'name': p.name, 'port': p.port} for p in protocols]
|
2023-02-03 11:44:59 +00:00
|
|
|
|
self.initial_data['protocols'] = protocols_data
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
|
def _init_field_choices(self):
|
|
|
|
|
request = self.context.get('request')
|
|
|
|
|
if not request:
|
|
|
|
|
return
|
|
|
|
|
category = request.path.strip('/').split('/')[-1].rstrip('s')
|
|
|
|
|
field_category = self.fields.get('category')
|
2023-04-17 07:10:34 +00:00
|
|
|
|
if not field_category:
|
|
|
|
|
return
|
2023-02-02 06:37:21 +00:00
|
|
|
|
field_category.choices = Category.filter_choices(category)
|
2023-01-16 11:02:09 +00:00
|
|
|
|
field_type = self.fields.get('type')
|
2023-04-17 07:10:34 +00:00
|
|
|
|
if not field_type:
|
|
|
|
|
return
|
2023-02-02 06:37:21 +00:00
|
|
|
|
field_type.choices = AllTypes.filter_choices(category)
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
|
""" Perform necessary eager loading of data. """
|
2023-02-23 08:14:24 +00:00
|
|
|
|
queryset = queryset.prefetch_related('domain', 'nodes', 'labels', 'protocols') \
|
|
|
|
|
.prefetch_related('platform', 'platform__automation') \
|
2022-08-24 08:36:42 +00:00
|
|
|
|
.annotate(category=F("platform__category")) \
|
2022-08-22 10:32:33 +00:00
|
|
|
|
.annotate(type=F("platform__type"))
|
2018-12-17 10:20:44 +00:00
|
|
|
|
return queryset
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def perform_nodes_display_create(instance, nodes_display):
|
2020-10-30 02:16:49 +00:00
|
|
|
|
if not nodes_display:
|
|
|
|
|
return
|
|
|
|
|
nodes_to_set = []
|
|
|
|
|
for full_value in nodes_display:
|
2022-10-25 11:31:13 +00:00
|
|
|
|
if not full_value.startswith('/'):
|
|
|
|
|
full_value = '/' + instance.org.name + '/' + full_value
|
2020-10-30 02:16:49 +00:00
|
|
|
|
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)
|
|
|
|
|
|
2023-02-23 09:44:38 +00:00
|
|
|
|
@property
|
2023-03-15 06:14:48 +00:00
|
|
|
|
def _asset_platform(self):
|
2023-02-03 11:44:59 +00:00
|
|
|
|
platform_id = self.initial_data.get('platform')
|
|
|
|
|
if isinstance(platform_id, dict):
|
|
|
|
|
platform_id = platform_id.get('id') or platform_id.get('pk')
|
2023-03-15 06:14:48 +00:00
|
|
|
|
|
|
|
|
|
if not platform_id and self.instance:
|
|
|
|
|
platform = self.instance.platform
|
|
|
|
|
else:
|
|
|
|
|
platform = Platform.objects.filter(id=platform_id).first()
|
|
|
|
|
|
2023-02-03 11:44:59 +00:00
|
|
|
|
if not platform:
|
|
|
|
|
raise serializers.ValidationError({'platform': _("Platform not exist")})
|
|
|
|
|
return platform
|
|
|
|
|
|
|
|
|
|
def validate_domain(self, value):
|
2023-03-15 06:14:48 +00:00
|
|
|
|
platform = self._asset_platform
|
2023-02-03 11:44:59 +00:00
|
|
|
|
if platform.domain_enabled:
|
|
|
|
|
return value
|
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
2022-09-09 03:00:09 +00:00
|
|
|
|
def validate_nodes(self, nodes):
|
|
|
|
|
if nodes:
|
|
|
|
|
return nodes
|
2023-02-19 07:43:56 +00:00
|
|
|
|
nodes_display = self.initial_data.get('nodes_display')
|
|
|
|
|
if nodes_display:
|
|
|
|
|
return nodes
|
2023-04-18 08:29:41 +00:00
|
|
|
|
default_node = Node.org_root()
|
2022-09-09 03:00:09 +00:00
|
|
|
|
request = self.context.get('request')
|
|
|
|
|
if not request:
|
2023-04-18 08:29:41 +00:00
|
|
|
|
return [default_node]
|
2022-09-09 03:00:09 +00:00
|
|
|
|
node_id = request.query_params.get('node_id')
|
|
|
|
|
if not node_id:
|
2023-04-18 08:29:41 +00:00
|
|
|
|
return [default_node]
|
2023-02-09 12:48:25 +00:00
|
|
|
|
nodes = Node.objects.filter(id=node_id)
|
|
|
|
|
return nodes
|
2022-09-09 03:00:09 +00:00
|
|
|
|
|
2023-02-03 11:44:59 +00:00
|
|
|
|
def is_valid(self, raise_exception=False):
|
|
|
|
|
self._set_protocols_default()
|
2023-07-27 02:19:47 +00:00
|
|
|
|
return super().is_valid(raise_exception=raise_exception)
|
2022-10-18 12:37:17 +00:00
|
|
|
|
|
2023-02-03 11:44:59 +00:00
|
|
|
|
def validate_protocols(self, protocols_data):
|
|
|
|
|
# 目的是去重
|
2022-10-18 12:37:17 +00:00
|
|
|
|
protocols_data_map = {p['name']: p for p in protocols_data}
|
2023-02-03 08:03:34 +00:00
|
|
|
|
for p in protocols_data:
|
|
|
|
|
port = p.get('port', 0)
|
2023-05-31 08:21:24 +00:00
|
|
|
|
if port < 0 or port > 65535:
|
|
|
|
|
error = p.get('name') + ': ' + _("port out of range (0-65535)")
|
2023-02-03 08:03:34 +00:00
|
|
|
|
raise serializers.ValidationError(error)
|
|
|
|
|
|
2023-07-19 11:00:15 +00:00
|
|
|
|
protocols_required, __ = self._get_protocols_required_default()
|
2022-10-18 12:37:17 +00:00
|
|
|
|
protocols_not_found = [p.name for p in protocols_required if p.name not in protocols_data_map]
|
|
|
|
|
if protocols_not_found:
|
|
|
|
|
raise serializers.ValidationError({
|
|
|
|
|
'protocols': _("Protocol is required: {}").format(', '.join(protocols_not_found))
|
|
|
|
|
})
|
|
|
|
|
return protocols_data_map.values()
|
|
|
|
|
|
2023-02-06 03:00:36 +00:00
|
|
|
|
@staticmethod
|
|
|
|
|
def accounts_create(accounts_data, asset):
|
2023-03-13 09:57:50 +00:00
|
|
|
|
if not accounts_data:
|
|
|
|
|
return
|
2023-02-06 03:00:36 +00:00
|
|
|
|
for data in accounts_data:
|
2023-04-03 10:18:31 +00:00
|
|
|
|
data['asset'] = asset.id
|
|
|
|
|
s = AssetAccountSerializer(data=accounts_data, many=True)
|
|
|
|
|
s.is_valid(raise_exception=True)
|
|
|
|
|
s.save()
|
2023-02-06 03:00:36 +00:00
|
|
|
|
|
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)
|
2023-07-20 06:57:51 +00:00
|
|
|
|
self.accounts_create(self._accounts, instance)
|
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
|
|
|
|
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
|
class DetailMixin(serializers.Serializer):
|
2022-12-27 08:53:23 +00:00
|
|
|
|
accounts = AssetAccountSerializer(many=True, required=False, label=_('Accounts'))
|
2023-04-10 02:57:44 +00:00
|
|
|
|
spec_info = MethodSerializer(label=_('Spec info'), read_only=True)
|
|
|
|
|
gathered_info = MethodSerializer(label=_('Gathered info'), read_only=True)
|
|
|
|
|
auto_config = serializers.DictField(read_only=True, label=_('Auto info'))
|
|
|
|
|
|
|
|
|
|
def get_instance(self):
|
|
|
|
|
request = self.context.get('request')
|
2023-04-18 09:07:01 +00:00
|
|
|
|
if not self.instance and UUID_PATTERN.findall(request.path):
|
|
|
|
|
pk = UUID_PATTERN.findall(request.path)[0]
|
2023-04-10 02:57:44 +00:00
|
|
|
|
self.instance = Asset.objects.filter(id=pk).first()
|
|
|
|
|
return self.instance
|
2022-12-27 08:53:23 +00:00
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
|
def get_field_names(self, declared_fields, info):
|
|
|
|
|
names = super().get_field_names(declared_fields, info)
|
|
|
|
|
names.extend([
|
2023-04-10 02:57:44 +00:00
|
|
|
|
'accounts', 'gathered_info', 'spec_info',
|
|
|
|
|
'auto_config',
|
2023-01-16 11:02:09 +00:00
|
|
|
|
])
|
|
|
|
|
return names
|
|
|
|
|
|
2023-04-10 02:57:44 +00:00
|
|
|
|
def get_category(self):
|
|
|
|
|
request = self.context.get('request')
|
|
|
|
|
if request.query_params.get('category'):
|
|
|
|
|
category = request.query_params.get('category')
|
|
|
|
|
else:
|
|
|
|
|
instance = self.get_instance()
|
2023-04-20 07:23:00 +00:00
|
|
|
|
category = instance.category if instance else 'host'
|
2023-04-10 02:57:44 +00:00
|
|
|
|
return category
|
|
|
|
|
|
|
|
|
|
def get_gathered_info_serializer(self):
|
|
|
|
|
category = self.get_category()
|
|
|
|
|
from .info.gathered import category_gathered_serializer_map
|
2023-04-20 07:23:00 +00:00
|
|
|
|
serializer_cls = category_gathered_serializer_map.get(category, DictSerializer)
|
2023-04-10 02:57:44 +00:00
|
|
|
|
return serializer_cls()
|
|
|
|
|
|
|
|
|
|
def get_spec_info_serializer(self):
|
|
|
|
|
category = self.get_category()
|
|
|
|
|
from .info.spec import category_spec_serializer_map
|
2023-04-20 07:23:00 +00:00
|
|
|
|
serializer_cls = category_spec_serializer_map.get(category, DictSerializer)
|
2023-04-10 02:57:44 +00:00
|
|
|
|
return serializer_cls()
|
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
|
|
class AssetDetailSerializer(DetailMixin, AssetSerializer):
|
|
|
|
|
pass
|
2022-12-27 08:53:23 +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-09-21 03:17:14 +00:00
|
|
|
|
'id', 'name', 'address', '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
|
|
|
|
|
)
|
2022-11-02 09:27:47 +00:00
|
|
|
|
accounts = serializers.PrimaryKeyRelatedField(
|
|
|
|
|
queryset=Account.objects, required=False, allow_empty=True, many=True
|
|
|
|
|
)
|