:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
"""Adapter for the org-scoped managed warehouse provisioning API (duckgres).
Centralizes everything the `DataWarehouseViewSet` provisioning actions need so the
ViewSet stays a thin pass-through: org-scoped feature gating, duckgres org-URL
construction and request/error mapping, warehouse-name validation (shared by provision
and availability checks), and connection presentation.
A managed warehouse is shared by every team in an organization, so the duckgres org
identifier is the PostHog `organization_id` and the Data ops feature flag is evaluated
per organization (not per team).
"""
import re
from collections.abc import Callable
from datetime import date
from functools import partial
from typing import Literal, TypedDict, cast
from uuid import UUID
from django.conf import settings
import requests as http_requests
import structlog
import posthoganalytics
from rest_framework import status
from rest_framework.response import Response
from posthog.security.outbound_proxy import internal_requests
from products.managed_warehouse.backend.facade.access import (
background_loading_configured,
enable_customer_warehouse_access,
is_internal_warehouse,
)
from products.managed_warehouse.backend.facade.feature_flags import DATA_WAREHOUSE_SCENE_FLAG, MANAGED_TRINO_QUERY_FLAG
from products.managed_warehouse.backend.presentation import hogtower
logger = structlog.get_logger(__name__)
# The Postgres database to connect to is always "ducklake"; the user-chosen warehouse
# name becomes the SNI subdomain (e.g. my-warehouse.dw.us.postwh.com) and the DNS zone
# is selected by the deployment.
MANAGED_WAREHOUSE_DATABASE = "ducklake"
_MANAGED_WAREHOUSE_DOMAINS = {
"US": "us.postwh.com",
"EU": "eu.postwh.com",
"DEV": "dev.postwh.com",
}
# A warehouse name becomes a DNS-1123 label (the connection's SNI subdomain), so it must
# be lowercase alphanumerics and hyphens, starting and ending alphanumeric — no
# underscores. Mirrors duckgres's own org-id constraint.
WAREHOUSE_NAME_MIN_LENGTH = 3
WAREHOUSE_NAME_MAX_LENGTH = 63
WAREHOUSE_NAME_PATTERN = re.compile(r"^[a-z]([a-z0-9-]*[a-z0-9])?$")
class PresentedConnection(TypedDict):
host: str
port: int
database: str
username: str
class PresentedTrinoConnection(TypedDict):
host: str
port: int
catalog: str
username: str
TrinoStatusState = Literal["not_enabled", "pending", "provisioning", "ready", "failed", "unavailable"]
class PresentedTrinoStatus(TypedDict):
state: TrinoStatusState
ready_at: str | None
connection: PresentedTrinoConnection | None
_TRINO_LIFECYCLE_STATES = frozenset({"pending", "provisioning", "ready", "failed"})
ManagedWarehouseMonitoringMetric = Literal[
"query_rate",
"error_ratio",
"duration_p50",
"duration_p95",
"sessions_active",
"s3_bytes_rate",
"acquire_p95",
"acquire_by_source",
"storage_bytes",
"worker_crash_rate",
]
ManagedWarehouseTrinoMonitoringMetric = Literal[
"queries_in_flight",
"query_rate",
"error_ratio",
"duration_p50",
"duration_p95",
"queue_time_p95",
"scanned_bytes_rate",
"cpu_seconds_rate",
"storage_bytes",
]
ManagedWarehouseMonitoringWindow = Literal["1h", "6h", "24h", "7d", "30d"]
DataOpsVariant = Literal["trino", "duckdb"]
def managed_warehouse_domain() -> str:
"""The zone managed warehouse hostnames are served under in this deployment.
The value is shared with `managed_ingress`, which the warehouse-source host guard reads. It
is defined here because a presentation module cannot import a plain backend module without
breaking the facade contract.
"""
deployment = (getattr(settings, "CLOUD_DEPLOYMENT", None) or "").upper()
return _MANAGED_WAREHOUSE_DOMAINS.get(deployment, "test.local")
def validate_warehouse_name(name: str | None) -> str | None:
"""Return a human-readable error if `name` is not a valid warehouse name, else None."""
if not name:
return "database_name is required"
if not (WAREHOUSE_NAME_MIN_LENGTH <= len(name) <= WAREHOUSE_NAME_MAX_LENGTH):
return f"Warehouse name must be {WAREHOUSE_NAME_MIN_LENGTH}-{WAREHOUSE_NAME_MAX_LENGTH} characters"
if not WAREHOUSE_NAME_PATTERN.match(name):
return (
"Warehouse name must be a DNS label: lowercase letters, numbers, and hyphens, "
"starting and ending with a letter or number"
)
return None
def _organization_flag_enabled(flag: str, organization_id: UUID | str) -> bool:
"""Evaluate one managed-warehouse flag for the organization.
Identity is the organization so every team in the org resolves the same value.
"""
org_id = str(organization_id)
try:
return bool(
posthoganalytics.feature_enabled(
flag,
org_id,
groups={"organization": org_id},
group_properties={"organization": {"id": org_id}},
only_evaluate_locally=True,
send_feature_flag_events=False,
)
)
except Exception:
logger.warning("Failed to evaluate managed warehouse feature flag", organization_id=org_id, flag=flag)
return False
def data_ops_variant(organization_id: UUID | str) -> DataOpsVariant | None:
"""Which Data ops variant the organization gets, or None when it has neither flag.
The Trino flag wins when both are on, so an organization never moves between variants
because of Trino's state.
"""
variant: DataOpsVariant | None = None
if _organization_flag_enabled(MANAGED_TRINO_QUERY_FLAG, organization_id):
return "trino" if enable_customer_warehouse_access(organization_id, trino_enabled=True) else None
elif _organization_flag_enabled(DATA_WAREHOUSE_SCENE_FLAG, organization_id):
variant = "duckdb"
return None if variant is not None and is_internal_warehouse(organization_id) else variant
def is_enabled(organization_id: UUID | str) -> bool:
"""Whether the organization has the managed warehouse through either flag."""
return data_ops_variant(organization_id) is not None
def _present_connection(raw: dict) -> PresentedConnection:
"""Build the public-facing connection from duckgres's raw status connection.
duckgres returns the org's chosen warehouse name as `database`; that becomes the SNI
subdomain of the host, and the database to connect to is always "ducklake".
"""
warehouse_name = raw.get("database")
host = f"{warehouse_name}.dw.{managed_warehouse_domain()}" if warehouse_name else raw.get("host", "")
return PresentedConnection(
host=host,
port=getattr(settings, "DUCKGRES_PG_PORT", 5432),
database=MANAGED_WAREHOUSE_DATABASE,
username=raw.get("username", "root"),
)
def _request(
method: str,
organization_id: UUID | str,
path: str,
json_body: dict | None = None,
params: dict | None = None,
timeout: int = 30,
require_enabled: bool = True,
) -> Response:
"""Proxy a request to the managed-warehouse control plane, gated on the org's feature flag.
The control plane is duckgres (`DUCKGRES_API_URL`, /api/v1) unless `HOGTOWER_API_URL`
is set, in which case the same call is served by hogtower's /api/v2 through
`products.managed_warehouse.backend.presentation.hogtower`, which returns the duckgres v1 body.
An empty path targets the org resource itself (`/api/v1/orgs/{org}`, e.g. to delete it);
paths starting with "/" are org-scoped (`/api/v1/orgs/{org}{path}`); others are global
API paths (`/api/v1/{path}`).
`require_enabled` gates on the user-facing managed warehouse flags and is the right
default for UI-driven calls. Backend/background callers (e.g. the Dagster duckling
backfill) must pass `require_enabled=False`: the flag is evaluated only-locally and a
worker without the flag definition loaded would otherwise get a spurious 403 even when
the control plane can answer.
"""
if require_enabled and not is_enabled(organization_id):
return Response({"error": "This feature is not enabled"}, status=status.HTTP_403_FORBIDDEN)
org_id = str(organization_id)
send: Callable[[], http_requests.Response | hogtower.TranslatedResponse]
if hogtower.is_configured():
# hogtower serves the same calls from its /api/v2 routes, translated back to the
# duckgres v1 bodies below.
send = partial(hogtower.request, method, org_id, path, json_body=json_body, params=params, timeout=timeout)
else:
base_url = getattr(settings, "DUCKGRES_API_URL", None)
token = getattr(settings, "DUCKGRES_INTERNAL_SECRET", None)
if not base_url:
logger.warning("Provisioning request rejected: DUCKGRES_API_URL not configured", organization_id=org_id)
return Response(
{"error": "Managed warehouse provisioning is not configured"},
status=status.HTTP_501_NOT_IMPLEMENTED,
)
if path == "":
url = f"{base_url.rstrip('/')}/api/v1/orgs/{org_id}"
elif path.startswith("/"):
url = f"{base_url.rstrip('/')}/api/v1/orgs/{org_id}{path}"
else:
url = f"{base_url.rstrip('/')}/api/v1/{path}"
headers = {}
if token:
headers["X-Duckgres-Internal-Secret"] = token
send = partial(
internal_requests.request, method, url, json=json_body, params=params, headers=headers, timeout=timeout
)
try:
resp = send()
except http_requests.Timeout:
logger.warning("Provisioning API timeout", method=method, path=path, organization_id=org_id)
return Response({"error": "Provisioning service timed out"}, status=status.HTTP_504_GATEWAY_TIMEOUT)
except http_requests.ConnectionError:
logger.warning("Provisioning API connection refused", method=method, path=path, organization_id=org_id)
return Response({"error": "Provisioning service is unreachable"}, status=status.HTTP_502_BAD_GATEWAY)
except Exception:
logger.exception("Provisioning API unexpected error", method=method, path=path, organization_id=org_id)
return Response(
{"error": "An error occurred contacting the provisioning service"},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
if resp.status_code >= 400:
logger.warning(
"Provisioning API returned error",
method=method,
path=path,
organization_id=org_id,
status_code=resp.status_code,
response_body=resp.text[:500],
)
else:
logger.info("Provisioning API request succeeded", method=method, path=path, organization_id=org_id)
try:
body = resp.json()
except ValueError:
body = {"error": resp.text[:500]}
return Response(body, status=resp.status_code)
def _get_project_team_row(*, organization_id: UUID | str, team_id: int) -> dict | None:
"""Fetch the org-team row Duckgres currently holds for this project, if any."""
resp = _request("GET", organization_id, "/teams", require_enabled=False)
if not status.is_success(resp.status_code) or not isinstance(resp.data, dict):
raise RuntimeError("Failed to read the organization's managed warehouse team rows")
for row in resp.data.get("teams") or []:
if isinstance(row, dict) and _row_team_id(row) == team_id:
return row
return None
def _row_team_id(row: dict) -> int | None:
"""A control-plane row's team id as int, tolerating a string serialization."""
try:
return int(row["team_id"])
except (KeyError, TypeError, ValueError):
return None
def _block_if_pending_deletion(organization_id: UUID | str) -> Response | None:
"""Refuse warehouse-creating calls for an organization whose deletion is underway.
Organization deletion is asynchronous: ``perform_destroy`` marks the org
``is_pending_deletion`` and hands off to the Temporal workflow, whose first step
deprovisions the org's managed warehouse. API-key routes bypass the UI's
pending-deletion lockout, so without this guard a caller could provision a NEW
warehouse (or add a team row) after that deprovision step and before the org cascade —
recreating exactly the orphaned-warehouse hole the workflow closes (the fresh
``DuckgresServer`` row is cascade-deleted without ever being deprovisioned). Every
entrypoint that can create duckgres state calls this first; read-only endpoints stay
accessible.
"""
from products.managed_warehouse.backend.facade.api import organization_is_pending_deletion # noqa: PLC0415
if organization_is_pending_deletion(organization_id):
return Response(
{"error": "This organization is pending deletion; its managed warehouse can no longer be modified."},
status=status.HTTP_409_CONFLICT,
)
return None
def provision(
organization_id: UUID | str,
database_name: str | None,
team_id: int,
schema_name: str | None,
require_enabled: bool = True,
*,
triggered_by: str,
internal_only: bool = False,
) -> Response:
pending_deletion = _block_if_pending_deletion(organization_id)
if pending_deletion is not None:
return pending_deletion
name_error = validate_warehouse_name(database_name)
if name_error:
return Response({"error": name_error}, status=status.HTTP_400_BAD_REQUEST)
# Validate the schema name up front: the duckling backfill setup runs best-effort after the
# provision call, so a bad name there would be swallowed — catch it before provisioning.
schema_name_error = _validate_schema_name(schema_name)
if schema_name_error or schema_name is None:
return Response({"error": schema_name_error or "schema_name is required"}, status=status.HTTP_400_BAD_REQUEST)
if internal_only and require_enabled:
return Response({"error": "Internal provisioning requires the staff admin."}, status=status.HTTP_403_FORBIDDEN)
if internal_only:
from products.managed_warehouse.backend.facade.connection import reserve_internal_warehouse # noqa: PLC0415
if not reserve_internal_warehouse(organization_id=organization_id):
return Response(
{"error": "Deprovision the warehouse before enabling internal testing mode."},
status=status.HTTP_409_CONFLICT,
)
existing_internal = is_internal_warehouse(organization_id)
if _org_has_warehouse(organization_id) and existing_internal != internal_only:
return Response(
{"error": "Deprovision the warehouse before changing its internal testing mode."},
status=status.HTTP_409_CONFLICT,
)
source_generation = _managed_source_generation(organization_id)
resp = _request(
"POST",
organization_id,
"/provision",
json_body={
"database_name": database_name,
# The provisioning team becomes the warehouse's first team: duckgres creates
# its team row with this schema. In-product this is the calling (currently
# active) team; in the Django admin it's the mandatory team field on the
# provision form.
"team_id": team_id,
"schema_name": schema_name,
"ducklake": {"enabled": True},
"metadata_store": {"type": "cnpg-shard"},
"data_store": {"type": "s3bucket"},
# Onboarding also enrolls the org in the shared Trino cell, so Trino is
# available as a compute engine over the same DuckLake catalog the duckgres
# server reads. The control plane writes the opt-in row inside the provision
# transaction and its provisioner claims the org into a cell on the next
# reconcile. Idempotent, so re-provisioning is safe; omitting the key (the
# behavior before this) means PG-only, which is not the same as sending
# enabled=false — that is also a no-op, never a disable.
"trino": {"enabled": True},
},
require_enabled=require_enabled,
)
if status.is_success(resp.status_code):
logger.info(
"managed_warehouse_action",
action="provision",
internal_only=internal_only,
triggered_by=triggered_by,
organization_id=str(organization_id),
team_id=team_id,
database_name=database_name,
schema_name=schema_name,
)
if status.is_success(resp.status_code) and isinstance(resp.data, dict):
activated_generation = _activate_managed_source_lifecycle(
organization_id,
expected_generation=source_generation,
internal_only=internal_only,
)
if activated_generation is None:
logger.warning(
"Skipping stale managed warehouse provision completion",
organization_id=str(organization_id),
expected_generation=source_generation,
)
return resp
_invalidate_team_state_cache(organization_id)
_persist_duckgres_server(organization_id, database_name, resp.data)
# Complete the row BEFORE registering the team: registration kicks off the
# SQL-editor query-source setup and discovery against this row's warehouse.
_complete_provisioning_team_row(organization_id, team_id, schema_name, require_enabled=require_enabled)
_register_provisioning_team(organization_id, team_id, activated_generation)
# The bucket is internal infra detail, persisted above and consumed by the
# backfill via cp_bucket_for — not part of the UI-facing ProvisionWarehouseResponse
# schema. Strip it so the response matches its OpenAPI contract.
_strip_bucket_fields(resp.data)
return resp
def _complete_provisioning_team_row(
organization_id: UUID | str, team_id: int, schema_name: str, *, require_enabled: bool
) -> None:
"""Pin the first team's legacy table names onto the row duckgres just created.
The provision body cannot carry them, so without this step the row describes the derived
layout no data lands in yet (see onboard_team). Best-effort:
the warehouse is already provisioned, so a transient failure must not fail the provision —
re-running onboarding completes the row later.
"""
legacy = _legacy_table_fields(schema_name)
resp = create_team(
organization_id,
team_id,
schema_name,
events_table_name=legacy["events_table_name"],
persons_table_name=legacy["persons_table_name"],
schema_data_imports_name=legacy["schema_data_imports_name"],
require_enabled=require_enabled,
)
if not status.is_success(resp.status_code):
logger.warning(
"Provisioned warehouse team row could not be completed with legacy table names",
organization_id=str(organization_id),
team_id=team_id,
status_code=resp.status_code,
)
def _strip_bucket_fields(body: dict) -> None:
"""Drop the internal bucket fields from a UI-facing response body, in place."""
body.pop("bucket", None)
body.pop("bucket_region", None)
def _validate_schema_name(name: str | None) -> str | None:
"""Return an error message if `name` isn't a valid duckgres schema name, else None."""
# Keep ducklake.common (and its duckdb dependency) off the API import path.
from products.managed_warehouse.backend.facade.api import validate_schema_name # noqa: PLC0415
return validate_schema_name(name)
def team_backfill_state(team_id: int) -> dict[str, object]:
"""Return the calling team's duckling backfill state for the warehouse-status response."""
# Keep ducklake.common (and its duckdb dependency) off the API import path.
from products.managed_warehouse.backend.facade.api import get_team_backfill_state # noqa: PLC0415
state = get_team_backfill_state(team_id)
return {"has_backfill": state.has_backfill, "table_suffix": state.table_suffix}
def _register_provisioning_team(organization_id: UUID | str, team_id: int, source_generation: int) -> None:
"""Finish the provisioning (calling) team's onboarding after its row exists.
duckgres creates the provisioning team's row from the provision request itself (and
`_complete_provisioning_team_row` pins its legacy table names), so nothing is written
here. The team gets its managed SQL-editor source and starts its earliest-event-date
sync, matching the tail that `onboard_team` runs later.
Best-effort, mirroring `_persist_duckgres_server`: a failure is logged, not raised, so
the one-time provision password is never lost to it.
"""
try:
_finish_team_loading_setup(organization_id, team_id, source_generation)
except Exception:
logger.exception("Failed to register provisioning team after provision", team_id=team_id)
def _finish_team_loading_setup(organization_id: UUID | str, team_id: int, source_generation: int) -> None:
_schedule_earliest_event_date_sync(team_id)
if background_loading_configured(organization_id):
from django.db import transaction # noqa: PLC0415
from products.managed_warehouse.backend.facade.tasks import load_internal_warehouse # noqa: PLC0415
transaction.on_commit(lambda: load_internal_warehouse.delay(str(organization_id), team_id, source_generation))
if not is_internal_warehouse(organization_id):
_ensure_direct_source(team_id, organization_id, source_generation)
def _active_managed_source_generation(organization_id: UUID | str) -> int | None:
from products.managed_warehouse.backend.facade.connection import ( # noqa: PLC0415
get_active_managed_warehouse_source_generation,
)
return get_active_managed_warehouse_source_generation(organization_id=organization_id)
def _managed_source_generation(organization_id: UUID | str) -> int:
from products.managed_warehouse.backend.facade.connection import ( # noqa: PLC0415
get_managed_warehouse_source_generation,
)
return get_managed_warehouse_source_generation(organization_id=organization_id)
def _activate_managed_source_lifecycle(
organization_id: UUID | str,
*,
expected_generation: int,
internal_only: bool = False,
) -> int | None:
from products.managed_warehouse.backend.facade.connection import ( # noqa: PLC0415
activate_managed_warehouse_source_lifecycle,
)
return activate_managed_warehouse_source_lifecycle(
organization_id=organization_id,
expected_generation=expected_generation,
internal_only=internal_only,
)
def _deactivate_managed_source_lifecycle(
organization_id: UUID | str,
*,
expected_generation: int,
) -> int | None:
from products.managed_warehouse.backend.facade.connection import ( # noqa: PLC0415
deactivate_managed_warehouse_source_lifecycle,
)
return deactivate_managed_warehouse_source_lifecycle(
organization_id=organization_id,
expected_generation=expected_generation,
)
def _ensure_direct_source(team_id: int, organization_id: UUID | str, source_generation: int) -> None:
try:
from products.managed_warehouse.backend.facade.connection import ( # noqa: PLC0415
ensure_managed_warehouse_direct_source,
)
ensure_managed_warehouse_direct_source(
team_id=team_id,
organization_id=organization_id,
expected_generation=source_generation,
)
except Exception:
logger.exception("Failed to register managed warehouse query source", team_id=team_id)
try:
from products.data_warehouse.backend.facade.api import ( # noqa: PLC0415
schedule_managed_warehouse_direct_source_ensure,
)
schedule_managed_warehouse_direct_source_ensure(
team_id=team_id,
organization_id=organization_id,
expected_generation=source_generation,
)
except Exception:
logger.exception("Failed to schedule managed warehouse query source recovery", team_id=team_id)
def _persist_duckgres_server(organization_id: UUID | str, database_name: str | None, body: dict) -> None:
"""Persist the org's DuckgresServer row from a successful provision response.
The Dagster duckling backfill connects via this row, so provisioning must leave it in
place. The connection mirrors `_present_connection` (the SNI host + "ducklake" database);
the password is returned only in this provision response, so it's read from `body`.
Best-effort: a persistence failure is logged but never raised, because the provision
password is shown to the user exactly once (here) and must not be lost to a DB hiccup —
the row can be reconciled later from the warehouse status.
"""
# Keep ducklake.common (and its duckdb dependency) off the API import path.
from products.managed_warehouse.backend.facade.api import ( # noqa: PLC0415
default_bucket_region,
persist_duckgres_server_for_org,
)
# The control plane is the single owner of the bucket name — it provisions
# the bucket, pins the name on the Duckling CR's spec.dataStore.bucketName,
# and returns it here. Persist it verbatim; never re-derive (the old local
# derivation drifted from the Crossplane composition and named buckets that
# don't exist). A response without a bucket (external data store, or a CP too
# old to return it) leaves the column unset — upsert treats None as "leave
# unset" — and status_for()'s self-heal fills it in on the next status read.
bucket: str | None = body.get("bucket")
# Region from the response too when present, so a CP outside this deployment's
# home region isn't silently mis-recorded; the fallback is the deployment's own
# managed-warehouse region. None when there's no bucket to region.
bucket_region: str | None = (body.get("bucket_region") or default_bucket_region()) if bucket else None
try:
connection = _present_connection({"database": database_name, "username": body.get("username", "root")})
persist_duckgres_server_for_org(
organization_id,
host=connection["host"],
port=connection["port"],
database=connection["database"],
username=connection["username"],
password=body.get("password", ""),
bucket=bucket,
bucket_region=bucket_region,
)
except Exception:
logger.exception("Failed to persist DuckgresServer after provision", organization_id=str(organization_id))
def list_teams(organization_id: UUID | str, require_enabled: bool = True) -> Response:
"""List the org's duckgres team rows (schema names, legacy table names)."""
return _request("GET", organization_id, "/teams", require_enabled=require_enabled)
def list_all_teams() -> Response:
"""List every duckgres team row across orgs (global internal endpoint).
Backend-only: feeds the cp-mode sensor enumeration in products.managed_warehouse.backend.cp_teams,
which is why the feature-flag gate is bypassed.
"""
return _request("GET", "", "teams", require_enabled=False)
def create_team(
organization_id: UUID | str,
team_id: int,
schema_name: str | None,
*,
enabled: bool | None = None,
backfill_enabled: bool | None = None,
events_table_name: str | None = None,
persons_table_name: str | None = None,
schema_data_imports_name: str | None = None,
require_enabled: bool = True,
) -> Response:
"""Upsert a team row in the org's duckgres warehouse.
duckgres answers 409 when the schema name is already used by another team in the org.
Legacy table-name fields are only sent when set — leaving them NULL makes duckgres derive
the layout (`<schema_name>.events`, `<schema_name>.persons`, `<schema_name>_data_imports.*`);
grandfathered teams pass their explicit legacy names instead.
"""
schema_error = _validate_schema_name(schema_name)
if schema_error:
return Response({"error": schema_error}, status=status.HTTP_400_BAD_REQUEST)
body: dict = {"team_id": team_id, "schema_name": schema_name}
optional_fields = {
"enabled": enabled,
"backfill_enabled": backfill_enabled,
"events_table_name": events_table_name,
"persons_table_name": persons_table_name,
"schema_data_imports_name": schema_data_imports_name,
}
body.update({key: value for key, value in optional_fields.items() if value is not None})
resp = _request("POST", organization_id, "/teams", json_body=body, require_enabled=require_enabled)
if status.is_success(resp.status_code):
_invalidate_team_state_cache(organization_id)
return resp
def update_team(
organization_id: UUID | str,
team_id: int,
*,
require_enabled: bool = True,
**fields: object,
) -> Response:
"""Update fields on an existing duckgres team row via the admin PUT endpoint.
Presence-aware on the duckgres side: only the fields present in the body change, so
callers pass exactly what they want written (e.g. just ``earliest_event_date``).
"""
resp = _request(
"PUT", organization_id, f"/teams/{team_id}", json_body=dict(fields), require_enabled=require_enabled
)
if status.is_success(resp.status_code) or resp.status_code == status.HTTP_404_NOT_FOUND:
_invalidate_team_state_cache(organization_id)
return resp
def push_team_earliest_event_date(organization_id: UUID | str, team_id: int, earliest: date | None) -> bool:
"""Persist a team's resolved earliest event date onto its duckgres team row — the
read source for the backfill sensors.
A failure is logged and swallowed; returns True when the control plane accepted the
value, so callers (the provisioning-time task, the full-backfill sensor) can leave
the date unresolved and retry on a later tick.
"""
if earliest is None:
return False
try:
resp = update_team(organization_id, team_id, require_enabled=False, earliest_event_date=earliest.isoformat())
except Exception:
logger.exception(
"Failed to push earliest event date to duckgres",
organization_id=str(organization_id),
team_id=team_id,
)
return False
if not status.is_success(resp.status_code):
logger.warning(
"Duckgres rejected earliest event date push",
organization_id=str(organization_id),
team_id=team_id,
status_code=resp.status_code,
)
return False
return True
def _schedule_earliest_event_date_sync(team_id: int) -> None:
"""Dispatch the earliest-event-date resolution task for a freshly onboarded team.
Best-effort: a dispatch failure is logged, not raised — the full-backfill sensor
resolves the date lazily regardless.
"""
# Keep the Celery task module (and its import graph) off the API import path; the
# facade is the allowed boundary for presentation -> tasks.
from products.data_warehouse.backend.facade.tasks import sync_team_earliest_event_date # noqa: PLC0415
try:
sync_team_earliest_event_date.delay(team_id)
except Exception:
logger.exception("Failed to schedule earliest event date sync", team_id=team_id)
def delete_team(organization_id: UUID | str, team_id: int, require_enabled: bool = True) -> Response:
"""Delete a team row from the org's duckgres warehouse.
duckgres answers 409 for the org's last team (the org must be deprovisioned or deleted
instead).
"""
resp = _request("DELETE", organization_id, f"/teams/{team_id}", require_enabled=require_enabled)
if status.is_success(resp.status_code) or resp.status_code == status.HTTP_404_NOT_FOUND:
_invalidate_team_state_cache(organization_id)
return resp
def _invalidate_team_state_cache(organization_id: UUID | str) -> None:
from products.managed_warehouse.backend.facade.cp_teams import invalidate_team_membership_cache # noqa: PLC0415
invalidate_team_membership_cache(str(organization_id))
def onboard_team(
organization_id: UUID | str,
team_id: int,
schema_name: str | None,
require_enabled: bool = True,
*,
triggered_by: str,
) -> Response:
"""Onboard a team onto the org's existing managed warehouse with its own schema.
The duckgres control plane owns per-team state: it enforces schema uniqueness across
the org's teams (409 on a conflict, mapped to a user-facing message here) and stores
the row the backfill sensors and the v3 sink read.
The schema name is write-once: it names the team's warehouse tables, so changing it
after data is written would split the team's data across two layouts. The control-plane
POST is an upsert, so an already-onboarded team is guarded here — re-onboarding with
the current name is an idempotent no-op, any other name is rejected.
Backend/ops callers (the Django admin) pass `require_enabled=False` to bypass the
org feature-flag gate.
"""
pending_deletion = _block_if_pending_deletion(organization_id)
if pending_deletion is not None:
return pending_deletion
if require_enabled and not is_enabled(organization_id):
return Response({"error": "This feature is not enabled"}, status=status.HTTP_403_FORBIDDEN)
schema_error = _validate_schema_name(schema_name)
if schema_error or schema_name is None:
return Response({"error": schema_error or "schema_name is required"}, status=status.HTTP_400_BAD_REQUEST)
if not _org_has_warehouse(organization_id):
return Response(
{"error": "No managed warehouse is provisioned for this organization. Provision one first."},
status=status.HTTP_400_BAD_REQUEST,
)
source_generation = _active_managed_source_generation(organization_id)
if source_generation is None:
return Response(
{"error": "No managed warehouse is provisioned for this organization. Provision one first."},
status=status.HTTP_400_BAD_REQUEST,
)
try:
existing = _get_project_team_row(organization_id=organization_id, team_id=team_id)
except RuntimeError:
return Response(
{"error": "Could not read the organization's managed warehouse state. Try again in a few minutes."},
status=status.HTTP_502_BAD_GATEWAY,
)
if existing is not None:
if existing.get("schema_name") != schema_name:
events_table = existing.get("events_table_name") or f"events_{existing.get('schema_name')}"
current = "the shared tables" if events_table == "events" else events_table
return Response(
{
"error": f"This project already writes to {current}, and its warehouse table can't be changed — "
"that would split its existing data across two tables."
},
status=status.HTTP_400_BAD_REQUEST,
)
else:
# Pin the legacy table names the duckling DAG writes today (posthog.events_<suffix>,
# posthog_data_imports_<suffix>): the suffix for a newly onboarded team IS its schema
# name. A row without them describes the derived layout no data lands in yet (the EU
# placeholder-row bug). Drop this once the duckling DAG writes the derived
# <schema_name>.events layout for real.
legacy = _legacy_table_fields(schema_name)
resp = create_team(
organization_id,
team_id,
schema_name,
events_table_name=legacy["events_table_name"],
persons_table_name=legacy["persons_table_name"],
schema_data_imports_name=legacy["schema_data_imports_name"],
require_enabled=require_enabled,
)
if resp.status_code == status.HTTP_409_CONFLICT:
return Response(
{"error": f"The schema name '{schema_name}' is already used by another project in this organization."},
status=status.HTTP_409_CONFLICT,
)
if not status.is_success(resp.status_code):
return resp
logger.info(
"managed_warehouse_action",
action="onboard_team",
triggered_by=triggered_by,
organization_id=str(organization_id),
team_id=team_id,
schema_name=schema_name,
created=existing is None,
)
_finish_team_loading_setup(organization_id, team_id, source_generation)
return Response({"onboarded": True, "schema_name": schema_name}, status=status.HTTP_200_OK)
def _org_has_warehouse(organization_id: UUID | str) -> bool:
"""Whether the org has a provisioned managed warehouse (its connection row exists)."""
from products.managed_warehouse.backend.facade.api import has_provisioned_warehouse # noqa: PLC0415
return has_provisioned_warehouse(organization_id)
def _legacy_table_fields(schema_name: str) -> dict:
"""The legacy suffix-derived table names to pin on a new duckgres team row.
The duckling DAG and the v3 sink still write the suffix-derived layout
(`posthog.events_<schema>`, `posthog_data_imports_<schema>`), so a new row pins those
names explicitly instead of leaving duckgres to derive the future
`<schema_name>.events` layout no data lands in yet.
"""
return {
"events_table_name": f"events_{schema_name}",
"persons_table_name": f"persons_{schema_name}",
"schema_data_imports_name": f"posthog_data_imports_{schema_name}",
}
def _teams_from_response(resp: Response) -> list[dict] | None:
"""Extract the team rows from a list-teams response, or None when unusable."""
if not status.is_success(resp.status_code):
return None
data = resp.data
if isinstance(data, dict):
naming_version = data.get("data_imports_table_naming_version")
data = data.get("teams")
if isinstance(data, list) and isinstance(naming_version, str) and naming_version:
data = [
{**row, "data_imports_table_naming_version": naming_version} if isinstance(row, dict) else row
for row in data
]
if not isinstance(data, list):
return None
return [row for row in data if isinstance(row, dict)]
def team_onboarding_state(organization_id: UUID | str, team_id: int) -> dict:
"""Resolve the calling team's duckgres onboarding state for the warehouse-status response.
Best-effort: a status read must never 500, so a failed list-teams call degrades to the
not-onboarded shape.
"""
duckgres_team: dict | None = None
try:
teams = _teams_from_response(list_teams(organization_id, require_enabled=False))
if teams is not None:
duckgres_team = next((row for row in teams if _row_team_id(row) == team_id), None)
except Exception:
logger.exception(
"Failed to resolve duckgres team onboarding state",
organization_id=str(organization_id),
team_id=team_id,
)
schema_name = duckgres_team.get("schema_name") if duckgres_team is not None else None
return {"team_onboarded": duckgres_team is not None, "schema_name": schema_name}
def check_schema_name(organization_id: UUID | str, name: str | None) -> Response:
"""Check whether a schema name is free within the org's warehouse."""
schema_error = _validate_schema_name(name)
if schema_error:
return Response({"error": schema_error}, status=status.HTTP_400_BAD_REQUEST)
resp = list_teams(organization_id)
teams = _teams_from_response(resp)
if teams is None:
return resp
taken = any(row.get("schema_name") == name for row in teams)
return Response({"name": name, "available": not taken}, status=status.HTTP_200_OK)
def block_team_deletion(team_id: int, organization_id: UUID | str) -> str | None:
"""Remove the team from the org's duckgres warehouse ahead of a Django team deletion.
Returns a user-facing error message when the deletion must be blocked, else None.
Narrow coupling: orgs without a managed warehouse never trigger a control-plane call, so
unrelated team deletions are unaffected by a duckgres outage.
duckgres refuses to delete the org's last team (409) — the warehouse must be deprovisioned
(or the organization deleted) first, so the Django deletion is blocked with that guidance.
When the control plane can't confirm the deletion for a team that is warehouse-onboarded,
the deletion is blocked with a retry error rather than silently orphaning the duckgres row.
"""
from products.managed_warehouse.backend.facade.api import has_provisioned_warehouse # noqa: PLC0415
from products.managed_warehouse.backend.facade.team_state import backfill_row_exists # noqa: PLC0415
if not has_provisioned_warehouse(organization_id):
return None
resp = delete_team(organization_id, team_id, require_enabled=False)
if status.is_success(resp.status_code) or resp.status_code == status.HTTP_404_NOT_FOUND:
return None
if resp.status_code == status.HTTP_409_CONFLICT:
return (
"This is the last project in your organization's managed warehouse. "
"Deprovision the managed warehouse in Data ops settings, or delete the organization, "
"before deleting this project."
)
if backfill_row_exists(team_id, str(organization_id)):
return "Could not remove this project from your organization's managed warehouse. Try again in a few minutes."
# Org has a warehouse but this team has no membership row: almost certainly not
# onboarded, so a control-plane hiccup must not block its deletion. If a duckgres-only
# row does exist it is orphaned here, which the control plane tolerates.
logger.warning(
"Proceeding with team deletion despite duckgres delete-team failure",
organization_id=str(organization_id),
team_id=team_id,
status_code=resp.status_code,
)
return None
def deprovision(organization_id: UUID | str, require_enabled: bool = True, *, triggered_by: str) -> Response:
expected_generation = _active_managed_source_generation(organization_id)
inactive_cleanup_generation = _managed_source_generation(organization_id) if expected_generation is None else None
resp = _request("POST", organization_id, "/deprovision", require_enabled=require_enabled)
control_plane_converged = status.is_success(resp.status_code) or resp.status_code == status.HTTP_404_NOT_FOUND
if not control_plane_converged and (
resp.status_code == status.HTTP_409_CONFLICT or resp.status_code >= status.HTTP_500_INTERNAL_SERVER_ERROR
):
status_response = _request(
"GET",
organization_id,
"/warehouse/status",
require_enabled=False,
)
control_plane_converged = status_response.status_code == status.HTTP_404_NOT_FOUND or (
status_response.status_code == status.HTTP_200_OK
and isinstance(status_response.data, dict)
and status_response.data.get("state") in {"deleting", "deleted"}
)
if not control_plane_converged:
return resp
converged_response = (
resp
if status.is_success(resp.status_code)
else Response({"status": "deprovisioning started"}, status=status.HTTP_202_ACCEPTED)
)
logger.info(
"managed_warehouse_action",
action="deprovision",
triggered_by=triggered_by,
organization_id=str(organization_id),
status_code=resp.status_code,
)
_invalidate_team_state_cache(organization_id)
cleanup_generation = inactive_cleanup_generation
if expected_generation is not None:
try:
cleanup_generation = _deactivate_managed_source_lifecycle(
organization_id,
expected_generation=expected_generation,
)
except Exception:
logger.exception("Failed to record managed warehouse deprovision", organization_id=str(organization_id))
return Response(
{
"error": "The warehouse accepted deprovisioning, but its local SQL connection state was not updated. Retry deprovisioning."
},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
if cleanup_generation is None:
return converged_response
try:
_remove_direct_connection_sources(organization_id, cleanup_generation)
except Exception:
logger.exception("Failed to remove managed warehouse query sources", organization_id=str(organization_id))
try:
_schedule_remove_direct_connection_sources(organization_id, cleanup_generation)
except Exception:
logger.exception(
"Failed to schedule managed warehouse query source removal",
organization_id=str(organization_id),
)
return Response(
{
"error": "The warehouse was deprovisioned but its SQL connections could not be removed or scheduled for removal. They must be cleaned up manually."
},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
return converged_response
def deprovision_for_org_deletion(organization_id: UUID | str) -> None:
"""Deprovision the org's managed warehouse ahead of an organization deletion.
Called from the organization-deletion Temporal workflow while the ``DuckgresServer``
row still exists (the Django cascade destroys it with the org). Without this call the
duckgres warehouse outlives the organization fully alive: external writers keep
ingesting into it, storage keeps being metered, and its credentials stay valid — while
the Django pointer to it is gone.
No-op for orgs without a managed warehouse (mirrors ``block_team_deletion``'s gating,
so unrelated org deletions never touch the control plane). Idempotent against
duckgres: 404 means the warehouse is absent. A repeated deprovision that returns 409
is normalized to 202 only after status reports ``deleting`` or ``deleted``. Any
other failure raises so the Temporal activity retries: the org-record deletion (whose
cascade drops the ``DuckgresServer`` pointer) is conditional on this call being
accepted, so a persistent duckgres outage stalls the deletion workflow — visibly, and
resumable once the control plane is reachable — instead of orphaning a live warehouse
with no pointer left for any later cleanup.
"""
from products.managed_warehouse.backend.facade.api import has_provisioned_warehouse # noqa: PLC0415
org_id = str(organization_id)
if not has_provisioned_warehouse(organization_id):
return
# Backend caller: bypass the user-facing feature flag so the deletion never depends on
# flag evaluation on the Temporal worker.
resp = deprovision(organization_id, require_enabled=False, triggered_by="system:organization-deletion")
if status.is_success(resp.status_code):
return
if resp.status_code == status.HTTP_404_NOT_FOUND:
logger.info(
"Managed warehouse already deprovisioned or unknown to duckgres; continuing organization deletion",
organization_id=org_id,
status_code=resp.status_code,
)
return
if resp.status_code == status.HTTP_501_NOT_IMPLEMENTED and not hogtower.is_configured():
# Neither HOGTOWER_API_URL nor DUCKGRES_API_URL is configured (e.g. a
# dev/env-var-backed DuckgresServer row): there is no control plane to deprovision against.
# A 501 from the hogtower adapter means an unmapped route instead, and must not skip.
logger.warning(
"Managed warehouse deprovisioning skipped: provisioning API not configured",
organization_id=org_id,
)
return
raise RuntimeError(
f"duckgres deprovision failed with status {resp.status_code} for organization {org_id}; "
"the org's managed warehouse must be deprovisioned before the Django cascade drops its pointer"
)
def _remove_direct_connection_sources(organization_id: UUID | str, expected_generation: int) -> None:
"""Soft-delete the org's auto-created Postgres query connections after deprovisioning."""
from products.managed_warehouse.backend.facade.connection import (
soft_delete_managed_warehouse_sources, # noqa: PLC0415
)
soft_delete_managed_warehouse_sources(
organization_id=organization_id,
expected_generation=expected_generation,
)
def _schedule_remove_direct_connection_sources(organization_id: UUID | str, expected_generation: int) -> None:
"""Queue the retrying cleanup task when the inline soft-delete failed."""
# Keep the Celery task stack off this adapter's import path.
from products.data_warehouse.backend.facade.api import ( # noqa: PLC0415
schedule_soft_delete_managed_warehouse_sources,
)
schedule_soft_delete_managed_warehouse_sources(
organization_id=organization_id,
expected_generation=expected_generation,
)
def ensure_direct_connection_tables(team_id: int, organization_id: UUID | str) -> None:
"""Queue discovery of the team's managed-warehouse tables for the SQL editor.
Called from the warehouse-status read once the warehouse is `ready`. Repeated requests are
coalesced for a short interval, while later runs re-introspect the live catalog so newly created
project tables appear automatically.
"""
# Keep the Celery and data-warehouse task stack off this adapter's import path.
from products.data_warehouse.backend.facade.api import schedule_managed_warehouse_tables_reconcile # noqa: PLC0415
try:
schedule_managed_warehouse_tables_reconcile(team_id=team_id, organization_id=organization_id)
except Exception:
logger.exception(
"Failed to schedule managed warehouse direct connection table reconciliation",
organization_id=str(organization_id),
team_id=team_id,
)
def delete_org(organization_id: UUID | str, require_enabled: bool = True, *, triggered_by: str) -> Response:
"""Delete the org's provisioning record once teardown has finished, freeing its warehouse name.
`deprovision` tears the warehouse down (status goes deleting → deleted); this removes the
now-empty org row from the control plane so its `database_name` can be reused. Deprovision
teardown has no terminal failed state (the provisioner retries indefinitely), so callers
should only issue this once the warehouse status reports `deleted`.
"""
resp = _request("DELETE", organization_id, "", require_enabled=require_enabled)
if status.is_success(resp.status_code):
logger.info(
"managed_warehouse_action",
action="delete_org",
triggered_by=triggered_by,
organization_id=str(organization_id),
)
return resp
def status_for(organization_id: UUID | str) -> Response:
resp = _request("GET", organization_id, "/warehouse/status")
if resp.status_code == 200 and isinstance(resp.data, dict):
# Self-heal: the control plane is the authoritative source of the per-org
# bucket name. If the stored DuckgresServer row disagrees — NULL (row
# created before the CP returned it) or a stale locally-derived name — fix
# it here so the value converges on every status read without a separate
# backfill job. The UI reads status when viewing a warehouse, so a deploy
# heals each org the first time its status is fetched.
_reconcile_bucket_from_status(organization_id, resp.data)
if isinstance(resp.data.get("connection"), dict):
resp.data["connection"] = _present_connection(resp.data["connection"])
# Internal infra detail — reconciled above, not part of the UI-facing
# WarehouseStatusResponse schema. Backend callers use cp_bucket_for instead.
_strip_bucket_fields(resp.data)
return resp
def _present_trino_connection(trino_status: dict) -> PresentedTrinoConnection | None:
catalog = trino_status.get("trino_catalog_name") or trino_status.get("catalog")
connection = trino_status.get("connection")
if not isinstance(catalog, str) or not catalog.strip() or not isinstance(connection, dict):
return None
host = connection.get("host")
username = connection.get("username")
port = connection.get("port")
if not isinstance(host, str) or not host.strip() or not isinstance(username, str) or not username.strip():
return None
if isinstance(port, bool) or not isinstance(port, int) or not 1 <= port <= 65535:
return None
return PresentedTrinoConnection(host=host.strip(), port=port, catalog=catalog.strip(), username=username.strip())
def trino_status_for(organization_id: UUID | str) -> PresentedTrinoStatus:
"""Customer-safe summary of the organization's Trino enablement, for the Trino Data ops variant.
The control plane's Trino status is an operator payload. Only the lifecycle state, the ready
time, and the connection target leave this function.
"""
unavailable = PresentedTrinoStatus(state="unavailable", ready_at=None, connection=None)
resp = _request("GET", organization_id, "/trino", timeout=10)
if not status.is_success(resp.status_code) or not isinstance(resp.data, dict):
return unavailable
if resp.data.get("enabled") is not True:
return PresentedTrinoStatus(state="not_enabled", ready_at=None, connection=None)
trino_status = resp.data.get("status")
if not isinstance(trino_status, dict):
return unavailable
if str(trino_status.get("org")) != str(organization_id):
logger.warning(
"refusing_trino_status_for_mismatched_organization",
requested_organization_id=str(organization_id),
response_organization_id=str(trino_status.get("org")),
)
return unavailable
raw_state = trino_status.get("state")
# A row that has not been reconciled yet has no state, which means pending.
if raw_state is None or raw_state == "":
state: TrinoStatusState = "pending"
elif isinstance(raw_state, str) and raw_state in _TRINO_LIFECYCLE_STATES:
state = cast(TrinoStatusState, raw_state)
else:
# The control plane has lifecycle states this view does not present, such as deleting. Showing
# them as pending would make the scene poll for a setup that never completes.
logger.warning("unexpected_trino_lifecycle_state", organization_id=str(organization_id), state=str(raw_state))
return unavailable
ready_at = trino_status.get("ready_at")
return PresentedTrinoStatus(
state=state,
ready_at=ready_at if isinstance(ready_at, str) else None,
connection=_present_trino_connection(trino_status) if state == "ready" else None,
)
def monitoring_snapshot_for(organization_id: UUID | str) -> Response:
"""Fetch tenant-safe live monitoring data for one organization."""
return _request("GET", organization_id, "/monitoring/snapshot", timeout=10)
def monitoring_series_for(
organization_id: UUID | str,
metric: ManagedWarehouseMonitoringMetric,
window: ManagedWarehouseMonitoringWindow,
) -> Response:
"""Fetch one allow-listed monitoring series for one organization."""
return _request(
"GET",
organization_id,
"/monitoring/series",
params={"metric": metric, "window": window},
timeout=10,
)
def trino_monitoring_snapshot_for(organization_id: UUID | str) -> Response:
"""Fetch tenant-safe live Trino query data for one organization."""
return _request("GET", organization_id, "/monitoring/trino/snapshot", timeout=10)
def trino_monitoring_series_for(
organization_id: UUID | str,
metric: ManagedWarehouseTrinoMonitoringMetric,
window: ManagedWarehouseMonitoringWindow,
) -> Response:
"""Fetch one allow-listed Trino monitoring series for one organization."""
return _request(
"GET",
organization_id,
"/monitoring/trino/series",
params={"metric": metric, "window": window},
timeout=10,
)
def cp_bucket_for(organization_id: UUID | str) -> str | None:
"""Authoritative S3 bucket for the org's duckling, straight from the control plane.
For backend/background callers (the Dagster duckling backfill), not the UI. Unlike
`status_for` it:
- bypasses the user-facing feature-flag gate (`require_enabled=False`), so a Dagster
worker without the flag loaded locally doesn't get a spurious 403; and
- returns the bucket only when the status body's own `org_id` matches the org asked
about, so a CP bug or misrouted proxy can never hand back another tenant's bucket.
Also reconciles the stored `DuckgresServer` row so it converges for next time. Returns
None when the control plane is unreachable, unconfigured, or names no bucket.
"""
resp = _request("GET", organization_id, "/warehouse/status", require_enabled=False)
if resp.status_code != 200 or not isinstance(resp.data, dict):
return None
response_org = resp.data.get("org_id")
if response_org is not None and str(response_org) != str(organization_id):
logger.warning(
"Refusing to use control-plane bucket: status org_id mismatch",
requested_organization_id=str(organization_id),
response_org_id=str(response_org),
)
return None
_reconcile_bucket_from_status(organization_id, resp.data)
return resp.data.get("bucket") or None
def _reconcile_bucket_from_status(organization_id: UUID | str, body: dict) -> None:
"""Converge DuckgresServer.bucket/bucket_region onto the control-plane-reported values.
Best-effort and side-effect-only: a single UPDATE touching only rows where the
bucket OR the region differs (no fetch, no create). A DB hiccup is swallowed — it
must never fail the status read it piggybacks on.
"""
# Defense-in-depth: the request is per-org (the URL carries the org id), but
# only write back when the response's own org_id agrees with the one we asked
# about. A mismatch (CP bug, misrouted proxy) must never let one tenant's
# status overwrite another's bucket mapping — that would redirect backfill
# reads/writes to the wrong S3 bucket.
response_org = body.get("org_id")
if response_org is not None and str(response_org) != str(organization_id):
logger.warning(
"Refusing to reconcile DuckgresServer bucket: status org_id mismatch",
requested_organization_id=str(organization_id),
response_org_id=str(response_org),
)
return
bucket = body.get("bucket")
if not bucket:
# External data stores / not-yet-backfilled ducklings report no bucket —
# nothing authoritative to copy.
return
from products.managed_warehouse.backend.facade.api import ( # noqa: PLC0415
default_bucket_region,
reconcile_stored_bucket_config,
)
bucket_region = body.get("bucket_region") or default_bucket_region()
try:
updated = reconcile_stored_bucket_config(
organization_id,
bucket=bucket,
bucket_region=bucket_region,
)
if updated:
logger.info(
"duckgres_server_bucket_reconciled_from_status",
organization_id=str(organization_id),
bucket=bucket,
)
except Exception:
logger.exception(
"Failed to reconcile DuckgresServer bucket from status",
organization_id=str(organization_id),
)
def reset_password(organization_id: UUID | str, *, triggered_by: str) -> Response:
resp = _request("POST", organization_id, "/reset-password")
if status.is_success(resp.status_code):
logger.info(
"managed_warehouse_action",
action="reset_password",
triggered_by=triggered_by,
organization_id=str(organization_id),
)
if status.is_success(resp.status_code) and isinstance(resp.data, dict) and resp.data.get("password"):
try:
_update_direct_connection_password(organization_id, resp.data["password"])
except Exception:
logger.exception("Failed to update managed warehouse stored password", organization_id=str(organization_id))
return Response(
{"error": "The password was rotated but could not be saved. Retry the password reset."},
status=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
return resp
def _update_direct_connection_password(organization_id: UUID | str, password: str) -> None:
"""Sync the rotated root password into the server row and query connections."""
from products.managed_warehouse.backend.facade.connection import (
update_managed_warehouse_root_password, # noqa: PLC0415
)
update_managed_warehouse_root_password(organization_id=organization_id, password=password)
def check_name(organization_id: UUID | str, name: str | None) -> Response:
name_error = validate_warehouse_name(name)
if name_error:
return Response({"error": name_error}, status=status.HTTP_400_BAD_REQUEST)
return _request("GET", organization_id, "database-name/check", params={"name": name}, timeout=10)