jumpserver/apps/assets/serializers.py

273 lines
7.7 KiB
Python
Raw Normal View History

2016-10-22 12:21:36 +00:00
# -*- coding: utf-8 -*-
from django.core.cache import cache
2017-12-18 10:38:30 +00:00
from rest_framework import serializers
2017-12-12 04:19:45 +00:00
from rest_framework_bulk.serializers import BulkListSerializer
2016-10-22 12:21:36 +00:00
2017-12-12 04:19:45 +00:00
from common.mixins import BulkSerializerMixin
2017-12-10 16:29:25 +00:00
from .models import AssetGroup, Asset, Cluster, AdminUser, SystemUser
2017-12-15 07:50:15 +00:00
from .const import ADMIN_USER_CONN_CACHE_KEY, SYSTEM_USER_CONN_CACHE_KEY
2017-12-10 16:29:25 +00:00
2016-11-01 09:21:16 +00:00
2017-01-06 12:34:24 +00:00
class AssetGroupSerializer(BulkSerializerMixin, serializers.ModelSerializer):
2017-12-18 10:38:30 +00:00
"""
资产组序列化数据模型
"""
2016-11-10 14:06:23 +00:00
assets_amount = serializers.SerializerMethodField()
2017-01-07 14:34:12 +00:00
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
2016-11-10 14:06:23 +00:00
2016-11-06 13:29:04 +00:00
class Meta:
model = AssetGroup
2017-01-07 14:34:12 +00:00
list_serializer_class = BulkListSerializer
fields = ['id', 'name', 'comment', 'assets_amount', 'assets']
2016-11-06 13:29:04 +00:00
2016-11-10 14:06:23 +00:00
@staticmethod
def get_assets_amount(obj):
return obj.assets.count()
2016-12-29 11:17:00 +00:00
2016-12-15 11:55:15 +00:00
class AssetUpdateSystemUserSerializer(serializers.ModelSerializer):
2017-12-18 10:38:30 +00:00
"""
资产更新其系统用户请求的数据结构定义
"""
2016-12-15 11:55:15 +00:00
system_users = serializers.PrimaryKeyRelatedField(many=True, queryset=SystemUser.objects.all())
class Meta:
model = Asset
fields = ['id', 'system_users']
2016-11-06 13:29:04 +00:00
2016-12-29 11:17:00 +00:00
class GroupUpdateAssetsSerializer(serializers.ModelSerializer):
2017-12-18 10:38:30 +00:00
"""
资产组更新需要的数据结构
"""
2017-01-07 14:34:12 +00:00
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
class Meta:
model = AssetGroup
fields = ['id', 'assets']
2017-01-06 12:34:24 +00:00
2017-12-07 08:25:50 +00:00
class ClusterUpdateAssetsSerializer(serializers.ModelSerializer):
2017-12-18 10:38:30 +00:00
"""
集群更新资产数据结构
"""
2017-01-07 14:34:12 +00:00
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
class Meta:
2017-12-07 08:25:50 +00:00
model = Cluster
2017-01-07 14:34:12 +00:00
fields = ['id', 'assets']
2017-01-06 12:34:24 +00:00
2016-11-06 13:29:04 +00:00
class AdminUserSerializer(serializers.ModelSerializer):
2017-12-18 10:38:30 +00:00
"""
管理用户
"""
assets_amount = serializers.SerializerMethodField()
2017-12-10 16:29:25 +00:00
unreachable_amount = serializers.SerializerMethodField()
2016-11-06 13:29:04 +00:00
class Meta:
model = AdminUser
fields = '__all__'
2016-11-06 13:29:04 +00:00
2017-12-10 16:29:25 +00:00
@staticmethod
def get_unreachable_amount(obj):
2017-12-15 07:50:15 +00:00
data = cache.get(ADMIN_USER_CONN_CACHE_KEY.format(obj.name))
2017-12-10 16:29:25 +00:00
if data:
return len(data.get('dark'))
else:
return 'Unknown'
@staticmethod
def get_assets_amount(obj):
2017-12-19 04:41:00 +00:00
return obj.assets_amount
2016-11-06 14:45:26 +00:00
2016-11-06 13:29:04 +00:00
class SystemUserSerializer(serializers.ModelSerializer):
2017-12-18 10:38:30 +00:00
"""
系统用户
"""
2017-12-10 16:29:25 +00:00
unreachable_amount = serializers.SerializerMethodField()
2017-12-19 04:41:00 +00:00
reachable_amount = serializers.SerializerMethodField()
unreachable_assets = serializers.SerializerMethodField()
reachable_assets = serializers.SerializerMethodField()
assets_amount = serializers.SerializerMethodField()
2017-12-10 16:29:25 +00:00
2016-11-06 13:29:04 +00:00
class Meta:
model = SystemUser
2016-11-10 14:06:23 +00:00
exclude = ('_password', '_private_key', '_public_key')
2016-11-06 13:29:04 +00:00
2017-12-10 16:29:25 +00:00
@staticmethod
2017-12-19 04:41:00 +00:00
def get_unreachable_assets(obj):
return obj.unreachable_assets
@staticmethod
def get_reachable_assets(obj):
return obj.reachable_assets
def get_unreachable_amount(self, obj):
return len(self.get_unreachable_assets(obj))
def get_reachable_amount(self, obj):
return len(self.get_reachable_assets(obj))
2017-12-10 16:29:25 +00:00
@staticmethod
def get_assets_amount(obj):
amount = 0
for cluster in obj.cluster.all():
amount += cluster.assets.all().count()
return amount
2016-11-06 14:45:26 +00:00
2016-11-06 13:29:04 +00:00
class AdminUserUpdateClusterSerializer(serializers.ModelSerializer):
2017-12-18 10:38:30 +00:00
"""
管理用户更新关联到的集群
2017-12-18 10:38:30 +00:00
"""
clusters = serializers.PrimaryKeyRelatedField(many=True, queryset=Cluster.objects.all())
2017-01-07 14:34:12 +00:00
class Meta:
model = AdminUser
fields = ['id', 'clusters']
2017-01-06 12:34:24 +00:00
class AssetSystemUserSerializer(serializers.ModelSerializer):
2017-12-18 10:38:30 +00:00
"""
查看授权的资产系统用户的数据结构这个和AssetSerializer不同字段少
2017-12-18 10:38:30 +00:00
"""
2017-01-07 14:34:12 +00:00
class Meta:
model = SystemUser
fields = ('id', 'name', 'username', 'priority', 'protocol', 'comment',)
2017-01-07 14:34:12 +00:00
2016-12-29 11:17:00 +00:00
class SystemUserSimpleSerializer(serializers.ModelSerializer):
2017-12-18 10:38:30 +00:00
"""
系统用户最基本信息的数据结构
"""
2016-12-29 11:17:00 +00:00
class Meta:
model = SystemUser
fields = ('id', 'name', 'username')
2016-11-10 14:06:23 +00:00
class AssetSerializer(BulkSerializerMixin, serializers.ModelSerializer):
2017-12-18 10:38:30 +00:00
"""
资产的数据结构
"""
2017-12-28 10:01:15 +00:00
cluster_name = serializers.SerializerMethodField()
2016-11-10 14:06:23 +00:00
class Meta(object):
model = Asset
list_serializer_class = BulkListSerializer
fields = '__all__'
2017-12-12 04:19:45 +00:00
validators = [] # If not set to [], partial bulk update will be error
2016-11-27 06:37:50 +00:00
def get_field_names(self, declared_fields, info):
2017-12-12 04:19:45 +00:00
fields = super().get_field_names(declared_fields, info)
fields.extend([
'get_type_display', 'get_env_display',
'hardware_info', 'is_connective',
])
2016-11-27 06:37:50 +00:00
return fields
2017-12-28 10:01:15 +00:00
@staticmethod
def get_cluster_name(obj):
return obj.cluster.name
2016-11-10 14:06:23 +00:00
class AssetGrantedSerializer(serializers.ModelSerializer):
2017-12-18 10:38:30 +00:00
"""
被授权资产的数据结构
"""
2017-01-17 08:34:47 +00:00
system_users_granted = AssetSystemUserSerializer(many=True, read_only=True)
2016-11-10 14:06:23 +00:00
is_inherited = serializers.SerializerMethodField()
system_users_join = serializers.SerializerMethodField()
class Meta(object):
model = Asset
fields = ("id", "hostname", "ip", "port", "system_users_granted",
"is_inherited", "is_active", "system_users_join",
"platform", "comment",)
2016-11-10 14:06:23 +00:00
@staticmethod
def get_is_inherited(obj):
if getattr(obj, 'inherited', ''):
return True
else:
return False
@staticmethod
def get_system_users_join(obj):
2017-12-10 16:29:25 +00:00
return ', '.join([system_user.username for system_user in obj.system_users_granted])
class MyAssetGrantedSerializer(AssetGrantedSerializer):
2017-12-18 10:38:30 +00:00
"""
普通用户获取授权的资产定义的数据结构
"""
class Meta(object):
model = Asset
fields = ("id", "hostname", "system_users_granted", "is_inherited",
"is_active", "system_users_join", "comment")
2016-11-10 14:06:23 +00:00
2017-12-07 08:25:50 +00:00
class ClusterSerializer(BulkSerializerMixin, serializers.ModelSerializer):
2017-12-18 10:38:30 +00:00
"""
cluster
"""
2016-11-06 13:29:04 +00:00
assets_amount = serializers.SerializerMethodField()
admin_user_name = serializers.SerializerMethodField()
2017-01-07 14:34:12 +00:00
assets = serializers.PrimaryKeyRelatedField(many=True, queryset=Asset.objects.all())
2017-12-31 04:20:08 +00:00
system_users = serializers.SerializerMethodField()
2016-11-06 13:29:04 +00:00
class Meta:
2017-12-07 08:25:50 +00:00
model = Cluster
fields = '__all__'
2016-11-06 13:29:04 +00:00
@staticmethod
def get_assets_amount(obj):
return obj.assets.count()
@staticmethod
def get_admin_user_name(obj):
2017-12-13 09:21:08 +00:00
try:
return obj.admin_user.name
except AttributeError:
return ''
2016-11-06 13:29:04 +00:00
2017-12-31 04:20:08 +00:00
@staticmethod
def get_system_users(obj):
return ', '.join(obj.name for obj in obj.systemuser_set.all())
2017-11-14 01:44:16 +00:00
class AssetGroupGrantedSerializer(BulkSerializerMixin, serializers.ModelSerializer):
2017-12-18 10:38:30 +00:00
"""
授权资产组
"""
2017-11-14 01:44:16 +00:00
assets_granted = AssetGrantedSerializer(many=True, read_only=True)
assets_amount = serializers.SerializerMethodField()
class Meta:
model = AssetGroup
list_serializer_class = BulkListSerializer
fields = '__all__'
@staticmethod
def get_assets_amount(obj):
return len(obj.assets_granted)
class MyAssetGroupGrantedSerializer(serializers.ModelSerializer):
2017-12-18 10:38:30 +00:00
"""
普通用户授权资产组结构
"""
assets_granted = MyAssetGrantedSerializer(many=True, read_only=True)
assets_amount = serializers.SerializerMethodField()
class Meta:
model = AssetGroup
list_serializer_class = BulkListSerializer
fields = '__all__'
@staticmethod
def get_assets_amount(obj):
return len(obj.assets_granted)