From 8d93144e24878d80dc919d1db361ca86f5641495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adolfo=20G=C3=B3mez=20Garc=C3=ADa?= Date: Fri, 28 Jan 2022 11:54:18 +0100 Subject: [PATCH] Fixed meta pools non being correctly checked --- .../uds/REST/methods/meta_service_pools.py | 2 +- server/src/uds/core/managers/user_service.py | 55 +++++++++++++++---- server/src/uds/web/views/service.py | 16 ++++-- 3 files changed, 55 insertions(+), 18 deletions(-) diff --git a/server/src/uds/REST/methods/meta_service_pools.py b/server/src/uds/REST/methods/meta_service_pools.py index 91d7e466a..638220712 100644 --- a/server/src/uds/REST/methods/meta_service_pools.py +++ b/server/src/uds/REST/methods/meta_service_pools.py @@ -154,7 +154,7 @@ class MetaAssignedService(DetailHandler): return UserService.objects.filter( uuid=processUuid(userServiceId), cache_level=0, - eployed_service__in=[i.pool for i in metaPool.members.all()], + deployed_service__in=[i.pool for i in metaPool.members.all()], )[0] except Exception: pass diff --git a/server/src/uds/core/managers/user_service.py b/server/src/uds/core/managers/user_service.py index 45b1d57da..ab6fcbd65 100644 --- a/server/src/uds/core/managers/user_service.py +++ b/server/src/uds/core/managers/user_service.py @@ -702,23 +702,27 @@ class UserServiceManager(metaclass=singleton.Singleton): logger.debug('Kind of service: %s, idService: %s', kind, uuidService) userService: typing.Optional[UserService] = None - if kind == 'A': # This is an assigned service + if kind in 'A': # This is an assigned service logger.debug('Getting A service %s', uuidService) userService = UserService.objects.get(uuid=uuidService, user=user) typing.cast(UserService, userService).deployed_service.validateUser(user) else: - servicePool: ServicePool = ServicePool.objects.get(uuid=uuidService) - # We first do a sanity check for this, if the user has access to this service - # If it fails, will raise an exception - servicePool.validateUser(user) + try: + servicePool: ServicePool = ServicePool.objects.get(uuid=uuidService) + # We first do a sanity check for this, if the user has access to this service + # If it fails, will raise an exception + servicePool.validateUser(user) - # Now we have to locate an instance of the service, so we can assign it to user. - if ( - create - ): # getAssignation, if no assignation is found, tries to create one - userService = self.getAssignationForUser(servicePool, user) - else: # Sometimes maybe we only need to locate the existint user service - userService = self.getExistingAssignationForUser(servicePool, user) + # Now we have to locate an instance of the service, so we can assign it to user. + if ( + create + ): # getAssignation, if no assignation is found, tries to create one + userService = self.getAssignationForUser(servicePool, user) + else: # Sometimes maybe we only need to locate the existint user service + userService = self.getExistingAssignationForUser(servicePool, user) + except ServicePool.DoesNotExist: + logger.debug('Service pool does not exist') + return None logger.debug('Found service: %s', userService) @@ -900,6 +904,33 @@ class UserServiceManager(metaclass=singleton.Singleton): code=serviceNotReadyCode, userService=userService, transport=transport ) + def isMetaService(self, metaId: str) -> bool: + return metaId[0] == 'M' + + def locateMetaService( + self, user: User, idService: str, create: bool = False + ) -> typing.Optional[UserService]: + kind, uuidMetapool = idService[0], idService[1:] + if kind != 'M': + return None + + meta: MetaPool = MetaPool.objects.get(uuid=uuidMetapool) + # Get pool members. Just pools "visible" and "usable" + pools = [ + p.pool for p in meta.members.all() if p.pool.isVisible() and p.pool.isUsable() + ] + # look for an existing user service in the pool + try: + return UserService.objects.filter( + deployed_service__in=pools, + state__in=State.VALID_STATES, + user=user, + cache_level=0, + ).order_by('deployed_service__name')[0] + except IndexError: + return None + + def getMeta( self, user: User, diff --git a/server/src/uds/web/views/service.py b/server/src/uds/web/views/service.py index 282c10bd9..e60aeb3fa 100644 --- a/server/src/uds/web/views/service.py +++ b/server/src/uds/web/views/service.py @@ -51,6 +51,7 @@ from uds.web.util import services # Not imported at runtime, just for type checking if typing.TYPE_CHECKING: from uds.core.util.request import ExtendedHttpRequest, ExtendedHttpRequestWithUser + from uds.models import UserService logger = logging.getLogger(__name__) @@ -155,12 +156,17 @@ def userServiceStatus( Note: ''' ip: typing.Union[str, None, bool] - userService = None + userService: typing.Optional['UserService'] = None status = 'running' - # If service exists - userService = userServiceManager().locateUserService( - user=request.user, idService=idService, create=False - ) + # If service exists (meta or not) + if userServiceManager().isMetaService(idService): + userService = userServiceManager().locateMetaService( + user=request.user, idService=idService + ) + else: + userService = userServiceManager().locateUserService( + user=request.user, idService=idService, create=False + ) if userService: # Service exists... try: