diff --git a/nexus/db-model/src/volume_resource_usage.rs b/nexus/db-model/src/volume_resource_usage.rs index 14fb935d25..c55b053c62 100644 --- a/nexus/db-model/src/volume_resource_usage.rs +++ b/nexus/db-model/src/volume_resource_usage.rs @@ -102,31 +102,40 @@ impl VolumeResourceUsageRecord { } } -impl From for VolumeResourceUsage { - fn from(record: VolumeResourceUsageRecord) -> VolumeResourceUsage { +impl TryFrom for VolumeResourceUsage { + type Error = String; + + fn try_from( + record: VolumeResourceUsageRecord, + ) -> Result { match record.usage_type { VolumeResourceUsageType::ReadOnlyRegion => { - VolumeResourceUsage::ReadOnlyRegion { - region_id: record - .region_id - .expect("valid read-only region usage record"), - } + let Some(region_id) = record.region_id else { + return Err("valid read-only region usage record".into()); + }; + + Ok(VolumeResourceUsage::ReadOnlyRegion { region_id }) } VolumeResourceUsageType::RegionSnapshot => { - VolumeResourceUsage::RegionSnapshot { - dataset_id: record - .region_snapshot_dataset_id - .expect("valid region snapshot usage record"), - - region_id: record - .region_snapshot_region_id - .expect("valid region snapshot usage record"), - - snapshot_id: record - .region_snapshot_snapshot_id - .expect("valid region snapshot usage record"), - } + let Some(dataset_id) = record.region_snapshot_dataset_id else { + return Err("valid region snapshot usage record".into()); + }; + + let Some(region_id) = record.region_snapshot_region_id else { + return Err("valid region snapshot usage record".into()); + }; + + let Some(snapshot_id) = record.region_snapshot_snapshot_id + else { + return Err("valid region snapshot usage record".into()); + }; + + Ok(VolumeResourceUsage::RegionSnapshot { + dataset_id, + region_id, + snapshot_id, + }) } } }