2016-10-16 14:12:13 +00:00
|
|
|
from rest_framework import serializers
|
2020-09-14 06:30:45 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-10-16 14:12:13 +00:00
|
|
|
|
2020-07-29 06:57:44 +00:00
|
|
|
from common.drf.serializers import BulkModelSerializer, AdaptedBulkListSerializer
|
2020-09-14 06:30:45 +00:00
|
|
|
from common.utils import is_uuid
|
2021-01-20 10:33:38 +00:00
|
|
|
from users.serializers import ServiceAccountSerializer
|
|
|
|
from common.utils import get_request_ip
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
from ..models import (
|
2020-09-14 06:30:45 +00:00
|
|
|
Terminal, Status, Session, Task, CommandStorage, ReplayStorage
|
2019-12-05 07:09:25 +00:00
|
|
|
)
|
2021-03-26 11:09:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StatusSerializer(serializers.ModelSerializer):
|
|
|
|
sessions = serializers.ListSerializer(
|
|
|
|
child=serializers.CharField(max_length=35), write_only=True
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
fields = [
|
|
|
|
'id',
|
|
|
|
'cpu_load', 'memory_used', 'disk_used',
|
|
|
|
'session_online', 'sessions',
|
|
|
|
'terminal', 'date_created',
|
|
|
|
]
|
|
|
|
extra_kwargs = {
|
|
|
|
"cpu_load": {'default': 0},
|
|
|
|
"memory_used": {'default': 0},
|
|
|
|
"disk_used": {'default': 0},
|
|
|
|
}
|
|
|
|
model = Status
|
2016-10-16 14:12:13 +00:00
|
|
|
|
|
|
|
|
2020-07-29 06:57:44 +00:00
|
|
|
class TerminalSerializer(BulkModelSerializer):
|
2017-12-01 09:55:19 +00:00
|
|
|
session_online = serializers.SerializerMethodField()
|
2019-01-21 09:43:08 +00:00
|
|
|
is_alive = serializers.BooleanField(read_only=True)
|
2021-03-26 11:09:34 +00:00
|
|
|
status = serializers.CharField(read_only=True, source='latest_status')
|
|
|
|
status_display = serializers.CharField(read_only=True, source='latest_status_display')
|
|
|
|
stat = StatusSerializer(read_only=True, source='latest_stat')
|
2016-10-19 10:33:14 +00:00
|
|
|
|
2016-10-16 14:12:13 +00:00
|
|
|
class Meta:
|
|
|
|
model = Terminal
|
2017-12-25 04:22:49 +00:00
|
|
|
fields = [
|
2020-12-10 12:50:22 +00:00
|
|
|
'id', 'name', 'type', 'remote_addr', 'http_port', 'ssh_port',
|
2017-12-25 04:22:49 +00:00
|
|
|
'comment', 'is_accepted', "is_active", 'session_online',
|
2020-12-10 12:50:22 +00:00
|
|
|
'is_alive', 'date_created', 'command_storage', 'replay_storage',
|
2021-03-26 11:09:34 +00:00
|
|
|
'status', 'status_display', 'stat'
|
2017-12-25 04:22:49 +00:00
|
|
|
]
|
2020-12-10 12:50:22 +00:00
|
|
|
read_only_fields = ['type', 'date_created']
|
2016-10-19 10:33:14 +00:00
|
|
|
|
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-19 10:33:14 +00:00
|
|
|
@staticmethod
|
2017-12-01 09:55:19 +00:00
|
|
|
def get_session_online(obj):
|
2019-01-21 09:05:31 +00:00
|
|
|
return Session.objects.filter(terminal=obj, is_finished=False).count()
|
2016-11-13 14:34:38 +00:00
|
|
|
|
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
|
2019-04-25 02:11:50 +00:00
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
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']
|
|
|
|
extra_fields = {
|
|
|
|
'remote_addr': {'readonly': True}
|
|
|
|
}
|
|
|
|
|
|
|
|
def is_valid(self, raise_exception=False):
|
|
|
|
valid = super().is_valid(raise_exception=raise_exception)
|
|
|
|
if not valid:
|
|
|
|
return valid
|
|
|
|
data = {'name': self.validated_data.get('name')}
|
|
|
|
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
|
|
|
|
|
|
|
|
def save(self, **kwargs):
|
|
|
|
instance = super().save(**kwargs)
|
|
|
|
request = self.context.get('request')
|
|
|
|
instance.is_accepted = True
|
|
|
|
if request:
|
|
|
|
instance.remote_addr = get_request_ip(request)
|
|
|
|
sa = self.service_account.save()
|
|
|
|
instance.user = sa
|
|
|
|
instance.save()
|
|
|
|
return instance
|