2018-02-07 15:25:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from rest_framework import serializers
|
2020-03-12 08:24:38 +00:00
|
|
|
from django.db.models import Prefetch, F
|
|
|
|
|
2019-05-21 08:24:01 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2019-04-25 02:11:50 +00:00
|
|
|
from common.serializers import AdaptedBulkListSerializer
|
2019-12-16 08:53:29 +00:00
|
|
|
from ..models import Asset, Node, Label, Platform
|
2019-06-24 12:16:18 +00:00
|
|
|
from .base import ConnectivitySerializer
|
2018-02-07 15:25:15 +00:00
|
|
|
|
2018-04-24 03:07:09 +00:00
|
|
|
__all__ = [
|
2019-07-01 11:30:18 +00:00
|
|
|
'AssetSerializer', 'AssetSimpleSerializer',
|
2020-03-12 08:24:38 +00:00
|
|
|
'AssetDisplaySerializer',
|
2019-12-16 08:53:29 +00:00
|
|
|
'ProtocolsField', 'PlatformSerializer',
|
2020-03-12 08:24:38 +00:00
|
|
|
'AssetDetailSerializer', 'AssetTaskSerializer',
|
2018-04-24 03:07:09 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-07-05 10:07:10 +00:00
|
|
|
class ProtocolField(serializers.RegexField):
|
|
|
|
protocols = '|'.join(dict(Asset.PROTOCOL_CHOICES).keys())
|
|
|
|
default_error_messages = {
|
|
|
|
'invalid': _('Protocol format should {}/{}'.format(protocols, '1-65535'))
|
|
|
|
}
|
|
|
|
regex = r'^(%s)/(\d{1,5})$' % protocols
|
2019-06-13 10:58:43 +00:00
|
|
|
|
2019-07-05 10:07:10 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(self.regex, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def validate_duplicate_protocols(values):
|
|
|
|
errors = []
|
|
|
|
names = []
|
|
|
|
|
|
|
|
for value in values:
|
|
|
|
if not value or '/' not in value:
|
|
|
|
continue
|
|
|
|
name = value.split('/')[0]
|
|
|
|
if name in names:
|
|
|
|
errors.append(_("Protocol duplicate: {}").format(name))
|
|
|
|
names.append(name)
|
|
|
|
errors.append('')
|
|
|
|
if any(errors):
|
|
|
|
raise serializers.ValidationError(errors)
|
2019-06-13 10:58:43 +00:00
|
|
|
|
2019-06-19 11:25:21 +00:00
|
|
|
|
2019-07-05 10:07:10 +00:00
|
|
|
class ProtocolsField(serializers.ListField):
|
|
|
|
default_validators = [validate_duplicate_protocols]
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs['child'] = ProtocolField()
|
|
|
|
kwargs['allow_null'] = True
|
|
|
|
kwargs['allow_empty'] = True
|
|
|
|
kwargs['min_length'] = 1
|
|
|
|
kwargs['max_length'] = 4
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def to_representation(self, value):
|
|
|
|
if not value:
|
|
|
|
return []
|
|
|
|
return value.split(' ')
|
2019-06-19 11:25:21 +00:00
|
|
|
|
|
|
|
|
2019-06-19 08:45:14 +00:00
|
|
|
class AssetSerializer(BulkOrgResourceModelSerializer):
|
2019-12-16 08:53:29 +00:00
|
|
|
platform = serializers.SlugRelatedField(
|
|
|
|
slug_field='name', queryset=Platform.objects.all(), label=_("Platform")
|
|
|
|
)
|
2019-07-05 10:07:10 +00:00
|
|
|
protocols = ProtocolsField(label=_('Protocols'), required=False)
|
2018-02-07 15:25:15 +00:00
|
|
|
"""
|
|
|
|
资产的数据结构
|
|
|
|
"""
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
2019-04-25 02:11:50 +00:00
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
2019-05-21 08:24:01 +00:00
|
|
|
fields = [
|
2019-06-19 08:45:14 +00:00
|
|
|
'id', 'ip', 'hostname', 'protocol', 'port',
|
2019-06-13 10:58:43 +00:00
|
|
|
'protocols', 'platform', 'is_active', 'public_ip', 'domain',
|
|
|
|
'admin_user', 'nodes', 'labels', 'number', 'vendor', 'model', 'sn',
|
2019-05-21 08:24:01 +00:00
|
|
|
'cpu_model', 'cpu_count', 'cpu_cores', 'cpu_vcpus', 'memory',
|
|
|
|
'disk_total', 'disk_info', 'os', 'os_version', 'os_arch',
|
|
|
|
'hostname_raw', 'comment', 'created_by', 'date_created',
|
2020-03-12 08:24:38 +00:00
|
|
|
'hardware_info',
|
2019-05-21 08:24:01 +00:00
|
|
|
]
|
|
|
|
read_only_fields = (
|
2019-06-13 10:58:43 +00:00
|
|
|
'vendor', 'model', 'sn', 'cpu_model', 'cpu_count',
|
2019-05-21 08:24:01 +00:00
|
|
|
'cpu_cores', 'cpu_vcpus', 'memory', 'disk_total', 'disk_info',
|
|
|
|
'os', 'os_version', 'os_arch', 'hostname_raw',
|
|
|
|
'created_by', 'date_created',
|
|
|
|
)
|
|
|
|
extra_kwargs = {
|
2019-06-19 11:25:21 +00:00
|
|
|
'protocol': {'write_only': True},
|
|
|
|
'port': {'write_only': True},
|
2019-05-21 08:24:01 +00:00
|
|
|
'hardware_info': {'label': _('Hardware info')},
|
|
|
|
'org_name': {'label': _('Org name')}
|
|
|
|
}
|
2018-02-07 15:25:15 +00:00
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
""" Perform necessary eager loading of data. """
|
2019-07-04 13:17:39 +00:00
|
|
|
queryset = queryset.prefetch_related(
|
|
|
|
Prefetch('nodes', queryset=Node.objects.all().only('id')),
|
|
|
|
Prefetch('labels', queryset=Label.objects.all().only('id')),
|
2020-03-12 08:24:38 +00:00
|
|
|
).select_related('admin_user', 'domain', 'platform') \
|
|
|
|
.annotate(platform_base=F('platform__base'))
|
2018-12-17 10:20:44 +00:00
|
|
|
return queryset
|
|
|
|
|
2019-07-05 10:07:10 +00:00
|
|
|
def compatible_with_old_protocol(self, validated_data):
|
2019-06-19 11:25:21 +00:00
|
|
|
protocols_data = validated_data.pop("protocols", [])
|
2019-06-13 10:58:43 +00:00
|
|
|
|
|
|
|
# 兼容老的api
|
2019-07-05 10:07:10 +00:00
|
|
|
name = validated_data.get("protocol")
|
2019-06-13 10:58:43 +00:00
|
|
|
port = validated_data.get("port")
|
2019-07-05 10:07:10 +00:00
|
|
|
if not protocols_data and name and port:
|
|
|
|
protocols_data.insert(0, '/'.join([name, str(port)]))
|
|
|
|
elif not name and not port and protocols_data:
|
|
|
|
protocol = protocols_data[0].split('/')
|
|
|
|
validated_data["protocol"] = protocol[0]
|
|
|
|
validated_data["port"] = int(protocol[1])
|
2019-07-17 04:10:14 +00:00
|
|
|
if protocols_data:
|
2019-07-05 10:07:10 +00:00
|
|
|
validated_data["protocols"] = ' '.join(protocols_data)
|
2019-06-13 10:58:43 +00:00
|
|
|
|
2019-07-05 10:07:10 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
self.compatible_with_old_protocol(validated_data)
|
2019-06-13 10:58:43 +00:00
|
|
|
instance = super().create(validated_data)
|
|
|
|
return instance
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
2019-07-05 10:07:10 +00:00
|
|
|
self.compatible_with_old_protocol(validated_data)
|
|
|
|
return super().update(instance, validated_data)
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class AssetDisplaySerializer(AssetSerializer):
|
|
|
|
connectivity = ConnectivitySerializer(read_only=True, label=_("Connectivity"))
|
|
|
|
|
|
|
|
class Meta(AssetSerializer.Meta):
|
|
|
|
fields = [
|
|
|
|
'id', 'ip', 'hostname', 'protocol', 'port',
|
|
|
|
'protocols', 'is_active', 'public_ip',
|
|
|
|
'number', 'vendor', 'model', 'sn',
|
|
|
|
'cpu_model', 'cpu_count', 'cpu_cores', 'cpu_vcpus', 'memory',
|
|
|
|
'disk_total', 'disk_info', 'os', 'os_version', 'os_arch',
|
|
|
|
'hostname_raw', 'comment', 'created_by', 'date_created',
|
|
|
|
'hardware_info', 'connectivity',
|
|
|
|
]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
""" Perform necessary eager loading of data. """
|
|
|
|
queryset = queryset\
|
|
|
|
.annotate(admin_user_username=F('admin_user__username'))
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
class PlatformSerializer(serializers.ModelSerializer):
|
|
|
|
meta = serializers.DictField(required=False, allow_null=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Platform
|
|
|
|
fields = [
|
|
|
|
'id', 'name', 'base', 'charset',
|
|
|
|
'internal', 'meta', 'comment'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class AssetDetailSerializer(AssetSerializer):
|
|
|
|
platform = PlatformSerializer(read_only=True)
|
|
|
|
|
|
|
|
|
2018-12-18 09:28:45 +00:00
|
|
|
class AssetSimpleSerializer(serializers.ModelSerializer):
|
2019-10-12 10:00:40 +00:00
|
|
|
connectivity = ConnectivitySerializer(read_only=True, label=_("Connectivity"))
|
2019-06-17 08:46:01 +00:00
|
|
|
|
2018-12-18 09:28:45 +00:00
|
|
|
class Meta:
|
|
|
|
model = Asset
|
2019-06-20 07:50:28 +00:00
|
|
|
fields = ['id', 'hostname', 'ip', 'connectivity', 'port']
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetTaskSerializer(serializers.Serializer):
|
|
|
|
ACTION_CHOICES = (
|
|
|
|
('refresh', 'refresh'),
|
|
|
|
('test', 'test'),
|
|
|
|
)
|
|
|
|
task = serializers.CharField(read_only=True)
|
|
|
|
action = serializers.ChoiceField(choices=ACTION_CHOICES, write_only=True)
|