Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish "Tenants.changed!" when new tenants are loaded #618

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 40 additions & 34 deletions asab/web/tenant/service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import logging
import typing

import aiohttp

Expand Down Expand Up @@ -31,29 +32,17 @@ def __init__(self, app, service_name="asab.TenantService"):
self.App = app
self.TenantWebHandler = None
self.TenantsTrusted = Config.getboolean("tenants", "trusted")
self.Tenants = set()

# Load tenants from configuration
for tenant_id in re.split(r"[,\s]+", Config.get("tenants", "ids"), flags=re.MULTILINE):
tenant_id = tenant_id.strip()
# Skip comments and empty lines
if len(tenant_id) == 0:
continue
if tenant_id[0] == '#':
continue
if tenant_id[0] == ';':
continue
self.Tenants.add(tenant_id)
self.Tenants: typing.Set[str] = set()
self._StaticTenants: typing.Set[str] = _read_tenants_from_config()

# Load tenants from URL
self.TenantUrl = Config.get("tenants", "tenant_url")
if len(self.TenantUrl) > 0:
app.PubSub.subscribe("Application.tick/300!", self._update_tenants)


async def initialize(self, app):
if len(self.TenantUrl) > 0:
await self._update_tenants()
# TODO: Websocket persistent API should be added to seacat auth to feed these changes in realtime (eventually)
app.PubSub.subscribe("Application.tick/300!", self._update_tenants)
await self._update_tenants()


def locate_tenant(self, tenant_id):
Expand All @@ -66,11 +55,6 @@ def locate_tenant(self, tenant_id):
return None


def get_tenant_ids(self):
# TODO: REMOVE?
return self.get_tenants()


def get_tenants(self):
return list(self.Tenants)

Expand All @@ -81,15 +65,37 @@ def add_web_api(self, web_container):


async def _update_tenants(self, message_name=None):
async with aiohttp.ClientSession() as session:
async with session.get(self.TenantUrl) as resp:
if resp.status == 200:
tenants_list = await resp.json()
for tenant in tenants_list:
if isinstance(tenant, str):
self.Tenants.add(tenant)
elif isinstance(tenant, dict) and "_id" in tenant:
# TODO: REMOVE?
self.Tenants.add(tenant["_id"])
else:
L.warning("Unknown tenant format: {}".format(tenant))
new_tenants = set()

if len(self.TenantUrl) > 0:
async with aiohttp.ClientSession() as session:
async with session.get(self.TenantUrl) as resp:
if resp.status == 200:
external_tenants = await resp.json()
else:
L.warning("Failed to load tenants.", struct_data={"url": self.TenantUrl})
return

new_tenants.update(external_tenants)

if len(self._StaticTenants) > 0:
new_tenants.update(self._StaticTenants)

if self.Tenants != new_tenants:
self.App.PubSub.publish("Tenants.change!")
self.Tenants = new_tenants


def _read_tenants_from_config() -> typing.Set[str]:
tenants = set()
for tenant_id in re.split(r"[,\s]+", Config.get("tenants", "ids"), flags=re.MULTILINE):
tenant_id = tenant_id.strip()
# Skip comments and empty lines
if len(tenant_id) == 0:
continue
if tenant_id[0] == '#':
continue
if tenant_id[0] == ';':
continue
tenants.add(tenant_id)
return tenants
Loading