From 26231a6d0dd21844593bb4ebe1746ed9a10ab59f Mon Sep 17 00:00:00 2001 From: sdairs Date: Tue, 17 Nov 2026 16:58:20 +0000 Subject: [PATCH] fix(api): add Postgres private endpoint operations Adds the four beta `postgresPrivateEndpoint*` operations as `postgres_private_endpoint_{get,post,patch,delete}`, their models (`PostgresPrivateEndpoint`, `PostgresPrivateEndpointPort`, the post and patch requests, and the split `PostgresPrivateEndpointVpcEndpoint` / `PostgresPrivateEndpointVpcEndpointResponse` pair with a `TryFrom` write-back), the operation descriptors and the Beta list entries. `PostgresPrivateEndpointPostRequest` is listed as an optional request schema: its only field needs a feature most organizations lack. The live Postgres suite gains a private endpoint phase, gated on AWS and `CLICKHOUSE_CLOUD_POSTGRES_PRIVATE_ENDPOINT`. Fixes #1482 Co-Authored-By: Claude Opus 5.5 --- crates/clickhouse-cloud-api/CHANGELOG.md | 19 ++ .../src/client/postgres.rs | 124 +++++++++++ .../src/convert/postgres.rs | 19 ++ crates/clickhouse-cloud-api/src/meta.rs | 4 + .../src/meta/operations.rs | 40 ++++ crates/clickhouse-cloud-api/src/models.rs | 5 +- .../src/models/postgres.rs | 119 ++++++++++ .../tests/client/postgres.rs | 208 ++++++++++++++++++ .../tests/integration_postgres_test.rs | 166 ++++++++++++++ .../tests/model_facade_test.rs | 24 ++ .../tests/models/postgres.rs | 71 ++++++ .../clickhouse-openapi-analyzer/src/config.rs | 6 + docs/development.md | 2 +- 13 files changed, 805 insertions(+), 2 deletions(-) diff --git a/crates/clickhouse-cloud-api/CHANGELOG.md b/crates/clickhouse-cloud-api/CHANGELOG.md index c633a4708..77b9a35dd 100644 --- a/crates/clickhouse-cloud-api/CHANGELOG.md +++ b/crates/clickhouse-cloud-api/CHANGELOG.md @@ -234,6 +234,25 @@ more pages remain and `ApiResponse::limit` echoes the page size. Migrate the request sent is unchanged, but an organization with more than 250 roles now needs to follow `next_cursor` to see them all. +The beta Postgres private endpoint methods are `postgres_private_endpoint_get`, +`postgres_private_endpoint_post`, `postgres_private_endpoint_patch` and +`postgres_private_endpoint_delete`. They manage the AWS private endpoint +service of a Postgres service, available only for AWS services in +organizations with the feature enabled. Reads, create and patch return +`PostgresPrivateEndpoint` (state, endpoint service name and ID, private +hostname and DNS name, ports, supported regions and approved VPC endpoints); +get returns `Error::Api { status: 404, .. }` before one is created, and +delete returns no `result`. Create takes `PostgresPrivateEndpointPostRequest` +(optional `supported_regions`) and stays `creating` for about 5 to 10 +minutes. Patch takes `PostgresPrivateEndpointPatchRequest`: each list set +(`allowed_vpc_endpoints`, `supported_regions`) replaces the existing one in +full. An approved endpoint is `PostgresPrivateEndpointVpcEndpoint` (required +`vpc_endpoint_id`, optional `description`) in requests and +`PostgresPrivateEndpointVpcEndpointResponse` in responses, with `TryFrom` +from the response type for writing a fetched list back. Get needs +`control-plane:postgres-service:view`; the others need +`control-plane:postgres-service:manage`. + ## 0.5.1 No changes to this crate; released in lockstep with `clickhousectl` 0.5.1. diff --git a/crates/clickhouse-cloud-api/src/client/postgres.rs b/crates/clickhouse-cloud-api/src/client/postgres.rs index d0e923f4c..b9fa9b1b8 100644 --- a/crates/clickhouse-cloud-api/src/client/postgres.rs +++ b/crates/clickhouse-cloud-api/src/client/postgres.rs @@ -285,6 +285,130 @@ impl Client { Ok(serde_json::from_str(&body_text)?) } + /// Get the AWS private endpoint service of a Postgres service (Beta). + /// + /// Returns `Error::Api { status: 404, .. }` if it has not been created. + /// Private endpoints are available only for AWS Postgres services, and only + /// for organizations with the feature enabled. + pub async fn postgres_private_endpoint_get( + &self, + organization_id: &str, + postgres_id: &str, + ) -> Result, Error> { + let req = self.request( + &operations::POSTGRES_PRIVATE_ENDPOINT_GET, + &[organization_id, postgres_id], + )?; + let resp = req.send().await?; + let status = resp.status(); + let body_text = resp.text().await?; + if !status.is_success() { + return Err(Error::Api { + status: status.as_u16(), + message: serde_json::from_str::>(&body_text) + .ok() + .and_then(|r| r.error) + .unwrap_or(body_text.clone()), + }); + } + Ok(serde_json::from_str(&body_text)?) + } + + /// Create the AWS private endpoint service of a Postgres service (Beta). + /// + /// Creation takes about 5 to 10 minutes: `state` is `creating` until it is + /// `available`. A VPC endpoint can connect only after its ID is approved with + /// [`Client::postgres_private_endpoint_patch`]. + /// Private endpoints are available only for AWS Postgres services, and only + /// for organizations with the feature enabled. + pub async fn postgres_private_endpoint_post( + &self, + organization_id: &str, + postgres_id: &str, + body: &PostgresPrivateEndpointPostRequest, + ) -> Result, Error> { + let mut req = self.request( + &operations::POSTGRES_PRIVATE_ENDPOINT_POST, + &[organization_id, postgres_id], + )?; + req = req.json(body); + let resp = req.send().await?; + let status = resp.status(); + let body_text = resp.text().await?; + if !status.is_success() { + return Err(Error::Api { + status: status.as_u16(), + message: serde_json::from_str::>(&body_text) + .ok() + .and_then(|r| r.error) + .unwrap_or(body_text.clone()), + }); + } + Ok(serde_json::from_str(&body_text)?) + } + + /// Approve VPC endpoints and set the supported regions of a Postgres private + /// endpoint service (Beta). + /// + /// Set at least one field of `body`; each one set replaces that list in full. + /// Private endpoints are available only for AWS Postgres services, and only + /// for organizations with the feature enabled. + pub async fn postgres_private_endpoint_patch( + &self, + organization_id: &str, + postgres_id: &str, + body: &PostgresPrivateEndpointPatchRequest, + ) -> Result, Error> { + let mut req = self.request( + &operations::POSTGRES_PRIVATE_ENDPOINT_PATCH, + &[organization_id, postgres_id], + )?; + req = req.json(body); + let resp = req.send().await?; + let status = resp.status(); + let body_text = resp.text().await?; + if !status.is_success() { + return Err(Error::Api { + status: status.as_u16(), + message: serde_json::from_str::>(&body_text) + .ok() + .and_then(|r| r.error) + .unwrap_or(body_text.clone()), + }); + } + Ok(serde_json::from_str(&body_text)?) + } + + /// Delete the AWS private endpoint service of a Postgres service (Beta). + /// + /// Connections through approved VPC endpoints stop working. The response + /// carries no `result`. + /// Private endpoints are available only for AWS Postgres services, and only + /// for organizations with the feature enabled. + pub async fn postgres_private_endpoint_delete( + &self, + organization_id: &str, + postgres_id: &str, + ) -> Result, Error> { + let req = self.request( + &operations::POSTGRES_PRIVATE_ENDPOINT_DELETE, + &[organization_id, postgres_id], + )?; + let resp = req.send().await?; + let status = resp.status(); + let body_text = resp.text().await?; + if !status.is_success() { + return Err(Error::Api { + status: status.as_u16(), + message: serde_json::from_str::>(&body_text) + .ok() + .and_then(|r| r.error) + .unwrap_or(body_text.clone()), + }); + } + Ok(serde_json::from_str(&body_text)?) + } + /// Update Postgres superuser password pub async fn postgres_service_set_password( &self, diff --git a/crates/clickhouse-cloud-api/src/convert/postgres.rs b/crates/clickhouse-cloud-api/src/convert/postgres.rs index 1b2fb2dff..0b558490b 100644 --- a/crates/clickhouse-cloud-api/src/convert/postgres.rs +++ b/crates/clickhouse-cloud-api/src/convert/postgres.rs @@ -124,3 +124,22 @@ impl TryFrom for PostgresIpAccessListEntry { } } } + +impl TryFrom for PostgresPrivateEndpointVpcEndpoint { + type Error = MissingRequiredFields; + + /// Turns a fetched approved VPC endpoint into one that can be sent back. + /// + /// The patch replaces the approved list in full, so a caller that adds or + /// removes one endpoint writes back every other entry it read. An entry + /// without `vpcEndpointId` cannot be written back. + fn try_from(value: PostgresPrivateEndpointVpcEndpointResponse) -> Result { + match value.vpc_endpoint_id { + Some(vpc_endpoint_id) => Ok(Self { + description: value.description, + vpc_endpoint_id, + }), + None => Err(MissingRequiredFields::new(vec!["vpcEndpointId"])), + } + } +} diff --git a/crates/clickhouse-cloud-api/src/meta.rs b/crates/clickhouse-cloud-api/src/meta.rs index 6e50dc0ec..39f8519cd 100644 --- a/crates/clickhouse-cloud-api/src/meta.rs +++ b/crates/clickhouse-cloud-api/src/meta.rs @@ -113,6 +113,10 @@ pub const BETA_OPERATIONS: &[&str] = &[ "organization_public_previews_get_list", "organization_quota_get", "organization_quotas_get_list", + "postgres_private_endpoint_delete", + "postgres_private_endpoint_get", + "postgres_private_endpoint_patch", + "postgres_private_endpoint_post", "postgres_service_create", "replica_aware_routing_get", "replica_aware_routing_update", diff --git a/crates/clickhouse-cloud-api/src/meta/operations.rs b/crates/clickhouse-cloud-api/src/meta/operations.rs index 2fd27c2e2..e4233d901 100644 --- a/crates/clickhouse-cloud-api/src/meta/operations.rs +++ b/crates/clickhouse-cloud-api/src/meta/operations.rs @@ -1203,6 +1203,42 @@ pub const POSTGRES_ORG_PROMETHEUS_GET: OperationMetadata = OperationMetadata { required_permissions: &["control-plane:organization:view"], }; +/// `DELETE /v1/organizations/{organizationId}/postgres/{postgresId}/privateEndpoint` (`postgresPrivateEndpointDelete`). +pub const POSTGRES_PRIVATE_ENDPOINT_DELETE: OperationMetadata = OperationMetadata { + operation_id: "postgresPrivateEndpointDelete", + rust_method: "postgres_private_endpoint_delete", + method: "DELETE", + path: "/v1/organizations/{organizationId}/postgres/{postgresId}/privateEndpoint", + required_permissions: &["control-plane:postgres-service:manage"], +}; + +/// `GET /v1/organizations/{organizationId}/postgres/{postgresId}/privateEndpoint` (`postgresPrivateEndpointGet`). +pub const POSTGRES_PRIVATE_ENDPOINT_GET: OperationMetadata = OperationMetadata { + operation_id: "postgresPrivateEndpointGet", + rust_method: "postgres_private_endpoint_get", + method: "GET", + path: "/v1/organizations/{organizationId}/postgres/{postgresId}/privateEndpoint", + required_permissions: &["control-plane:postgres-service:view"], +}; + +/// `PATCH /v1/organizations/{organizationId}/postgres/{postgresId}/privateEndpoint` (`postgresPrivateEndpointPatch`). +pub const POSTGRES_PRIVATE_ENDPOINT_PATCH: OperationMetadata = OperationMetadata { + operation_id: "postgresPrivateEndpointPatch", + rust_method: "postgres_private_endpoint_patch", + method: "PATCH", + path: "/v1/organizations/{organizationId}/postgres/{postgresId}/privateEndpoint", + required_permissions: &["control-plane:postgres-service:manage"], +}; + +/// `POST /v1/organizations/{organizationId}/postgres/{postgresId}/privateEndpoint` (`postgresPrivateEndpointPost`). +pub const POSTGRES_PRIVATE_ENDPOINT_POST: OperationMetadata = OperationMetadata { + operation_id: "postgresPrivateEndpointPost", + rust_method: "postgres_private_endpoint_post", + method: "POST", + path: "/v1/organizations/{organizationId}/postgres/{postgresId}/privateEndpoint", + required_permissions: &["control-plane:postgres-service:manage"], +}; + /// `GET /v1/organizations/{organizationId}/postgres/{postgresId}/backups` (`postgresServiceBackupGetList`). pub const POSTGRES_SERVICE_BACKUP_GET_LIST: OperationMetadata = OperationMetadata { operation_id: "postgresServiceBackupGetList", @@ -1814,6 +1850,10 @@ pub const ALL: &[OperationMetadata] = &[ POSTGRES_INSTANCE_RESTORE, POSTGRES_LOGS_GET_LIST, POSTGRES_ORG_PROMETHEUS_GET, + POSTGRES_PRIVATE_ENDPOINT_DELETE, + POSTGRES_PRIVATE_ENDPOINT_GET, + POSTGRES_PRIVATE_ENDPOINT_PATCH, + POSTGRES_PRIVATE_ENDPOINT_POST, POSTGRES_SERVICE_BACKUP_GET_LIST, POSTGRES_SERVICE_CERTS_GET, POSTGRES_SERVICE_CREATE, diff --git a/crates/clickhouse-cloud-api/src/models.rs b/crates/clickhouse-cloud-api/src/models.rs index 289707f6a..f41eb3d98 100644 --- a/crates/clickhouse-cloud-api/src/models.rs +++ b/crates/clickhouse-cloud-api/src/models.rs @@ -216,7 +216,10 @@ pub use postgres::{ PostgresIpAccessListEntry, PostgresIpAccessListEntryResponse, PostgresIpAccessListPatch, PostgresIpAccessListRuleReference, PostgresLogEntry, PostgresLogsGetListSeverity, PostgresLogsGetListSortorder, PostgresMetric, PostgresMetricDataPoint, PostgresMetricSeries, - PostgresMetrics, PostgresQueryExecution, PostgresService, PostgresServiceListItem, + PostgresMetrics, PostgresPrivateEndpoint, PostgresPrivateEndpointPatchRequest, + PostgresPrivateEndpointPort, PostgresPrivateEndpointPostRequest, + PostgresPrivateEndpointVpcEndpoint, PostgresPrivateEndpointVpcEndpointResponse, + PostgresQueryExecution, PostgresService, PostgresServiceListItem, PostgresServicePasswordResource, PostgresServicePatchRequest, PostgresServicePostRequest, PostgresServiceReadReplicaRequest, PostgresServiceRestoreRequest, PostgresServiceSetPassword, PostgresServiceSetState, PostgresServiceSetStateCommand, PostgresSlowQueryPattern, diff --git a/crates/clickhouse-cloud-api/src/models/postgres.rs b/crates/clickhouse-cloud-api/src/models/postgres.rs index 4b5f93f63..c7f3ec397 100644 --- a/crates/clickhouse-cloud-api/src/models/postgres.rs +++ b/crates/clickhouse-cloud-api/src/models/postgres.rs @@ -2038,3 +2038,122 @@ pub struct PostgresBackup { #[serde(rename = "status", skip_serializing_if = "Option::is_none")] pub status: Option, } + +/// `PostgresPrivateEndpoint` from the ClickHouse Cloud API: the AWS private +/// endpoint service of a Postgres service. +/// +/// Used in response position only: every field is `Option`, so a field the +/// API drops or sends as `null` deserializes as `None`. +#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)] +pub struct PostgresPrivateEndpoint { + /// VPC endpoints approved to connect. + #[serde( + rename = "allowedVpcEndpoints", + skip_serializing_if = "Option::is_none" + )] + pub allowed_vpc_endpoints: Option>, + /// ID of the AWS endpoint service. Absent until the service is created. + #[serde(rename = "endpointServiceId", skip_serializing_if = "Option::is_none")] + pub endpoint_service_id: Option, + /// Name of the AWS endpoint service: the service name to use when creating + /// a VPC endpoint. Absent until the service is created. + #[serde( + rename = "endpointServiceName", + skip_serializing_if = "Option::is_none" + )] + pub endpoint_service_name: Option, + /// ID of the private endpoint service. + #[serde(skip_serializing_if = "Option::is_none")] + pub id: Option, + /// IP address type of the endpoint service, such as `dual`. + #[serde(rename = "ipAddressType", skip_serializing_if = "Option::is_none")] + pub ip_address_type: Option, + /// Ports the endpoint service forwards. + #[serde(skip_serializing_if = "Option::is_none")] + pub ports: Option>, + /// Private DNS name of the endpoint service. Absent until it is assigned. + #[serde(rename = "privateDnsName", skip_serializing_if = "Option::is_none")] + pub private_dns_name: Option, + /// Verification state of the private DNS name. Absent until it is assigned. + #[serde( + rename = "privateDnsVerificationState", + skip_serializing_if = "Option::is_none" + )] + pub private_dns_verification_state: Option, + /// Hostname that resolves to the Postgres service from an approved VPC + /// endpoint. Absent until the service is available. + #[serde(rename = "privateHostname", skip_serializing_if = "Option::is_none")] + pub private_hostname: Option, + /// `creating`, `available` or `deleting`; the API may add more states. It + /// is `creating` for about 5 to 10 minutes after creation. + #[serde(skip_serializing_if = "Option::is_none")] + pub state: Option, + /// AWS region IDs, other than the service region, from which VPC endpoints + /// can connect. + #[serde(rename = "supportedRegions", skip_serializing_if = "Option::is_none")] + pub supported_regions: Option>, +} + +/// `PostgresPrivateEndpointPort` from the ClickHouse Cloud API: one port the +/// endpoint service forwards. Used in response position only. +#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)] +pub struct PostgresPrivateEndpointPort { + /// Port exposed by the endpoint service. + #[serde(skip_serializing_if = "Option::is_none")] + pub port: Option, + /// Port on the Postgres service it forwards to. + #[serde(rename = "targetPort", skip_serializing_if = "Option::is_none")] + pub target_port: Option, +} + +/// `PostgresPrivateEndpointVpcEndpoint` from the ClickHouse Cloud API: one VPC +/// endpoint approved to connect, as sent in +/// [`PostgresPrivateEndpointPatchRequest`]. +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct PostgresPrivateEndpointVpcEndpoint { + /// Optional description, at most 255 characters. + #[serde(skip_serializing_if = "Option::is_none")] + pub description: Option, + /// ID of the AWS VPC endpoint, such as `vpce-0123456789abcdef0`. + #[serde(rename = "vpcEndpointId")] + pub vpc_endpoint_id: String, +} + +/// `PostgresPrivateEndpointVpcEndpoint` in response position: one approved VPC +/// endpoint of [`PostgresPrivateEndpoint`]. Every field is `Option`. +#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)] +pub struct PostgresPrivateEndpointVpcEndpointResponse { + #[serde(skip_serializing_if = "Option::is_none")] + pub description: Option, + #[serde(rename = "vpcEndpointId", skip_serializing_if = "Option::is_none")] + pub vpc_endpoint_id: Option, +} + +/// `PostgresPrivateEndpointPostRequest` from the ClickHouse Cloud API: the body +/// that creates a Postgres private endpoint service. +#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)] +pub struct PostgresPrivateEndpointPostRequest { + /// AWS region IDs, other than the service region, from which VPC endpoints + /// can connect, such as `us-west-2`. Requires cross-region private + /// endpoints to be enabled for the organization. + #[serde(rename = "supportedRegions", skip_serializing_if = "Option::is_none")] + pub supported_regions: Option>, +} + +/// `PostgresPrivateEndpointPatchRequest` from the ClickHouse Cloud API. +/// +/// Set at least one field. Each one set replaces the existing list in full, so +/// include every entry to keep; `None` leaves that list unchanged. +#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)] +pub struct PostgresPrivateEndpointPatchRequest { + /// Every VPC endpoint approved to connect, at most 50, each ID once. + /// Endpoints not listed can no longer connect. + #[serde( + rename = "allowedVpcEndpoints", + skip_serializing_if = "Option::is_none" + )] + pub allowed_vpc_endpoints: Option>, + /// Every supported region, other than the service region. + #[serde(rename = "supportedRegions", skip_serializing_if = "Option::is_none")] + pub supported_regions: Option>, +} diff --git a/crates/clickhouse-cloud-api/tests/client/postgres.rs b/crates/clickhouse-cloud-api/tests/client/postgres.rs index 6e31ea707..271a2d97b 100644 --- a/crates/clickhouse-cloud-api/tests/client/postgres.rs +++ b/crates/clickhouse-cloud-api/tests/client/postgres.rs @@ -754,3 +754,211 @@ async fn postgres_service_backup_get_list_propagates_api_errors() { ); } } + +// =========================================================================== +// Postgres private endpoints +// =========================================================================== + +fn private_endpoint_json() -> serde_json::Value { + serde_json::json!({ + "id": "ps0123456789abcdefghjkmnpq", + "state": "available", + "endpointServiceName": "com.amazonaws.vpce.us-east-1.vpce-svc-0123456789abcdef0", + "endpointServiceId": "vpce-svc-0123456789abcdef0", + "privateHostname": "pg-1.private.example", + "ipAddressType": "dual", + "ports": [{"port": 5432, "targetPort": 5432}], + "supportedRegions": ["us-west-2"], + "allowedVpcEndpoints": [ + {"vpcEndpointId": "vpce-0123456789abcdef0", "description": "Production VPC"} + ] + }) +} + +#[tokio::test] +async fn get_postgres_private_endpoint() { + let (s, c) = setup().await; + Mock::given(method("GET")) + .and(path( + "/v1/organizations/org-1/postgres/pg-1/privateEndpoint", + )) + .and(basic_auth("key", "secret")) + .respond_with(ok_json(private_endpoint_json())) + .expect(1) + .mount(&s) + .await; + + let endpoint = c + .postgres_private_endpoint_get("org-1", "pg-1") + .await + .unwrap() + .result + .unwrap(); + assert_eq!(endpoint.state.as_deref(), Some("available")); + assert_eq!( + endpoint.endpoint_service_name.as_deref(), + Some("com.amazonaws.vpce.us-east-1.vpce-svc-0123456789abcdef0") + ); + assert_eq!( + endpoint.ports, + Some(vec![PostgresPrivateEndpointPort { + port: Some(5432), + target_port: Some(5432), + }]) + ); + assert_eq!(endpoint.supported_regions, Some(vec!["us-west-2".into()])); + assert_eq!( + endpoint.allowed_vpc_endpoints, + Some(vec![PostgresPrivateEndpointVpcEndpointResponse { + vpc_endpoint_id: Some("vpce-0123456789abcdef0".into()), + description: Some("Production VPC".into()), + }]) + ); +} + +#[tokio::test] +async fn get_postgres_private_endpoint_not_created_is_a_404() { + let (s, c) = setup().await; + Mock::given(method("GET")) + .and(path( + "/v1/organizations/org-1/postgres/pg-1/privateEndpoint", + )) + .respond_with(ResponseTemplate::new(404).set_body_json(serde_json::json!({ + "status": 404, "error": "Private endpoint not found" + }))) + .mount(&s) + .await; + + let error = c + .postgres_private_endpoint_get("org-1", "pg-1") + .await + .unwrap_err(); + assert!(matches!( + error, + clickhouse_cloud_api::Error::Api { status: 404, .. } + )); +} + +#[tokio::test] +async fn create_postgres_private_endpoint() { + let (s, c) = setup().await; + Mock::given(method("POST")) + .and(path( + "/v1/organizations/org-1/postgres/pg-1/privateEndpoint", + )) + .and(body_partial_json(serde_json::json!({ + "supportedRegions": ["us-west-2"] + }))) + .respond_with(ok_json(serde_json::json!({ + "id": "ps0123456789abcdefghjkmnpq", + "state": "creating", + "ipAddressType": "dual", + "ports": [], + "supportedRegions": ["us-west-2"], + "allowedVpcEndpoints": [] + }))) + .expect(1) + .mount(&s) + .await; + + let endpoint = c + .postgres_private_endpoint_post( + "org-1", + "pg-1", + &PostgresPrivateEndpointPostRequest { + supported_regions: Some(vec!["us-west-2".into()]), + }, + ) + .await + .unwrap() + .result + .unwrap(); + assert_eq!(endpoint.state.as_deref(), Some("creating")); + assert_eq!(endpoint.endpoint_service_name, None); +} + +#[tokio::test] +async fn create_postgres_private_endpoint_without_regions_sends_an_empty_object() { + let (s, c) = setup().await; + Mock::given(method("POST")) + .and(path( + "/v1/organizations/org-1/postgres/pg-1/privateEndpoint", + )) + .respond_with(ok_json(serde_json::json!({"state": "creating"}))) + .expect(1) + .mount(&s) + .await; + + c.postgres_private_endpoint_post( + "org-1", + "pg-1", + &PostgresPrivateEndpointPostRequest::default(), + ) + .await + .unwrap(); + let requests = s.received_requests().await.unwrap(); + let body: serde_json::Value = serde_json::from_slice(&requests[0].body).unwrap(); + assert_eq!(body, serde_json::json!({})); +} + +#[tokio::test] +async fn patch_postgres_private_endpoint() { + let (s, c) = setup().await; + Mock::given(method("PATCH")) + .and(path( + "/v1/organizations/org-1/postgres/pg-1/privateEndpoint", + )) + .respond_with(ok_json(private_endpoint_json())) + .expect(1) + .mount(&s) + .await; + + let endpoint = c + .postgres_private_endpoint_patch( + "org-1", + "pg-1", + &PostgresPrivateEndpointPatchRequest { + allowed_vpc_endpoints: Some(vec![PostgresPrivateEndpointVpcEndpoint { + vpc_endpoint_id: "vpce-0123456789abcdef0".into(), + description: Some("Production VPC".into()), + }]), + supported_regions: None, + }, + ) + .await + .unwrap() + .result + .unwrap(); + assert_eq!(endpoint.state.as_deref(), Some("available")); + // `supportedRegions: None` is omitted, so the server keeps that list. + let requests = s.received_requests().await.unwrap(); + let body: serde_json::Value = serde_json::from_slice(&requests[0].body).unwrap(); + assert_eq!( + body, + serde_json::json!({ + "allowedVpcEndpoints": [ + {"vpcEndpointId": "vpce-0123456789abcdef0", "description": "Production VPC"} + ] + }) + ); +} + +#[tokio::test] +async fn delete_postgres_private_endpoint() { + let (s, c) = setup().await; + Mock::given(method("DELETE")) + .and(path( + "/v1/organizations/org-1/postgres/pg-1/privateEndpoint", + )) + .respond_with(ok_empty()) + .expect(1) + .mount(&s) + .await; + + let resp = c + .postgres_private_endpoint_delete("org-1", "pg-1") + .await + .unwrap(); + assert_eq!(resp.status, Some(200)); + assert_eq!(resp.result, None); +} diff --git a/crates/clickhouse-cloud-api/tests/integration_postgres_test.rs b/crates/clickhouse-cloud-api/tests/integration_postgres_test.rs index 7a9e50216..568721533 100644 --- a/crates/clickhouse-cloud-api/tests/integration_postgres_test.rs +++ b/crates/clickhouse-cloud-api/tests/integration_postgres_test.rs @@ -922,6 +922,172 @@ async fn cloud_postgres_crud_lifecycle() -> TestResult<()> { .await?; cleanup.unregister_postgres_replica(&replica_id); + // ── Private endpoint ──────────────────────────────────────── + + // Private endpoints exist only for AWS Postgres services in + // organizations with the feature enabled, and creation takes 5 to 10 + // minutes, so the phase runs only when the test org opts in. Deleting + // the service also removes an endpoint left behind by a failed step. + if ctx.provider == "aws" + && env::var("CLICKHOUSE_CLOUD_POSTGRES_PRIVATE_ENDPOINT") + .ok() + .filter(|s| !s.is_empty()) + .is_some() + { + log_phase("Private endpoint"); + failures + .run( + &ctx, + StepKind::NonBlocking, + "get postgres private endpoint before create is 404", + || { + let client = client.clone(); + let org_id = ctx.org_id.clone(); + let postgres_id = postgres_id.clone(); + async move { + match client + .postgres_private_endpoint_get(&org_id, &postgres_id) + .await + { + Err(clickhouse_cloud_api::Error::Api { status: 404, .. }) => Ok(()), + Err(error) => Err(error.into()), + Ok(_) => Err("private endpoint exists before create".into()), + } + } + }, + ) + .await?; + + let created = failures + .run( + &ctx, + StepKind::NonBlocking, + "create postgres private endpoint", + || { + let client = client.clone(); + let org_id = ctx.org_id.clone(); + let postgres_id = postgres_id.clone(); + async move { + let resp = client + .postgres_private_endpoint_post( + &org_id, + &postgres_id, + &PostgresPrivateEndpointPostRequest::default(), + ) + .await?; + resp.result + .ok_or_else(|| "private endpoint create returned no result".into()) + } + }, + ) + .await?; + + if created.is_some() { + failures + .run( + &ctx, + StepKind::NonBlocking, + "wait for postgres private endpoint available", + || { + let client = client.clone(); + let org_id = ctx.org_id.clone(); + let postgres_id = postgres_id.clone(); + let timeout = ctx.create_timeout; + let interval = ctx.poll_interval; + async move { + let endpoint = poll_until( + "postgres private endpoint available", + timeout, + interval, + || { + let client = client.clone(); + let org_id = org_id.clone(); + let postgres_id = postgres_id.clone(); + async move { + let endpoint = client + .postgres_private_endpoint_get( + &org_id, + &postgres_id, + ) + .await? + .result + .ok_or("private endpoint get returned no result")?; + Ok((endpoint.state.as_deref() == Some("available")) + .then_some(endpoint)) + } + }, + ) + .await?; + if endpoint.endpoint_service_name.is_none() { + return Err( + "available private endpoint has no endpointServiceName" + .into(), + ); + } + Ok(()) + } + }, + ) + .await?; + + failures + .run( + &ctx, + StepKind::NonBlocking, + "patch postgres private endpoint lists", + || { + let client = client.clone(); + let org_id = ctx.org_id.clone(); + let postgres_id = postgres_id.clone(); + async move { + // Empty lists are valid in every org: no VPC + // endpoint has to exist and no cross-region + // feature is needed. + let body = PostgresPrivateEndpointPatchRequest { + allowed_vpc_endpoints: Some(vec![]), + supported_regions: Some(vec![]), + }; + let endpoint = client + .postgres_private_endpoint_patch(&org_id, &postgres_id, &body) + .await? + .result + .ok_or("private endpoint patch returned no result")?; + if !endpoint + .allowed_vpc_endpoints + .unwrap_or_default() + .is_empty() + { + return Err( + "allowedVpcEndpoints not empty after patch".into() + ); + } + Ok(()) + } + }, + ) + .await?; + + failures + .run( + &ctx, + StepKind::NonBlocking, + "delete postgres private endpoint", + || { + let client = client.clone(); + let org_id = ctx.org_id.clone(); + let postgres_id = postgres_id.clone(); + async move { + client + .postgres_private_endpoint_delete(&org_id, &postgres_id) + .await?; + Ok(()) + } + }, + ) + .await?; + } + } + // ── Delete ────────────────────────────────────────────────── log_phase("Delete"); diff --git a/crates/clickhouse-cloud-api/tests/model_facade_test.rs b/crates/clickhouse-cloud-api/tests/model_facade_test.rs index 2acd63c64..c0b78ecae 100644 --- a/crates/clickhouse-cloud-api/tests/model_facade_test.rs +++ b/crates/clickhouse-cloud-api/tests/model_facade_test.rs @@ -138,6 +138,30 @@ fn extracted_models_keep_root_and_models_paths() { PhantomData::, PhantomData::, ); + assert_same_type( + PhantomData::, + PhantomData::, + ); + assert_same_type( + PhantomData::, + PhantomData::, + ); + assert_same_type( + PhantomData::, + PhantomData::, + ); + assert_same_type( + PhantomData::, + PhantomData::, + ); + assert_same_type( + PhantomData::, + PhantomData::, + ); + assert_same_type( + PhantomData::, + PhantomData::, + ); assert_same_type( PhantomData::, PhantomData::, diff --git a/crates/clickhouse-cloud-api/tests/models/postgres.rs b/crates/clickhouse-cloud-api/tests/models/postgres.rs index 4ba60a843..4e0699514 100644 --- a/crates/clickhouse-cloud-api/tests/models/postgres.rs +++ b/crates/clickhouse-cloud-api/tests/models/postgres.rs @@ -1136,3 +1136,74 @@ fn postgres_requests_send_standby_count_only_when_set() { create.standby_count = Some(PgStandbyCount::Two); assert_eq!(serde_json::to_value(&create).unwrap()["standbyCount"], 2); } + +#[test] +fn postgres_private_endpoint_vpc_endpoint_requires_its_id() { + let missing: Result = + serde_json::from_value(serde_json::json!({"description": "Production VPC"})); + assert!(missing.is_err()); + + let endpoint = PostgresPrivateEndpointVpcEndpoint { + vpc_endpoint_id: "vpce-0123456789abcdef0".into(), + description: None, + }; + assert_eq!( + serde_json::to_value(&endpoint).unwrap(), + serde_json::json!({"vpcEndpointId": "vpce-0123456789abcdef0"}) + ); +} + +#[test] +fn postgres_private_endpoint_vpc_endpoint_response_converts_back_into_a_request_entry() { + let endpoint = + PostgresPrivateEndpointVpcEndpoint::try_from(PostgresPrivateEndpointVpcEndpointResponse { + vpc_endpoint_id: Some("vpce-0123456789abcdef0".into()), + description: Some("Production VPC".into()), + }) + .unwrap(); + assert_eq!( + serde_json::to_value(&endpoint).unwrap(), + serde_json::json!({ + "vpcEndpointId": "vpce-0123456789abcdef0", "description": "Production VPC" + }) + ); + + let missing = + PostgresPrivateEndpointVpcEndpoint::try_from(PostgresPrivateEndpointVpcEndpointResponse { + description: Some("Production VPC".into()), + ..Default::default() + }) + .unwrap_err(); + assert_eq!(missing.fields(), ["vpcEndpointId"]); +} + +#[test] +fn postgres_private_endpoint_response_tolerates_missing_and_null_fields() { + let endpoint: PostgresPrivateEndpoint = serde_json::from_value(serde_json::json!({ + "id": "ps0123456789abcdefghjkmnpq", + "state": "creating", + "endpointServiceName": null, + "ports": [{"port": 5432}], + "allowedVpcEndpoints": [{"description": "no id"}] + })) + .unwrap(); + assert_eq!(endpoint.endpoint_service_name, None); + assert_eq!(endpoint.supported_regions, None); + assert_eq!(endpoint.ports.unwrap()[0].target_port, None); + assert_eq!( + endpoint.allowed_vpc_endpoints.unwrap()[0].vpc_endpoint_id, + None + ); +} + +#[test] +fn postgres_private_endpoint_patch_request_omits_unset_lists() { + assert_eq!( + serde_json::to_value(PostgresPrivateEndpointPatchRequest { + supported_regions: Some(vec![]), + ..Default::default() + }) + .unwrap(), + serde_json::json!({"supportedRegions": []}) + ); +} diff --git a/crates/clickhouse-openapi-analyzer/src/config.rs b/crates/clickhouse-openapi-analyzer/src/config.rs index 1101c6583..d204243e6 100644 --- a/crates/clickhouse-openapi-analyzer/src/config.rs +++ b/crates/clickhouse-openapi-analyzer/src/config.rs @@ -132,6 +132,12 @@ const OPTIONAL_REQUEST_SCHEMAS: &[&str] = &[ // Postgres configuration is partial: zero-value defaults fail, and omission // selects the server default. "pgConfig", + // The Postgres private endpoint create body has one property, + // `supportedRegions`, and the operation does not require a body at all. + // The field needs cross-region private endpoints enabled for the + // organization, so an organization without the feature must be able to + // omit it; the description fallback would mark it required. + "PostgresPrivateEndpointPostRequest", ]; const OPTIONALITY_EXEMPTIONS: &[(&str, &str)] = &[ diff --git a/docs/development.md b/docs/development.md index d3b4a7b6f..8f9d85120 100644 --- a/docs/development.md +++ b/docs/development.md @@ -192,7 +192,7 @@ cargo test --test clickpipe_smoke_test -- --ignored --nocapture # creat The suites load AWS credentials through the default provider chain: environment variables, shared profiles, SSO, or `credential_process`. `aws login` sessions are not read directly. To use one, export its credentials first with `eval "$(aws configure export-credentials --format env)"`. -All require `CLICKHOUSE_CLOUD_API_KEY`, `CLICKHOUSE_CLOUD_API_SECRET`, `CLICKHOUSE_CLOUD_TEST_ORG_ID`, `CLICKHOUSE_CLOUD_TEST_PROVIDER`, and `CLICKHOUSE_CLOUD_TEST_REGION` in the environment, and are wired into the scheduled `Cloud Integration` GitHub Actions workflow. The ClickPipes E2E suites additionally need AWS credentials and an `eu-west-1` region quota; `clickpipe_smoke_test` reads a pre-provisioned service ID from `CLICKHOUSE_CLOUD_TEST_CLICKPIPE_SERVICE_ID`. +All require `CLICKHOUSE_CLOUD_API_KEY`, `CLICKHOUSE_CLOUD_API_SECRET`, `CLICKHOUSE_CLOUD_TEST_ORG_ID`, `CLICKHOUSE_CLOUD_TEST_PROVIDER`, and `CLICKHOUSE_CLOUD_TEST_REGION` in the environment, and are wired into the scheduled `Cloud Integration` GitHub Actions workflow. The ClickPipes E2E suites additionally need AWS credentials and an `eu-west-1` region quota; `clickpipe_smoke_test` reads a pre-provisioned service ID from `CLICKHOUSE_CLOUD_TEST_CLICKPIPE_SERVICE_ID`. The Postgres suite's private endpoint phase runs only on AWS with `CLICKHOUSE_CLOUD_POSTGRES_PRIVATE_ENDPOINT` set, because private endpoints need an organization with the feature enabled; creation adds 5 to 10 minutes. Two live tests drive the real `clickhousectl` binary, so they live in the CLI crate as `crates/clickhousectl/tests/live_*_test.rs`. Cargo builds the binary for them, and they share the library's harness in `crates/clickhouse-cloud-api/tests/common/` through `#[path]`. They need the same Cloud environment variables, are `#[ignore]`d, and assert JSON error codes, exit codes and Cloud API state rather than CLI wording. The published `clickhousectl` crate ships only `src/**` (plus `LICENSE` and the root README), because those `#[path]` includes point outside the crate. The CLI sources each test drives also select its live suite: `services/query.rs` and `services/query_key.rs` select `service`, and the `clickpipe create postgres` path (`clickpipes/create.rs`, `destination.rs`, `table_mapping.rs`, `sources/mod.rs`, `sources/postgres.rs`) selects `clickpipes`: