|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//! `omdb db user-data-export` subcommands |
| 6 | +
|
| 7 | +use crate::Omdb; |
| 8 | +use crate::check_allow_destructive::DestructiveOperationToken; |
| 9 | +use crate::helpers::display_debug; |
| 10 | +use crate::helpers::display_option_blank; |
| 11 | +use async_bb8_diesel::AsyncRunQueryDsl; |
| 12 | +use clap::Args; |
| 13 | +use clap::Subcommand; |
| 14 | +use diesel::prelude::*; |
| 15 | +use nexus_db_model::UserDataExportRecord; |
| 16 | +use nexus_db_model::UserDataExportResourceType; |
| 17 | +use nexus_db_model::UserDataExportState; |
| 18 | +use nexus_db_queries::context::OpContext; |
| 19 | +use nexus_db_queries::db::DataStore; |
| 20 | +use omicron_uuid_kinds::UserDataExportUuid; |
| 21 | +use omicron_uuid_kinds::VolumeUuid; |
| 22 | +use std::net::SocketAddrV6; |
| 23 | +use tabled::Tabled; |
| 24 | +use uuid::Uuid; |
| 25 | + |
| 26 | +/// `omdb db user-data-export` subcommand |
| 27 | +#[derive(Debug, Args, Clone)] |
| 28 | +pub struct UserDataExportArgs { |
| 29 | + #[command(subcommand)] |
| 30 | + command: UserDataExportCommands, |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Debug, Subcommand, Clone)] |
| 34 | +enum UserDataExportCommands { |
| 35 | + /// Check if a read-only resource has a user data export object |
| 36 | + Query(UserDataExportQueryArgs), |
| 37 | + |
| 38 | + /// Manually request that a user data export object be deleted. |
| 39 | + Delete(UserDataExportDeleteArgs), |
| 40 | + |
| 41 | + /// Show the user data export related changes required by Nexus |
| 42 | + ShowChangeset, |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(Clone, Debug, Args)] |
| 46 | +struct UserDataExportQueryArgs { |
| 47 | + #[clap(long)] |
| 48 | + resource_type: UserDataExportResourceType, |
| 49 | + |
| 50 | + #[clap(long)] |
| 51 | + resource_id: Uuid, |
| 52 | +} |
| 53 | + |
| 54 | +#[derive(Clone, Debug, Args)] |
| 55 | +struct UserDataExportDeleteArgs { |
| 56 | + #[clap(long)] |
| 57 | + user_data_export_id: UserDataExportUuid, |
| 58 | +} |
| 59 | + |
| 60 | +impl UserDataExportArgs { |
| 61 | + pub async fn exec( |
| 62 | + &self, |
| 63 | + omdb: &Omdb, |
| 64 | + opctx: &OpContext, |
| 65 | + datastore: &DataStore, |
| 66 | + ) -> Result<(), anyhow::Error> { |
| 67 | + match &self.command { |
| 68 | + UserDataExportCommands::Query(args) => { |
| 69 | + cmd_user_data_export_query(opctx, datastore, args).await |
| 70 | + } |
| 71 | + |
| 72 | + UserDataExportCommands::Delete(args) => { |
| 73 | + let token = omdb.check_allow_destructive()?; |
| 74 | + |
| 75 | + cmd_user_data_export_delete(opctx, datastore, args, token).await |
| 76 | + } |
| 77 | + |
| 78 | + UserDataExportCommands::ShowChangeset => { |
| 79 | + cmd_user_data_export_show_changeset(opctx, datastore).await |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +async fn cmd_user_data_export_query( |
| 86 | + _opctx: &OpContext, |
| 87 | + datastore: &DataStore, |
| 88 | + args: &UserDataExportQueryArgs, |
| 89 | +) -> Result<(), anyhow::Error> { |
| 90 | + let conn = datastore.pool_connection_for_tests().await?; |
| 91 | + |
| 92 | + let record = { |
| 93 | + use nexus_db_schema::schema::user_data_export::dsl; |
| 94 | + |
| 95 | + dsl::user_data_export |
| 96 | + .filter(dsl::resource_type.eq(args.resource_type)) |
| 97 | + .filter(dsl::resource_id.eq(args.resource_id)) |
| 98 | + .select(UserDataExportRecord::as_select()) |
| 99 | + .first_async(&*conn) |
| 100 | + .await? |
| 101 | + }; |
| 102 | + |
| 103 | + #[derive(Tabled)] |
| 104 | + struct Row { |
| 105 | + id: UserDataExportUuid, |
| 106 | + |
| 107 | + #[tabled(display_with = "display_debug")] |
| 108 | + state: UserDataExportState, |
| 109 | + #[tabled(display_with = "display_option_blank")] |
| 110 | + operating_saga_id: Option<Uuid>, |
| 111 | + generation: i64, |
| 112 | + |
| 113 | + resource_type: String, |
| 114 | + resource_id: Uuid, |
| 115 | + resource_deleted: bool, |
| 116 | + |
| 117 | + #[tabled(display_with = "display_option_blank")] |
| 118 | + pantry_address: Option<SocketAddrV6>, |
| 119 | + #[tabled(display_with = "display_option_blank")] |
| 120 | + volume_id: Option<VolumeUuid>, |
| 121 | + } |
| 122 | + |
| 123 | + let rows: Vec<_> = vec![Row { |
| 124 | + id: record.id(), |
| 125 | + |
| 126 | + state: record.state(), |
| 127 | + operating_saga_id: record.operating_saga_id(), |
| 128 | + generation: record.generation(), |
| 129 | + |
| 130 | + resource_type: args.resource_type.to_string(), |
| 131 | + resource_id: args.resource_id, |
| 132 | + resource_deleted: record.deleted(), |
| 133 | + |
| 134 | + pantry_address: record.pantry_address(), |
| 135 | + volume_id: record.volume_id(), |
| 136 | + }]; |
| 137 | + |
| 138 | + let table = tabled::Table::new(rows) |
| 139 | + .with(tabled::settings::Style::psql()) |
| 140 | + .to_string(); |
| 141 | + |
| 142 | + println!("{}", table); |
| 143 | + |
| 144 | + Ok(()) |
| 145 | +} |
| 146 | + |
| 147 | +async fn cmd_user_data_export_delete( |
| 148 | + _opctx: &OpContext, |
| 149 | + datastore: &DataStore, |
| 150 | + args: &UserDataExportDeleteArgs, |
| 151 | + _destruction_token: DestructiveOperationToken, |
| 152 | +) -> Result<(), anyhow::Error> { |
| 153 | + datastore.user_data_export_mark_deleted(args.user_data_export_id).await?; |
| 154 | + |
| 155 | + println!("marked record {} for deletion", args.user_data_export_id); |
| 156 | + |
| 157 | + Ok(()) |
| 158 | +} |
| 159 | + |
| 160 | +async fn cmd_user_data_export_show_changeset( |
| 161 | + opctx: &OpContext, |
| 162 | + datastore: &DataStore, |
| 163 | +) -> Result<(), anyhow::Error> { |
| 164 | + let changeset = datastore.compute_user_data_export_changeset(opctx).await?; |
| 165 | + |
| 166 | + #[derive(Tabled)] |
| 167 | + struct RequestRow { |
| 168 | + action: String, |
| 169 | + resource_type: String, |
| 170 | + resource_id: Uuid, |
| 171 | + } |
| 172 | + |
| 173 | + #[derive(Tabled)] |
| 174 | + struct CreateOrDeleteRow { |
| 175 | + action: String, |
| 176 | + |
| 177 | + id: UserDataExportUuid, |
| 178 | + |
| 179 | + #[tabled(display_with = "display_debug")] |
| 180 | + state: UserDataExportState, |
| 181 | + #[tabled(display_with = "display_option_blank")] |
| 182 | + operating_saga_id: Option<Uuid>, |
| 183 | + generation: i64, |
| 184 | + |
| 185 | + resource_type: String, |
| 186 | + resource_id: Uuid, |
| 187 | + resource_deleted: bool, |
| 188 | + |
| 189 | + #[tabled(display_with = "display_option_blank")] |
| 190 | + pantry_address: Option<SocketAddrV6>, |
| 191 | + #[tabled(display_with = "display_option_blank")] |
| 192 | + volume_id: Option<VolumeUuid>, |
| 193 | + } |
| 194 | + |
| 195 | + let request_rows: Vec<_> = changeset |
| 196 | + .request_required |
| 197 | + .iter() |
| 198 | + .map(|resource| RequestRow { |
| 199 | + action: String::from("request"), |
| 200 | + resource_type: resource.type_string(), |
| 201 | + resource_id: resource.id(), |
| 202 | + }) |
| 203 | + .collect(); |
| 204 | + |
| 205 | + let create_rows: Vec<_> = changeset |
| 206 | + .create_required |
| 207 | + .iter() |
| 208 | + .map(|record| CreateOrDeleteRow { |
| 209 | + action: String::from("create"), |
| 210 | + |
| 211 | + id: record.id(), |
| 212 | + |
| 213 | + state: record.state(), |
| 214 | + operating_saga_id: record.operating_saga_id(), |
| 215 | + generation: record.generation(), |
| 216 | + |
| 217 | + resource_type: record.resource().type_string(), |
| 218 | + resource_id: record.resource().id(), |
| 219 | + resource_deleted: record.deleted(), |
| 220 | + |
| 221 | + pantry_address: record.pantry_address(), |
| 222 | + volume_id: record.volume_id(), |
| 223 | + }) |
| 224 | + .collect(); |
| 225 | + |
| 226 | + let delete_rows: Vec<_> = changeset |
| 227 | + .delete_required |
| 228 | + .iter() |
| 229 | + .map(|record| CreateOrDeleteRow { |
| 230 | + action: String::from("delete"), |
| 231 | + |
| 232 | + id: record.id(), |
| 233 | + |
| 234 | + state: record.state(), |
| 235 | + operating_saga_id: record.operating_saga_id(), |
| 236 | + generation: record.generation(), |
| 237 | + |
| 238 | + resource_type: record.resource().type_string(), |
| 239 | + resource_id: record.resource().id(), |
| 240 | + resource_deleted: record.deleted(), |
| 241 | + |
| 242 | + pantry_address: record.pantry_address(), |
| 243 | + volume_id: record.volume_id(), |
| 244 | + }) |
| 245 | + .collect(); |
| 246 | + |
| 247 | + let table = tabled::Table::new(request_rows) |
| 248 | + .with(tabled::settings::Style::psql()) |
| 249 | + .to_string(); |
| 250 | + |
| 251 | + println!("{}", table); |
| 252 | + |
| 253 | + let table = tabled::Table::new(create_rows) |
| 254 | + .with(tabled::settings::Style::psql()) |
| 255 | + .to_string(); |
| 256 | + |
| 257 | + println!("{}", table); |
| 258 | + |
| 259 | + let table = tabled::Table::new(delete_rows) |
| 260 | + .with(tabled::settings::Style::psql()) |
| 261 | + .to_string(); |
| 262 | + |
| 263 | + println!("{}", table); |
| 264 | + |
| 265 | + Ok(()) |
| 266 | +} |
0 commit comments