This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
sfa.rs
314 lines (285 loc) · 10.5 KB
/
sfa.rs
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright (c) 2020 DDN. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
pub use crate::models::{ChromaCoreSfastoragesystem, EnclosureType, HealthState, MemberState};
#[cfg(feature = "postgres-interop")]
use crate::{
schema::{
chroma_core_sfadiskdrive as sd, chroma_core_sfadiskdrive, chroma_core_sfaenclosure as se,
chroma_core_sfaenclosure, chroma_core_sfastoragesystem as ss, chroma_core_sfastoragesystem,
},
Executable,
};
#[cfg(feature = "postgres-interop")]
use diesel::{pg::upsert::excluded, prelude::*};
#[cfg(feature = "wbem-interop")]
use std::convert::{TryFrom, TryInto};
#[cfg(feature = "wbem-interop")]
use thiserror::Error;
#[cfg(feature = "wbem-interop")]
use wbem_client::{
resp::{Cim, IReturnValueInstance, Instance},
CimXmlError,
};
#[cfg(feature = "wbem-interop")]
#[derive(Error, Debug)]
pub enum SfaClassError {
#[error(transparent)]
CimXmlError(#[from] CimXmlError),
#[error("Expected class {0}, found class {1}")]
UnexpectedInstance(&'static str, String),
#[error("HealthState {0} not known")]
UnknownHealth(String),
#[error("Enclosure Type {0} not known")]
UnknownEnclosure(String),
#[error("MemberState {0} not known")]
UnknownMemberState(String),
#[error(transparent)]
ParseBoolError(#[from] std::str::ParseBoolError),
#[error(transparent)]
ParseIntError(#[from] std::num::ParseIntError),
}
#[cfg(feature = "wbem-interop")]
impl<'a> TryFrom<&'a str> for EnclosureType {
type Error = SfaClassError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
match s {
"0" => Ok(EnclosureType::None),
"1" => Ok(EnclosureType::Disk),
"2" => Ok(EnclosureType::Controller),
"3" => Ok(EnclosureType::Ups),
"255" => Ok(EnclosureType::Unknown),
x => Err(SfaClassError::UnknownEnclosure(x.into())),
}
}
}
#[cfg(feature = "wbem-interop")]
impl<'a> TryFrom<&'a str> for HealthState {
type Error = SfaClassError;
fn try_from(s: &str) -> Result<Self, Self::Error> {
match s {
"0" => Ok(HealthState::None),
"1" => Ok(HealthState::Ok),
"2" => Ok(HealthState::NonCritical),
"3" => Ok(HealthState::Critical),
"255" => Ok(HealthState::Unknown),
x => Err(SfaClassError::UnknownHealth(x.into())),
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(
feature = "postgres-interop",
derive(Queryable, Insertable, AsChangeset, Identifiable)
)]
#[cfg_attr(feature = "postgres-interop", primary_key(index))]
#[cfg_attr(feature = "postgres-interop", table_name = "chroma_core_sfaenclosure")]
pub struct SfaEnclosure {
/// Specifies the index, part of the OID, of the enclosure.
pub index: i32,
pub element_name: String,
pub health_state: HealthState,
pub position: i16,
pub enclosure_type: EnclosureType,
pub storage_system: Option<String>,
}
pub type Table = se::table;
#[cfg(feature = "postgres-interop")]
impl SfaEnclosure {
pub fn all() -> Table {
se::table
}
pub fn batch_upsert(xs: Vec<Self>) -> impl Executable {
diesel::insert_into(Self::all())
.values(xs)
.on_conflict(se::index)
.do_update()
.set((
se::element_name.eq(excluded(se::element_name)),
se::health_state.eq(excluded(se::health_state)),
se::position.eq(excluded(se::position)),
se::enclosure_type.eq(excluded(se::enclosure_type)),
se::storage_system.eq(excluded(se::storage_system)),
))
}
pub fn upsert(self) -> impl Executable {
diesel::insert_into(se::table)
.values(self.clone())
.on_conflict(se::index)
.do_update()
.set(self)
}
}
#[derive(Debug, Clone)]
#[cfg_attr(
feature = "postgres-interop",
derive(Queryable, Insertable, AsChangeset)
)]
#[cfg_attr(
feature = "postgres-interop",
table_name = "chroma_core_sfastoragesystem"
)]
pub struct SfaStorageSystem {
pub child_health_state: HealthState,
pub health_state_reason: String,
pub health_state: HealthState,
pub uuid: String,
}
#[cfg(feature = "wbem-interop")]
impl TryFrom<Instance> for SfaEnclosure {
type Error = SfaClassError;
fn try_from(x: Instance) -> Result<Self, Self::Error> {
if x.class_name != "DDN_SFAEnclosure" {
return Err(SfaClassError::UnexpectedInstance(
"DDN_SFAEnclosure",
x.class_name.to_string(),
));
}
Ok(SfaEnclosure {
index: x.try_get_property("Index")?.parse::<i32>()?,
element_name: x.try_get_property("ElementName")?.into(),
health_state: x.try_get_property("HealthState")?.try_into()?,
position: x.try_get_property("Position")?.parse::<i16>()?,
enclosure_type: x.try_get_property("Type")?.try_into()?,
storage_system: None,
})
}
}
#[cfg(feature = "postgres-interop")]
impl SfaStorageSystem {
pub fn upsert(x: Self) -> impl Executable {
diesel::insert_into(ss::table)
.values(x.clone())
.on_conflict(ss::uuid)
.do_update()
.set(x)
}
}
#[cfg(feature = "wbem-interop")]
impl TryFrom<Instance> for SfaStorageSystem {
type Error = SfaClassError;
fn try_from(x: Instance) -> Result<Self, Self::Error> {
if x.class_name != "DDN_SFAStorageSystem" {
return Err(SfaClassError::UnexpectedInstance(
"DDN_SFAStorageSystem",
x.class_name.to_string(),
));
}
Ok(SfaStorageSystem {
child_health_state: x.try_get_property("ChildHealthState")?.try_into()?,
health_state_reason: x.try_get_property("HealthStateReason")?.into(),
health_state: x.try_get_property("HealthState")?.try_into()?,
uuid: x.try_get_property("UUID")?.into(),
})
}
}
#[cfg(feature = "wbem-interop")]
impl TryFrom<Cim<IReturnValueInstance>> for SfaStorageSystem {
type Error = SfaClassError;
fn try_from(x: Cim<IReturnValueInstance>) -> Result<Self, Self::Error> {
let x = x.message.simplersp.imethodresponse.i_return_value.instance;
SfaStorageSystem::try_from(x)
}
}
#[cfg(feature = "wbem-interop")]
impl<'a> TryFrom<&'a str> for MemberState {
type Error = SfaClassError;
fn try_from(x: &str) -> Result<Self, Self::Error> {
match x {
"0" => Ok(Self::MemberStateNormal),
"1" => Ok(Self::MemberStateMissing),
"2" => Ok(Self::MemberStateAlreadyMissing),
"3" => Ok(Self::MemberStateRebuilding),
"4" => Ok(Self::MemberStateWaitingToRebuild),
"5" => Ok(Self::MemberStateFailed),
"6" => Ok(Self::MemberStateMissingNoRebuild),
"7" => Ok(Self::MemberStateErrorRec),
"8" => Ok(Self::MemberStateUnassigned),
"9" => Ok(Self::MemberStateCopyback),
"10" => Ok(Self::MemberStateWaitingCopyback),
"11" => Ok(Self::MemberStateLocked),
"12" => Ok(Self::MemberStateLockedNoRebuild),
"13" => Ok(Self::MemberStateAlreadyLocked),
"14" => Ok(Self::MemberStateMissingPreventsRebuild),
"15" => Ok(Self::MemberStateLockedPreventsRebuild),
"255" => Ok(Self::MemberStateUnknown),
x => Err(SfaClassError::UnknownMemberState(x.into())),
}
}
}
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(
feature = "postgres-interop",
derive(Queryable, Insertable, AsChangeset)
)]
#[cfg_attr(feature = "postgres-interop", primary_key(index))]
#[cfg_attr(feature = "postgres-interop", table_name = "chroma_core_sfadiskdrive")]
pub struct SfaDiskDrive {
pub index: i16,
pub child_health_state: HealthState,
pub failed: bool,
pub health_state_reason: String,
pub slot_number: i32,
pub health_state: HealthState,
/// Specifies the member index of the disk drive.
/// If the disk drive is not a member of a pool, this value will be not be set.
pub member_index: Option<i16>,
/// Specifies the state of the disk drive relative to a containing pool.
pub member_state: MemberState,
pub enclosure_index: i32,
}
#[cfg(feature = "postgres-interop")]
impl SfaDiskDrive {
pub fn all() -> sd::table {
sd::table
}
pub fn batch_upsert(xs: Vec<Self>) -> impl Executable {
diesel::insert_into(Self::all())
.values(xs)
.on_conflict(sd::index)
.do_update()
.set((
sd::child_health_state.eq(excluded(sd::child_health_state)),
sd::failed.eq(excluded(sd::failed)),
sd::health_state_reason.eq(excluded(sd::health_state_reason)),
sd::health_state.eq(excluded(sd::health_state)),
sd::member_index.eq(excluded(sd::member_index)),
sd::member_state.eq(excluded(sd::member_state)),
sd::enclosure_index.eq(excluded(sd::enclosure_index)),
))
}
pub fn batch_remove(xs: Vec<i16>) -> impl Executable {
diesel::delete(Self::all()).filter(sd::index.eq_any(xs))
}
}
#[cfg(feature = "wbem-interop")]
impl TryFrom<Instance> for SfaDiskDrive {
type Error = SfaClassError;
fn try_from(x: Instance) -> Result<Self, Self::Error> {
if x.class_name != "DDN_SFADiskDrive" {
return Err(SfaClassError::UnexpectedInstance(
"DDN_SFADiskDrive",
x.class_name.to_string(),
));
}
Ok(SfaDiskDrive {
index: x.try_get_property("Index")?.parse::<i16>()?,
child_health_state: x.try_get_property("ChildHealthState")?.try_into()?,
enclosure_index: x.try_get_property("EnclosureIndex")?.parse::<i32>()?,
failed: x
.try_get_property("Failed")?
.to_lowercase()
.parse::<bool>()?,
health_state_reason: x.try_get_property("HealthStateReason")?.into(),
health_state: x.try_get_property("HealthState")?.try_into()?,
member_index: x
.get_property("MemberIndex")
.map(|x| x.parse::<i16>())
.transpose()?,
member_state: x.try_get_property("MemberState")?.try_into()?,
slot_number: x.try_get_property("DiskSlotNumber")?.parse::<i32>()?,
})
}
}
pub struct SfaStoragePool {}
pub struct SfaVirtualDisk {}