-
Notifications
You must be signed in to change notification settings - Fork 162
feat(admin-cli): erase site metadata for a BMC MAC (#3046) #4013
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| use clap::Parser; | ||
| use rpc::forge::EraseHostMetadataByBmcMacRequest; | ||
|
|
||
| /// Erase all NICo-owned site records for a server BMC MAC address. | ||
| #[derive(Parser, Debug)] | ||
| #[command(after_long_help = "\ | ||
| EXAMPLES: | ||
|
|
||
| Preview which records exist for a BMC MAC (nothing is deleted): | ||
| $ nico-admin-cli managed-host erase-metadata --bmc-mac 00:11:22:33:44:55 --dry-run | ||
|
|
||
| Erase all lingering records for a BMC MAC to prepare for re-ingestion: | ||
| $ nico-admin-cli managed-host erase-metadata --bmc-mac 00:11:22:33:44:55 --confirm | ||
|
|
||
| ")] | ||
| pub struct Args { | ||
| #[clap( | ||
| long, | ||
| required(true), | ||
| help = "Server BMC MAC address whose lingering site records should be erased" | ||
| )] | ||
| pub bmc_mac: String, | ||
|
|
||
| #[clap( | ||
| long, | ||
| action, | ||
| help = "Report the records that would be erased without deleting anything" | ||
| )] | ||
| pub dry_run: bool, | ||
|
|
||
| #[clap( | ||
| long, | ||
| action, | ||
| help = "Confirm you want to erase these records. Required for a real run (ignored with --dry-run)." | ||
| )] | ||
| pub confirm: bool, | ||
| } | ||
|
|
||
| impl From<&Args> for EraseHostMetadataByBmcMacRequest { | ||
| fn from(args: &Args) -> Self { | ||
| Self { | ||
| bmc_mac: args.bmc_mac.clone(), | ||
| dry_run: args.dry_run, | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| use ::rpc::forge::EraseHostMetadataByBmcMacRequest; | ||
|
|
||
| use super::args::Args; | ||
| use crate::errors::{CarbideCliError, CarbideCliResult}; | ||
| use crate::rpc::ApiClient; | ||
|
|
||
| pub async fn erase_metadata(api_client: &ApiClient, args: Args) -> CarbideCliResult<()> { | ||
| // Guard the destructive path: a real run must be explicitly confirmed. --dry-run | ||
| // is always safe, so it does not require --confirm. Return an error (non-zero | ||
| // exit) so scripts can distinguish a refused run from a completed one. | ||
| if !args.dry_run && !args.confirm { | ||
| return Err(CarbideCliError::GenericError(format!( | ||
| "Refusing to erase records for BMC MAC {} without confirmation. \ | ||
| Re-run with --dry-run to preview, or add --confirm to proceed.", | ||
| args.bmc_mac | ||
| ))); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| let req: EraseHostMetadataByBmcMacRequest = (&args).into(); | ||
| let response = api_client.0.erase_host_metadata_by_bmc_mac(req).await?; | ||
|
|
||
| if response.dry_run { | ||
| println!("DRY RUN -- no records were deleted. The following would be erased:"); | ||
| } else { | ||
| println!("Erased the following records for BMC MAC {}:", args.bmc_mac); | ||
| } | ||
| println!("{}", serde_json::to_string_pretty(&response)?); | ||
|
|
||
| Ok(()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| pub mod args; | ||
| pub mod cmd; | ||
|
|
||
| pub use args::Args; | ||
|
|
||
| use crate::cfg::run::Run; | ||
| use crate::cfg::runtime::RuntimeContext; | ||
| use crate::errors::CarbideCliResult; | ||
|
|
||
| impl Run for Args { | ||
| async fn run(self, ctx: &mut RuntimeContext) -> CarbideCliResult<()> { | ||
| cmd::erase_metadata(&ctx.api_client, self).await?; | ||
| Ok(()) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -346,3 +346,51 @@ fn desired_power_state_value_enum() { | |
| } | ||
| ); | ||
| } | ||
|
|
||
| // erase-metadata routes to the EraseMetadata variant. Each row yields | ||
| // (bmc_mac, dry_run, confirm); the required --bmc-mac is missing in the failing row. | ||
| #[test] | ||
| fn parse_erase_metadata_routes_to_erase_metadata() { | ||
| scenarios!( | ||
| run = |argv| { | ||
| Cmd::try_parse_from(argv.iter().copied()) | ||
| .map(|cmd| match cmd { | ||
| Cmd::EraseMetadata(args) => (args.bmc_mac, args.dry_run, args.confirm), | ||
| _ => panic!("expected EraseMetadata variant"), | ||
| }) | ||
| .map_err(drop) | ||
| }; | ||
| "with bmc mac" { | ||
| &[ | ||
| "managed-host", | ||
| "erase-metadata", | ||
| "--bmc-mac", | ||
| "aa:bb:cc:dd:ee:ff", | ||
| ][..] => Yields(("aa:bb:cc:dd:ee:ff".to_string(), false, false)), | ||
| } | ||
|
|
||
| "with bmc mac and dry-run" { | ||
| &[ | ||
| "managed-host", | ||
| "erase-metadata", | ||
| "--bmc-mac", | ||
| "aa:bb:cc:dd:ee:ff", | ||
| "--dry-run", | ||
| ][..] => Yields(("aa:bb:cc:dd:ee:ff".to_string(), true, false)), | ||
| } | ||
|
|
||
| "with bmc mac and confirm" { | ||
| &[ | ||
| "managed-host", | ||
| "erase-metadata", | ||
| "--bmc-mac", | ||
| "aa:bb:cc:dd:ee:ff", | ||
| "--confirm", | ||
| ][..] => Yields(("aa:bb:cc:dd:ee:ff".to_string(), false, true)), | ||
| } | ||
|
|
||
| "missing required bmc mac" { | ||
| &["managed-host", "erase-metadata"][..] => Fails, | ||
| } | ||
| ); | ||
| } | ||
|
Comment on lines
+349
to
+396
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Include regenerated admin-cli reference documentation before merge. This adds the As per path instructions, 🤖 Prompt for AI AgentsSource: Path instructions |
||
Uh oh!
There was an error while loading. Please reload this page.