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

some minor refactoring on duplicate on DynamicService

This commit is contained in:
Adolfo Gómez García 2024-06-24 21:58:30 +02:00
parent e90193ce2a
commit 17027e50f4
No known key found for this signature in database
GPG Key ID: DD1ABF20724CDA23
4 changed files with 31 additions and 7 deletions

View File

@ -111,7 +111,7 @@ class DynamicService(services.Service, abc.ABC): # pylint: disable=too-many-pub
return name
# overridable
def find_duplicated_machines(self, name: str, mac: str) -> collections.abc.Iterable[str]:
def find_duplicates(self, name: str, mac: str) -> collections.abc.Iterable[str]:
"""
Checks if a machine with the same name or mac exists
Returns the list with the vmids of the duplicated machines
@ -126,10 +126,10 @@ class DynamicService(services.Service, abc.ABC): # pylint: disable=too-many-pub
Note:
Maybe we can only check name or mac, or both, depending on the service
"""
raise NotImplementedError('find_duplicated_machines must be implemented if remove_duplicates is used!')
raise NotImplementedError(f'{self.__class__}: find_duplicates must be implemented if remove_duplicates is used!')
@typing.final
def perform_find_duplicated_machines(self, name: str, mac: str) -> collections.abc.Iterable[str]:
def perform_find_duplicates(self, name: str, mac: str) -> collections.abc.Iterable[str]:
"""
Checks if a machine with the same name or mac exists
Returns the list with the vmids of the duplicated machines
@ -145,7 +145,7 @@ class DynamicService(services.Service, abc.ABC): # pylint: disable=too-many-pub
Maybe we can only check name or mac, or both, depending on the service
"""
if self.has_field('remove_duplicates') and self.remove_duplicates.value:
return self.find_duplicated_machines(name, mac)
return self.find_duplicates(name, mac)
# Not removing duplicates, so no duplicates
return []

View File

@ -516,7 +516,7 @@ class DynamicUserService(services.UserService, autoserializable.AutoSerializable
def remove_duplicated_names(self) -> None:
name = self.get_vmname()
try:
for vmid in self.service().perform_find_duplicated_machines(name, self.get_unique_id()):
for vmid in self.service().perform_find_duplicates(name, self.get_unique_id()):
userservice = self.db_obj()
log.log(
userservice.service_pool,

View File

@ -43,8 +43,8 @@ import time
import typing
import collections.abc
import abc
from django.conf import settings
from django.conf import settings
from django.utils.translation import gettext
from uds.core import consts, exceptions, types
@ -1498,7 +1498,7 @@ class UserInterface(metaclass=UserInterfaceType):
fld.value = values[fld_name]
else:
logger.warning('Field %s.%s not found in values data, ', self.__class__.__name__, fld_name)
if settings.DEBUG:
if getattr(settings, 'DEBUG', False):
for caller in itertools.islice(inspect.stack(), 1, 8):
logger.warning(' %s:%s:%s', caller.filename, caller.lineno, caller.function)

View File

@ -30,6 +30,7 @@
Author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import logging
import collections.abc
import typing
from django.utils.translation import gettext_noop as _
@ -223,6 +224,29 @@ class XenLinkedService(DynamicService): # pylint: disable=too-many-public-metho
Xen Seems to allow all kind of names
"""
return name
def find_duplicates(self, name: str, mac: str) -> collections.abc.Iterable[str]:
"""
Checks if a machine with the same name or mac exists
Returns the list with the vmids of the duplicated machines
Args:
name: Name of the machine
mac: Mac of the machine
Returns:
List of duplicated machines
Note:
Maybe we can only check name or mac, or both, depending on the service
"""
with self.provider().get_connection() as api:
vms = api.list_vms()
return [
vm.opaque_ref
for vm in vms
if vm.name == name
] # Only check for name, mac is harder to get, so by now, we only check for name
def start_deploy_of_template(self, name: str, comments: str) -> str:
"""