Skip to content

Add cloud postgres subcommand tree#127

Merged
sdairs merged 14 commits into
issue-48-debug-auth-sourcefrom
issue-116-cloud-postgres
May 5, 2026
Merged

Add cloud postgres subcommand tree#127
sdairs merged 14 commits into
issue-48-debug-auth-sourcefrom
issue-116-cloud-postgres

Conversation

@sdairs
Copy link
Copy Markdown
Collaborator

@sdairs sdairs commented Apr 16, 2026

Closes #116.

Stacked on top of #120 — merge that first.

Summary

Plumbs all 13 ClickHouse Cloud managed Postgres operations into the CLI under clickhousectl cloud postgres ...:

  • CRUD: list, get, create, update, delete
  • Lifecycle: restart, promote, switchover (flat subcommands, mirroring service start|stop)
  • Certs: certs get [--output PATH] — raw PEM to stdout or file (mode 0600 on unix), JSON mode wraps in {"certificate": ...}
  • Runtime config: config get|replace|patch — patch accepts either --set key=value (repeatable, parses values as JSON with string fallback) or --file PATH; mutually exclusive
  • Password: reset-password --password VALUE | --generate
  • Read replica: read-replica create <pg-id> --name …
  • PITR restore: restore <pg-id> --name … --restore-target <ISO8601>

All commands support --json, --debug, --org-id (auto-resolved), and inherit the OAuth/API-key auth-precedence and bearer-write-block that the cloud tree already uses.

Design

Everything lives in a single new module crates/clickhousectl/src/cloud/postgres.rs (~1150 lines) — the PostgresCommands clap enum, 13 handler functions, internal option structs, helpers, tests, and the is_write() classifier. This is a departure from the cli.rs + commands.rs split used for Service/Org/Backup, but it keeps the already-2400-line commands.rs from growing another 600 lines of Postgres-specific helpers (parse_pg_config_overrides, render_postgres_service, JSON-file loader, PEM file writer, password generator/validator).

  • cloud/cli.rs adds one CloudCommands::Postgres { command } variant plus a one-line delegation in is_write_command()command.is_write(), keeping the exhaustive-match compiler guard intact.
  • cloud/commands.rs unchanged except for bumping resolve_org_id, parse_tag, parse_tags, parse_serde_enum from private to pub(super) so postgres.rs can reuse them.
  • cloud/cli.rs::parse_datetime bumped to pub(super) for --restore-target validation.
  • uuid gains the v4 feature (for --generate password, which concatenates two UUIDv4s and prefixes A1 to satisfy upper+lower+digit).

UX notes

  • --size accepts any string (e.g. m7i.2xlarge); server validates. Same posture as --region.
  • --pg-version constrained to 18|17|16; --ha-type to none|async|sync; --provider defaults to aws.
  • update --add-tag K=V / --remove-tag K merges against current tags by fetching the pg service first and diffing.

