Skip to content

Commit 046e15c

Browse files
authored
add status handling (#711)
* chore: enable Default for cluster Signed-off-by: Mohammed Naser <[email protected]> * chore: add status field to Magnum object Signed-off-by: Mohammed Naser <[email protected]> --------- Signed-off-by: Mohammed Naser <[email protected]>
1 parent 35e2070 commit 046e15c

File tree

8 files changed

+110
-6
lines changed

8 files changed

+110
-6
lines changed

Cargo.lock

Lines changed: 13 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@ pythonize = "0.24.0"
3131
schemars = "0.8.21"
3232
serde = "1.0.218"
3333
serde_json = "1.0.138"
34+
serde_plain = "1.0.2"
3435
serde_yaml = "0.9.34"
3536
thiserror = "2.0.11"
3637
tokio = { version = "1.43.0", features = ["macros", "rt-multi-thread"] }
3738
typed-builder = "0.21.0"
3839

3940
[dependencies.pyo3]
4041
version = "0.24.0"
41-
features = ["abi3-py38", "extension-module", "serde"]
42+
features = ["abi3-py38", "serde"]
43+
44+
[features]
45+
extension-module = ["pyo3/extension-module"]
46+
default = []
4247

4348
[dev-dependencies]
4449
gtmpl = "0.7.1"

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ magnum_cluster_api = "magnum_cluster_api.conf:list_opts"
7676

7777
[tool.maturin]
7878
module-name = "magnum_cluster_api"
79+
features = ["extension-module"]
7980

8081
[tool.uv]
8182
cache-keys = [{file = "pyproject.toml"}, {file = "rust/Cargo.toml"}, {file = "**/*.rs"}]

src/addons/cilium.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ mod tests {
362362
cluster_template: magnum::ClusterTemplate {
363363
network_driver: "cilium".to_string(),
364364
},
365+
..Default::default()
365366
};
366367

367368
let values: CiliumValues = cluster.clone().try_into().expect("failed to create values");
@@ -467,6 +468,7 @@ mod tests {
467468
cluster_template: magnum::ClusterTemplate {
468469
network_driver: "cilium".to_string(),
469470
},
471+
..Default::default()
470472
};
471473

472474
let values: CiliumValues = cluster.clone().try_into().expect("failed to create values");
@@ -570,6 +572,7 @@ mod tests {
570572
cluster_template: magnum::ClusterTemplate {
571573
network_driver: "cilium".to_string(),
572574
},
575+
..Default::default()
573576
};
574577

575578
let addon = Addon::new(cluster.clone());

src/addons/cinder_csi.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ mod tests {
235235
cluster_template: magnum::ClusterTemplate {
236236
network_driver: "cilium".to_string(),
237237
},
238+
..Default::default()
238239
};
239240

240241
let values: CSIValues = cluster.clone().try_into().expect("failed to create values");
@@ -308,6 +309,7 @@ mod tests {
308309
cluster_template: magnum::ClusterTemplate {
309310
network_driver: "cilium".to_string(),
310311
},
312+
..Default::default()
311313
};
312314

313315
let values: CSIValues = cluster.clone().try_into().expect("failed to create values");
@@ -379,6 +381,7 @@ mod tests {
379381
cluster_template: magnum::ClusterTemplate {
380382
network_driver: "cilium".to_string(),
381383
},
384+
..Default::default()
382385
};
383386

384387
let values: CSIValues = cluster.clone().try_into().expect("failed to create values");
@@ -436,6 +439,7 @@ mod tests {
436439
cluster_template: magnum::ClusterTemplate {
437440
network_driver: "cilium".to_string(),
438441
},
442+
..Default::default()
439443
};
440444

441445
let addon = Addon::new(cluster.clone());

src/addons/cloud_controller_manager.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ mod tests {
196196
cluster_template: magnum::ClusterTemplate {
197197
network_driver: "cilium".to_string(),
198198
},
199+
..Default::default()
199200
};
200201

201202
let values: CloudControllerManagerValues =
@@ -220,6 +221,7 @@ mod tests {
220221
cluster_template: magnum::ClusterTemplate {
221222
network_driver: "cilium".to_string(),
222223
},
224+
..Default::default()
223225
};
224226

225227
let values: CloudControllerManagerValues =
@@ -241,6 +243,7 @@ mod tests {
241243
cluster_template: magnum::ClusterTemplate {
242244
network_driver: "cilium".to_string(),
243245
},
246+
..Default::default()
244247
};
245248

246249
let values: CloudControllerManagerValues =
@@ -297,6 +300,7 @@ mod tests {
297300
cluster_template: magnum::ClusterTemplate {
298301
network_driver: "cilium".to_string(),
299302
},
303+
..Default::default()
300304
};
301305

302306
let addon = Addon::new(cluster.clone());

src/addons/manila_csi.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ mod tests {
165165
cluster_template: magnum::ClusterTemplate {
166166
network_driver: "cilium".to_string(),
167167
},
168+
..Default::default()
168169
};
169170

170171
let values: CSIValues = cluster.clone().try_into().expect("failed to create values");
@@ -222,6 +223,7 @@ mod tests {
222223
cluster_template: magnum::ClusterTemplate {
223224
network_driver: "cilium".to_string(),
224225
},
226+
..Default::default()
225227
};
226228

227229
let values: CSIValues = cluster.clone().try_into().expect("failed to create values");
@@ -277,6 +279,7 @@ mod tests {
277279
cluster_template: magnum::ClusterTemplate {
278280
network_driver: "cilium".to_string(),
279281
},
282+
..Default::default()
280283
};
281284

282285
let values: CSIValues = cluster.clone().try_into().expect("failed to create values");
@@ -314,6 +317,7 @@ mod tests {
314317
cluster_template: magnum::ClusterTemplate {
315318
network_driver: "cilium".to_string(),
316319
},
320+
..Default::default()
317321
};
318322

319323
let addon = Addon::new(cluster.clone());

src/magnum.rs

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::collections::BTreeMap;
1414
use thiserror::Error;
1515
use typed_builder::TypedBuilder;
1616

17-
#[derive(Clone, Deserialize, FromPyObject)]
17+
#[derive(Clone, Default, Deserialize, FromPyObject)]
1818
pub struct ClusterTemplate {
1919
pub network_driver: String,
2020
}
@@ -114,12 +114,50 @@ impl From<ClusterError> for PyErr {
114114
}
115115
}
116116

