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.

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

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


@dataclass(frozen=True)
class HetznerEndpointConfig:
    name: str
    path: str
    # JSON envelope key the list lives under, e.g. {"servers": [...], "meta": {...}}.
    response_key: str
    # Stable, immutable datetime field to partition by (never a mutable `updated`-style field).
    # None for the pricing-catalog endpoints, which carry no timestamps.
    partition_key: Optional[str] = None
    # Value passed to the API's `sort` param (Hetzner format, e.g. "id:asc"). Sorting a stable
    # monotonic column ascending keeps new rows appending to the end so a full-refresh sync can't
    # skip or duplicate rows across page boundaries. None leaves the endpoint at its default order
    # (used for the small catalog endpoints, whose sort support we could not curl-verify).
    sort: Optional[str] = None
    primary_keys: list[str] = field(default_factory=lambda: ["id"])
    should_sync_default: bool = True
    # False for single-object endpoints (e.g. /pricing) that ignore `page` and carry no
    # `meta.pagination`, so a page-number paginator would request page 2 forever.
    paginated: bool = True


@dataclass(frozen=True)
class HetznerMetricsEndpointConfig:
    name: str
    # Key into HETZNER_ENDPOINTS for the list whose resources the metrics fan out over.
    parent: str
    # Column carrying the parent resource id on every metric row.
    parent_id_column: str
    # Comma-separated `type` values; one request returns every series of these types.
    metric_types: str


@dataclass(frozen=True)
class HetznerChildEndpointConfig:
    name: str
    # Key into HETZNER_ENDPOINTS for the list whose resources the child list fans out over.
    parent: str
    # Column carrying the parent resource id on every child row.
    parent_id_column: str
    # Path under `{parent path}/{id}`, e.g. "/members".
    path_suffix: str
    response_key: str
    primary_keys: list[str]
    sort: str


# The Hetzner Cloud API exposes no server-side timestamp filter on any list endpoint (no
# `created_since` / `updated_after` / `id_gt`), so every table is full refresh only — there is no
# cursor that would make an "incremental" sync cheaper than re-reading every page. Actions are
# append-only and immutable, but without a server filter they too must be full refreshed. Sorting
# by `id:asc` gives a stable ascending order so pagination stays consistent while a full refresh runs.
HETZNER_ENDPOINTS: dict[str, HetznerEndpointConfig] = {
    "servers": HetznerEndpointConfig(
        name="servers",
        path="/servers",
        response_key="servers",
        partition_key="created",
        sort="id:asc",
    ),
    "volumes": HetznerEndpointConfig(
        name="volumes",
        path="/volumes",
        response_key="volumes",
        partition_key="created",
        sort="id:asc",
    ),
    "load_balancers": HetznerEndpointConfig(
        name="load_balancers",
        path="/load_balancers",
        response_key="load_balancers",
        partition_key="created",
        sort="id:asc",
    ),
    "networks": HetznerEndpointConfig(
        name="networks",
        path="/networks",
        response_key="networks",
        partition_key="created",
        sort="id:asc",
    ),
    "firewalls": HetznerEndpointConfig(
        name="firewalls",
        path="/firewalls",
        response_key="firewalls",
        partition_key="created",
        sort="id:asc",
    ),
    "images": HetznerEndpointConfig(
        name="images",
        path="/images",
        response_key="images",
        partition_key="created",
        sort="id:asc",
    ),
    "floating_ips": HetznerEndpointConfig(
        name="floating_ips",
        path="/floating_ips",
        response_key="floating_ips",
        partition_key="created",
        sort="id:asc",
    ),
    "primary_ips": HetznerEndpointConfig(
        name="primary_ips",
        path="/primary_ips",
        response_key="primary_ips",
        partition_key="created",
        sort="id:asc",
    ),
    "certificates": HetznerEndpointConfig(
        name="certificates",
        path="/certificates",
        response_key="certificates",
        partition_key="created",
        sort="id:asc",
    ),
    "ssh_keys": HetznerEndpointConfig(
        name="ssh_keys",
        path="/ssh_keys",
        response_key="ssh_keys",
        partition_key="created",
        sort="id:asc",
    ),
    "placement_groups": HetznerEndpointConfig(
        name="placement_groups",
        path="/placement_groups",
        response_key="placement_groups",
        partition_key="created",
        sort="id:asc",
    ),
    # Append-only audit history of every operation. No `created` field, so no datetime partition;
    # `id` is monotonic, so sort ascending for stable pagination.
    "actions": HetznerEndpointConfig(
        name="actions",
        path="/actions",
        response_key="actions",
        sort="id:asc",
    ),
    # Pricing / infrastructure catalog. Same for every project, no timestamps, small enough to fit a
    # page or two, so no partitioning and no explicit sort.
    "server_types": HetznerEndpointConfig(
        name="server_types",
        path="/server_types",
        response_key="server_types",
    ),
    "load_balancer_types": HetznerEndpointConfig(
        name="load_balancer_types",
        path="/load_balancer_types",
        response_key="load_balancer_types",
    ),
    "datacenters": HetznerEndpointConfig(
        name="datacenters",
        path="/datacenters",
        response_key="datacenters",
    ),
    "locations": HetznerEndpointConfig(
        name="locations",
        path="/locations",
        response_key="locations",
    ),
    "isos": HetznerEndpointConfig(
        name="isos",
        path="/isos",
        response_key="isos",
    ),
    "zones": HetznerEndpointConfig(
        name="zones",
        path="/zones",
        response_key="zones",
        partition_key="created",
        sort="id:asc",
    ),
    # A single object (currency, VAT rate and per-location prices for every resource kind), so it
    # lands as one row keyed by its currency.
    "pricing": HetznerEndpointConfig(
        name="pricing",
        path="/pricing",
        response_key="pricing",
        primary_keys=["currency"],
        paginated=False,
    ),
}