Test plan

  • cargo test -p clickhousectl234 passing (27 new in postgres.rs, plus 14 new write-classification assertions in cli.rs::tests)
  • cargo clippy -p clickhousectl --all-targets — clean
  • Release build clean
  • Manual smoke: cargo run -p clickhousectl -- cloud postgres list --org-id … hits the API end-to-end (returns well-formed FORBIDDEN: early access API endpoint when auth'd via OAuth — expected, since pg is beta-gated to API-key auth)
  • cloud postgres --help renders the full subcommand tree with the CONTEXT FOR AGENTS block

New tests

CLI-parse (17): list/get/delete, create minimal/all-flags/rejects-missing/rejects-invalid-version, update tag-diff/no-fields, certs get stdout+output, config get, config replace requires-file, config patch with-set/with-file/rejects-set+file-together, reset-password with-password/with-generate/rejects-both, restore valid-rfc3339/rejects-invalid-datetime, read-replica create, restart+promote+switchover.

Helper unit (6): parse_pg_config_overrides numeric/string coercion, malformed rejection, last-wins on duplicates; validate_password_rules; generated_password_is_compliant; merge_tags_adds_and_removes.

Write-classification (14 assertions): extends the existing is_write_command_read_only_commands and is_write_command_destructive_commands tests with all 14 Postgres variants.

🤖 Generated with Claude Code

Plumbs all 13 ClickHouse Cloud managed Postgres operations into the CLI
under `clickhousectl cloud postgres ...` — CRUD, lifecycle (restart/
promote/switchover), CA certs, runtime config (get/replace/patch with
--set key=value overrides), password reset, read replica creation, and
PITR restore. Lives in its own src/cloud/postgres.rs module with 33 new
unit + parse tests and the full write-classification coverage.

Closes #116

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@sdairs sdairs requested a review from iskakaushik as a code owner April 16, 2026 22:07
@sdairs sdairs temporarily deployed to cloud-integration April 16, 2026 22:07 — with GitHub Actions Inactive
Mirrors the ClickHouse service lifecycle test (create, wait-running, list,
certs, config get, PATCH tags, password reset, restart, delete) against the
Postgres endpoints, wired into the scheduled Cloud Integration workflow.

Password step treats a successful 200 as the pass condition: per the OpenAPI
spec, PostgresServicePasswordResource.password is only populated when the
request omits `password`, so the supplied-password path returns empty by
design.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@sdairs sdairs temporarily deployed to cloud-integration April 17, 2026 13:42 — with GitHub Actions Inactive
Tables now render using only ASCII `|` and `-` instead of the
non-standard rounded box-drawing characters, so output is readable in
minimal terminals and log aggregators and pasteable into issues/PRs.

Closes #126
sdairs and others added 11 commits May 4, 2026 17:56
Closes #133. Uses the is-ai-agent crate to detect when the CLI is invoked under a known agent (Claude Code, Cursor, Gemini CLI, Codex, Goose, Devin, etc.) and appends an agent=<id> query param to outbound requests to ClickHouse-owned hosts (builds.clickhouse.com, packages.clickhouse.com, api.clickhouse.cloud). GitHub and other third-party hosts are not annotated. The cloud library gains a generic Client::with_extra_query_params builder so the CLI can attach the tag to every request.
Three of the four credential branches in CloudClient::new differed only in which (key, secret) pair they pulled and which AuthSource label to attach. Introduce ResolvedAuth + resolve_auth() to walk the precedence ladder once, then build the lib client and tag it with the agent param at a single site. resolve_active_auth_source becomes a thin wrapper that preserves its lenient half-set CLI-flag behavior for cloud auth status.
The helper is only called from cloud auth status, which never has --api-key/--api-secret to pass (the subcommand doesn't accept them). The half-set lenient branch and its test were protecting a contract no production caller exercises. Inlining the only sensible call removes the dead parameters and the dead branch, leaving a one-line wrapper that documents its actual purpose: peek at credential precedence without erroring on the empty case (which auth status needs to render no-creds-configured correctly).
The local AgentId -> kebab-case match arm was duplicating a mapping the crate already maintains for its AGENT= env var parser. is-ai-agent 0.2.1 exposes AgentId::as_str returning the canonical kebab-case id (claude-code, gemini-cli, etc.) — the inverse of the parser. Switch to it. Replaces our 12-arm match plus exhaustive variant test with a single delegation and a contract test against the upstream lookup. New agents added upstream automatically flow through without code changes here.
…arams

Rather than thread an agent search param through every clickhouse.com request (with a URL-domain gate, two helpers, and a generic extra-query-params feature on the cloud library), fold the signal into the User-Agent header that every outbound request already carries: clickhousectl/0.1.18 (agent=claude-code). RFC 7231 allows parenthesised comments in User-Agent, and this matches conventional shapes (Mozilla/5.0 etc). Detection still uses is-ai-agent. The change deletes the agent_signal module, the cloud library extra_query_params API + tests, the URL-host parser, and the per-call-site wrappers in version_manager — net -146 lines vs the previous implementation. Every reqwest::Client::builder() in the codebase already calls user_agent::user_agent(), so the new attribution flows through with zero per-call-site wiring.
It's now a one-line implementation detail of an analytics signal — not user-facing functionality, not configurable, not surprising for a future reader to understand from the user_agent.rs source. Doesn't earn a documentation entry.
Pull in upstream security fixes flagged by Dependabot. Both are transitive dependencies; lockfile-only update, no API or behaviour changes. Build and full workspace tests pass.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Tag clickhouse.com requests with detected AI agent
@sdairs sdairs temporarily deployed to cloud-integration May 5, 2026 17:03 — with GitHub Actions Inactive
@sdairs sdairs merged commit 9f92ee8 into issue-48-debug-auth-source May 5, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants