mirror of
https://github.com/dkmstr/openuds.git
synced 2025-01-24 02:04:09 +03:00
Updating more names, code review and fixes.
This commit is contained in:
parent
c2024c23b8
commit
de3985c841
@ -347,7 +347,7 @@ class BaseModelHandler(Handler):
|
||||
if isinstance(item, ManagedObjectModel):
|
||||
i = item.get_instance()
|
||||
i.init_gui() # Defaults & stuff
|
||||
res.update(i.get_dict_of_fields_values())
|
||||
res.update(i.get_fields_as_dict())
|
||||
return res
|
||||
|
||||
# Exceptions
|
||||
|
@ -276,11 +276,11 @@ class RegexLdap(auths.Authenticator):
|
||||
def mfa_identifier(self, username: str) -> str:
|
||||
return self.storage.get_unpickle(self.mfaStorageKey(username)) or ''
|
||||
|
||||
def get_dict_of_fields_values(self) -> gui.ValuesDictType:
|
||||
def get_fields_as_dict(self) -> gui.ValuesDictType:
|
||||
return {
|
||||
'host': self._host,
|
||||
'port': self._port,
|
||||
'ssl': gui.from_bool(self._ssl),
|
||||
'ssl': gui.bool_as_str(self._ssl),
|
||||
'username': self._username,
|
||||
'password': self._password,
|
||||
'timeout': self._timeout,
|
||||
@ -291,7 +291,7 @@ class RegexLdap(auths.Authenticator):
|
||||
'userNameAttr': self._userNameAttr,
|
||||
'altClass': self._altClass,
|
||||
'mfaAttr': self._mfaAttr,
|
||||
'verifySsl': gui.from_bool(self._verifySsl),
|
||||
'verifySsl': gui.bool_as_str(self._verifySsl),
|
||||
'certificate': self._certificate,
|
||||
}
|
||||
|
||||
@ -301,7 +301,7 @@ class RegexLdap(auths.Authenticator):
|
||||
'v5',
|
||||
self._host,
|
||||
self._port,
|
||||
gui.from_bool(self._ssl),
|
||||
gui.bool_as_str(self._ssl),
|
||||
self._username,
|
||||
self._password,
|
||||
self._timeout,
|
||||
@ -312,7 +312,7 @@ class RegexLdap(auths.Authenticator):
|
||||
self._userNameAttr,
|
||||
self._altClass,
|
||||
self._mfaAttr,
|
||||
gui.from_bool(self._verifySsl),
|
||||
gui.bool_as_str(self._verifySsl),
|
||||
self._certificate.strip(),
|
||||
]
|
||||
).encode('utf8')
|
||||
|
@ -241,11 +241,11 @@ class SimpleLDAPAuthenticator(auths.Authenticator):
|
||||
self._verifySsl = gui.as_bool(values['verifySsl'])
|
||||
self._certificate = values['certificate']
|
||||
|
||||
def get_dict_of_fields_values(self) -> gui.ValuesDictType:
|
||||
def get_fields_as_dict(self) -> gui.ValuesDictType:
|
||||
return {
|
||||
'host': self._host,
|
||||
'port': self._port,
|
||||
'ssl': gui.from_bool(self._ssl),
|
||||
'ssl': gui.bool_as_str(self._ssl),
|
||||
'username': self._username,
|
||||
'password': self._password,
|
||||
'timeout': self._timeout,
|
||||
@ -257,7 +257,7 @@ class SimpleLDAPAuthenticator(auths.Authenticator):
|
||||
'memberAttr': self._memberAttr,
|
||||
'userNameAttr': self._userNameAttr,
|
||||
'mfaAttr': self._mfaAttr,
|
||||
'verifySsl': gui.from_bool(self._verifySsl),
|
||||
'verifySsl': gui.bool_as_str(self._verifySsl),
|
||||
'certificate': self._certificate,
|
||||
}
|
||||
|
||||
@ -267,7 +267,7 @@ class SimpleLDAPAuthenticator(auths.Authenticator):
|
||||
'v2',
|
||||
self._host,
|
||||
self._port,
|
||||
gui.from_bool(self._ssl),
|
||||
gui.bool_as_str(self._ssl),
|
||||
self._username,
|
||||
self._password,
|
||||
self._timeout,
|
||||
@ -279,7 +279,7 @@ class SimpleLDAPAuthenticator(auths.Authenticator):
|
||||
self._memberAttr,
|
||||
self._userNameAttr,
|
||||
self._mfaAttr,
|
||||
gui.from_bool(self._verifySsl),
|
||||
gui.bool_as_str(self._verifySsl),
|
||||
self._certificate.strip(),
|
||||
]
|
||||
).encode('utf8')
|
||||
|
@ -72,3 +72,5 @@ FALSE_STR: typing.Final[str] = 'false'
|
||||
# Constant to mark an "UNLIMITED" value
|
||||
UNLIMITED: typing.Final[int] = -1
|
||||
|
||||
# Constant marking no more names available
|
||||
NO_MORE_NAMES: typing.Final[str] = 'NO-NAME-ERROR'
|
||||
|
@ -124,7 +124,7 @@ class gui:
|
||||
Helper method to create a single choice item.
|
||||
"""
|
||||
return {'id': str(id_), 'text': str(text)}
|
||||
|
||||
|
||||
@staticmethod
|
||||
def choice_image(id_: typing.Union[str, int], text: str, img: str) -> types.ui.ChoiceItem:
|
||||
"""
|
||||
@ -178,8 +178,10 @@ class gui:
|
||||
raise ValueError(f'Invalid type for convertToChoices: {vals}')
|
||||
|
||||
@staticmethod
|
||||
def sorted_choices(choices: collections.abc.Iterable[types.ui.ChoiceItem]):
|
||||
return sorted(choices, key=lambda item: item['text'].lower())
|
||||
def sorted_choices(choices: collections.abc.Iterable[types.ui.ChoiceItem], *, by_id: bool = False, reverse: bool = False) -> list[types.ui.ChoiceItem]:
|
||||
if by_id:
|
||||
return sorted(choices, key=lambda item: item['id'], reverse=reverse)
|
||||
return sorted(choices, key=lambda item: item['text'].lower(), reverse=reverse)
|
||||
|
||||
@staticmethod
|
||||
def as_bool(value: typing.Union[str, bytes, bool, int]) -> bool:
|
||||
@ -196,7 +198,7 @@ class gui:
|
||||
return value in consts.BOOL_TRUE_VALUES
|
||||
|
||||
@staticmethod
|
||||
def from_bool(bol: bool) -> str:
|
||||
def bool_as_str(bol: bool) -> str:
|
||||
"""
|
||||
Converts a boolean to the string representation. True is converted to
|
||||
"true", False to "false". (gui.TRUE and gui.FALSE are the same)
|
||||
@ -207,9 +209,7 @@ class gui:
|
||||
Returns:
|
||||
"true" if bol evals to True, "false" if don't.
|
||||
"""
|
||||
if bol:
|
||||
return consts.TRUE_STR
|
||||
return consts.FALSE_STR
|
||||
return consts.TRUE_STR if bol else consts.FALSE_STR
|
||||
|
||||
@staticmethod
|
||||
def as_int(value: typing.Union[str, bytes, bool, int]) -> int:
|
||||
@ -1387,7 +1387,7 @@ class UserInterface(metaclass=UserInterfaceAbstract):
|
||||
of this posibility in a near version...
|
||||
"""
|
||||
|
||||
def get_dict_of_fields_values(self) -> gui.ValuesDictType:
|
||||
def get_fields_as_dict(self) -> gui.ValuesDictType:
|
||||
"""
|
||||
Returns own data needed for user interaction as a dict of key-names ->
|
||||
values. The values returned must be strings.
|
||||
|
@ -57,7 +57,7 @@ def getSerializedFromManagedObject(
|
||||
try:
|
||||
obj = mod.get_instance()
|
||||
gui = {i['name']: i['gui']['type'] for i in obj.gui_description()}
|
||||
values = obj.get_dict_of_fields_values()
|
||||
values = obj.get_fields_as_dict()
|
||||
# Remove password fields
|
||||
for k, v in gui.items():
|
||||
if v == 'password':
|
||||
|
@ -237,8 +237,8 @@ class LinuxOsADManager(LinuxOsManager):
|
||||
self._automatic_id_mapping = values[10]
|
||||
super().unmarshal(codecs.decode(values[11].encode(), 'hex'))
|
||||
|
||||
def get_dict_of_fields_values(self) -> gui.ValuesDictType:
|
||||
dct = super().get_dict_of_fields_values()
|
||||
def get_fields_as_dict(self) -> gui.ValuesDictType:
|
||||
dct = super().get_fields_as_dict()
|
||||
dct['domain'] = self._domain
|
||||
dct['account'] = self._account
|
||||
dct['password'] = self._password
|
||||
|
@ -223,8 +223,8 @@ class LinuxOsFreeIPAManager(LinuxOsManager):
|
||||
self._automatic_id_mapping = values[9]
|
||||
super().unmarshal(codecs.decode(values[10].encode(), 'hex'))
|
||||
|
||||
def get_dict_of_fields_values(self) -> gui.ValuesDictType:
|
||||
dct = super().get_dict_of_fields_values()
|
||||
def get_fields_as_dict(self) -> gui.ValuesDictType:
|
||||
dct = super().get_fields_as_dict()
|
||||
dct['domain'] = self._domain
|
||||
dct['account'] = self._account
|
||||
dct['password'] = self._password
|
||||
|
@ -196,7 +196,7 @@ class LinuxOsManager(osmanagers.OSManager):
|
||||
Serializes the os manager data so we can store it in database
|
||||
"""
|
||||
return '\t'.join(
|
||||
['v3', self._on_logout, str(self._idle), gui.from_bool(self._deadline)]
|
||||
['v3', self._on_logout, str(self._idle), gui.bool_as_str(self._deadline)]
|
||||
).encode('utf8')
|
||||
|
||||
def unmarshal(self, data: bytes) -> None:
|
||||
@ -216,9 +216,9 @@ class LinuxOsManager(osmanagers.OSManager):
|
||||
|
||||
self._flag_processes_unused_machines()
|
||||
|
||||
def get_dict_of_fields_values(self) -> gui.ValuesDictType:
|
||||
def get_fields_as_dict(self) -> gui.ValuesDictType:
|
||||
return {
|
||||
'on_logout': self._on_logout,
|
||||
'idle': str(self._idle),
|
||||
'deadline': gui.from_bool(self._deadline),
|
||||
'deadline': gui.bool_as_str(self._deadline),
|
||||
}
|
||||
|
@ -146,7 +146,7 @@ class LinuxRandomPassManager(LinuxOsManager):
|
||||
self._user_account = values[1].decode()
|
||||
LinuxOsManager.unmarshal(self, codecs.decode(values[2], 'hex'))
|
||||
|
||||
def get_dict_of_fields_values(self) -> gui.ValuesDictType:
|
||||
dic = LinuxOsManager.get_dict_of_fields_values(self)
|
||||
def get_fields_as_dict(self) -> gui.ValuesDictType:
|
||||
dic = LinuxOsManager.get_fields_as_dict(self)
|
||||
dic['user_account'] = self._user_account
|
||||
return dic
|
||||
|
@ -224,7 +224,7 @@ class WindowsOsManager(osmanagers.OSManager):
|
||||
Serializes the os manager data so we can store it in database
|
||||
"""
|
||||
return '\t'.join(
|
||||
['v3', self._on_logout, str(self._idle), gui.from_bool(self._deadline)]
|
||||
['v3', self._on_logout, str(self._idle), gui.bool_as_str(self._deadline)]
|
||||
).encode('utf8')
|
||||
|
||||
def unmarshal(self, data: bytes) -> None:
|
||||
@ -249,9 +249,9 @@ class WindowsOsManager(osmanagers.OSManager):
|
||||
|
||||
self._set_handles_unused()
|
||||
|
||||
def get_dict_of_fields_values(self) -> 'gui.ValuesDictType':
|
||||
def get_fields_as_dict(self) -> 'gui.ValuesDictType':
|
||||
return {
|
||||
'on_logout': self._on_logout,
|
||||
'idle': str(self._idle),
|
||||
'deadline': gui.from_bool(self._deadline),
|
||||
'deadline': gui.bool_as_str(self._deadline),
|
||||
}
|
||||
|
@ -503,8 +503,8 @@ class WinDomainOsManager(WindowsOsManager):
|
||||
self._remove_on_exit = 'y'
|
||||
super().unmarshal(codecs.decode(values[5].encode(), 'hex'))
|
||||
|
||||
def get_dict_of_fields_values(self) -> gui.ValuesDictType:
|
||||
dct = super().get_dict_of_fields_values()
|
||||
def get_fields_as_dict(self) -> gui.ValuesDictType:
|
||||
dct = super().get_fields_as_dict()
|
||||
dct['domain'] = self._domain
|
||||
dct['ou'] = self._ou
|
||||
dct['account'] = self._account
|
||||
|
@ -159,8 +159,8 @@ class WinRandomPassManager(WindowsOsManager):
|
||||
self._password = CryptoManager().decrypt(values[2])
|
||||
super().unmarshal(codecs.decode(values[3].encode(), 'hex'))
|
||||
|
||||
def get_dict_of_fields_values(self) -> gui.ValuesDictType:
|
||||
dic = super().get_dict_of_fields_values()
|
||||
def get_fields_as_dict(self) -> gui.ValuesDictType:
|
||||
dic = super().get_fields_as_dict()
|
||||
dic['user_account'] = self._user_account
|
||||
dic['password'] = self._password
|
||||
return dic
|
||||
|
@ -15,7 +15,7 @@ if typing.TYPE_CHECKING:
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def getResources(parameters: typing.Any) -> list[dict[str, typing.Any]]:
|
||||
def get_resources(parameters: typing.Any) -> list[dict[str, typing.Any]]:
|
||||
"""
|
||||
This helper is designed as a callback for machine selector, so we can provide valid clusters and datastores domains based on it
|
||||
"""
|
||||
|
@ -111,7 +111,7 @@ class OVirtLinkedService(services.Service): # pylint: disable=too-many-public-m
|
||||
order=100,
|
||||
fills={
|
||||
'callback_name': 'ovFillResourcesFromCluster',
|
||||
'function': helpers.getResources,
|
||||
'function': helpers.get_resources,
|
||||
'parameters': ['cluster', 'ov', 'ev'],
|
||||
},
|
||||
tooltip=_("Cluster to contain services"),
|
||||
|
@ -254,7 +254,7 @@ class LiveService(services.Service):
|
||||
|
||||
self.ov.value = self.parent().serialize()
|
||||
self.ev.value = self.parent().env.key
|
||||
self.legacy.value = gui.from_bool(self.parent().legacy)
|
||||
self.legacy.value = gui.bool_as_str(self.parent().legacy)
|
||||
|
||||
@property
|
||||
def api(self) -> 'openstack.Client':
|
||||
|
@ -189,7 +189,7 @@ class IPMachinesService(IPServiceBase):
|
||||
def get_token(self):
|
||||
return self._token or None
|
||||
|
||||
def get_dict_of_fields_values(self) -> gui.ValuesDictType:
|
||||
def get_fields_as_dict(self) -> gui.ValuesDictType:
|
||||
ips = (i.split('~')[0] for i in self._ips)
|
||||
return {
|
||||
'ipList': ensure.is_list(ips),
|
||||
@ -197,8 +197,8 @@ class IPMachinesService(IPServiceBase):
|
||||
'port': str(self._port),
|
||||
'skipTimeOnFailure': str(self._skipTimeOnFailure),
|
||||
'maxSessionForMachine': str(self._maxSessionForMachine),
|
||||
'lockByExternalAccess': gui.from_bool(self._lockByExternalAccess),
|
||||
'useRandomIp': gui.from_bool(self._useRandomIp),
|
||||
'lockByExternalAccess': gui.bool_as_str(self._lockByExternalAccess),
|
||||
'useRandomIp': gui.bool_as_str(self._useRandomIp),
|
||||
}
|
||||
|
||||
def marshal(self) -> bytes:
|
||||
@ -210,8 +210,8 @@ class IPMachinesService(IPServiceBase):
|
||||
str(self._port).encode(),
|
||||
str(self._skipTimeOnFailure).encode(),
|
||||
str(self._maxSessionForMachine).encode(),
|
||||
gui.from_bool(self._lockByExternalAccess).encode(),
|
||||
gui.from_bool(self._useRandomIp).encode(),
|
||||
gui.bool_as_str(self._lockByExternalAccess).encode(),
|
||||
gui.bool_as_str(self._useRandomIp).encode(),
|
||||
]
|
||||
)
|
||||
|
||||
|
@ -151,7 +151,7 @@ class ProxmoxDeployment(services.UserService):
|
||||
if self._name == '':
|
||||
try:
|
||||
self._name = self.name_generator().get(
|
||||
self.service().get_basename(), self.service().getLenName()
|
||||
self.service().get_basename(), self.service().get_lenname()
|
||||
)
|
||||
except KeyError:
|
||||
return NO_MORE_NAMES
|
||||
@ -459,7 +459,7 @@ if sys.platform == 'win32':
|
||||
) # Enable HA before continuing here
|
||||
|
||||
# Set vm mac address now on first interface
|
||||
self.service().setVmMac(int(self._vmid), self.get_unique_id())
|
||||
self.service().set_machine_mac(int(self._vmid), self.get_unique_id())
|
||||
except Exception as e:
|
||||
logger.exception('Setting HA and MAC on proxmox')
|
||||
raise Exception(f'Error setting MAC and HA on proxmox: {e}') from e
|
||||
|
@ -39,7 +39,7 @@ from django.utils.translation import gettext as _
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def getStorage(parameters: typing.Any) -> list[dict[str, typing.Any]]:
|
||||
def get_storage(parameters: typing.Any) -> list[dict[str, typing.Any]]:
|
||||
from .provider import ProxmoxProvider # pylint: disable=import-outside-toplevel
|
||||
|
||||
logger.debug('Parameters received by getResources Helper: %s', parameters)
|
||||
|
@ -284,7 +284,7 @@ class ProxmoxProvider(
|
||||
) -> None:
|
||||
self._getApi().enableVmHA(vmId, started, group)
|
||||
|
||||
def setVmMac(
|
||||
def set_machine_mac(
|
||||
self, vmId: int, macAddress: str
|
||||
) -> None:
|
||||
self._getApi().setVmMac(vmId, macAddress)
|
||||
@ -292,12 +292,12 @@ class ProxmoxProvider(
|
||||
def disableHA(self, vmId: int) -> None:
|
||||
self._getApi().disableVmHA(vmId)
|
||||
|
||||
def setProtection(
|
||||
def set_protection(
|
||||
self, vmId: int, node: typing.Optional[str] = None, protection: bool = False
|
||||
) -> None:
|
||||
self._getApi().setProtection(vmId, node, protection)
|
||||
|
||||
def listHaGroups(self) -> list[str]:
|
||||
def list_ha_groups(self) -> list[str]:
|
||||
return self._getApi().listHAGroups()
|
||||
|
||||
def getConsoleConnection(
|
||||
|
@ -159,7 +159,7 @@ class ProxmoxPublication(services.Publication):
|
||||
self._state = State.FINISHED
|
||||
if self._operation == 'p': # not Destroying
|
||||
# Disable Protection (removal)
|
||||
self.service().setProtection(int(self._vm), protection=False)
|
||||
self.service().set_protection(int(self._vm), protection=False)
|
||||
time.sleep(
|
||||
0.5
|
||||
) # Give some tome to proxmox. We have observed some concurrency issues
|
||||
|
@ -137,7 +137,7 @@ class ProxmoxLinkedService(services.Service): # pylint: disable=too-many-public
|
||||
order=110,
|
||||
fills={
|
||||
'callback_name': 'pmFillResourcesFromMachine',
|
||||
'function': helpers.getStorage,
|
||||
'function': helpers.get_storage,
|
||||
'parameters': ['machine', 'ov', 'ev'],
|
||||
},
|
||||
tooltip=_('Service base machine'),
|
||||
@ -224,7 +224,7 @@ class ProxmoxLinkedService(services.Service): # pylint: disable=too-many-public
|
||||
)
|
||||
self.ha.set_choices(
|
||||
[gui.choice_item('', _('Enabled')), gui.choice_item('__', _('Disabled'))]
|
||||
+ [gui.choice_item(group, group) for group in self.parent().listHaGroups()]
|
||||
+ [gui.choice_item(group, group) for group in self.parent().list_ha_groups()]
|
||||
)
|
||||
|
||||
def parent(self) -> 'ProxmoxProvider':
|
||||
@ -310,18 +310,18 @@ class ProxmoxLinkedService(services.Service): # pylint: disable=too-many-public
|
||||
return
|
||||
self.parent().disableHA(vmId)
|
||||
|
||||
def setProtection(
|
||||
def set_protection(
|
||||
self, vmId: int, node: typing.Optional[str] = None, protection: bool = False
|
||||
) -> None:
|
||||
self.parent().setProtection(vmId, node, protection)
|
||||
self.parent().set_protection(vmId, node, protection)
|
||||
|
||||
def setVmMac(self, vmId: int, mac: str) -> None:
|
||||
self.parent().setVmMac(vmId, mac)
|
||||
def set_machine_mac(self, vmId: int, mac: str) -> None:
|
||||
self.parent().set_machine_mac(vmId, mac)
|
||||
|
||||
def get_basename(self) -> str:
|
||||
return self.baseName.value
|
||||
|
||||
def getLenName(self) -> int:
|
||||
def get_lenname(self) -> int:
|
||||
return int(self.lenName.value)
|
||||
|
||||
def getMacRange(self) -> str:
|
||||
@ -331,7 +331,7 @@ class ProxmoxLinkedService(services.Service): # pylint: disable=too-many-public
|
||||
return self.parent().getMacRange()
|
||||
|
||||
def isHaEnabled(self) -> bool:
|
||||
return self.ha.as_bool()
|
||||
return self.ha.value != '__'
|
||||
|
||||
def tryGracelyShutdown(self) -> bool:
|
||||
return self.guestShutdown.as_bool()
|
||||
|
@ -151,7 +151,7 @@ class UserinterfaceInternalTest(UDSTestCase):
|
||||
def test_valuesDict(self):
|
||||
ui = TestingUserInterface()
|
||||
self.assertEqual(
|
||||
ui.get_dict_of_fields_values(),
|
||||
ui.get_fields_as_dict(),
|
||||
{
|
||||
'str_field': DEFAULTS['str_field'],
|
||||
'str_auto_field': DEFAULTS['str_auto_field'],
|
||||
|
2
server/tests/fixtures/services.py
vendored
2
server/tests/fixtures/services.py
vendored
@ -164,7 +164,7 @@ def create_test_transport() -> models.Transport:
|
||||
|
||||
values = TestTransport(
|
||||
environment.Environment.get_temporary_environment(), None
|
||||
).get_dict_of_fields_values()
|
||||
).get_fields_as_dict()
|
||||
transport: 'models.Transport' = models.Transport.objects.create(
|
||||
name='Transport %d' % (glob['transport_id']),
|
||||
comments='Comment for Transport %d' % (glob['transport_id']),
|
||||
|
Loading…
x
Reference in New Issue
Block a user