2019-07-01 11:30:18 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
from rest_framework import serializers
|
2019-07-11 10:12:14 +00:00
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2019-07-01 11:30:18 +00:00
|
|
|
|
|
2021-04-07 10:20:54 +00:00
|
|
|
|
from assets.models import Node, SystemUser, Asset, Platform
|
2019-07-11 10:12:14 +00:00
|
|
|
|
from assets.serializers import ProtocolsField
|
2020-10-22 10:13:14 +00:00
|
|
|
|
from perms.serializers.asset.permission import ActionsField
|
2019-07-01 11:30:18 +00:00
|
|
|
|
|
|
|
|
|
__all__ = [
|
2019-08-28 03:43:55 +00:00
|
|
|
|
'NodeGrantedSerializer',
|
|
|
|
|
'AssetGrantedSerializer',
|
2019-07-11 10:12:14 +00:00
|
|
|
|
'ActionsSerializer', 'AssetSystemUserSerializer',
|
2019-09-12 10:25:22 +00:00
|
|
|
|
'RemoteAppSystemUserSerializer',
|
2019-12-19 07:28:17 +00:00
|
|
|
|
'DatabaseAppSystemUserSerializer',
|
2020-08-07 03:52:34 +00:00
|
|
|
|
'K8sAppSystemUserSerializer',
|
2019-07-01 11:30:18 +00:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AssetSystemUserSerializer(serializers.ModelSerializer):
|
|
|
|
|
"""
|
|
|
|
|
查看授权的资产系统用户的数据结构,这个和AssetSerializer不同,字段少
|
|
|
|
|
"""
|
2019-07-02 09:51:50 +00:00
|
|
|
|
actions = ActionsField(read_only=True)
|
2019-07-01 11:30:18 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
2019-07-11 10:12:14 +00:00
|
|
|
|
only_fields = (
|
2019-09-12 10:25:22 +00:00
|
|
|
|
'id', 'name', 'username', 'priority', 'protocol', 'login_mode',
|
2020-03-12 08:24:38 +00:00
|
|
|
|
'sftp_root', 'username_same_with_user',
|
2019-07-01 11:30:18 +00:00
|
|
|
|
)
|
2019-07-11 10:12:14 +00:00
|
|
|
|
fields = list(only_fields) + ["actions"]
|
|
|
|
|
read_only_fields = fields
|
2019-07-01 11:30:18 +00:00
|
|
|
|
|
|
|
|
|
|
2020-10-22 10:13:14 +00:00
|
|
|
|
class AssetGrantedSerializer(serializers.ModelSerializer):
|
2020-10-22 09:05:47 +00:00
|
|
|
|
"""
|
2020-10-22 10:13:14 +00:00
|
|
|
|
被授权资产的数据结构
|
2020-10-22 09:05:47 +00:00
|
|
|
|
"""
|
2020-10-22 10:13:14 +00:00
|
|
|
|
protocols = ProtocolsField(label=_('Protocols'), required=False, read_only=True)
|
2021-04-07 10:20:54 +00:00
|
|
|
|
platform = serializers.SlugRelatedField(
|
|
|
|
|
slug_field='name', queryset=Platform.objects.all(), label=_("Platform")
|
|
|
|
|
)
|
2020-10-22 10:13:14 +00:00
|
|
|
|
|
2020-10-22 09:05:47 +00:00
|
|
|
|
class Meta:
|
2020-10-22 10:13:14 +00:00
|
|
|
|
model = Asset
|
|
|
|
|
only_fields = [
|
|
|
|
|
"id", "hostname", "ip", "protocols", "os", 'domain',
|
|
|
|
|
"platform", "comment", "org_id", "is_active"
|
|
|
|
|
]
|
|
|
|
|
fields = only_fields + ['org_name']
|
2020-10-22 09:05:47 +00:00
|
|
|
|
read_only_fields = fields
|
|
|
|
|
|
|
|
|
|
|
2020-10-22 10:13:14 +00:00
|
|
|
|
class NodeGrantedSerializer(serializers.ModelSerializer):
|
|
|
|
|
class Meta:
|
|
|
|
|
model = Node
|
|
|
|
|
fields = [
|
|
|
|
|
'id', 'name', 'key', 'value', 'org_id', "assets_amount"
|
|
|
|
|
]
|
|
|
|
|
read_only_fields = fields
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ActionsSerializer(serializers.Serializer):
|
|
|
|
|
actions = ActionsField(read_only=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: 删除
|
2019-09-12 10:25:22 +00:00
|
|
|
|
class RemoteAppSystemUserSerializer(serializers.ModelSerializer):
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
|
|
|
|
only_fields = (
|
|
|
|
|
'id', 'name', 'username', 'priority', 'protocol', 'login_mode',
|
|
|
|
|
)
|
|
|
|
|
fields = list(only_fields)
|
|
|
|
|
read_only_fields = fields
|
|
|
|
|
|
|
|
|
|
|
2019-12-19 07:28:17 +00:00
|
|
|
|
class DatabaseAppSystemUserSerializer(serializers.ModelSerializer):
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
|
|
|
|
only_fields = (
|
|
|
|
|
'id', 'name', 'username', 'priority', 'protocol', 'login_mode',
|
|
|
|
|
)
|
|
|
|
|
fields = list(only_fields)
|
|
|
|
|
read_only_fields = fields
|
|
|
|
|
|
|
|
|
|
|
2020-08-07 03:52:34 +00:00
|
|
|
|
class K8sAppSystemUserSerializer(serializers.ModelSerializer):
|
|
|
|
|
class Meta:
|
|
|
|
|
model = SystemUser
|
|
|
|
|
only_fields = (
|
|
|
|
|
'id', 'name', 'username', 'priority', 'protocol', 'login_mode',
|
|
|
|
|
)
|
|
|
|
|
fields = list(only_fields)
|
|
|
|
|
read_only_fields = fields
|
|
|
|
|
|