|
|
|
@ -4,12 +4,13 @@ from rest_framework import serializers
|
|
|
|
|
from rest_framework.generics import get_object_or_404 |
|
|
|
|
from django.utils.translation import ugettext_lazy as _ |
|
|
|
|
|
|
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer |
|
|
|
|
from common.drf.serializers import SecretReadableMixin |
|
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer, OrgResourceSerializerMixin |
|
|
|
|
from common.drf.serializers import SecretReadableMixin, WritableNestedModelSerializer |
|
|
|
|
from common.drf.fields import ObjectRelatedField, EncryptedField |
|
|
|
|
from assets.const import SecretType |
|
|
|
|
from ..models import Domain, Asset, Account |
|
|
|
|
from ..serializers import HostSerializer |
|
|
|
|
from assets.models import Platform, Node |
|
|
|
|
from assets.const import SecretType, GATEWAY_NAME |
|
|
|
|
from ..serializers import AssetProtocolsSerializer |
|
|
|
|
from ..models import Domain, Asset, Account, Host |
|
|
|
|
from .utils import validate_password_for_ansible, validate_ssh_key |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -41,7 +42,7 @@ class DomainSerializer(BulkOrgResourceModelSerializer):
|
|
|
|
|
return obj.gateways.count() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GatewaySerializer(HostSerializer): |
|
|
|
|
class GatewaySerializer(BulkOrgResourceModelSerializer, WritableNestedModelSerializer): |
|
|
|
|
password = EncryptedField( |
|
|
|
|
label=_('Password'), required=False, allow_blank=True, allow_null=True, max_length=1024, |
|
|
|
|
validators=[validate_password_for_ansible], write_only=True |
|
|
|
@ -55,13 +56,27 @@ class GatewaySerializer(HostSerializer):
|
|
|
|
|
max_length=512, |
|
|
|
|
) |
|
|
|
|
username = serializers.CharField( |
|
|
|
|
label=_('Username'), allow_blank=True, max_length=128, required=True, |
|
|
|
|
label=_('Username'), allow_blank=True, max_length=128, required=True, write_only=True |
|
|
|
|
) |
|
|
|
|
username_display = serializers.SerializerMethodField(label=_('Username')) |
|
|
|
|
protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols')) |
|
|
|
|
|
|
|
|
|
class Meta(HostSerializer.Meta): |
|
|
|
|
fields = HostSerializer.Meta.fields + [ |
|
|
|
|
'username', 'password', 'private_key', 'passphrase' |
|
|
|
|
class Meta: |
|
|
|
|
model = Host |
|
|
|
|
fields_mini = ['id', 'name', 'address'] |
|
|
|
|
fields_small = fields_mini + ['is_active', 'comment'] |
|
|
|
|
fields = fields_small + ['domain', 'protocols'] + [ |
|
|
|
|
'username', 'password', 'private_key', 'passphrase', 'username_display' |
|
|
|
|
] |
|
|
|
|
extra_kwargs = { |
|
|
|
|
'name': {'label': _("Name")}, |
|
|
|
|
'address': {'label': _('Address')}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@staticmethod |
|
|
|
|
def get_username_display(obj): |
|
|
|
|
account = obj.accounts.order_by('-privileged').first() |
|
|
|
|
return account.username if account else '' |
|
|
|
|
|
|
|
|
|
def validate_private_key(self, secret): |
|
|
|
|
if not secret: |
|
|
|
@ -79,6 +94,15 @@ class GatewaySerializer(HostSerializer):
|
|
|
|
|
validated_data.pop('passphrase', None) |
|
|
|
|
return username, password, private_key |
|
|
|
|
|
|
|
|
|
@staticmethod |
|
|
|
|
def generate_default_data(): |
|
|
|
|
platform = Platform.objects.get(name=GATEWAY_NAME, internal=True) |
|
|
|
|
# node = Node.objects.all().order_by('date_created').first() |
|
|
|
|
data = { |
|
|
|
|
'platform': platform, |
|
|
|
|
} |
|
|
|
|
return data |
|
|
|
|
|
|
|
|
|
@staticmethod |
|
|
|
|
def create_accounts(instance, username, password, private_key): |
|
|
|
|
account_name = f'{instance.name}-{_("Gateway")}' |
|
|
|
@ -112,6 +136,7 @@ class GatewaySerializer(HostSerializer):
|
|
|
|
|
|
|
|
|
|
def create(self, validated_data): |
|
|
|
|
auth_fields = self.clean_auth_fields(validated_data) |
|
|
|
|
validated_data.update(self.generate_default_data()) |
|
|
|
|
instance = super().create(validated_data) |
|
|
|
|
self.create_accounts(instance, *auth_fields) |
|
|
|
|
return instance |
|
|
|
|