|
39 | 39 | import logging
|
40 | 40 | from jormungandr.protobuf_to_dict import protobuf_to_dict
|
41 | 41 | from jormungandr.exceptions import ApiNotFound, RegionNotFound, DeadSocketException, InvalidArguments
|
42 |
| -from jormungandr import authentication, cache, app |
| 42 | +from jormungandr.authentication import abort_request, can_read_user |
| 43 | +from jormungandr import authentication, cache, memory_cache, app |
43 | 44 | from jormungandr.instance import Instance
|
44 | 45 | import gevent
|
45 | 46 | import os
|
@@ -99,7 +100,7 @@ def __init__(
|
99 | 100 | self.start_ping = start_ping
|
100 | 101 | self.instances = {}
|
101 | 102 | self.context = zmq.Context()
|
102 |
| - self.is_ready = False |
| 103 | + self.is_ready = False # type: bool |
103 | 104 |
|
104 | 105 | def __repr__(self):
|
105 | 106 | return '<InstanceManager>'
|
@@ -297,7 +298,7 @@ def get_instances(self, name=None, lon=None, lat=None, object_id=None, api='ALL'
|
297 | 298 | else:
|
298 | 299 | # Requests without any coverage
|
299 | 300 | # fetch all the authorized instances (free + private) using cached function has_access()
|
300 |
| - authorized_instances = self._get_authorized_instances(user, api) |
| 301 | + authorized_instances = self.get_all_available_instances(user) |
301 | 302 | if not authorized_instances:
|
302 | 303 | # user doesn't have access to any of the instances
|
303 | 304 | context = 'User has no access to any instance'
|
@@ -341,3 +342,26 @@ def regions(self, region=None, lon=None, lat=None, request_id=None):
|
341 | 342 | resp_dict['region_id'] = key_region
|
342 | 343 | response['regions'].append(resp_dict)
|
343 | 344 | return response
|
| 345 | + |
| 346 | + @memory_cache.memoize(app.config[str('MEMORY_CACHE_CONFIGURATION')].get(str('TIMEOUT_AUTHENTICATION'), 30)) |
| 347 | + @cache.memoize(app.config[str('CACHE_CONFIGURATION')].get(str('TIMEOUT_AUTHENTICATION'), 300)) |
| 348 | + def get_all_available_instances(self, user): |
| 349 | + result = [] |
| 350 | + if app.config.get('PUBLIC', False) or app.config.get('DISABLE_DATABASE', False): |
| 351 | + return list(self.instances.values()) |
| 352 | + |
| 353 | + if not user: |
| 354 | + logging.getLogger(__name__).warning('get all available instances no user') |
| 355 | + # for not-public navitia a user is mandatory |
| 356 | + # To manage database error of the following type we should fetch one more time from database |
| 357 | + # Can connect to database but at least one table/attribute is not accessible due to transaction problem |
| 358 | + if can_read_user(): |
| 359 | + abort_request(user=user) |
| 360 | + else: |
| 361 | + return result |
| 362 | + |
| 363 | + bdd_instances = user.get_all_available_instances() |
| 364 | + for bdd_instance in bdd_instances: |
| 365 | + if bdd_instance.name in self.instances: |
| 366 | + result.append(self.instances[bdd_instance.name]) |
| 367 | + return result |
0 commit comments