|
| 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 alert requests. |
| 6 | +
|
| 7 | +use crate::AlertClass; |
| 8 | +use crate::DbTypedUuid; |
| 9 | +use nexus_db_schema::schema::fm_alert_request; |
| 10 | +use nexus_types::fm; |
| 11 | +use omicron_uuid_kinds::{ |
| 12 | + AlertKind, CaseKind, CaseUuid, SitrepKind, SitrepUuid, |
| 13 | +}; |
| 14 | + |
| 15 | +#[derive(Queryable, Insertable, Clone, Debug, Selectable)] |
| 16 | +#[diesel(table_name = fm_alert_request)] |
| 17 | +pub struct AlertRequest { |
| 18 | + pub id: DbTypedUuid<AlertKind>, |
| 19 | + pub sitrep_id: DbTypedUuid<SitrepKind>, |
| 20 | + pub requested_sitrep_id: DbTypedUuid<SitrepKind>, |
| 21 | + pub case_id: DbTypedUuid<CaseKind>, |
| 22 | + #[diesel(column_name = "class")] |
| 23 | + pub class: AlertClass, |
| 24 | + pub payload: serde_json::Value, |
| 25 | +} |
| 26 | + |
| 27 | +impl AlertRequest { |
| 28 | + pub fn new( |
| 29 | + current_sitrep_id: SitrepUuid, |
| 30 | + case_id: CaseUuid, |
| 31 | + req: fm::AlertRequest, |
| 32 | + ) -> Self { |
| 33 | + let fm::AlertRequest { id, requested_sitrep_id, payload, class } = req; |
| 34 | + AlertRequest { |
| 35 | + id: id.into(), |
| 36 | + sitrep_id: current_sitrep_id.into(), |
| 37 | + requested_sitrep_id: requested_sitrep_id.into(), |
| 38 | + case_id: case_id.into(), |
| 39 | + class: class.into(), |
| 40 | + payload, |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl TryFrom<AlertRequest> for fm::AlertRequest { |
| 46 | + type Error = <fm::AlertClass as TryFrom<AlertClass>>::Error; |
| 47 | + fn try_from(req: AlertRequest) -> Result<Self, Self::Error> { |
| 48 | + Ok(fm::AlertRequest { |
| 49 | + id: req.id.into(), |
| 50 | + requested_sitrep_id: req.requested_sitrep_id.into(), |
| 51 | + payload: req.payload, |
| 52 | + class: req.class.try_into()?, |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
0 commit comments