k8s_openapi_ext/ext/
configmap.rs

1use super::*;
2
3/// Extension trait for `corev1::ConfigMap`.
4/// Fluent builders and mutable accessors
5///
6pub trait ConfigMapExt: ResourceBuilder {
7    /// Creates new `corev1::ConfigMap object with given `name`
8    ///
9    fn new(name: impl ToString) -> Self;
10
11    /// Initializes `immutable` field
12    ///
13    fn immutable(self, yes: bool) -> Self;
14
15    /// Initializes `binary_data` field
16    ///
17    fn binary_data(self, data: impl IntoIterator<Item = (impl ToString, ByteString)>) -> Self;
18
19    /// Initializes `data` field
20    ///
21    fn data(self, data: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self;
22
23    /// Mutable access to `data`.
24    /// Initializes `data` with empty `BTreeMap` if absent
25    ///
26    fn data_mut(&mut self) -> &mut BTreeMap<String, String>;
27
28    /// Mutable access to `binary_data`.
29    /// Initializes `binary_data` with empty `BTreeMap` if absent
30    ///
31    fn binary_data_mut(&mut self) -> &mut BTreeMap<String, ByteString>;
32}
33
34impl ConfigMapExt for corev1::ConfigMap {
35    fn new(name: impl ToString) -> Self {
36        let metadata = metadata(name);
37        Self {
38            metadata,
39            // binary_data: todo!(),
40            // data: todo!(),
41            // immutable: todo!(),
42            ..default()
43        }
44    }
45
46    fn immutable(self, yes: bool) -> Self {
47        let immutable = Some(yes);
48        Self { immutable, ..self }
49    }
50
51    fn binary_data(self, data: impl IntoIterator<Item = (impl ToString, ByteString)>) -> Self {
52        let data = data
53            .into_iter()
54            .map(|(key, value)| (key.to_string(), value))
55            .collect();
56        Self {
57            binary_data: Some(data),
58            ..self
59        }
60    }
61
62    fn data(self, data: impl IntoIterator<Item = (impl ToString, impl ToString)>) -> Self {
63        let data = data
64            .into_iter()
65            .map(|(key, value)| (key.to_string(), value.to_string()))
66            .collect();
67        Self {
68            data: Some(data),
69            ..self
70        }
71    }
72
73    fn data_mut(&mut self) -> &mut BTreeMap<String, String> {
74        self.data.get_or_insert_with(default)
75    }
76
77    fn binary_data_mut(&mut self) -> &mut BTreeMap<String, ByteString> {
78        self.binary_data.get_or_insert_with(default)
79    }
80}