feat: initialize governed datasources module

This commit is contained in:
2026-07-28 12:43:02 +02:00
commit 1cb6228442
37 changed files with 5910 additions and 0 deletions
@@ -0,0 +1,287 @@
from __future__ import annotations
from pathlib import Path
from govoplan_core.core.access import (
CAPABILITY_AUTH_PERMISSION_EVALUATOR,
CAPABILITY_AUTH_PRINCIPAL_RESOLVER,
)
from govoplan_core.core.datasources import (
CAPABILITY_DATASOURCE_CATALOGUE,
CAPABILITY_DATASOURCE_LIFECYCLE,
CAPABILITY_DATASOURCE_ORIGINS,
)
from govoplan_core.core.module_guards import (
drop_table_retirement_provider,
persistent_table_uninstall_guard,
)
from govoplan_core.core.modules import (
DocumentationTopic,
FrontendModule,
MigrationSpec,
ModuleContext,
ModuleInterfaceProvider,
ModuleInterfaceRequirement,
ModuleManifest,
NavItem,
PermissionDefinition,
RoleTemplate,
)
from govoplan_core.db.base import Base
from govoplan_datasources.backend.db import models as datasource_models
from govoplan_datasources.backend.service import (
ADMIN_SCOPE,
CATALOGUE_READ_SCOPE,
SOURCE_WRITE_SCOPE,
STAGE_WRITE_SCOPE,
SqlDatasourceProvider,
)
MODULE_ID = "datasources"
MODULE_NAME = "Datasources"
MODULE_VERSION = "0.1.14"
DATASOURCE_INTERFACE_VERSION = "0.1.0"
def _permission(scope: str, label: str, description: str) -> PermissionDefinition:
module_id, resource, action = scope.split(":", 2)
return PermissionDefinition(
scope=scope,
label=label,
description=description,
category="Datasources",
level="tenant",
module_id=module_id,
resource=resource,
action=action,
)
PERMISSIONS = (
_permission(
CATALOGUE_READ_SCOPE,
"View datasources",
"Discover and preview policy-visible datasources and materializations.",
),
_permission(
SOURCE_WRITE_SCOPE,
"Manage datasources",
"Register, refresh, freeze, and retire governed datasources.",
),
_permission(
STAGE_WRITE_SCOPE,
"Stage datasource content",
"Upload, validate, inspect, and promote bounded datasource stages.",
),
_permission(
ADMIN_SCOPE,
"Administer datasources",
"Manage every tenant datasource, stage, materialization, and lifecycle policy.",
),
)
ROLE_TEMPLATES = (
RoleTemplate(
slug="datasource_manager",
name="Datasource manager",
description="Register, stage, refresh, freeze, and retire governed data.",
permissions=(
CATALOGUE_READ_SCOPE,
SOURCE_WRITE_SCOPE,
STAGE_WRITE_SCOPE,
),
),
RoleTemplate(
slug="datasource_reader",
name="Datasource reader",
description="Discover and preview policy-visible datasource states.",
permissions=(CATALOGUE_READ_SCOPE,),
),
)
def _router(context: ModuleContext):
from govoplan_datasources.backend.runtime import configure_runtime
configure_runtime(registry=context.registry)
from govoplan_datasources.backend.router import router
return router
def _provider(context: ModuleContext) -> SqlDatasourceProvider:
return SqlDatasourceProvider(registry=context.registry)
def _tenant_summary(session, tenant_id: str) -> dict[str, int]:
return {
"datasources": (
session.query(datasource_models.DatasourceRecord)
.filter(
datasource_models.DatasourceRecord.tenant_id == tenant_id,
datasource_models.DatasourceRecord.deleted_at.is_(None),
)
.count()
),
"datasource_stages_ready": (
session.query(datasource_models.DatasourceStageRecord)
.filter(
datasource_models.DatasourceStageRecord.tenant_id == tenant_id,
datasource_models.DatasourceStageRecord.state == "ready",
)
.count()
),
"datasource_frozen_states": (
session.query(datasource_models.DatasourceMaterializationRecord)
.filter(
datasource_models.DatasourceMaterializationRecord.tenant_id
== tenant_id,
datasource_models.DatasourceMaterializationRecord.frozen_at.is_not(
None
),
)
.count()
),
}
manifest = ModuleManifest(
id=MODULE_ID,
name=MODULE_NAME,
version=MODULE_VERSION,
dependencies=(),
optional_dependencies=(
"access",
"audit",
"connectors",
"files",
"notifications",
"policy",
),
optional_capabilities=(
CAPABILITY_AUTH_PRINCIPAL_RESOLVER,
CAPABILITY_AUTH_PERMISSION_EVALUATOR,
CAPABILITY_DATASOURCE_ORIGINS,
),
provides_interfaces=(
ModuleInterfaceProvider(
name="datasources.catalogue",
version=DATASOURCE_INTERFACE_VERSION,
),
ModuleInterfaceProvider(
name="datasources.lifecycle",
version=DATASOURCE_INTERFACE_VERSION,
),
ModuleInterfaceProvider(
name="datasources.materializations",
version=DATASOURCE_INTERFACE_VERSION,
),
ModuleInterfaceProvider(
name="datasources.staging",
version=DATASOURCE_INTERFACE_VERSION,
),
),
requires_interfaces=(
ModuleInterfaceRequirement(
name="connectors.datasource_origins",
version_min="0.1.0",
version_max_exclusive="1.0.0",
optional=True,
),
),
permissions=PERMISSIONS,
role_templates=ROLE_TEMPLATES,
nav_items=(
NavItem(
path="/datasources",
label="Datasources",
icon="database-zap",
required_any=(CATALOGUE_READ_SCOPE, ADMIN_SCOPE),
order=70,
),
),
frontend=FrontendModule(
module_id=MODULE_ID,
package_name="@govoplan/datasources-webui",
nav_items=(
NavItem(
path="/datasources",
label="Datasources",
icon="database-zap",
required_any=(CATALOGUE_READ_SCOPE, ADMIN_SCOPE),
order=70,
),
),
),
route_factory=_router,
capability_factories={
CAPABILITY_DATASOURCE_CATALOGUE: _provider,
CAPABILITY_DATASOURCE_LIFECYCLE: _provider,
},
tenant_summary_providers=(_tenant_summary,),
migration_spec=MigrationSpec(
module_id=MODULE_ID,
metadata=Base.metadata,
script_location=str(Path(__file__).with_name("migrations") / "versions"),
retirement_supported=True,
retirement_provider=drop_table_retirement_provider(
datasource_models.DatasourceStageRecord,
datasource_models.DatasourceMaterializationRecord,
datasource_models.DatasourceRecord,
label="Datasources",
),
retirement_notes=(
"Destructive retirement drops datasource catalogue entries, immutable "
"materializations, and staging evidence after a database snapshot."
),
),
uninstall_guard_providers=(
persistent_table_uninstall_guard(
datasource_models.DatasourceRecord,
datasource_models.DatasourceMaterializationRecord,
datasource_models.DatasourceStageRecord,
label="Datasources",
),
),
documentation=(
DocumentationTopic(
id="datasources.lifecycle",
title="Datasource lifecycle",
summary="Governed live, cached, and static data with staging and frozen states.",
body=(
"Datasources is the provider-neutral catalogue consumed by Dataflow, "
"Workflow, Reporting, and policy-aware modules. Static data is staged "
"before promotion. Cached data refreshes connector origins into immutable "
"materializations. Live data is read through a connector and may be frozen "
"for reproducible evidence. Connectors owns protocols and credentials; "
"Datasources owns data identity, provenance, lifecycle, and read semantics."
),
layer="available",
documentation_types=("admin", "user"),
audience=("operator", "module_admin", "power_user", "product_owner"),
related_modules=(
"connectors",
"dataflow",
"workflow",
"files",
"reporting",
"risk_compliance",
),
order=70,
),
),
)
def get_manifest() -> ModuleManifest:
return manifest
__all__ = [
"DATASOURCE_INTERFACE_VERSION",
"MODULE_ID",
"MODULE_VERSION",
"get_manifest",
"manifest",
]