2018-02-07 15:25:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django.core.cache import cache
|
2019-05-21 08:24:01 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-02-07 15:25:15 +00:00
|
|
|
from rest_framework import serializers
|
2018-03-27 10:34:41 +00:00
|
|
|
|
2019-04-25 02:11:50 +00:00
|
|
|
from common.serializers import AdaptedBulkListSerializer
|
|
|
|
|
2018-02-09 07:24:44 +00:00
|
|
|
from ..models import Node, AdminUser
|
2018-02-07 15:25:15 +00:00
|
|
|
from ..const import ADMIN_USER_CONN_CACHE_KEY
|
2019-06-19 08:45:14 +00:00
|
|
|
from orgs.mixins import BulkOrgResourceModelSerializer
|
2018-02-07 15:25:15 +00:00
|
|
|
|
2018-03-27 10:34:41 +00:00
|
|
|
from .base import AuthSerializer
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
|
2019-06-19 08:45:14 +00:00
|
|
|
class AdminUserSerializer(BulkOrgResourceModelSerializer):
|
2018-02-07 15:25:15 +00:00
|
|
|
"""
|
|
|
|
管理用户
|
|
|
|
"""
|
2019-05-21 08:24:01 +00:00
|
|
|
password = serializers.CharField(
|
|
|
|
required=False, write_only=True, label=_('Password')
|
|
|
|
)
|
|
|
|
unreachable_amount = serializers.SerializerMethodField(label=_('Unreachable'))
|
|
|
|
assets_amount = serializers.SerializerMethodField(label=_('Asset'))
|
|
|
|
reachable_amount = serializers.SerializerMethodField(label=_('Reachable'))
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
class Meta:
|
2019-04-25 02:11:50 +00:00
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
2018-02-07 15:25:15 +00:00
|
|
|
model = AdminUser
|
2019-05-21 08:24:01 +00:00
|
|
|
fields = [
|
2019-06-19 08:45:14 +00:00
|
|
|
'id', 'name', 'username', 'assets_amount',
|
2019-05-21 08:24:01 +00:00
|
|
|
'reachable_amount', 'unreachable_amount', 'password', 'comment',
|
|
|
|
'date_created', 'date_updated', 'become', 'become_method',
|
|
|
|
'become_user', 'created_by',
|
|
|
|
]
|
|
|
|
|
|
|
|
extra_kwargs = {
|
|
|
|
'date_created': {'label': _('Date created')},
|
|
|
|
'date_updated': {'label': _('Date updated')},
|
|
|
|
'become': {'read_only': True}, 'become_method': {'read_only': True},
|
|
|
|
'become_user': {'read_only': True}, 'created_by': {'read_only': True}
|
|
|
|
}
|
2018-02-07 15:25:15 +00:00
|
|
|
|
2018-03-27 10:34:41 +00:00
|
|
|
def get_field_names(self, declared_fields, info):
|
|
|
|
fields = super().get_field_names(declared_fields, info)
|
|
|
|
return [f for f in fields if not f.startswith('_')]
|
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_unreachable_amount(obj):
|
|
|
|
data = cache.get(ADMIN_USER_CONN_CACHE_KEY.format(obj.name))
|
|
|
|
if data:
|
|
|
|
return len(data.get('dark'))
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_reachable_amount(obj):
|
|
|
|
data = cache.get(ADMIN_USER_CONN_CACHE_KEY.format(obj.name))
|
|
|
|
if data:
|
|
|
|
return len(data.get('contacted'))
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_assets_amount(obj):
|
|
|
|
return obj.assets_amount
|
|
|
|
|
|
|
|
|
2018-03-27 10:34:41 +00:00
|
|
|
class AdminUserAuthSerializer(AuthSerializer):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AdminUser
|
|
|
|
fields = ['password', 'private_key']
|
|
|
|
|
|
|
|
|
2018-02-09 07:24:44 +00:00
|
|
|
class ReplaceNodeAdminUserSerializer(serializers.ModelSerializer):
|
2018-02-07 15:25:15 +00:00
|
|
|
"""
|
|
|
|
管理用户更新关联到的集群
|
|
|
|
"""
|
2018-02-09 07:24:44 +00:00
|
|
|
nodes = serializers.PrimaryKeyRelatedField(
|
2019-01-15 02:23:30 +00:00
|
|
|
many=True, queryset=Node.objects.all()
|
2018-02-07 15:25:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AdminUser
|
2018-02-09 07:24:44 +00:00
|
|
|
fields = ['id', 'nodes']
|
2018-03-27 10:34:41 +00:00
|
|
|
|
|
|
|
|
2019-01-15 02:23:30 +00:00
|
|
|
class TaskIDSerializer(serializers.Serializer):
|
|
|
|
task = serializers.CharField(read_only=True)
|