# Time series per resource. Unlike the list endpoints these take a required `start`/`end` window,
# which is a real server-side filter, so they support incremental sync on `timestamp`.
HETZNER_METRICS_ENDPOINTS: dict[str, HetznerMetricsEndpointConfig] = {
    "server_metrics": HetznerMetricsEndpointConfig(
        name="server_metrics",
        parent="servers",
        parent_id_column="server_id",
        metric_types="cpu,disk,network",
    ),
    "load_balancer_metrics": HetznerMetricsEndpointConfig(
        name="load_balancer_metrics",
        parent="load_balancers",
        parent_id_column="load_balancer_id",
        metric_types="open_connections,connections_per_second,requests_per_second,bandwidth",
    ),
}

# Per-parent lists with no timestamps and no server-side filter, so full refresh only.
HETZNER_CHILD_ENDPOINTS: dict[str, HetznerChildEndpointConfig] = {
    # Servers and load balancers attached to each network. Their ids come from separate id spaces,
    # and one resource can join several networks, so the key needs the network id and the type.
    "network_members": HetznerChildEndpointConfig(
        name="network_members",
        parent="networks",
        parent_id_column="network_id",
        path_suffix="/members",
        response_key="members",
        primary_keys=["network_id", "type", "id"],
        sort="id:asc",
    ),
    # DNS records of each zone, grouped by name and type. The `id` ("www/A") is unique only within
    # its zone, and each row already carries the zone id in `zone`.
    "zone_rrsets": HetznerChildEndpointConfig(
        name="zone_rrsets",
        parent="zones",
        parent_id_column="zone",
        path_suffix="/rrsets",
        response_key="rrsets",
        primary_keys=["zone", "id"],
        sort="id:asc",
    ),
}

ENDPOINTS = (*HETZNER_ENDPOINTS.keys(), *HETZNER_METRICS_ENDPOINTS.keys(), *HETZNER_CHILD_ENDPOINTS.keys())

_TIMESTAMP_FIELD = IncrementalField(
    label="timestamp",
    type=IncrementalFieldType.DateTime,
    field="timestamp",
    field_type=IncrementalFieldType.DateTime,
)

INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {
    **{name: [] for name in HETZNER_ENDPOINTS},
    **{name: [] for name in HETZNER_CHILD_ENDPOINTS},
    **{name: [_TIMESTAMP_FIELD] for name in HETZNER_METRICS_ENDPOINTS},
}