diff --git a/crates/clickhousectl/src/cloud/output.rs b/crates/clickhousectl/src/cloud/output.rs index dde0a456..6fa5622d 100644 --- a/crates/clickhousectl/src/cloud/output.rs +++ b/crates/clickhousectl/src/cloud/output.rs @@ -102,6 +102,9 @@ pub enum CloudErrorCode { /// `update` downloaded an archive that the release's `SHA256SUMS` does /// not list or whose digest differs, and replaced nothing (#1307). VerificationFailed, + /// `cloud service delete` found the service's deletion protection on, so + /// nothing was deleted or stopped; `command` turns protection off. + DeletionProtected, /// `cloud service client` was pointed at a ClickHouse build whose version /// is malformed; the same code `local client` reports (#1443). InvalidVersion, @@ -178,6 +181,7 @@ impl CloudErrorCode { Self::ClickPipeStartNoEffect, Self::PackageManaged, Self::VerificationFailed, + Self::DeletionProtected, Self::InvalidVersion, Self::VersionNotInstalled, Self::VersionSelectionRequired, @@ -332,6 +336,15 @@ impl NextStep { /// `clickhousectl cloud --org-id `, each argument shell-quoted /// when it needs to be. +/// `yes`/`no` for an optional flag in a list table; [`ABSENT`] when missing. +pub(crate) fn yes_no(value: Option) -> String { + match value { + Some(true) => "yes".to_string(), + Some(false) => "no".to_string(), + None => ABSENT.to_string(), + } +} + pub(crate) fn cloud_command(args: &[&str], org_id: &str) -> String { let quote = |arg: &str| { shlex::try_quote(arg) @@ -864,6 +877,7 @@ mod tests { "clickpipe_start_no_effect", "package_managed", "verification_failed", + "deletion_protected", "invalid_version", "version_not_installed", "version_selection_required", diff --git a/crates/clickhousectl/src/cloud/postgres/lifecycle.rs b/crates/clickhousectl/src/cloud/postgres/lifecycle.rs index cf164091..111d5915 100644 --- a/crates/clickhousectl/src/cloud/postgres/lifecycle.rs +++ b/crates/clickhousectl/src/cloud/postgres/lifecycle.rs @@ -1,7 +1,7 @@ use super::config::{load_json_file, load_pg_config_file}; use crate::cloud::client::{CloudClient, CloudError, Result as CloudResult}; use crate::cloud::output::{ - ABSENT, NextStep, cloud_command, eprint_background_change, json_with_next, or_absent, + ABSENT, NextStep, cloud_command, eprint_background_change, json_with_next, or_absent, yes_no, }; use crate::cloud::shared::{parse_ip_or_cidr, parse_serde_enum, parse_tags, resolve_org_id}; use clickhouse_cloud_api::models::{ @@ -474,14 +474,6 @@ pub async fn postgres_list( deletion_protection: String, } - fn yes_no(value: Option) -> String { - match value { - Some(true) => "yes".to_string(), - Some(false) => "no".to_string(), - None => ABSENT.to_string(), - } - } - let rows: Vec = filtered .into_iter() .map(|i| Row { diff --git a/crates/clickhousectl/src/cloud/services/cli.rs b/crates/clickhousectl/src/cloud/services/cli.rs index fe7f56a3..aeee0d48 100644 --- a/crates/clickhousectl/src/cloud/services/cli.rs +++ b/crates/clickhousectl/src/cloud/services/cli.rs @@ -211,6 +211,10 @@ CONTEXT FOR AGENTS: #[arg(long)] enable_core_dumps: Option, + /// Turn on deletion protection: delete fails until it is turned off + #[arg(long)] + deletion_protection: bool, + /// Write connection settings and the password to FILE (.env if no FILE) /// /// An existing file is edited in place: only the CLICKHOUSE_* keys written are replaced. @@ -227,6 +231,8 @@ CONTEXT FOR AGENTS: #[command(after_help = "\ CONTEXT FOR AGENTS: Irreversible. A running service cannot be deleted: stop it first, or pass --force. + A service with deletion protection on is refused, and --force does not stop it: turn it off + first (`cloud service update --deletion-protection false`). --force stops the service and waits up to 10 minutes for the stop, with progress lines on stderr. If the wait times out (code `timeout`) or the service leaves the stop path, nothing is deleted: check `cloud service get `, then rerun.")] @@ -340,6 +346,12 @@ CONTEXT FOR AGENTS: /// Enable or disable service core dump collection #[arg(long)] enable_core_dumps: Option, + + /// Turn deletion protection on or off + /// + /// Not for ClickStack services: change theirs in the console. + #[arg(long)] + deletion_protection: Option, }, /// Update service scaling and idle behaviour @@ -496,7 +508,8 @@ CONTEXT FOR AGENTS: CONTEXT FOR AGENTS: Enterprise tier only; a service has at most 5 providers. Token holders sign in as the `default` user with the roles listed in the token's roles claim. - Create and update fetch the JWKS URL first: it must be public https:// with an RS256 key." + Create and update fetch the JWKS URL first: it must be public https:// with an RS256 key, + or on ClickHouse 26.8+ an ES256/ES384/ES512 (EC) key." )] JwtProvider { #[command(subcommand)] @@ -1276,6 +1289,7 @@ mod tests { disable_endpoint, private_preview_terms_checked, enable_core_dumps, + deletion_protection, dotenv, } = command else { @@ -1283,6 +1297,7 @@ mod tests { }; assert!(dotenv.is_none()); + assert!(!deletion_protection); assert_eq!(name, "default-service"); assert_eq!(provider, "aws"); assert_eq!(region, "us-east-1"); @@ -1368,6 +1383,7 @@ mod tests { "--private-preview-terms-checked", "--enable-core-dumps", "false", + "--deletion-protection", "--org-id", "org-1", ]); @@ -1397,6 +1413,7 @@ mod tests { disable_endpoint, private_preview_terms_checked, enable_core_dumps, + deletion_protection, .. } = command else { @@ -1430,6 +1447,7 @@ mod tests { assert_eq!(disable_endpoint, vec!["mysql"]); assert!(private_preview_terms_checked); assert_eq!(enable_core_dumps, Some(false)); + assert!(deletion_protection); assert!(custom_region.is_none()); } @@ -2386,6 +2404,8 @@ mod tests { "team=analytics", "--enable-core-dumps", "false", + "--deletion-protection", + "false", "--org-id", "org-1", ]); @@ -2403,12 +2423,14 @@ mod tests { add_tag, remove_tag, enable_core_dumps, + deletion_protection, } = command else { panic!("expected service update"); }; assert_eq!(service_id.id.as_deref(), Some("svc-1")); + assert_eq!(deletion_protection, Some(false)); assert_eq!(name.as_deref(), Some("renamed")); assert_eq!( add_ip_allow, diff --git a/crates/clickhousectl/src/cloud/services/client.rs b/crates/clickhousectl/src/cloud/services/client.rs index bace1405..c39a9164 100644 --- a/crates/clickhousectl/src/cloud/services/client.rs +++ b/crates/clickhousectl/src/cloud/services/client.rs @@ -1,5 +1,6 @@ use super::lifecycle::{ - build_service_state_patch_request, service_delete_error, service_delete_rejected, + build_service_state_patch_request, service_delete_error, service_delete_protected, + service_delete_rejected, }; use super::query_key::{KeyPropagation, upsert_until_key_propagates}; use crate::cloud::client::{CloudClient, CloudError, ResourceKind, ResourceLookup}; @@ -499,6 +500,11 @@ impl CloudClient { .service_delete_bad_request(error, org_id, service_id) .await); } + Err(error @ clickhouse_cloud_api::Error::Api { status: 409, .. }) => { + return Err(self + .service_delete_conflict(error, force, org_id, service_id) + .await); + } Err(error) => return Err(service_delete_error(self, error, force, service_id, org_id)), }; Ok(DeleteResponse { @@ -507,6 +513,34 @@ impl CloudClient { }) } + /// A 409 from a service DELETE: either the service is not stopped or its + /// deletion protection is on. A read of the service tells which; when the + /// read fails or shows no protection, the conflict is reported as before. + async fn service_delete_conflict( + &self, + error: clickhouse_cloud_api::Error, + force: bool, + org_id: &str, + service_id: &str, + ) -> CloudError { + let protected = matches!( + self.get_service_if_exists(org_id, service_id).await, + Ok(Some(Service { + deletion_protection: Some(true), + .. + })) + ); + if protected { + service_delete_protected( + Some(self.convert_error_for_organization(error, org_id)), + service_id, + org_id, + ) + } else { + service_delete_error(self, error, force, service_id, org_id) + } + } + /// A 400 from a service DELETE. It names a missing service only once a /// read of the service agrees (#666); for a service that exists, such as /// one still provisioning, it names the state instead (#1226). When the diff --git a/crates/clickhousectl/src/cloud/services/lifecycle.rs b/crates/clickhousectl/src/cloud/services/lifecycle.rs index 6aeee8f2..5b4bc39f 100644 --- a/crates/clickhousectl/src/cloud/services/lifecycle.rs +++ b/crates/clickhousectl/src/cloud/services/lifecycle.rs @@ -3,7 +3,8 @@ use crate::cloud::client::{ read_flag_file, }; use crate::cloud::output::{ - ABSENT, CloudErrorCode, NextStep, json_with_next, or_absent, print_human, + ABSENT, CloudErrorCode, CloudErrorDetail, NextStep, cloud_command, json_with_next, or_absent, + print_human, yes_no, }; use crate::cloud::shared::{Outcome, POLL_INTERVAL, PollProgress, Wait, wait_for}; use crate::cloud::shared::{parse_ip_access_entries, parse_serde_enum, parse_tags, resolve_org_id}; @@ -149,6 +150,8 @@ pub(super) async fn service_list( region: String, #[tabled(rename = "Endpoint")] endpoint: String, + #[tabled(rename = "Deletion protection")] + deletion_protection: String, } let rows: Vec = services .into_iter() @@ -159,6 +162,7 @@ pub(super) async fn service_list( provider: or_absent(service.provider.as_ref()), region: or_absent(service.region.as_ref()), endpoint: first_endpoint(service.endpoints.as_deref()), + deletion_protection: yes_no(service.deletion_protection), }) .collect(); println!("{}", Table::new(rows).with(Style::markdown())); @@ -366,6 +370,7 @@ pub(super) struct CreateServiceOptions { pub(super) disable_endpoints: Vec, pub(super) private_preview_terms_checked: bool, pub(super) enable_core_dumps: Option, + pub(super) deletion_protection: bool, } #[derive(Default)] @@ -382,6 +387,7 @@ pub(super) struct ServiceUpdateOptions { pub(super) add_tags: Vec, pub(super) remove_tags: Vec, pub(super) enable_core_dumps: Option, + pub(super) deletion_protection: Option, } #[derive(Default)] @@ -544,6 +550,7 @@ fn build_create_service_request(options: &CreateServiceOptions) -> CloudResult CloudError { CloudError { message, ..error } } +/// A delete refused because the service has deletion protection on, known +/// from a read of the service. `error` is the API's 409 when the DELETE was +/// sent; `None` when `--force` found protection before stopping anything. +pub(super) fn service_delete_protected( + error: Option, + service_id: &str, + org_id: &str, +) -> CloudError { + let command = cloud_command( + &[ + "service", + "update", + service_id, + "--deletion-protection", + "false", + ], + org_id, + ); + let message = format!( + "service {service_id} has deletion protection on, so it was not deleted{}. Turn it off \ + with `{command}`, then rerun the delete.", + if error.is_some() { "" } else { " or stopped" } + ); + let detail = CloudErrorDetail { + command: Some(command), + ..CloudErrorDetail::new(CloudErrorCode::DeletionProtected, message.clone()) + }; + match error { + Some(error) => CloudError { message, ..error }.with_details(detail), + None => CloudError::new(message).with_details(detail), + } +} + /// A delete the API rejected for a service that a read shows still exists, /// such as one still provisioning (#1226). The service's state comes from /// that read, never from the response text. @@ -1072,6 +1111,14 @@ pub(super) async fn service_delete( if force { let service = client.get_service_if_exists(&org_id, service_id).await?; + // A protected service would only be refused after the stop, leaving + // it stopped for nothing. + if service + .as_ref() + .is_some_and(|service| service.deletion_protection == Some(true)) + { + return Err(service_delete_protected(None, service_id, &org_id)); + } let state = service.as_ref().and_then(|service| service.state.as_ref()); if matches!( state, @@ -1230,6 +1277,12 @@ pub(super) async fn service_update( println!("Service {} updated", or_absent(service.name.as_deref())); println!(" ID: {}", or_absent(service.id)); println!(" State: {}", or_absent(service.state.as_ref())); + if options.deletion_protection.is_some() { + println!( + " Deletion protection: {}", + yes_no(service.deletion_protection) + ); + } } Ok(()) } @@ -1666,6 +1719,48 @@ mod tests { assert_eq!(detail.command.as_deref(), Some("cloud service list")); } + #[test] + fn service_delete_protected_names_the_update_command() { + let check = |error: &CloudError| { + let detail = error.details.as_deref().expect("structured details"); + assert_eq!(detail.code, CloudErrorCode::DeletionProtected); + assert_eq!(detail.message, error.message); + let command = detail.command.as_deref().expect("command"); + let args = shlex::split(command).unwrap(); + assert_eq!( + &args[..8], + [ + "clickhousectl", + "cloud", + "service", + "update", + "svc-1", + "--deletion-protection", + "false", + "--org-id" + ] + ); + assert_eq!(args[8], "org-1"); + assert!(error.message.contains(command), "{error}"); + }; + + let refused_before_stop = service_delete_protected(None, "svc-1", "org-1"); + check(&refused_before_stop); + assert_eq!( + refused_before_stop.kind, + crate::cloud::client::CloudErrorKind::Generic + ); + + let api_conflict = CloudError::new("opaque conflict response") + .with_failure(ApiFailure::with_status(FailureKind::Http4xx, 409)); + let rejected = service_delete_protected(Some(api_conflict), "svc-1", "org-1"); + check(&rejected); + assert_eq!( + rejected.failure, + Some(ApiFailure::with_status(FailureKind::Http4xx, 409)) + ); + } + #[test] fn generation_is_requested_unless_a_password_hash_is_sent() { let request = |new_password_hash: Option<&str>, new_double_sha1_hash: Option<&str>| { @@ -1756,6 +1851,7 @@ mod tests { assert!(request.private_preview_terms_checked.is_none()); assert!(request.endpoints.is_none()); assert!(request.enable_core_dumps.is_none()); + assert!(request.deletion_protection.is_none()); assert!(request.byoc_id.is_none()); } @@ -1788,6 +1884,7 @@ mod tests { disable_endpoints: vec!["mysql".to_string()], private_preview_terms_checked: true, enable_core_dumps: Some(true), + deletion_protection: true, ..minimal_create_options() }; let request = build_create_service_request(&options).unwrap(); @@ -1844,6 +1941,7 @@ mod tests { assert!(!endpoints[1].enabled); assert_eq!(request.private_preview_terms_checked, Some(true)); assert_eq!(request.enable_core_dumps, Some(true)); + assert_eq!(request.deletion_protection, Some(true)); assert!(request.byoc_id.is_none()); } @@ -2045,6 +2143,7 @@ mod tests { add_tags: vec!["env=staging".to_string()], remove_tags: vec!["old=tag".to_string()], enable_core_dumps: Some(false), + deletion_protection: Some(true), }; let request = build_update_service_request(&options).unwrap(); @@ -2081,7 +2180,20 @@ mod tests { assert_eq!(tags.remove[0].key, "old"); assert_eq!(tags.remove[0].value.as_deref(), Some("tag")); assert_eq!(request.enable_core_dumps, Some(false)); - assert!(request.deletion_protection.is_none()); + assert_eq!(request.deletion_protection, Some(true)); + } + + #[test] + fn build_update_service_request_sends_deletion_protection_off() { + let request = build_update_service_request(&ServiceUpdateOptions { + deletion_protection: Some(false), + ..Default::default() + }) + .unwrap(); + assert_eq!( + serde_json::to_value(&request).unwrap(), + serde_json::json!({"deletionProtection": false}) + ); } #[test] diff --git a/crates/clickhousectl/src/cloud/services/mod.rs b/crates/clickhousectl/src/cloud/services/mod.rs index e52e00de..3ae9343c 100644 --- a/crates/clickhousectl/src/cloud/services/mod.rs +++ b/crates/clickhousectl/src/cloud/services/mod.rs @@ -160,19 +160,28 @@ pub(super) const PERMISSIONS: &[Permission] = &[ .when(&[Conditional::flag("name", &[&op::INSTANCE_GET_LIST])]), Permission::api("service prometheus", &[&op::INSTANCE_PROMETHEUS_GET]) .when(&[Conditional::flag("name", &[&op::INSTANCE_GET_LIST])]), - Permission::api("service create", &[&op::INSTANCE_CREATE]).when(&[Conditional::new( - "Dynamic BYOC profile", - &[&op::SERVICE_PROFILES_LIST], - )]), + Permission::api("service create", &[&op::INSTANCE_CREATE]) + .when(&[Conditional::new( + "Dynamic BYOC profile", + &[&op::SERVICE_PROFILES_LIST], + )]) + .authorization( + "--deletion-protection also needs control-plane:service:delete on every service in \ + the organization.", + ), Permission::api("service delete", &[&op::INSTANCE_DELETE]).when(&[ Conditional::flag("name", &[&op::INSTANCE_GET_LIST]), Conditional::flag("force", &[&op::INSTANCE_GET, &op::INSTANCE_STATE_UPDATE]), Conditional::new("Diagnosing a rejected delete", &[&op::INSTANCE_GET]), ]), - Permission::api("service update", &[&op::INSTANCE_UPDATE]).when(&[ - Conditional::flag("name", &[&op::INSTANCE_GET_LIST]), - Conditional::new("Removing IPs/endpoints/tags", &[&op::INSTANCE_GET]), - ]), + Permission::api("service update", &[&op::INSTANCE_UPDATE]) + .when(&[ + Conditional::flag("name", &[&op::INSTANCE_GET_LIST]), + Conditional::new("Removing IPs/endpoints/tags", &[&op::INSTANCE_GET]), + ]) + .authorization( + "Changing --deletion-protection also needs control-plane:service:delete on the service.", + ), Permission::api("service query", &[]) .when(&[ Conditional::new("service ID", &[&op::INSTANCE_GET]), @@ -363,6 +372,7 @@ pub async fn run(client: &CloudClient, command: ServiceCommands, json: bool) -> disable_endpoint, private_preview_terms_checked, enable_core_dumps, + deletion_protection, dotenv, } => { let options = CreateServiceOptions { @@ -396,6 +406,7 @@ pub async fn run(client: &CloudClient, command: ServiceCommands, json: bool) -> disable_endpoints: disable_endpoint, private_preview_terms_checked, enable_core_dumps, + deletion_protection, }; service_create(client, options, dotenv.as_deref(), json).await } @@ -446,6 +457,7 @@ pub async fn run(client: &CloudClient, command: ServiceCommands, json: bool) -> add_tag, remove_tag, enable_core_dumps, + deletion_protection, } => { let options = ServiceUpdateOptions { name, @@ -460,6 +472,7 @@ pub async fn run(client: &CloudClient, command: ServiceCommands, json: bool) -> add_tags: add_tag, remove_tags: remove_tag, enable_core_dumps, + deletion_protection, }; service_update( client, diff --git a/crates/clickhousectl/tests/cloud/services.rs b/crates/clickhousectl/tests/cloud/services.rs index 709e30f4..b78e8198 100644 --- a/crates/clickhousectl/tests/cloud/services.rs +++ b/crates/clickhousectl/tests/cloud/services.rs @@ -474,6 +474,106 @@ async fn service_delete_conflict_suggests_force_without_inspecting_prose() { assert!(message.ends_with("opaque conflict response"), "{message}"); } +// ── Deletion protection (issue #1470) ───────────────────────────────────── + +async fn mount_protected_service_get(mock: &MockServer, state: &str) { + Mock::given(method("GET")) + .and(path(format!( + "/v1/organizations/org-1/services/{TEST_SERVICE_ID}" + ))) + .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({ + "result": { + "id": TEST_SERVICE_ID, + "name": "demo", + "state": state, + "deletionProtection": true, + }, + "status": 200, + "requestId": "stub-service-get", + }))) + .expect(1) + .mount(mock) + .await; +} + +/// The error names the update that turns protection off, re-parsed as argv. +fn assert_deletion_protected(output: &std::process::Output) { + assert_eq!(output.status.code(), Some(1)); + assert_eq!(String::from_utf8_lossy(&output.stdout), ""); + let error = cloud_runtime_error(output); + assert_eq!(error["code"], "deletion_protected"); + let command = shlex::split(error["command"].as_str().unwrap()).unwrap(); + assert_eq!( + command, + [ + "clickhousectl", + "cloud", + "service", + "update", + TEST_SERVICE_ID, + "--deletion-protection", + "false", + "--org-id", + "org-1", + ] + ); +} + +/// `--force` reads the service first; a protected one is refused before the +/// stop, so it is neither stopped nor sent a DELETE. +#[tokio::test] +async fn forced_service_delete_refuses_a_protected_service_without_stopping_it() { + let mock = MockServer::start().await; + mount_protected_service_get(&mock, "running").await; + + let dir = tempfile::tempdir().unwrap(); + let output = invoke_service_delete(&mock, dir.path(), true); + assert_deletion_protected(&output); + assert_eq!( + received_request_shape(&mock).await, + vec![( + "GET".to_string(), + format!("/v1/organizations/org-1/services/{TEST_SERVICE_ID}") + )] + ); +} + +/// Without `--force` the DELETE is sent; its 409 is diagnosed by a read, so a +/// protected service is not reported as one that needs stopping. +#[tokio::test] +async fn service_delete_conflict_on_a_protected_service_reports_deletion_protection() { + let mock = MockServer::start().await; + Mock::given(method("DELETE")) + .and(path(format!( + "/v1/organizations/org-1/services/{TEST_SERVICE_ID}" + ))) + .respond_with(ResponseTemplate::new(409).set_body_json(serde_json::json!({ + "status": 409, + "error": "opaque conflict response" + }))) + .expect(1) + .mount(&mock) + .await; + mount_protected_service_get(&mock, "stopped").await; + + let dir = tempfile::tempdir().unwrap(); + let output = invoke_service_delete(&mock, dir.path(), false); + assert_deletion_protected(&output); + assert_eq!( + received_request_shape(&mock).await, + vec![ + ( + "DELETE".to_string(), + format!("/v1/organizations/org-1/services/{TEST_SERVICE_ID}") + ), + ( + "GET".to_string(), + format!("/v1/organizations/org-1/services/{TEST_SERVICE_ID}") + ), + ] + ); +} + const DELETE_TEST_API_KEY_ID: &str = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"; fn write_service_query_key(root: &Path, organization_id: Option<&str>, api_key_id: Option<&str>) { diff --git a/docs/guides/cloud-service.md b/docs/guides/cloud-service.md index 6875716e..c6aa4003 100644 --- a/docs/guides/cloud-service.md +++ b/docs/guides/cloud-service.md @@ -99,7 +99,7 @@ Service tag and IP updates have explicit add/remove flags. Add and remove tags i ## Let users sign in with a JWT -JWT authentication providers are beta and need the Enterprise tier. A service accepts tokens from up to five providers. A token holder signs in as the `default` user and receives the ClickHouse roles listed in the token's roles claim. Cloud fetches the JWKS URL when you create or update a provider, so it must be a public `https://` URL that serves at least one RS256 key. +JWT authentication providers are beta and need the Enterprise tier. A service accepts tokens from up to five providers. A token holder signs in as the `default` user and receives the ClickHouse roles listed in the token's roles claim. Cloud fetches the JWKS URL when you create or update a provider, so it must be a public `https://` URL that serves at least one RS256 (RSA) key, or on ClickHouse 26.8 and later an ES256, ES384 or ES512 (EC) key. ```bash clickhousectl cloud service jwt-provider create corp-sso \ @@ -170,3 +170,21 @@ clickhousectl cloud service delete ``` `delete --force` stops a running service and polls until the stop completes before deleting it, which can take minutes. It waits up to 10 minutes; if the stop times out or the service enters a state such as `failed` or `softdeleted`, nothing is deleted, so check `service get` and rerun. It is not a general conflict override; inspect other conflicts before retrying. Deletion also cleans up stored per-service Query API keys when exact ownership metadata is available. A cleanup failure can occur after the service is gone; retain the local credentials file and inspect the reported key IDs instead of assuming nothing happened. + +### Protect a service from deletion + +Deletion protection refuses every delete of the service until it is turned off. Turn it on when you create the service or later: + +```bash +clickhousectl cloud service create --name prod --deletion-protection +clickhousectl cloud service update --deletion-protection true +``` + +Turning it on at create needs `control-plane:service:delete` granted on every service in the organization, not service by service, in addition to `control-plane:organization:create-service`. Changing it on an existing service, in either direction, needs `control-plane:service:delete` on that service. `service get` and `service list` show the setting. ClickStack services can't be updated through the API; change theirs in the console. + +`service delete` refuses a protected service with error code `deletion_protected`, and `--force` refuses it before stopping anything. Turn protection off first: + +```bash +clickhousectl cloud service update --deletion-protection false +clickhousectl cloud service delete +``` diff --git a/docs/reference/automation.md b/docs/reference/automation.md index 2a750250..1a9ab326 100644 --- a/docs/reference/automation.md +++ b/docs/reference/automation.md @@ -72,7 +72,7 @@ Local and Cloud runtime errors in JSON mode write an envelope to stderr: Error rendering adds nothing to stdout, but a streaming query or a command that already committed a change may have emitted a result before failing. Do not accept partial output as success or assume nonzero exit means no side effects. -Cloud generic codes are `auth_required`, `cancelled`, `http_4xx`, `http_5xx`, `rate_limited`, `transport`, `timeout`, `sql_error`, `service_stopped`, `io`, and `other`, with specific recovery codes taking precedence. `resource_not_found` means the API answered 404 for an identifier, or a `--name`/`--email` matched nothing; `command` lists what does exist in that scope. For a resource inside a service, including service-wide settings such as the scaling schedule or backup configuration and a Postgres service's logs, metrics and backups, the CLI reads the service to decide which one is missing; when the service exists, the API's own error is reported. A malformed identifier keeps the API's own diagnostic. ClickStack subresources can still report `http_4xx`. Treat a not-found from a delete as "already gone" only when `command` lists the kind you meant to delete, not its parent. A delete the API rejects for a service that still exists, such as one still provisioning, reports `http_4xx`, never `resource_not_found`; for a ClickHouse service the message names its current state. +Cloud generic codes are `auth_required`, `cancelled`, `http_4xx`, `http_5xx`, `rate_limited`, `transport`, `timeout`, `sql_error`, `service_stopped`, `io`, and `other`, with specific recovery codes taking precedence. `resource_not_found` means the API answered 404 for an identifier, or a `--name`/`--email` matched nothing; `command` lists what does exist in that scope. For a resource inside a service, including service-wide settings such as the scaling schedule or backup configuration and a Postgres service's logs, metrics and backups, the CLI reads the service to decide which one is missing; when the service exists, the API's own error is reported. A malformed identifier keeps the API's own diagnostic. ClickStack subresources can still report `http_4xx`. Treat a not-found from a delete as "already gone" only when `command` lists the kind you meant to delete, not its parent. A delete the API rejects for a service that still exists, such as one still provisioning, reports `http_4xx`, never `resource_not_found`; for a ClickHouse service the message names its current state. `cloud service delete` reports `deletion_protected` when the service has deletion protection on: nothing was deleted, `--force` did not stop it, and `command` is the `service update` that turns protection off. `cloud udf attach` reports the service's state when it cannot attach: `service_idle` (rerun with `--wake`, or wake it; `command` is the wake command), `service_stopped` (start it first; `command` is the start command), or `service_not_running` (starting, or another non-running state: wait for `running`, or start it). A `--wake` that finds the service stopped or failed while waiting reports `service_stopped` or `service_not_running`.