Connect platform tenant creation to tenant-engine
All checks were successful
CI Smoke / host-smoke (push) Successful in 1s
CI Smoke / container-smoke (push) Successful in 2s

This commit is contained in:
tegwick 2026-08-09 01:34:36 +02:00
parent 6589294ae7
commit c0da589dbe
2 changed files with 56 additions and 8 deletions

View file

@ -22,11 +22,11 @@ class HTTPTenantManagementAdapter:
correlation_id: str, correlation_id: str,
) -> TenantProvisioningResult: ) -> TenantProvisioningResult:
request = Request( request = Request(
self.base_url + "/v1/tenants", self.base_url + "/tenants",
data=json.dumps({ data=json.dumps({
"tenant": tenant, "display_name": display_name, "tenant_id": tenant,
"idempotency_key": idempotency_key, "identifier": tenant,
"correlation_id": correlation_id, "actor": "tenant-engine",
}).encode(), }).encode(),
headers={ headers={
"Authorization": f"Bearer {self.bearer_token}", "Authorization": f"Bearer {self.bearer_token}",
@ -41,12 +41,16 @@ class HTTPTenantManagementAdapter:
result = json.loads(response.read()) result = json.loads(response.read())
except HTTPError as exc: except HTTPError as exc:
exc.read(4096) exc.read(4096)
if exc.code == 409:
return TenantProvisioningResult(
tenant=tenant, status="existing", resumed=True, external_ref=tenant,
)
raise RuntimeError(f"tenant authority failed ({exc.code})") from exc raise RuntimeError(f"tenant authority failed ({exc.code})") from exc
except URLError as exc: except URLError as exc:
raise RuntimeError("tenant authority unavailable") from exc raise RuntimeError("tenant authority unavailable") from exc
return TenantProvisioningResult( return TenantProvisioningResult(
tenant=str(result.get("tenant") or tenant), tenant=str(result.get("identifier") or tenant),
status=str(result["status"]), status="created",
resumed=bool(result.get("resumed", False)), resumed=False,
external_ref=str(result["external_ref"]) if result.get("external_ref") else None, external_ref=str(result.get("tenant_id") or tenant),
) )

View file

@ -0,0 +1,44 @@
import io
import json
import unittest
from unittest.mock import patch
from user_engine.adapters.tenant_management import HTTPTenantManagementAdapter
class _Response(io.BytesIO):
def __enter__(self):
return self
def __exit__(self, *_args):
self.close()
class TenantManagementAdapterTests(unittest.TestCase):
def test_uses_tenant_engine_contract(self):
body = _Response(json.dumps({
"tenant_id": "tenant:friendly:new",
"identifier": "tenant:friendly:new",
"grouping": "friendly",
}).encode())
adapter = HTTPTenantManagementAdapter(
base_url="http://tenant-engine", bearer_token="opaque"
)
with patch("user_engine.adapters.tenant_management.urlopen", return_value=body) as call:
result = adapter.create_tenant(
tenant="tenant:friendly:new", display_name="New",
idempotency_key="tenant-create-123", correlation_id="corr-1",
)
request = call.call_args.args[0]
self.assertEqual(request.full_url, "http://tenant-engine/tenants")
self.assertEqual(json.loads(request.data), {
"tenant_id": "tenant:friendly:new",
"identifier": "tenant:friendly:new",
"actor": "tenant-engine",
})
self.assertEqual(result.status, "created")
self.assertEqual(result.external_ref, "tenant:friendly:new")
if __name__ == "__main__":
unittest.main()