|
| 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 | +//! Fault management support bundle requests and data selection models. |
| 6 | +
|
| 7 | +use crate::DbTypedUuid; |
| 8 | +use crate::impl_enum_type; |
| 9 | +use chrono::{DateTime, Utc}; |
| 10 | +use nexus_db_schema::schema::{ |
| 11 | + fm_sb_req_data_selection, fm_support_bundle_request, |
| 12 | +}; |
| 13 | +use nexus_types::fm; |
| 14 | +use nexus_types::fm::ereport::EreportFilters; |
| 15 | +use nexus_types::support_bundle as support_bundle_types; |
| 16 | +use nexus_types::support_bundle::{ |
| 17 | + BundleData, BundleDataSelection, SledSelection, |
| 18 | +}; |
| 19 | +use omicron_uuid_kinds::{ |
| 20 | + CaseKind, GenericUuid, SitrepKind, SledUuid, SupportBundleKind, |
| 21 | +}; |
| 22 | +use serde::{Deserialize, Serialize}; |
| 23 | + |
| 24 | +impl_enum_type!( |
| 25 | + BundleDataCategoryEnum: |
| 26 | + |
| 27 | + #[derive( |
| 28 | + Copy, |
| 29 | + Clone, |
| 30 | + Debug, |
| 31 | + PartialEq, |
| 32 | + Serialize, |
| 33 | + Deserialize, |
| 34 | + AsExpression, |
| 35 | + FromSqlRow, |
| 36 | + )] |
| 37 | + pub enum BundleDataCategory; |
| 38 | + |
| 39 | + Reconfigurator => b"reconfigurator" |
| 40 | + HostInfo => b"host_info" |
| 41 | + SledCubbyInfo => b"sled_cubby_info" |
| 42 | + SpDumps => b"sp_dumps" |
| 43 | + Ereports => b"ereports" |
| 44 | +); |
| 45 | + |
| 46 | +impl From<&support_bundle_types::BundleData> for BundleDataCategory { |
| 47 | + fn from(data: &support_bundle_types::BundleData) -> Self { |
| 48 | + match data { |
| 49 | + support_bundle_types::BundleData::Reconfigurator => { |
| 50 | + BundleDataCategory::Reconfigurator |
| 51 | + } |
| 52 | + support_bundle_types::BundleData::HostInfo(_) => { |
| 53 | + BundleDataCategory::HostInfo |
| 54 | + } |
| 55 | + support_bundle_types::BundleData::SledCubbyInfo => { |
| 56 | + BundleDataCategory::SledCubbyInfo |
| 57 | + } |
| 58 | + support_bundle_types::BundleData::SpDumps => { |
| 59 | + BundleDataCategory::SpDumps |
| 60 | + } |
| 61 | + support_bundle_types::BundleData::Ereports(_) => { |
| 62 | + BundleDataCategory::Ereports |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// --- SupportBundleRequest (parent row) --- |
| 69 | + |
| 70 | +#[derive(Queryable, Insertable, Clone, Debug, Selectable)] |
| 71 | +#[diesel(table_name = fm_support_bundle_request)] |
| 72 | +pub struct SupportBundleRequest { |
| 73 | + pub id: DbTypedUuid<SupportBundleKind>, |
| 74 | + pub sitrep_id: DbTypedUuid<SitrepKind>, |
| 75 | + pub requested_sitrep_id: DbTypedUuid<SitrepKind>, |
| 76 | + pub case_id: DbTypedUuid<CaseKind>, |
| 77 | +} |
| 78 | + |
| 79 | +impl SupportBundleRequest { |
| 80 | + pub fn from_sitrep( |
| 81 | + sitrep_id: impl Into<DbTypedUuid<SitrepKind>>, |
| 82 | + case_id: impl Into<DbTypedUuid<CaseKind>>, |
| 83 | + req: fm::case::SupportBundleRequest, |
| 84 | + ) -> Self { |
| 85 | + let fm::case::SupportBundleRequest { |
| 86 | + id, |
| 87 | + requested_sitrep_id, |
| 88 | + data_selection: _, |
| 89 | + } = req; |
| 90 | + SupportBundleRequest { |
| 91 | + id: id.into(), |
| 92 | + sitrep_id: sitrep_id.into(), |
| 93 | + requested_sitrep_id: requested_sitrep_id.into(), |
| 94 | + case_id: case_id.into(), |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// --- Data selection rows --- |
| 100 | + |
| 101 | +#[derive(Queryable, Insertable, Clone, Debug, Selectable)] |
| 102 | +#[diesel(table_name = fm_sb_req_data_selection)] |
| 103 | +pub struct SbReqDataSelection { |
| 104 | + pub sitrep_id: DbTypedUuid<SitrepKind>, |
| 105 | + pub request_id: DbTypedUuid<SupportBundleKind>, |
| 106 | + pub category: BundleDataCategory, |
| 107 | + // HostInfo fields |
| 108 | + pub all_sleds: Option<bool>, |
| 109 | + pub sled_ids: Option<Vec<uuid::Uuid>>, |
| 110 | + // Ereports fields |
| 111 | + pub ereport_start_time: Option<DateTime<Utc>>, |
| 112 | + pub ereport_end_time: Option<DateTime<Utc>>, |
| 113 | + pub ereport_only_serials: Option<Vec<String>>, |
| 114 | + pub ereport_only_classes: Option<Vec<String>>, |
| 115 | +} |
| 116 | + |
| 117 | +impl SbReqDataSelection { |
| 118 | + /// Create rows from a `BundleDataSelection` for a given request. |
| 119 | + pub fn from_data_selection( |
| 120 | + sitrep_id: impl Into<DbTypedUuid<SitrepKind>> + Copy, |
| 121 | + request_id: impl Into<DbTypedUuid<SupportBundleKind>> + Copy, |
| 122 | + selection: &BundleDataSelection, |
| 123 | + ) -> Vec<Self> { |
| 124 | + selection |
| 125 | + .iter() |
| 126 | + .map(|data| Self::from_bundle_data(sitrep_id, request_id, data)) |
| 127 | + .collect() |
| 128 | + } |
| 129 | + |
| 130 | + fn from_bundle_data( |
| 131 | + sitrep_id: impl Into<DbTypedUuid<SitrepKind>>, |
| 132 | + request_id: impl Into<DbTypedUuid<SupportBundleKind>>, |
| 133 | + data: &BundleData, |
| 134 | + ) -> Self { |
| 135 | + let sitrep_id = sitrep_id.into(); |
| 136 | + let request_id = request_id.into(); |
| 137 | + let base = Self { |
| 138 | + sitrep_id, |
| 139 | + request_id, |
| 140 | + category: BundleDataCategory::from(data), |
| 141 | + all_sleds: None, |
| 142 | + sled_ids: None, |
| 143 | + ereport_start_time: None, |
| 144 | + ereport_end_time: None, |
| 145 | + ereport_only_serials: None, |
| 146 | + ereport_only_classes: None, |
| 147 | + }; |
| 148 | + match data { |
| 149 | + BundleData::Reconfigurator |
| 150 | + | BundleData::SledCubbyInfo |
| 151 | + | BundleData::SpDumps => base, |
| 152 | + BundleData::HostInfo(sled_selection) => match sled_selection { |
| 153 | + SledSelection::All => Self { |
| 154 | + all_sleds: Some(true), |
| 155 | + sled_ids: Some(vec![]), |
| 156 | + ..base |
| 157 | + }, |
| 158 | + SledSelection::Specific(set) => Self { |
| 159 | + all_sleds: Some(false), |
| 160 | + sled_ids: Some( |
| 161 | + set.iter().map(|id| id.into_untyped_uuid()).collect(), |
| 162 | + ), |
| 163 | + ..base |
| 164 | + }, |
| 165 | + }, |
| 166 | + BundleData::Ereports(filters) => Self { |
| 167 | + ereport_start_time: filters.start_time(), |
| 168 | + ereport_end_time: filters.end_time(), |
| 169 | + ereport_only_serials: Some(filters.only_serials().to_vec()), |
| 170 | + ereport_only_classes: Some(filters.only_classes().to_vec()), |
| 171 | + ..base |
| 172 | + }, |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /// Convert a set of data selection rows back into a `BundleDataSelection`. |
| 177 | + /// Empty input produces the default selection (collect everything). |
| 178 | + pub fn into_data_selection(rows: Vec<Self>) -> BundleDataSelection { |
| 179 | + rows.into_iter().map(Self::into_bundle_data).collect() |
| 180 | + } |
| 181 | + |
| 182 | + fn into_bundle_data(self) -> BundleData { |
| 183 | + match self.category { |
| 184 | + BundleDataCategory::Reconfigurator => BundleData::Reconfigurator, |
| 185 | + BundleDataCategory::SledCubbyInfo => BundleData::SledCubbyInfo, |
| 186 | + BundleDataCategory::SpDumps => BundleData::SpDumps, |
| 187 | + BundleDataCategory::HostInfo => { |
| 188 | + if self.all_sleds.expect( |
| 189 | + "CHECK constraint guarantees all_sleds IS NOT NULL \ |
| 190 | + for host_info rows", |
| 191 | + ) { |
| 192 | + BundleData::HostInfo(SledSelection::All) |
| 193 | + } else { |
| 194 | + let ids = self.sled_ids.expect( |
| 195 | + "CHECK constraint guarantees sled_ids IS NOT NULL \ |
| 196 | + for host_info rows", |
| 197 | + ); |
| 198 | + BundleData::HostInfo(SledSelection::Specific( |
| 199 | + ids.into_iter() |
| 200 | + .map(SledUuid::from_untyped_uuid) |
| 201 | + .collect(), |
| 202 | + )) |
| 203 | + } |
| 204 | + } |
| 205 | + BundleDataCategory::Ereports => { |
| 206 | + let mut filters = EreportFilters::new(); |
| 207 | + if let Some(t) = self.ereport_start_time { |
| 208 | + filters = filters.with_start_time(t).expect( |
| 209 | + "CHECK constraint guarantees start_time <= end_time", |
| 210 | + ); |
| 211 | + } |
| 212 | + if let Some(t) = self.ereport_end_time { |
| 213 | + filters = filters.with_end_time(t).expect( |
| 214 | + "CHECK constraint guarantees start_time <= end_time", |
| 215 | + ); |
| 216 | + } |
| 217 | + filters = |
| 218 | + filters.with_serials(self.ereport_only_serials.expect( |
| 219 | + "CHECK constraint guarantees ereport_only_serials \ |
| 220 | + IS NOT NULL for ereports rows", |
| 221 | + )); |
| 222 | + filters = |
| 223 | + filters.with_classes(self.ereport_only_classes.expect( |
| 224 | + "CHECK constraint guarantees ereport_only_classes \ |
| 225 | + IS NOT NULL for ereports rows", |
| 226 | + )); |
| 227 | + BundleData::Ereports(filters) |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments