2021-07-08 06:23:18 +00:00
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
|
|
from assets.models import AuthBook
|
|
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
|
|
|
|
|
|
|
|
|
from .base import AuthSerializerMixin
|
2022-02-15 02:46:16 +00:00
|
|
|
|
from common.utils.encode import ssh_pubkey_gen
|
2022-05-17 13:12:59 +00:00
|
|
|
|
from common.drf.serializers import SecretReadableMixin
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AccountSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer):
|
|
|
|
|
ip = serializers.ReadOnlyField(label=_("IP"))
|
|
|
|
|
hostname = serializers.ReadOnlyField(label=_("Hostname"))
|
2022-01-17 07:58:39 +00:00
|
|
|
|
platform = serializers.ReadOnlyField(label=_("Platform"))
|
|
|
|
|
protocols = serializers.SerializerMethodField(label=_("Protocols"))
|
|
|
|
|
date_created = serializers.DateTimeField(
|
|
|
|
|
label=_('Date created'), format="%Y/%m/%d %H:%M:%S", read_only=True
|
|
|
|
|
)
|
|
|
|
|
date_updated = serializers.DateTimeField(
|
|
|
|
|
label=_('Date updated'), format="%Y/%m/%d %H:%M:%S", read_only=True
|
|
|
|
|
)
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = AuthBook
|
2022-01-17 07:58:39 +00:00
|
|
|
|
fields_mini = ['id', 'username', 'ip', 'hostname', 'platform', 'protocols', 'version']
|
2021-12-30 02:47:46 +00:00
|
|
|
|
fields_write_only = ['password', 'private_key', "public_key", 'passphrase']
|
2021-07-08 06:23:18 +00:00
|
|
|
|
fields_other = ['date_created', 'date_updated', 'connectivity', 'date_verified', 'comment']
|
|
|
|
|
fields_small = fields_mini + fields_write_only + fields_other
|
2021-07-12 10:20:32 +00:00
|
|
|
|
fields_fk = ['asset', 'systemuser', 'systemuser_display']
|
2021-07-08 06:23:18 +00:00
|
|
|
|
fields = fields_small + fields_fk
|
|
|
|
|
extra_kwargs = {
|
|
|
|
|
'username': {'required': True},
|
|
|
|
|
'private_key': {'write_only': True},
|
|
|
|
|
'public_key': {'write_only': True},
|
2021-07-27 08:06:00 +00:00
|
|
|
|
'systemuser_display': {'label': _('System user display')}
|
2021-07-08 06:23:18 +00:00
|
|
|
|
}
|
2021-07-26 02:54:57 +00:00
|
|
|
|
ref_name = 'AssetAccountSerializer'
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
2022-02-15 02:46:16 +00:00
|
|
|
|
def _validate_gen_key(self, attrs):
|
|
|
|
|
private_key = attrs.get('private_key')
|
|
|
|
|
if not private_key:
|
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
|
|
password = attrs.get('passphrase')
|
|
|
|
|
username = attrs.get('username')
|
|
|
|
|
public_key = ssh_pubkey_gen(private_key, password=password, username=username)
|
|
|
|
|
attrs['public_key'] = public_key
|
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
|
attrs = self._validate_gen_key(attrs)
|
|
|
|
|
return attrs
|
|
|
|
|
|
2022-01-17 07:58:39 +00:00
|
|
|
|
def get_protocols(self, v):
|
2022-06-22 09:06:53 +00:00
|
|
|
|
""" protocols 是 queryset 中返回的,Post 创建成功后返回序列化时没有这个字段 """
|
|
|
|
|
if hasattr(v, 'protocols'):
|
|
|
|
|
protocols = v.protocols
|
|
|
|
|
elif hasattr(v, 'asset') and v.asset:
|
|
|
|
|
protocols = v.asset.protocols
|
|
|
|
|
else:
|
|
|
|
|
protocols = ''
|
|
|
|
|
protocols = protocols.replace(' ', ', ')
|
|
|
|
|
return protocols
|
2022-01-17 07:58:39 +00:00
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
@classmethod
|
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
|
""" Perform necessary eager loading of data. """
|
|
|
|
|
queryset = queryset.prefetch_related('systemuser', 'asset')
|
|
|
|
|
return queryset
|
|
|
|
|
|
2021-07-15 11:11:53 +00:00
|
|
|
|
def to_representation(self, instance):
|
|
|
|
|
instance.load_auth()
|
|
|
|
|
return super().to_representation(instance)
|
|
|
|
|
|
2021-07-08 06:23:18 +00:00
|
|
|
|
|
2022-05-17 13:12:59 +00:00
|
|
|
|
class AccountSecretSerializer(SecretReadableMixin, AccountSerializer):
|
2021-07-08 06:23:18 +00:00
|
|
|
|
class Meta(AccountSerializer.Meta):
|
2022-01-17 07:58:39 +00:00
|
|
|
|
fields_backup = [
|
|
|
|
|
'hostname', 'ip', 'platform', 'protocols', 'username', 'password',
|
|
|
|
|
'private_key', 'public_key', 'date_created', 'date_updated', 'version'
|
|
|
|
|
]
|
2021-07-08 06:23:18 +00:00
|
|
|
|
extra_kwargs = {
|
|
|
|
|
'password': {'write_only': False},
|
|
|
|
|
'private_key': {'write_only': False},
|
|
|
|
|
'public_key': {'write_only': False},
|
2021-08-19 09:24:31 +00:00
|
|
|
|
'systemuser_display': {'label': _('System user display')}
|
2021-07-08 06:23:18 +00:00
|
|
|
|
}
|
2021-07-13 04:18:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AccountTaskSerializer(serializers.Serializer):
|
|
|
|
|
ACTION_CHOICES = (
|
|
|
|
|
('test', 'test'),
|
|
|
|
|
)
|
|
|
|
|
action = serializers.ChoiceField(choices=ACTION_CHOICES, write_only=True)
|
|
|
|
|
task = serializers.CharField(read_only=True)
|