117-
#[derive(Clone, Deserialize, FromPyObject)]
117+
#[derive(Clone, Debug, Deserialize, PartialEq, Default)]
118+
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
119+
pub enum ClusterStatus {
120+
#[default]
121+
CreateInProgress,
122+
CreateFailed,
123+
CreateComplete,
124+
UpdateInProgress,
125+
UpdateFailed,
126+
UpdateComplete,
127+
DeleteInProgress,
128+
DeleteFailed,
129+
DeleteComplete,
130+
ResumeComplete,
131+
ResumeFailed,
132+
RestoreComplete,
133+
RollbackInProgress,
134+
RollbackFailed,
135+
RollbackComplete,
136+
SnapshotComplete,
137+
CheckComplete,
138+
AdoptComplete,
139+
}
140+
141+
impl FromPyObject<'_> for ClusterStatus {
142+
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
143+
let status = ob.extract::<String>()?;
144+
145+
serde_plain::from_str(&status).map_err(|err| {
146+
PyErr::new::<PyRuntimeError, _>(format!(
147+
"failed to parse cluster status: {}: {}",
148+
status, err
149+
))
150+
})
151+
}
152+
}
153+
154+
#[derive(Clone, Default, Deserialize, FromPyObject)]
118155
pub struct Cluster {
119156
pub uuid: String,
120157
pub cluster_template: ClusterTemplate,
121158
pub stack_id: Option<String>,
122159
pub labels: ClusterLabels,
160+
pub status: ClusterStatus,
123161
}
124162

