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.3 KB
Raw Blame
from typing import Optional, cast

from products.warehouse_sources.sources.coupa._config import CoupaSourceConfig
from products.warehouse_sources.sources.coupa.coupa import (
    CoupaResumeConfig,
    coupa_source,
    hostname_of,
    validate_credentials as validate_coupa_credentials,
)
from products.warehouse_sources.sources.coupa.settings import ENDPOINTS, INCREMENTAL_FIELDS
from products.warehouse_sources.sources.sdk import (
    CanonicalDescriptions,
    DataWarehouseSourceCategory,
    ExternalDataSourceType,
    FieldType,
    ReleaseStatus,
    ResumableSource,
    ResumableSourceManager,
    SourceConfig,
    SourceFieldInputConfig,
    SourceFieldInputConfigType,
    SourceInputs,
    SourceRegistry,
    SourceResponse,
    SourceSchema,
    ValidateDatabaseHostMixin,
    build_endpoint_schemas,
    detect,
)


@SourceRegistry.register
class CoupaSource(ResumableSource[CoupaSourceConfig, CoupaResumeConfig], ValidateDatabaseHostMixin):
    codebase_detection = detect(env=("^COUPA_",))

    lists_tables_without_credentials = True  # static endpoint catalog — safe for public docs

    api_docs_url = "https://docs.coupa.com/en/developer-documentation/the-coupa-core-api/coupa-core-api-overview"

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

    @property
    def connection_host_fields(self) -> list[str]:
        # The instance URL decides where the stored credentials are sent;
        # retargeting it must re-require the secret.
        return ["instance_url"]

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

        return CANONICAL_DESCRIPTIONS

    def get_non_retryable_errors(self) -> dict[str, str | None]:
        return {
            "400 Client Error: Bad Request for url": "Coupa rejected the token request. Please check your client ID, client secret, and that the OIDC client has the required scopes.",
            "403 Client Error: Forbidden for url": "Coupa denied access. Please check that the OIDC client has the read scope for this dataset (e.g. core.invoice.read).",
        }

    @property
    def get_source_config(self) -> SourceConfig:
        return SourceConfig(
            name=ExternalDataSourceType.COUPA,
            category=DataWarehouseSourceCategory.FINANCE___ACCOUNTING,
            label="Coupa",
            caption="""Connect your Coupa instance to pull your spend management data into the PostHog Data warehouse.

A Coupa admin can create an OIDC client under Setup > Integrations > OAuth2/OpenID Connect Clients with the client credentials grant. Grant it the read scopes for the objects you want to sync (e.g. `core.invoice.read`, `core.purchase_order.read`). The instance URL is your Coupa host, e.g. `https://myorg.coupahost.com`.""",
            iconPath="/static/services/coupa.png",
            docsUrl="https://posthog.com/docs/cdp/sources/coupa",
            releaseStatus=ReleaseStatus.BETA,
            fields=cast(
                list[FieldType],
                [
                    SourceFieldInputConfig(
                        name="instance_url",
                        label="Instance URL",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="https://myorg.coupahost.com",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="client_id",
                        label="Client ID",
                        type=SourceFieldInputConfigType.TEXT,
                        required=True,
                        placeholder="",
                        secret=False,
                    ),
                    SourceFieldInputConfig(
                        name="client_secret",
                        label="Client secret",
                        type=SourceFieldInputConfigType.PASSWORD,
                        required=True,
                        placeholder="",
                        secret=True,
                    ),
                ],
            ),
        )

    def get_schemas(
        self,
        config: CoupaSourceConfig,
        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: CoupaSourceConfig, team_id: int, schema_name: Optional[str] = None, api_version: str | None = None
    ) -> tuple[bool, str | None]:
        try:
            host_valid, host_error = self.is_database_host_valid(hostname_of(config.instance_url), team_id)
        except ValueError:
            return False, "Invalid Coupa instance URL"
        if not host_valid:
            return False, host_error

        if validate_coupa_credentials(config.instance_url, config.client_id, config.client_secret):
            return True, None

        return False, "Invalid Coupa credentials. Check the instance URL, client ID, and client secret."

    def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[CoupaResumeConfig]:
        return ResumableSourceManager[CoupaResumeConfig](inputs, CoupaResumeConfig)

    def source_for_pipeline(
        self,
        config: CoupaSourceConfig,
        resumable_source_manager: ResumableSourceManager[CoupaResumeConfig],
        inputs: SourceInputs,
    ) -> SourceResponse:
        host_valid, host_error = self.is_database_host_valid(hostname_of(config.instance_url), inputs.team_id)
        if not host_valid:
            raise ValueError(host_error or "Invalid Coupa host")

        return coupa_source(
            instance_url=config.instance_url,
            client_id=config.client_id,
            client_secret=config.client_secret,
            endpoint=inputs.schema_name,
            logger=inputs.logger,
            resumable_source_manager=resumable_source_manager,
            should_use_incremental_field=inputs.should_use_incremental_field,
            db_incremental_field_last_value=inputs.db_incremental_field_last_value
            if inputs.should_use_incremental_field
            else None,
        )