Skip to content

Commit 3019a1b

Browse files
Add rudimentary command to omdb to map instance IDs to propolis zones (#4142)
1 parent 5cfcda6 commit 3019a1b

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

dev-tools/omdb/src/bin/omdb/db.rs

+52
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ enum DbCommands {
8080
Services(ServicesArgs),
8181
/// Print information about sleds
8282
Sleds,
83+
/// Print information about customer instances
84+
Instances,
8385
}
8486

8587
#[derive(Debug, Args)]
@@ -246,6 +248,9 @@ impl DbArgs {
246248
DbCommands::Sleds => {
247249
cmd_db_sleds(&opctx, &datastore, self.fetch_limit).await
248250
}
251+
DbCommands::Instances => {
252+
cmd_db_instances(&datastore, self.fetch_limit).await
253+
}
249254
}
250255
}
251256
}
@@ -680,6 +685,53 @@ async fn cmd_db_sleds(
680685
Ok(())
681686
}
682687

688+
#[derive(Tabled)]
689+
#[tabled(rename_all = "SCREAMING_SNAKE_CASE")]
690+
struct CustomerInstanceRow {
691+
id: Uuid,
692+
state: String,
693+
propolis_id: Uuid,
694+
sled_id: Uuid,
695+
}
696+
697+
impl From<Instance> for CustomerInstanceRow {
698+
fn from(i: Instance) -> Self {
699+
CustomerInstanceRow {
700+
id: i.id(),
701+
state: format!("{:?}", i.runtime_state.state.0),
702+
propolis_id: i.runtime_state.propolis_id,
703+
sled_id: i.runtime_state.sled_id,
704+
}
705+
}
706+
}
707+
708+
/// Run `omdb db instances`: list data about customer VMs.
709+
async fn cmd_db_instances(
710+
datastore: &DataStore,
711+
limit: NonZeroU32,
712+
) -> Result<(), anyhow::Error> {
713+
use db::schema::instance::dsl;
714+
let instances = dsl::instance
715+
.limit(i64::from(u32::from(limit)))
716+
.select(Instance::as_select())
717+
.load_async(datastore.pool_for_tests().await?)
718+
.await
719+
.context("loading instances")?;
720+
721+
let ctx = || "listing instances".to_string();
722+
check_limit(&instances, limit, ctx);
723+
724+
let rows = instances.into_iter().map(|i| CustomerInstanceRow::from(i));
725+
let table = tabled::Table::new(rows)
726+
.with(tabled::settings::Style::empty())
727+
.with(tabled::settings::Padding::new(0, 1, 0, 0))
728+
.to_string();
729+
730+
println!("{}", table);
731+
732+
Ok(())
733+
}
734+
683735
// DNS
684736

685737
/// Run `omdb db dns show`.

dev-tools/omdb/tests/usage_errors.out

+12-10
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ Query the control plane database (CockroachDB)
8686
Usage: omdb db [OPTIONS] <COMMAND>
8787

8888
Commands:
89-
disks Print information about disks
90-
dns Print information about internal and external DNS
91-
services Print information about control plane services
92-
sleds Print information about sleds
93-
help Print this message or the help of the given subcommand(s)
89+
disks Print information about disks
90+
dns Print information about internal and external DNS
91+
services Print information about control plane services
92+
sleds Print information about sleds
93+
instances Print information about customer instances
94+
help Print this message or the help of the given subcommand(s)
9495

9596
Options:
9697
--db-url <DB_URL> URL of the database SQL interface [env: OMDB_DB_URL=]
@@ -106,11 +107,12 @@ Query the control plane database (CockroachDB)
106107
Usage: omdb db [OPTIONS] <COMMAND>
107108

108109
Commands:
109-
disks Print information about disks
110-
dns Print information about internal and external DNS
111-
services Print information about control plane services
112-
sleds Print information about sleds
113-
help Print this message or the help of the given subcommand(s)
110+
disks Print information about disks
111+
dns Print information about internal and external DNS
112+
services Print information about control plane services
113+
sleds Print information about sleds
114+
instances Print information about customer instances
115+
help Print this message or the help of the given subcommand(s)
114116

115117
Options:
116118
--db-url <DB_URL> URL of the database SQL interface [env: OMDB_DB_URL=]

0 commit comments

Comments
 (0)