2020-09-14 06:30:45 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2022-11-16 13:05:15 +00:00
|
|
|
from rest_framework import serializers
|
2016-10-16 14:12:13 +00:00
|
|
|
|
2022-11-04 03:40:16 +00:00
|
|
|
from common.drf.fields import LabeledChoiceField
|
2022-11-16 13:05:15 +00:00
|
|
|
from common.drf.serializers import BulkModelSerializer
|
2022-11-04 03:40:16 +00:00
|
|
|
from common.utils import get_request_ip, pretty_string, is_uuid
|
2022-11-03 08:55:38 +00:00
|
|
|
from users.serializers import ServiceAccountSerializer
|
2021-09-27 03:28:29 +00:00
|
|
|
from .. import const
|
2022-11-04 03:40:16 +00:00
|
|
|
from ..models import Terminal, Status, Task, CommandStorage, ReplayStorage
|
2021-01-20 10:33:38 +00:00
|
|
|
|
2021-03-26 11:09:34 +00:00
|
|
|
|
2022-11-04 03:40:16 +00:00
|
|
|
class StatSerializer(serializers.ModelSerializer):
|
2021-03-26 11:09:34 +00:00
|
|
|
sessions = serializers.ListSerializer(
|
2022-11-04 03:40:16 +00:00
|
|
|
child=serializers.CharField(max_length=36),
|
|
|
|
write_only=True
|
2021-03-26 11:09:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
2022-11-04 03:40:16 +00:00
|
|
|
model = Status
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id']
|
2021-07-26 11:19:57 +00:00
|
|
|
fields_write_only = ['sessions', ]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_small = fields_mini + fields_write_only + [
|
2021-03-26 11:09:34 +00:00
|
|
|
'cpu_load', 'memory_used', 'disk_used',
|
2022-11-04 03:40:16 +00:00
|
|
|
'session_online', 'date_created'
|
2021-03-26 11:09:34 +00:00
|
|
|
]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_fk = ['terminal']
|
|
|
|
fields = fields_small + fields_fk
|
2021-03-26 11:09:34 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
"cpu_load": {'default': 0},
|
|
|
|
"memory_used": {'default': 0},
|
|
|
|
"disk_used": {'default': 0},
|
|
|
|
}
|
2016-10-16 14:12:13 +00:00
|
|
|
|
|
|
|
|
2020-07-29 06:57:44 +00:00
|
|
|
class TerminalSerializer(BulkModelSerializer):
|
2021-04-14 08:28:51 +00:00
|
|
|
session_online = serializers.ReadOnlyField(source='get_online_session_count')
|
2019-01-21 09:43:08 +00:00
|
|
|
is_alive = serializers.BooleanField(read_only=True)
|
2021-09-27 03:28:29 +00:00
|
|
|
is_active = serializers.BooleanField(read_only=True, label='Is active')
|
2022-11-04 03:40:16 +00:00
|
|
|
load = LabeledChoiceField(
|
|
|
|
read_only=True, choices=const.ComponentLoad.choices,
|
|
|
|
label=_('Load status')
|
2021-10-19 08:14:15 +00:00
|
|
|
)
|
2022-11-04 03:40:16 +00:00
|
|
|
stat = StatSerializer(read_only=True, source='last_stat')
|
2016-10-19 10:33:14 +00:00
|
|
|
|
2016-10-16 14:12:13 +00:00
|
|
|
class Meta:
|
|
|
|
model = Terminal
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id', 'name']
|
|
|
|
fields_small = fields_mini + [
|
2022-11-04 03:40:16 +00:00
|
|
|
'type', 'remote_addr', 'session_online',
|
|
|
|
'command_storage', 'replay_storage',
|
|
|
|
'is_active', 'is_alive',
|
2022-03-25 06:45:08 +00:00
|
|
|
'date_created', 'comment',
|
2017-12-25 04:22:49 +00:00
|
|
|
]
|
2022-11-04 03:40:16 +00:00
|
|
|
fields_fk = ['load', 'stat']
|
2021-04-29 11:10:45 +00:00
|
|
|
fields = fields_small + fields_fk
|
2020-12-10 12:50:22 +00:00
|
|
|
read_only_fields = ['type', 'date_created']
|
2021-07-26 11:19:57 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
'command_storage': {'required': True, },
|
|
|
|
'replay_storage': {'required': True, },
|
|
|
|
}
|
|
|
|
|
2020-09-14 06:30:45 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_kwargs_may_be_uuid(value):
|
|
|
|
kwargs = {}
|
|
|
|
if is_uuid(value):
|
|
|
|
kwargs['id'] = value
|
|
|
|
else:
|
|
|
|
kwargs['name'] = value
|
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def validate_command_storage(self, value):
|
|
|
|
kwargs = self.get_kwargs_may_be_uuid(value)
|
|
|
|
storage = CommandStorage.objects.filter(**kwargs).first()
|
|
|
|
if storage:
|
|
|
|
return storage.name
|
|
|
|
else:
|
|
|
|
raise serializers.ValidationError(_('Not found'))
|
|
|
|
|
|
|
|
def validate_replay_storage(self, value):
|
|
|
|
kwargs = self.get_kwargs_may_be_uuid(value)
|
|
|
|
storage = ReplayStorage.objects.filter(**kwargs).first()
|
|
|
|
if storage:
|
|
|
|
return storage.name
|
|
|
|
else:
|
|
|
|
raise serializers.ValidationError(_('Not found'))
|
|
|
|
|
2016-10-16 14:12:13 +00:00
|
|
|
|
2020-07-29 06:57:44 +00:00
|
|
|
class TaskSerializer(BulkModelSerializer):
|
2017-11-14 01:44:16 +00:00
|
|
|
class Meta:
|
|
|
|
fields = '__all__'
|
2017-12-01 09:28:47 +00:00
|
|
|
model = Task
|
2020-12-11 09:21:52 +00:00
|
|
|
ref_name = 'TerminalTaskSerializer'
|
2021-01-20 10:33:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TerminalRegistrationSerializer(serializers.ModelSerializer):
|
|
|
|
service_account = ServiceAccountSerializer(read_only=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Terminal
|
|
|
|
fields = ['name', 'type', 'comment', 'service_account', 'remote_addr']
|
2021-09-09 05:12:14 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
'name': {'max_length': 1024},
|
|
|
|
'remote_addr': {'read_only': True}
|
2021-01-20 10:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def is_valid(self, raise_exception=False):
|
|
|
|
valid = super().is_valid(raise_exception=raise_exception)
|
|
|
|
if not valid:
|
|
|
|
return valid
|
2022-03-30 11:07:49 +00:00
|
|
|
raw_name = self.validated_data.get('name')
|
|
|
|
name = pretty_string(raw_name)
|
|
|
|
self.validated_data['name'] = name
|
|
|
|
if len(raw_name) > 128:
|
|
|
|
self.validated_data['comment'] = raw_name
|
2021-09-09 05:12:14 +00:00
|
|
|
data = {'name': name}
|
2021-01-20 10:33:38 +00:00
|
|
|
kwargs = {'data': data}
|
|
|
|
if self.instance and self.instance.user:
|
|
|
|
kwargs['instance'] = self.instance.user
|
|
|
|
self.service_account = ServiceAccountSerializer(**kwargs)
|
|
|
|
valid = self.service_account.is_valid(raise_exception=True)
|
|
|
|
return valid
|
|
|
|
|
2022-02-18 06:28:28 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
instance = super().create(validated_data)
|
2021-01-20 10:33:38 +00:00
|
|
|
request = self.context.get('request')
|
|
|
|
instance.is_accepted = True
|
|
|
|
if request:
|
|
|
|
instance.remote_addr = get_request_ip(request)
|
2022-02-18 06:28:28 +00:00
|
|
|
sa = self.service_account.create(validated_data)
|
2022-03-25 06:45:08 +00:00
|
|
|
sa.system_roles.add_role_system_component()
|
2021-01-20 10:33:38 +00:00
|
|
|
instance.user = sa
|
2021-06-28 11:36:37 +00:00
|
|
|
instance.command_storage = CommandStorage.default().name
|
|
|
|
instance.replay_storage = ReplayStorage.default().name
|
2021-01-20 10:33:38 +00:00
|
|
|
instance.save()
|
|
|
|
return instance
|
2022-11-16 13:05:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ConnectMethodSerializer(serializers.Serializer):
|
2022-11-17 12:48:50 +00:00
|
|
|
value = serializers.CharField(max_length=128)
|
|
|
|
label = serializers.CharField(max_length=128)
|
2022-11-29 06:42:04 +00:00
|
|
|
type = serializers.CharField(max_length=128)
|
2022-12-05 02:48:19 +00:00
|
|
|
endpoint_protocol = serializers.CharField(max_length=128)
|
2022-11-29 06:42:04 +00:00
|
|
|
component = serializers.CharField(max_length=128)
|