Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

omdb nexus blueprints target set --diff #6435

Merged
merged 5 commits into from
Aug 26, 2024
Merged
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
48 changes: 42 additions & 6 deletions dev-tools/omdb/src/bin/omdb/nexus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::str::FromStr;
use tabled::Tabled;
use tokio::sync::OnceCell;
use uuid::Uuid;

/// Arguments to the "omdb nexus" subcommand
Expand Down Expand Up @@ -244,6 +245,10 @@ struct BlueprintTargetSetArgs {
blueprint_id: Uuid,
/// whether this blueprint should be enabled
enabled: BlueprintTargetSetEnabled,
/// if specified, diff against the current target and wait for confirmation
/// before proceeding
#[clap(long)]
diff: bool,
}

#[derive(Debug, Clone, Copy, ValueEnum)]
Expand Down Expand Up @@ -1722,6 +1727,38 @@ async fn cmd_nexus_blueprints_target_set(
args: &BlueprintTargetSetArgs,
_destruction_token: DestructiveOperationToken,
) -> Result<(), anyhow::Error> {
// Helper to only fetch the current target once. We may need it immediately
// if `args.diff` is true, or later if `args.enabled` is "inherit" (or
// both).
let current_target = OnceCell::new();
let get_current_target = || async {
current_target
.get_or_try_init(|| client.blueprint_target_view())
.await
.context("failed to fetch current target blueprint")
};

if args.diff {
let current_target = get_current_target().await?;
let blueprint1 = client
.blueprint_view(&current_target.target_id)
.await
.context("failed to fetch target blueprint")?
.into_inner();
let blueprint2 =
client.blueprint_view(&args.blueprint_id).await.with_context(
|| format!("fetching blueprint {}", args.blueprint_id),
)?;
let diff = blueprint2.diff_since_blueprint(&blueprint1);
println!("{}", diff.display());
println!(
"\nDo you want to make {} the target blueprint?",
args.blueprint_id
);
let mut prompt = ConfirmationPrompt::new();
prompt.read_and_validate("y/N", "y")?;
}

let enabled = match args.enabled {
BlueprintTargetSetEnabled::Enabled => true,
BlueprintTargetSetEnabled::Disabled => false,
Expand All @@ -1734,12 +1771,11 @@ async fn cmd_nexus_blueprints_target_set(
// operator. (In the case of the current target blueprint being changed
// entirely, that will result in a failure to set the current target
// below, because its parent will no longer be the current target.)
BlueprintTargetSetEnabled::Inherit => client
.blueprint_target_view()
.await
.map(|current| current.into_inner().enabled)
.context("failed to fetch current target blueprint")?,
BlueprintTargetSetEnabled::Inherit => {
get_current_target().await?.enabled
}
};

client
.blueprint_target_set(&nexus_client::types::BlueprintTargetSet {
target_id: args.blueprint_id,
Expand Down Expand Up @@ -1966,7 +2002,7 @@ impl ConfirmationPrompt {
{
Ok(input)
} else {
bail!("expungement aborted")
bail!("operation aborted")
}
}

Expand Down
Loading