2019-03-18 02:15:33 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
from common.serializers import AdaptedBulkListSerializer
|
2019-06-19 08:45:14 +00:00
|
|
|
from orgs.mixins import BulkOrgResourceModelSerializer
|
2019-06-24 12:16:18 +00:00
|
|
|
from ..models import AuthBook, Asset
|
|
|
|
from ..backends import AssetUserManager
|
2019-07-08 07:35:20 +00:00
|
|
|
from .base import ConnectivitySerializer, AuthSerializerMixin
|
2019-06-19 03:31:38 +00:00
|
|
|
|
2019-03-18 02:15:33 +00:00
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'AssetUserSerializer', 'AssetUserAuthInfoSerializer',
|
2019-06-19 03:31:38 +00:00
|
|
|
'AssetUserExportSerializer', 'AssetUserPushSerializer',
|
2019-03-18 02:15:33 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
class BasicAssetSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
fields = ['hostname', 'ip']
|
|
|
|
|
|
|
|
|
2019-07-08 07:35:20 +00:00
|
|
|
class AssetUserSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer):
|
2019-06-19 03:31:38 +00:00
|
|
|
hostname = serializers.CharField(read_only=True, label=_("Hostname"))
|
|
|
|
ip = serializers.CharField(read_only=True, label=_("IP"))
|
2019-06-24 12:16:18 +00:00
|
|
|
connectivity = ConnectivitySerializer(read_only=True, label=_("Connectivity"))
|
2019-03-18 02:15:33 +00:00
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
backend = serializers.CharField(read_only=True, label=_("Backend"))
|
2019-03-18 02:15:33 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AuthBook
|
2019-06-19 03:31:38 +00:00
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
2019-03-18 02:15:33 +00:00
|
|
|
read_only_fields = (
|
|
|
|
'date_created', 'date_updated', 'created_by',
|
|
|
|
'is_latest', 'version', 'connectivity',
|
|
|
|
)
|
2019-06-19 03:31:38 +00:00
|
|
|
fields = [
|
|
|
|
"id", "hostname", "ip", "username", "password", "asset", "version",
|
2019-06-19 08:45:14 +00:00
|
|
|
"is_latest", "connectivity", "backend",
|
2019-06-19 03:31:38 +00:00
|
|
|
"date_created", "date_updated", "private_key", "public_key",
|
|
|
|
]
|
2019-03-18 02:15:33 +00:00
|
|
|
extra_kwargs = {
|
2019-06-19 03:31:38 +00:00
|
|
|
'username': {'required': True},
|
2019-06-25 06:32:25 +00:00
|
|
|
'password': {'write_only': True},
|
|
|
|
'private_key': {'write_only': True},
|
|
|
|
'public_key': {'write_only': True},
|
2019-03-18 02:15:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
2019-06-25 06:32:25 +00:00
|
|
|
if not validated_data.get("name") and validated_data.get("username"):
|
|
|
|
validated_data["name"] = validated_data["username"]
|
|
|
|
instance = AssetUserManager.create(**validated_data)
|
2019-03-18 02:15:33 +00:00
|
|
|
return instance
|
|
|
|
|
|
|
|
|
2019-06-19 03:31:38 +00:00
|
|
|
class AssetUserExportSerializer(AssetUserSerializer):
|
|
|
|
password = serializers.CharField(
|
|
|
|
max_length=256, allow_blank=True, allow_null=True,
|
|
|
|
required=False, label=_('Password')
|
|
|
|
)
|
|
|
|
public_key = serializers.CharField(
|
|
|
|
max_length=4096, allow_blank=True, allow_null=True,
|
|
|
|
required=False, label=_('Public key')
|
|
|
|
)
|
|
|
|
private_key = serializers.CharField(
|
|
|
|
max_length=4096, allow_blank=True, allow_null=True,
|
|
|
|
required=False, label=_('Private key')
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-03-18 02:15:33 +00:00
|
|
|
class AssetUserAuthInfoSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = AuthBook
|
|
|
|
fields = ['password', 'private_key', 'public_key']
|
2019-06-19 03:31:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetUserPushSerializer(serializers.Serializer):
|
|
|
|
asset = serializers.PrimaryKeyRelatedField(queryset=Asset.objects.all(), label=_("Asset"))
|
|
|
|
username = serializers.CharField(max_length=1024)
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
pass
|