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 _
|
|
|
|
|
|
|
|
from orgs.mixins import OrgResourceSerializerMixin
|
2018-02-07 15:25:15 +00:00
|
|
|
from common.mixins import BulkSerializerMixin
|
2019-04-25 02:11:50 +00:00
|
|
|
from common.serializers import AdaptedBulkListSerializer
|
2018-09-07 04:40:26 +00:00
|
|
|
from ..models import Asset
|
2018-02-07 15:25:15 +00:00
|
|
|
from .system_user import AssetSystemUserSerializer
|
|
|
|
|
2018-04-24 03:07:09 +00:00
|
|
|
__all__ = [
|
|
|
|
'AssetSerializer', 'AssetGrantedSerializer', 'MyAssetGrantedSerializer',
|
2018-12-18 09:28:45 +00:00
|
|
|
'AssetAsNodeSerializer', 'AssetSimpleSerializer',
|
2018-04-24 03:07:09 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-05-21 08:24:01 +00:00
|
|
|
class AssetSerializer(BulkSerializerMixin, serializers.ModelSerializer, OrgResourceSerializerMixin):
|
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
|
|
|
# validators = [] # 解决批量导入时unique_together字段校验失败
|
|
|
|
fields = [
|
|
|
|
'id', 'org_id', 'org_name', 'ip', 'hostname', 'protocol', 'port',
|
|
|
|
'platform', 'is_active', 'public_ip', 'domain', 'admin_user',
|
|
|
|
'nodes', 'labels', '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'
|
|
|
|
]
|
|
|
|
read_only_fields = (
|
|
|
|
'number', 'vendor', 'model', 'sn', 'cpu_model', 'cpu_count',
|
|
|
|
'cpu_cores', 'cpu_vcpus', 'memory', 'disk_total', 'disk_info',
|
|
|
|
'os', 'os_version', 'os_arch', 'hostname_raw',
|
|
|
|
'created_by', 'date_created',
|
|
|
|
)
|
|
|
|
extra_kwargs = {
|
|
|
|
'hardware_info': {'label': _('Hardware info')},
|
|
|
|
'connectivity': {'label': _('Connectivity')},
|
|
|
|
'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. """
|
|
|
|
queryset = queryset.prefetch_related('labels', 'nodes')\
|
|
|
|
.select_related('admin_user')
|
|
|
|
return queryset
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
def get_field_names(self, declared_fields, info):
|
|
|
|
fields = super().get_field_names(declared_fields, info)
|
|
|
|
fields.extend([
|
2018-12-18 09:28:45 +00:00
|
|
|
'hardware_info', 'connectivity', 'org_name'
|
2018-02-07 15:25:15 +00:00
|
|
|
])
|
|
|
|
return fields
|
|
|
|
|
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
class AssetAsNodeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
fields = ['id', 'hostname', 'ip', 'port', 'platform', 'protocol']
|
|
|
|
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
class AssetGrantedSerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
被授权资产的数据结构
|
|
|
|
"""
|
|
|
|
system_users_granted = AssetSystemUserSerializer(many=True, read_only=True)
|
|
|
|
system_users_join = serializers.SerializerMethodField()
|
2018-05-28 05:20:26 +00:00
|
|
|
# nodes = NodeTMPSerializer(many=True, read_only=True)
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
fields = (
|
|
|
|
"id", "hostname", "ip", "port", "system_users_granted",
|
2018-05-28 05:20:26 +00:00
|
|
|
"is_active", "system_users_join", "os", 'domain',
|
2018-08-01 05:06:50 +00:00
|
|
|
"platform", "comment", "protocol", "org_id", "org_name",
|
2018-02-07 15:25:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_system_users_join(obj):
|
|
|
|
system_users = [s.username for s in obj.system_users_granted]
|
|
|
|
return ', '.join(system_users)
|
|
|
|
|
|
|
|
|
|
|
|
class MyAssetGrantedSerializer(AssetGrantedSerializer):
|
|
|
|
"""
|
|
|
|
普通用户获取授权的资产定义的数据结构
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
fields = (
|
|
|
|
"id", "hostname", "system_users_granted",
|
2018-08-01 05:06:50 +00:00
|
|
|
"is_active", "system_users_join", "org_name",
|
|
|
|
"os", "platform", "comment", "org_id", "protocol"
|
2018-02-07 15:25:15 +00:00
|
|
|
)
|
2018-12-18 09:28:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetSimpleSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
fields = ['id', 'hostname', 'port', 'ip', 'connectivity']
|