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.

20.6 KB
Raw Blame
from dataclasses import dataclass, field
from typing import Optional

from products.warehouse_sources.sources.sdk import DependentEndpointConfig, IncrementalField

# DigitalOcean caps `per_page` at 200; use the max to minimize round trips against the
# 5,000 req/hour + 250 req/minute rate limits.
PAGE_SIZE = 200


# Mutable by choice, not oversight: instances flow into `build_dependent_resource`'s
# `endpoint_configs: Mapping[str, FanoutEndpointLike]`, and mypy treats a frozen dataclass's
# fields as read-only, which is incompatible with that Protocol's plain (read-write) attributes.
@dataclass(frozen=False)
class DigitalOceanEndpointConfig:
    name: str
    # Path relative to the API root (https://api.digitalocean.com). A fan-out child's path
    # carries the `{...}` placeholder its `fanout.resolve_param` binds.
    path: str
    # Key the list of records is nested under in the JSON response body
    # (e.g. `{"droplets": [...]}` → "droplets"). `"$"` for an endpoint that returns the record
    # as the response body itself rather than wrapped in a list.
    data_selector: str
    # Columns that uniquely identify a record table-wide. DigitalOcean resource ids are
    # globally unique; keyless resources (domains, tags) use their natural name/ip key.
    primary_keys: list[str] = field(default_factory=lambda: ["id"])
    # Stable creation timestamp to partition by. `None` for resources the API doesn't stamp
    # with a creation time. Never a mutable field like `updated_at`.
    partition_key: Optional[str] = None
    # Extra query params merged into every request for this endpoint.
    extra_params: dict[str, str] = field(default_factory=dict)
    # Response keys carrying secrets (connection URIs, passwords, env-var values, log-shipping
    # credentials). Dropped from every record before it's stored — recursively, at any depth,
    # since some live nested inside spec/deployment objects — and their presence also disables
    # HTTP sample capture for the endpoint so the raw response can't leak into HTTP samples.
    sensitive_fields: frozenset[str] = frozenset()
    # `False` keeps the endpoint's raw response out of HTTP sample capture, which records the
    # response before the resource maps run. Set it for a response that carries data belonging in
    # the warehouse table but not in a diagnostic sample bucket, such as billing identity. A
    # `sensitive_fields` entry implies it, because a secret must never reach a sample either.
    captures_http_samples: bool = True
    # Set for endpoints that hang off a parent list endpoint (invoice line items and summaries
    # are only reachable per invoice uuid). `None` for top-level endpoints.
    fanout: Optional[DependentEndpointConfig] = None
    # `False` for an endpoint that returns a single record as the response body instead of a
    # paginated list. Declared rather than inferred because the REST framework assumes a path
    # ending in its resolved id is a single entity, and `/v2/customers/my/invoices/{invoice_uuid}`
    # returns a page of invoice line items instead.
    paginated: bool = True
    # Read by the fan-out helper's `FanoutEndpointLike` protocol. No DigitalOcean endpoint takes
    # a server-side time filter, so the incremental members stay empty for every endpoint.
    page_size: int = PAGE_SIZE
    incremental_fields: list[IncrementalField] = field(default_factory=list)
    default_incremental_field: Optional[str] = None


