forked from NVIDIA/ncx-infra-controller-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.rs
More file actions
218 lines (209 loc) · 9.62 KB
/
json.rs
File metadata and controls
218 lines (209 loc) · 9.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::collections::HashMap;
use carbide_uuid::instance_type::InstanceTypeId;
use carbide_uuid::machine::MachineId;
use chrono::{DateTime, Utc};
use config_version::{ConfigVersion, Versioned};
use health_report::HealthReport;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use crate::bmc_info::BmcInfo;
use crate::controller_outcome::PersistentStateHandlerOutcome;
use crate::hardware_info::{MachineInventory, MachineNvLinkInfo};
use crate::machine::health_override::HealthReportOverrides;
use crate::machine::infiniband::MachineInfinibandStatusObservation;
use crate::machine::network::{MachineNetworkStatusObservation, ManagedHostNetworkConfig};
use crate::machine::nvlink::MachineNvLinkStatusObservation;
use crate::machine::topology::MachineTopology;
use crate::machine::{
Dpf, FailureDetails, HostReprovisionRequest, Machine, MachineInterfaceSnapshot,
MachineLastRebootRequested, MachineStateHistory, ManagedHostState, ReprovisionRequest,
UpgradeDecision,
};
use crate::metadata::Metadata;
use crate::power_manager::PowerOptions;
use crate::sku::SkuStatus;
/// This represents the structure of a machine we get from postgres via the row_to_json or
/// JSONB_AGG functions. Its fields need to match the column names of the machine_snapshots query
/// exactly. It's expected that we read this directly from the JSON returned by the query, and then
/// convert it into a Machine.
#[derive(Serialize, Deserialize)]
pub struct MachineSnapshotPgJson {
pub id: MachineId,
pub created: DateTime<Utc>,
pub updated: DateTime<Utc>,
pub deployed: Option<DateTime<Utc>>,
pub agent_reported_inventory: Option<MachineInventory>,
pub network_config_version: String,
pub network_config: ManagedHostNetworkConfig,
pub network_status_observation: Option<MachineNetworkStatusObservation>,
pub infiniband_status_observation: Option<MachineInfinibandStatusObservation>,
pub nvlink_status_observation: Option<MachineNvLinkStatusObservation>,
pub controller_state_version: String,
pub controller_state: ManagedHostState,
pub last_discovery_time: Option<DateTime<Utc>>,
pub last_scout_contact_time: Option<DateTime<Utc>>,
pub last_reboot_time: Option<DateTime<Utc>>,
pub last_reboot_requested: Option<MachineLastRebootRequested>,
pub last_cleanup_time: Option<DateTime<Utc>>,
pub failure_details: FailureDetails,
pub reprovisioning_requested: Option<ReprovisionRequest>,
pub host_reprovisioning_requested: Option<HostReprovisionRequest>,
pub manual_firmware_upgrade_completed: Option<DateTime<Utc>>,
pub bios_password_set_time: Option<DateTime<Utc>>,
pub last_machine_validation_time: Option<DateTime<Utc>>,
pub discovery_machine_validation_id: Option<uuid::Uuid>,
pub cleanup_machine_validation_id: Option<uuid::Uuid>,
pub dpu_agent_health_report: Option<HealthReport>,
pub dpu_agent_upgrade_requested: Option<UpgradeDecision>,
pub machine_validation_health_report: HealthReport,
pub site_explorer_health_report: Option<HealthReport>,
pub firmware_autoupdate: Option<bool>,
pub health_report_overrides: Option<HealthReportOverrides>,
pub on_demand_machine_validation_id: Option<uuid::Uuid>,
pub on_demand_machine_validation_request: Option<bool>,
pub asn: Option<u32>,
pub controller_state_outcome: Option<PersistentStateHandlerOutcome>,
pub current_machine_validation_id: Option<uuid::Uuid>,
pub machine_state_model_version: i32,
pub instance_type_id: Option<InstanceTypeId>,
pub interfaces: Vec<MachineInterfaceSnapshot>,
pub topology: Vec<MachineTopology>,
pub labels: HashMap<String, String>,
pub name: String,
pub description: String,
#[serde(default)] // History is only brought in if the search config requested it
pub history: Vec<MachineStateHistory>,
pub version: String,
pub hw_sku: Option<String>,
pub hw_sku_status: Option<SkuStatus>,
pub sku_validation_health_report: Option<HealthReport>,
#[serde(default)] // Power options are valid only for host, not for DPUs.
pub power_options: Option<PowerOptions>,
pub hw_sku_device_type: Option<String>,
pub update_complete: bool,
pub nvlink_info: Option<MachineNvLinkInfo>,
pub dpf: Dpf,
}
impl TryFrom<MachineSnapshotPgJson> for Machine {
type Error = sqlx::Error;
fn try_from(value: MachineSnapshotPgJson) -> sqlx::Result<Self> {
let (hardware_info, bmc_info) = value
.topology
.into_iter()
.map(|t| {
let topology = t.into_topology();
(
Some(topology.discovery_data.info.clone()),
topology.bmc_info,
)
})
.next()
.unwrap_or((None, BmcInfo::default()));
let metadata = Metadata {
name: value.name,
description: value.description,
labels: value.labels,
};
let version: ConfigVersion =
value
.version
.parse()
.map_err(|e| sqlx::error::Error::ColumnDecode {
index: "version".to_string(),
source: Box::new(e),
})?;
let history = value
.history
.into_iter()
.sorted_by(
|s1: &crate::machine::MachineStateHistory,
s2: &crate::machine::MachineStateHistory| {
Ord::cmp(&s1.state_version.timestamp(), &s2.state_version.timestamp())
},
)
.collect();
Ok(Self {
id: value.id,
state: Versioned {
value: value.controller_state,
version: value.controller_state_version.parse().map_err(|e| {
sqlx::error::Error::ColumnDecode {
index: "controller_state_version".to_string(),
source: Box::new(e),
}
})?,
},
network_config: Versioned {
value: value.network_config,
version: value.network_config_version.parse().map_err(|e| {
sqlx::error::Error::ColumnDecode {
index: "network_config_version".to_string(),
source: Box::new(e),
}
})?,
},
network_status_observation: value.network_status_observation,
infiniband_status_observation: value.infiniband_status_observation,
nvlink_status_observation: value.nvlink_status_observation,
history,
interfaces: value.interfaces,
hardware_info,
bmc_info,
last_reboot_time: value.last_reboot_time,
last_cleanup_time: value.last_cleanup_time,
last_discovery_time: value.last_discovery_time,
last_scout_contact_time: value.last_scout_contact_time,
failure_details: value.failure_details,
reprovision_requested: value.reprovisioning_requested,
host_reprovision_requested: value.host_reprovisioning_requested,
manual_firmware_upgrade_completed: value.manual_firmware_upgrade_completed,
dpu_agent_upgrade_requested: value.dpu_agent_upgrade_requested,
dpu_agent_health_report: value.dpu_agent_health_report,
machine_validation_health_report: value.machine_validation_health_report,
site_explorer_health_report: value.site_explorer_health_report,
health_report_overrides: value.health_report_overrides.unwrap_or_default(),
inventory: value.agent_reported_inventory,
last_reboot_requested: value.last_reboot_requested,
controller_state_outcome: value.controller_state_outcome,
bios_password_set_time: value.bios_password_set_time,
last_machine_validation_time: value.last_machine_validation_time,
discovery_machine_validation_id: value.discovery_machine_validation_id,
cleanup_machine_validation_id: value.cleanup_machine_validation_id,
firmware_autoupdate: value.firmware_autoupdate,
on_demand_machine_validation_id: value.on_demand_machine_validation_id,
on_demand_machine_validation_request: value.on_demand_machine_validation_request,
asn: value.asn,
metadata,
instance_type_id: value.instance_type_id,
version,
// Columns for these exist, but are unused in rust code
// deployed: value.deployed,
// created: value.created,
// updated: value.updated,
hw_sku: value.hw_sku,
hw_sku_status: value.hw_sku_status,
sku_validation_health_report: value.sku_validation_health_report,
power_options: value.power_options,
hw_sku_device_type: value.hw_sku_device_type,
update_complete: value.update_complete,
nvlink_info: value.nvlink_info,
dpf: value.dpf,
})
}
}