125163
impl From<Cluster> for ObjectMeta {
@@ -230,6 +268,7 @@ mod tests {
230268
use super::*;
231269
use crate::addons;
232270
use pretty_assertions::assert_eq;
271+
use pyo3::{prepare_freethreaded_python, types::PyString};
233272
use rstest::rstest;
234273
use serde_yaml::{Mapping, Value};
235274
use std::path::PathBuf;
@@ -243,6 +282,32 @@ mod tests {
243282
"StorageClass",
244283
];
245284

285+
#[rstest]
286+
#[case("CREATE_IN_PROGRESS", ClusterStatus::CreateInProgress)]
287+
#[case("CREATE_FAILED", ClusterStatus::CreateFailed)]
288+
fn test_cluster_status_from_pyobject(#[case] status: &str, #[case] expected: ClusterStatus) {
289+
prepare_freethreaded_python();
290+
291+
Python::with_gil(|py| {
292+
let py_status = PyString::new(py, status);
293+
let result: ClusterStatus = py_status
294+
.extract()
295+
.expect("Failed to extract ClusterStatus");
296+
assert_eq!(result, expected);
297+
});
298+
}
299+
300+
#[test]
301+
fn test_cluster_status_from_pyobject_invalid() {
302+
prepare_freethreaded_python();
303+
304+
Python::with_gil(|py| {
305+
let py_status = PyString::new(py, "INVALID_STATUS");
306+
let result: Result<ClusterStatus, _> = py_status.extract();
307+
assert!(result.is_err());
308+
});
309+
}
310+
246311
#[test]
247312
fn test_object_meta_from_cluster() {
248313
let cluster = Cluster {
@@ -252,6 +317,7 @@ mod tests {
252317
cluster_template: ClusterTemplate {
253318
network_driver: "calico".to_string(),
254319
},
320+
..Default::default()
255321
};
256322

257323
let object_meta: ObjectMeta = cluster.into();
@@ -268,6 +334,7 @@ mod tests {
268334
cluster_template: ClusterTemplate {
269335
network_driver: "calico".to_string(),
270336
},
337+
..Default::default()
271338
};
272339

273340
let result = cluster.stack_id().expect("failed to get stack id");
@@ -283,6 +350,7 @@ mod tests {
283350
cluster_template: ClusterTemplate {
284351
network_driver: "calico".to_string(),
285352
},
353+
..Default::default()
286354
};
287355

288356
let result = cluster
@@ -306,6 +374,7 @@ mod tests {
306374
cluster_template: ClusterTemplate {
307375
network_driver: "calico".to_string(),
308376
},
377+
..Default::default()
309378
};
310379

311380
let mut mock_addon = addons::MockClusterAddon::default();
@@ -352,6 +421,7 @@ mod tests {
352421
cluster_template: ClusterTemplate {
353422
network_driver: "calico".to_string(),
354423
},
424+
..Default::default()
355425
};
356426

357427
let mut mock_addon = addons::MockClusterAddon::default();
@@ -392,6 +462,7 @@ mod tests {
392462
cluster_template: ClusterTemplate {
393463
network_driver: "calico".to_string(),
394464
},
465+
..Default::default()
395466
};
396467

397468
let mut mock_addon = addons::MockClusterAddon::default();
@@ -424,6 +495,7 @@ mod tests {
424495
cluster_template: ClusterTemplate {
425496
network_driver: "calico".to_string(),
426497
},
498+
..Default::default()
427499
};
428500

429501
let crs: ClusterResourceSet = cluster.into();
@@ -508,6 +580,7 @@ mod tests {
508580
cluster_template: ClusterTemplate {
509581
network_driver: "calico".to_string(),
510582
},
583+
..Default::default()
511584
};
512585

513586
let secret: Secret = cluster.clone().into();

0 commit comments

Comments
 (0)