jumpserver/apps/terminal/serializers/applet_host.py

111 lines
3.7 KiB
Python
Raw Normal View History

2022-11-01 12:37:04 +00:00
from rest_framework import serializers
from django.utils.translation import gettext_lazy as _
from common.validators import ProjectUniqueValidator
2022-11-07 12:41:18 +00:00
from common.drf.fields import ObjectRelatedField, LabeledChoiceField
2022-11-03 08:55:38 +00:00
from assets.models import Platform, Account
2022-11-01 12:37:04 +00:00
from assets.serializers import HostSerializer
from ..models import AppletHost, AppletHostDeployment, Applet
from .applet import AppletSerializer
2022-11-07 12:41:18 +00:00
from .. import const
2022-11-01 12:37:04 +00:00
__all__ = [
'AppletHostSerializer', 'AppletHostDeploymentSerializer',
2022-11-07 12:41:18 +00:00
'AppletHostAccountSerializer', 'AppletHostAppletReportSerializer',
'AppletHostStartupSerializer',
2022-11-01 12:37:04 +00:00
]
class DeployOptionsSerializer(serializers.Serializer):
LICENSE_MODE_CHOICES = (
(4, _('Per Session')),
(2, _('Per Device')),
)
SESSION_PER_USER = (
(1, _("Disabled")),
(0, _("Enabled")),
)
RDS_Licensing = serializers.BooleanField(default=False, label=_("RDS Licensing"))
RDS_LicenseServer = serializers.CharField(default='127.0.0.1', label=_('RDS License Server'), max_length=1024)
RDS_LicensingMode = serializers.ChoiceField(choices=LICENSE_MODE_CHOICES, default=4, label=_('RDS Licensing Mode'))
RDS_fSingleSessionPerUser = serializers.ChoiceField(choices=SESSION_PER_USER, default=1, label=_("RDS fSingleSessionPerUser"))
RDS_MaxDisconnectionTime = serializers.IntegerField(default=60000, label=_("RDS Max Disconnection Time"))
RDS_RemoteAppLogoffTimeLimit = serializers.IntegerField(default=0, label=_("RDS Remote App Logoff Time Limit"))
class AppletHostSerializer(HostSerializer):
deploy_options = DeployOptionsSerializer(required=False, label=_("Deploy options"))
2022-11-07 12:41:18 +00:00
load = LabeledChoiceField(
read_only=True, label=_('Load status'), choices=const.ComponentLoad.choices,
)
2022-11-01 12:37:04 +00:00
class Meta(HostSerializer.Meta):
model = AppletHost
fields = HostSerializer.Meta.fields + [
2022-11-07 12:41:18 +00:00
'load', 'date_synced', 'deploy_options'
2022-11-01 12:37:04 +00:00
]
extra_kwargs = {
'date_synced': {'read_only': True}
}
def __init__(self, *args, data=None, **kwargs):
if data:
self.set_initial_data(data)
kwargs['data'] = data
super().__init__(*args, **kwargs)
@staticmethod
def set_initial_data(data):
platform = Platform.objects.get(name='RemoteAppHost')
data.update({
'platform': platform.id,
'nodes_display': [
'RemoteAppHosts'
]
})
def get_validators(self):
validators = super().get_validators()
# 不知道为啥没有继承过来
uniq_validator = ProjectUniqueValidator(
queryset=AppletHost.objects.all(),
fields=('org_id', 'name')
)
validators.append(uniq_validator)
return validators
class HostAppletSerializer(AppletSerializer):
publication = serializers.SerializerMethodField()
class Meta(AppletSerializer.Meta):
fields = AppletSerializer.Meta.fields + ['publication']
class AppletHostDeploymentSerializer(serializers.ModelSerializer):
class Meta:
model = AppletHostDeployment
fields_mini = ['id', 'host', 'status']
read_only_fields = [
'status', 'date_created', 'date_updated',
'date_start', 'date_finished'
]
fields = fields_mini + ['comment'] + read_only_fields
2022-11-03 08:55:38 +00:00
class AppletHostAccountSerializer(serializers.ModelSerializer):
class Meta:
model = Account
2022-11-08 05:55:06 +00:00
fields = ['id', 'username', 'secret', 'is_active', 'date_updated']
2022-11-03 08:55:38 +00:00
2022-11-07 12:41:18 +00:00
class AppletHostAppletReportSerializer(serializers.Serializer):
id = serializers.UUIDField(read_only=True)
name = serializers.CharField()
version = serializers.CharField()
class AppletHostStartupSerializer(serializers.Serializer):
pass