# DigitalOcean's list endpoints all share the same page-based pagination
# (`links.pages.next` full URLs, `meta.total`) and none expose a server-side
# `updated_since`/`created_since` filter, so every endpoint is full-refresh only.
DIGITALOCEAN_ENDPOINTS: dict[str, DigitalOceanEndpointConfig] = {
    "droplets": DigitalOceanEndpointConfig(
        name="droplets",
        path="/v2/droplets",
        data_selector="droplets",
        partition_key="created_at",
    ),
    # `GET /v2/apps` returns full app specs whose components carry environment-variable values
    # and log-destination credentials (Datadog/Papertrail/OpenSearch). Those `envs` and
    # `log_destinations` blocks live nested inside `spec` (and the deployment spec copies), so
    # they're stripped recursively; the surrounding app/service metadata is kept.
    "apps": DigitalOceanEndpointConfig(
        name="apps",
        path="/v2/apps",
        data_selector="apps",
        partition_key="created_at",
        sensitive_fields=frozenset({"envs", "log_destinations"}),
    ),
    # Account-wide state-transition log (creates, resizes, power cycles). `/v2/droplets/{id}/actions`
    # returns the same action objects filtered to one droplet, so it adds no columns, because these
    # rows already carry `resource_id` and `resource_type`. Fanning out per droplet would also
    # multiply requests against the account rate limit.
    "actions": DigitalOceanEndpointConfig(
        name="actions",
        path="/v2/actions",
        data_selector="actions",
        partition_key="started_at",
    ),
    "kubernetes_clusters": DigitalOceanEndpointConfig(
        name="kubernetes_clusters",
        path="/v2/kubernetes/clusters",
        data_selector="kubernetes_clusters",
        partition_key="created_at",
    ),
    # `GET /v2/databases` embeds live cluster credentials (connection URIs + passwords and
    # the per-database `users` list) alongside the metadata we want. Strip those fields so
    # they never land in a queryable warehouse table.
    "databases": DigitalOceanEndpointConfig(
        name="databases",
        path="/v2/databases",
        data_selector="databases",
        partition_key="created_at",
        sensitive_fields=frozenset(
            {
                "connection",
                "private_connection",
                "standby_connection",
                "standby_private_connection",
                "users",
            }
        ),
    ),
    "volumes": DigitalOceanEndpointConfig(
        name="volumes",
        path="/v2/volumes",
        data_selector="volumes",
        partition_key="created_at",
    ),
    "snapshots": DigitalOceanEndpointConfig(
        name="snapshots",
        path="/v2/snapshots",
        data_selector="snapshots",
        partition_key="created_at",
    ),
    "load_balancers": DigitalOceanEndpointConfig(
        name="load_balancers",
        path="/v2/load_balancers",
        data_selector="load_balancers",
        partition_key="created_at",
    ),
    "projects": DigitalOceanEndpointConfig(
        name="projects",
        path="/v2/projects",
        data_selector="projects",
        partition_key="created_at",
    ),
    "vpcs": DigitalOceanEndpointConfig(
        name="vpcs",
        path="/v2/vpcs",
        data_selector="vpcs",
        partition_key="created_at",
    ),
    # User-owned images only; the unfiltered list also returns every public distribution
    # and application image, which is huge and identical for every account.
    "images": DigitalOceanEndpointConfig(
        name="images",
        path="/v2/images",
        data_selector="images",
        partition_key="created_at",
        extra_params={"private": "true"},
    ),
    "domains": DigitalOceanEndpointConfig(
        name="domains",
        path="/v2/domains",
        data_selector="domains",
        primary_keys=["name"],
    ),
    "ssh_keys": DigitalOceanEndpointConfig(
        name="ssh_keys",
        path="/v2/account/keys",
        data_selector="ssh_keys",
    ),
    "reserved_ips": DigitalOceanEndpointConfig(
        name="reserved_ips",
        path="/v2/reserved_ips",
        data_selector="reserved_ips",
        primary_keys=["ip"],
    ),
    "tags": DigitalOceanEndpointConfig(
        name="tags",
        path="/v2/tags",
        data_selector="tags",
        primary_keys=["name"],
    ),
    # Lookups that resolve the `size_slug` and `region` slugs carried by droplets, databases,
    # load balancers and volumes. Both are account-independent catalogs keyed by slug.
    "sizes": DigitalOceanEndpointConfig(
        name="sizes",
        path="/v2/sizes",
        data_selector="sizes",
        primary_keys=["slug"],
    ),
    "regions": DigitalOceanEndpointConfig(
        name="regions",
        path="/v2/regions",
        data_selector="regions",
        primary_keys=["slug"],
    ),
    "invoices": DigitalOceanEndpointConfig(
        name="invoices",
        path="/v2/customers/my/invoices",
        data_selector="invoices",
        primary_keys=["invoice_uuid"],
    ),
    # Append-only financial ledger with no natural unique key. The composite is a best-effort
    # dedup guard; the endpoint is full-refresh (replace) so it's rewritten wholesale each sync.
    "billing_history": DigitalOceanEndpointConfig(
        name="billing_history",
        path="/v2/customers/my/billing_history",
        data_selector="billing_history",
        primary_keys=["date", "type", "amount", "description"],
    ),
    # Per-resource line items behind each invoice, so spend can be attributed to the droplet,
    # database or cluster that incurred it. Line items carry no invoice reference of their own,
    # so the parent's `invoice_uuid` is projected onto every row.
    "invoice_items": DigitalOceanEndpointConfig(
        name="invoice_items",
        path="/v2/customers/my/invoices/{invoice_uuid}",
        data_selector="invoice_items",
        captures_http_samples=False,
        # Line items have no id. Like `billing_history`, this composite is a best-effort dedup
        # guard rather than a true key, because the endpoint is full-refresh and each sync
        # rewrites the whole table.
        primary_keys=[
            "invoice_uuid",
            "product",
            "resource_uuid",
            "group_description",
            "description",
            "start_time",
            "end_time",
        ],
        partition_key="start_time",
        fanout=DependentEndpointConfig(
            parent_name="invoices",
            resolve_param="invoice_uuid",
            resolve_field="invoice_uuid",
            include_from_parent=["invoice_uuid"],
            parent_field_renames={"invoice_uuid": "invoice_uuid"},
            parent_params={"per_page": PAGE_SIZE},
            child_params={"per_page": PAGE_SIZE},
        ),
    ),
    # Invoice totals split into product charges, overages, taxes and credits, one row per
    # invoice. The endpoint returns a single object rather than a paginated list. The parent's
    # `invoice_uuid` is projected over the body's own, because the spec does not require the
    # summary body to carry it and it is this table's primary key.
    "invoice_summaries": DigitalOceanEndpointConfig(
        name="invoice_summaries",
        path="/v2/customers/my/invoices/{invoice_uuid}/summary",
        data_selector="$",
        primary_keys=["invoice_uuid"],
        paginated=False,
        captures_http_samples=False,
        fanout=DependentEndpointConfig(
            parent_name="invoices",
            resolve_param="invoice_uuid",
            resolve_field="invoice_uuid",
            include_from_parent=["invoice_uuid"],
            parent_field_renames={"invoice_uuid": "invoice_uuid"},
            parent_params={"per_page": PAGE_SIZE},
        ),
    ),
    # The join table between projects and everything in them: `/v2/projects` syncs the project
    # headers, but membership only exists behind this per-project endpoint. Resources are
    # identified by URN (`do:droplet:13457723`), which resolves against the id columns already
    # synced on droplets, databases, volumes and the rest.
    # `/v2/projects/default/resources` is an alias for whichever project carries `is_default`,
    # and that project is in the `/v2/projects` listing, so fanning out over every project
    # already covers it.
    "project_resources": DigitalOceanEndpointConfig(
        name="project_resources",
        path="/v2/projects/{project_id}/resources",
        data_selector="resources",
        # A resource sits in one project at a time, but the API does not document the URN as
        # unique across the whole fan-out, so the parent id is part of the key.
        primary_keys=["project_id", "urn"],
        partition_key="assigned_at",
        fanout=DependentEndpointConfig(
            parent_name="projects",
            resolve_param="project_id",
            resolve_field="id",
            include_from_parent=["id"],
            parent_field_renames={"id": "project_id"},
            parent_params={"per_page": PAGE_SIZE},
            child_params={"per_page": PAGE_SIZE},
        ),
    ),
    # DNS records under each synced domain. The domain itself is only a name, so the records
    # are where the zone's actual contents live.
    "domain_records": DigitalOceanEndpointConfig(
        name="domain_records",
        path="/v2/domains/{domain_name}/records",
        data_selector="domain_records",
        # The record id is documented as unique per domain, not across the account.
        primary_keys=["domain_name", "id"],
        fanout=DependentEndpointConfig(
            parent_name="domains",
            resolve_param="domain_name",
            resolve_field="name",
            include_from_parent=["name"],
            # Renamed off the parent prefix, and away from the record's own `name` column,
            # which holds the host part of the record rather than the zone.
            parent_field_renames={"name": "domain_name"},
            parent_params={"per_page": PAGE_SIZE},
            child_params={"per_page": PAGE_SIZE},
        ),
    ),
    # Backup inventory per managed database cluster, so retention and backup size can be read
    # alongside the cluster metadata. The endpoint takes no page params and returns the whole
    # list in one response.
    "database_backups": DigitalOceanEndpointConfig(
        name="database_backups",
        path="/v2/databases/{database_cluster_uuid}/backups",
        data_selector="backups",
        # A backup record has no id of its own; it is identified by when it was taken.
        primary_keys=["database_cluster_uuid", "created_at"],
        partition_key="created_at",
        # Both legs of the fan-out share one session, and the parent leg is `/v2/databases`,
        # whose response embeds live connection URIs and passwords.
        captures_http_samples=False,
        fanout=DependentEndpointConfig(
            parent_name="databases",
            resolve_param="database_cluster_uuid",
            resolve_field="id",
            include_from_parent=["id"],
            parent_field_renames={"id": "database_cluster_uuid"},
            parent_params={"per_page": PAGE_SIZE},
            # Caching and Valkey clusters do not support backups, and the fan-out visits every
            # cluster the account has. Treating the endpoint's documented not-found response as
            # an empty page keeps one such cluster from failing the whole table.
            child_response_actions=[{"status_code": 404, "action": "ignore"}],
        ),
    ),
    # Cluster event history (create, update, maintenance, failover, power cycles) — the
    # state-transition log for managed databases, which `/v2/actions` does not cover.
    "database_events": DigitalOceanEndpointConfig(
        name="database_events",
        path="/v2/databases/{database_cluster_uuid}/events",
        data_selector="events",
        # The event id is only documented as identifying the event within its cluster.
        primary_keys=["database_cluster_uuid", "id"],
        partition_key="create_time",
        # Same shared-session reason as `database_backups`: the parent leg is `/v2/databases`.
        captures_http_samples=False,
        fanout=DependentEndpointConfig(
            parent_name="databases",
            resolve_param="database_cluster_uuid",
            resolve_field="id",
            include_from_parent=["id"],
            parent_field_renames={"id": "database_cluster_uuid"},
            parent_params={"per_page": PAGE_SIZE},
        ),
    ),
    # Deployment history per App Platform app, with the phase each deployment reached. Every
    # deployment carries a copy of the app spec, so the same secrets as `apps` are stripped, plus
    # the image pull credentials that the deployment spec can hold.
    "app_deployments": DigitalOceanEndpointConfig(
        name="app_deployments",
        path="/v2/apps/{app_id}/deployments",
        data_selector="deployments",
        primary_keys=["app_id", "id"],
        partition_key="created_at",
        sensitive_fields=frozenset({"envs", "log_destinations", "registry_credentials"}),
        fanout=DependentEndpointConfig(
            parent_name="apps",
            resolve_param="app_id",
            resolve_field="id",
            include_from_parent=["id"],
            parent_field_renames={"id": "app_id"},
            parent_params={"per_page": PAGE_SIZE},
            child_params={"per_page": PAGE_SIZE},
        ),
    ),
    # Deployment and autoscaling events per app. A deployment event embeds the full deployment,
    # spec included, so it is stripped the same way as `app_deployments`.
    "app_events": DigitalOceanEndpointConfig(
        name="app_events",
        path="/v2/apps/{app_id}/events",
        data_selector="events",
        primary_keys=["app_id", "id"],
        partition_key="created_at",
        sensitive_fields=frozenset({"envs", "log_destinations", "registry_credentials"}),
        fanout=DependentEndpointConfig(
            parent_name="apps",
            resolve_param="app_id",
            resolve_field="id",
            include_from_parent=["id"],
            parent_field_renames={"id": "app_id"},
            parent_params={"per_page": PAGE_SIZE},
            child_params={"per_page": PAGE_SIZE},
        ),
    ),
    # Node pool sizing and autoscaling bounds per Kubernetes cluster. The endpoint takes no page
    # params and returns every pool of the cluster in one response.
    "kubernetes_node_pools": DigitalOceanEndpointConfig(
        name="kubernetes_node_pools",
        path="/v2/kubernetes/clusters/{cluster_id}/node_pools",
        data_selector="node_pools",
        primary_keys=["kubernetes_cluster_id", "id"],
        fanout=DependentEndpointConfig(
            parent_name="kubernetes_clusters",
            resolve_param="cluster_id",
            resolve_field="id",
            include_from_parent=["id"],
            parent_field_renames={"id": "kubernetes_cluster_id"},
            parent_params={"per_page": PAGE_SIZE},
        ),
    ),
    "uptime_checks": DigitalOceanEndpointConfig(
        name="uptime_checks",
        path="/v2/uptime/checks",
        data_selector="checks",
    ),
    # Current per-region status and the last outage of each uptime check. The endpoint returns
    # one object under `state` rather than a list, and the object carries no check id.
    "uptime_check_states": DigitalOceanEndpointConfig(
        name="uptime_check_states",
        path="/v2/uptime/checks/{check_id}/state",
        data_selector="state",
        primary_keys=["check_id"],
        paginated=False,
        fanout=DependentEndpointConfig(
            parent_name="uptime_checks",
            resolve_param="check_id",
            resolve_field="id",
            include_from_parent=["id"],
            parent_field_renames={"id": "check_id"},
            parent_params={"per_page": PAGE_SIZE},
        ),
    ),
    # `/v2/registries` replaces the deprecated single-registry `/v2/registry` endpoints, which
    # stop working once an account has more than one registry.
    "registries": DigitalOceanEndpointConfig(
        name="registries",
        path="/v2/registries",
        data_selector="registries",
        primary_keys=["name"],
        partition_key="created_at",
    ),
    # Repositories per container registry, with tag and manifest counts and the size of the
    # latest manifest.
    "registry_repositories": DigitalOceanEndpointConfig(
        name="registry_repositories",
        path="/v2/registries/{registry_name}/repositoriesV2",
        data_selector="repositories",
        # Repository names are only unique within their registry.
        primary_keys=["registry_name", "name"],
        fanout=DependentEndpointConfig(
            parent_name="registries",
            resolve_param="registry_name",
            resolve_field="name",
            include_from_parent=["name"],
            # Lands on the row's own `registry_name`, which holds the same value, and keeps the
            # repository's `name` column intact.
            parent_field_renames={"name": "registry_name"},
            parent_params={"per_page": PAGE_SIZE},
            child_params={"per_page": PAGE_SIZE},
        ),
    ),
}

ENDPOINTS = tuple(DIGITALOCEAN_ENDPOINTS.keys())

# DigitalOcean exposes no server-side timestamp filter on any list endpoint, so no endpoint
# supports genuine incremental sync. Kept for parity with the schema-building convention.
INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {}