Skip to content

: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.

6.8 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.coolify._config import CoolifySourceConfig
from products.warehouse_sources.sources.coolify.coolify import (
    audit_events_permission,
    coolify_source,
    validate_credentials as validate_coolify_credentials,
)
from products.warehouse_sources.sources.coolify.settings import COOLIFY_ENDPOINTS, ENDPOINTS, INCREMENTAL_FIELDS
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    SimpleSource,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    build_endpoint_schemas,
    detect,
)


@SourceRegistry.register
class CoolifySource(SimpleSource[CoolifySourceConfig]):
    codebase_detection = detect(env=("^COOLIFY_",))

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs
    # Coolify's API has a single, never-versioned base (`/api/v1` on every instance) and no
    # version header or changelog of version tokens, so it stays on the unversioned default.
    api_docs_url = "https://coolify.io/docs/api-reference/authorization"

    @property
    def source_type(self) -> ExternalDataSourceType:
        return ExternalDataSourceType.COOLIFY

    @property
    def connection_host_fields(self) -> list[str]:
        # `base_url` is where the stored API token is sent, so retargeting it must re-require the token.
        return ["base_url"]

    def get_canonical_descriptions(self) -> CanonicalDescriptions:
        from products.warehouse_sources.sources.coolify.canonical_descriptions import CANONICAL_DESCRIPTIONS

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            # The base host is customer-specific (self-hosted instances), so the keys match the
            # stable status text rather than a fixed host. Coolify answers an unrecognized token
            # with 400 "Invalid token." rather than 401, so both map to the credential message.
            "400 Client Error: Bad Request for url": "Coolify rejected the API token. Check the token, or create a new one under Keys & Tokens > API tokens on your Coolify instance, then reconnect.",
            "401 Client Error: Unauthorized for url": "Coolify rejected the API token. Check the token, or create a new one under Keys & Tokens > API tokens on your Coolify instance, then reconnect.",
            "403 Client Error: Forbidden for url": "Your API token doesn't have read permission. Create a token with read permission under Keys & Tokens > API tokens on your Coolify instance, then reconnect. Audit events also need a token created by a team admin or owner.",
        }

    def get_schemas(
        self,
        config: CoolifySourceConfig,
        team_id: int,
        with_counts: bool = False,
        names: list[str] | None = None,
        force_refresh: bool = False,
        api_version: str | None = None,
    ) -> list[SourceSchema]:
        return build_endpoint_schemas(ENDPOINTS, INCREMENTAL_FIELDS, names)

    def validate_credentials(
        self,
        config: CoolifySourceConfig,
        team_id: int,
        schema_name: Optional[str] = None,
        api_version: str | None = None,
    ) -> tuple[bool, str | None]:
        return validate_coolify_credentials(config.base_url, config.api_token, team_id=team_id, schema_name=schema_name)

    def get_endpoint_permissions(
        self, config: CoolifySourceConfig, team_id: int, endpoints: list[str], api_version: str | None = None
    ) -> dict[str, str | None]:
        # Tokens carry global abilities, so only the admin-only audit log can differ per table.
        permissions: dict[str, str | None] = dict.fromkeys(endpoints)
        if "audit_events" in permissions:
            permissions["audit_events"] = audit_events_permission(config.base_url, config.api_token, team_id)
        return permissions

    def source_for_pipeline(self, config: CoolifySourceConfig, inputs: SourceInputs) -> SourceResponse:
        endpoint_config = COOLIFY_ENDPOINTS[inputs.schema_name]
        resource = coolify_source(
            base_url=config.base_url,
            api_token=config.api_token,
            endpoint=inputs.schema_name,
            team_id=inputs.team_id,
            job_id=inputs.job_id,
        )
        response = SourceResponse(
            name=resource.name,
            items=lambda: resource,
            primary_keys=endpoint_config.primary_keys,
            column_hints=resource.column_hints,
        )

        if endpoint_config.partition_key:
            response.partition_count = 1
            response.partition_size = 1
            response.partition_mode = "datetime"
            response.partition_format = "month"
            response.partition_keys = [endpoint_config.partition_key]

        return response

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.COOLIFY,
            category=DataWarehouseSourceCategory.ENGINEERING___MONITORING,
            label="Coolify",
            releaseStatus=ReleaseStatus.GA,
            caption="""Enter your Coolify instance URL and an API token to pull your servers, projects, environments, applications, preview deployments, databases, services and their containers and databases, deployments, scheduled task and backup runs, audit events, and teams into the PostHog Data warehouse.

Create a token under **Keys & Tokens > API tokens** in your Coolify dashboard. A read-only token is enough, because this source never writes or deploys. Audit events need a token created by a team admin or owner.

The instance URL is where your Coolify dashboard runs, for example `https://coolify.example.com` (use `https://app.coolify.io` for Coolify Cloud).""",
            iconPath="/static/services/coolify.png",
            docsUrl="https://posthog.com/docs/cdp/sources/coolify",
            keywords=["paas", "self-hosted", "coollabs", "deployments"],
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="base_url",
                        label="Instance URL",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="https://coolify.example.com",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="api_token",
                        label="API token",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )