Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions crates/clickhousectl/src/cloud/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -178,6 +181,7 @@ impl CloudErrorCode {
Self::ClickPipeStartNoEffect,
Self::PackageManaged,
Self::VerificationFailed,
Self::DeletionProtected,
Self::InvalidVersion,
Self::VersionNotInstalled,
Self::VersionSelectionRequired,
Expand Down Expand Up @@ -332,6 +336,15 @@ impl NextStep {

/// `clickhousectl cloud <args> --org-id <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<bool>) -> 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)
Expand Down Expand Up @@ -864,6 +877,7 @@ mod tests {
"clickpipe_start_no_effect",
"package_managed",
"verification_failed",
"deletion_protected",
"invalid_version",
"version_not_installed",
"version_selection_required",
Expand Down
10 changes: 1 addition & 9 deletions crates/clickhousectl/src/cloud/postgres/lifecycle.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -474,14 +474,6 @@ pub async fn postgres_list(
deletion_protection: String,
}

fn yes_no(value: Option<bool>) -> String {
match value {
Some(true) => "yes".to_string(),
Some(false) => "no".to_string(),
None => ABSENT.to_string(),
}
}

let rows: Vec<Row> = filtered
.into_iter()
.map(|i| Row {
Expand Down
24 changes: 23 additions & 1 deletion crates/clickhousectl/src/cloud/services/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ CONTEXT FOR AGENTS:
#[arg(long)]
enable_core_dumps: Option<bool>,

/// 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.
Expand All @@ -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 <id> --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 <id>`, then rerun.")]
Expand Down Expand Up @@ -340,6 +346,12 @@ CONTEXT FOR AGENTS:
/// Enable or disable service core dump collection
#[arg(long)]
enable_core_dumps: Option<bool>,

/// Turn deletion protection on or off
///
/// Not for ClickStack services: change theirs in the console.
#[arg(long)]
deletion_protection: Option<bool>,
},

/// Update service scaling and idle behaviour
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -1276,13 +1289,15 @@ mod tests {
disable_endpoint,
private_preview_terms_checked,
enable_core_dumps,
deletion_protection,
dotenv,
} = command
else {
panic!("expected service create");
};

assert!(dotenv.is_none());
assert!(!deletion_protection);
assert_eq!(name, "default-service");
assert_eq!(provider, "aws");
assert_eq!(region, "us-east-1");
Expand Down Expand Up @@ -1368,6 +1383,7 @@ mod tests {
"--private-preview-terms-checked",
"--enable-core-dumps",
"false",
"--deletion-protection",
"--org-id",
"org-1",
]);
Expand Down Expand Up @@ -1397,6 +1413,7 @@ mod tests {
disable_endpoint,
private_preview_terms_checked,
enable_core_dumps,
deletion_protection,
..
} = command
else {
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -2386,6 +2404,8 @@ mod tests {
"team=analytics",
"--enable-core-dumps",
"false",
"--deletion-protection",
"false",
"--org-id",
"org-1",
]);
Expand All @@ -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,
Expand Down
36 changes: 35 additions & 1 deletion crates/clickhousectl/src/cloud/services/client.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Loading
Loading