1
0
mirror of https://github.com/dkmstr/openuds.git synced 2024-12-22 13:34:04 +03:00

Done server groups & servers management

This commit is contained in:
Adolfo Gómez García 2023-09-02 20:42:03 +02:00
parent c4850e0aed
commit 1b2b1f81b3
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23

View File

@ -36,9 +36,7 @@ from django.utils.translation import gettext
from django.utils.translation import gettext_lazy as _
from uds import models
from uds.core import consts, types
from uds.core.types import permissions as permtypes
from uds.core.types import rest, servers
from uds.core import consts, types, ui
from uds.core.util import permissions
from uds.core.util.model import getSqlDatetime, processUuid
from uds.REST.exceptions import NotFound, RequestError
@ -93,7 +91,7 @@ class ServersTokens(ModelHandler):
raise RequestError('Delete need one and only one argument')
self.ensureAccess(
self.model(), permtypes.PermissionType.ALL, root=True
self.model(), types.permissions.PermissionType.ALL, root=True
) # Must have write permissions to delete
try:
@ -165,42 +163,67 @@ class ServersServers(DetailHandler):
return {'field': 'maintenance_mode', 'prefix': 'row-maintenance-'}
def getGui(self, parent: 'models.ServerGroup', forType: str = '') -> typing.List[typing.Any]:
kind, subkind = parent.serverType.name, parent.subtype
title = _('of type') + f' {subkind.upper()} {kind}'
return self.addField(
[],
[
{
'name': 'hostname',
'value': '',
'label': gettext('Hostname'),
'tooltip': gettext('Hostname of the server. It must be resolvable by UDS'),
'type': types.ui.FieldType.TEXT,
'order': 100, # At end
},
{
'name': 'ip',
'value': '',
'label': gettext('IP'),
'tooltip': gettext('IP of the server. Used if hostname is not resolvable by UDS'),
'type': types.ui.FieldType.TEXT,
'order': 101, # At end
},
{
'name': 'listen_port',
'value': 0,
'label': gettext('Port'),
'tooltip': gettext('Port of server. 0 means "service default"'),
'type': types.ui.FieldType.NUMERIC,
'order': 102, # At end
},
{
'name': 'title',
'value': title,
'type': types.ui.FieldType.INFO,
},
],
)
kind, subkind = parent.serverType, parent.subtype
title = _('of type') + f' {subkind.upper()} {kind.name.capitalize()}'
if kind == types.servers.ServerType.UNMANAGED:
return self.addField(
[],
[
{
'name': 'hostname',
'value': '',
'label': gettext('Hostname'),
'tooltip': gettext('Hostname of the server. It must be resolvable by UDS'),
'type': types.ui.FieldType.TEXT,
'order': 100, # At end
},
{
'name': 'ip',
'value': '',
'label': gettext('IP'),
'tooltip': gettext('IP of the server. Used if hostname is not resolvable by UDS'),
'type': types.ui.FieldType.TEXT,
'order': 101, # At end
},
{
'name': 'listen_port',
'value': 0,
'label': gettext('Port'),
'tooltip': gettext('Port of server. 0 means "service default"'),
'type': types.ui.FieldType.NUMERIC,
'order': 102, # At end
},
{
'name': 'title',
'value': title,
'type': types.ui.FieldType.INFO,
},
],
)
else:
return self.addField(
[],
[
{
'name': 'server',
'value': '',
'label': gettext('Server'),
'tooltip': gettext('Server to include on group'),
'type': types.ui.FieldType.CHOICE,
'choices': [
ui.gui.choiceItem(item.uuid, item.hostname)
for item in models.Server.objects.filter(type=parent.type, subtype=parent.subtype)
if item not in parent.servers.all()
],
'order': 100, # At end
},
{
'name': 'title',
'value': title,
'type': types.ui.FieldType.INFO,
},
],
)
def saveItem(self, parent: 'models.ServerGroup', item: typing.Optional[str]) -> None:
# Item is the uuid of the server to add
@ -259,7 +282,12 @@ class ServersServers(DetailHandler):
parent.servers.add(server)
return
elif parent.type == types.servers.ServerType.SERVER:
# TODO: Implement this
# Get server
try:
server = models.Server.objects.get(uuid=processUuid(self._params['server']))
parent.servers.add(server)
except Exception:
raise self.invalidItemException() from None
pass
else:
try:
@ -275,7 +303,7 @@ class ServersServers(DetailHandler):
server = models.Server.objects.get(uuid=processUuid(item))
if parent.serverType == types.servers.ServerType.UNMANAGED:
parent.servers.remove(server) # Remove reference
server.delete() # and delete server
server.delete() # and delete server
else:
parent.servers.remove(server) # Just remove reference
except Exception:
@ -288,7 +316,7 @@ class ServersServers(DetailHandler):
:param item:
"""
item = models.Server.objects.get(uuid=processUuid(id))
self.ensureAccess(item, permtypes.PermissionType.MANAGEMENT)
self.ensureAccess(item, types.permissions.PermissionType.MANAGEMENT)
item.maintenance_mode = not item.maintenance_mode
item.save()
return 'ok'
@ -319,8 +347,8 @@ class ServersGroups(ModelHandler):
]
def getTypes(self, *args, **kwargs) -> typing.Generator[typing.Dict[str, typing.Any], None, None]:
for i in servers.ServerSubType.manager().enum():
v = rest.TypeInfo(
for i in types.servers.ServerSubType.manager().enum():
v = types.rest.TypeInfo(
name=i.description, type=f'{i.type.name}@{i.subtype}', description='', icon=i.icon
).asDict(group=gettext('Managed') if i.managed else gettext('Unmanaged'))
yield v