fluvio_controlplane_metadata/spu/
mod.rs

1mod spec;
2mod status;
3
4pub use self::spec::*;
5pub use self::status::*;
6pub use custom_metadata::CustomSpuKey;
7
8#[cfg(feature = "k8")]
9mod k8;
10
11mod metadata {
12
13    use crate::core::{Spec, Status};
14    use crate::spg::SpuGroupSpec;
15    use crate::extended::{ObjectType, SpecExt};
16
17    use super::*;
18
19    impl Spec for SpuSpec {
20        const LABEL: &'static str = "SPU";
21        type IndexKey = String;
22        type Owner = SpuGroupSpec;
23        type Status = SpuStatus;
24    }
25
26    impl SpecExt for SpuSpec {
27        const OBJECT_TYPE: ObjectType = ObjectType::Spu;
28    }
29
30    impl Status for SpuStatus {}
31
32    #[cfg(feature = "k8")]
33    mod extended {
34
35        use crate::store::k8::{K8ExtendedSpec, K8ConvertError, K8MetaItem};
36        use crate::store::MetadataStoreObject;
37        use crate::k8_types::K8Obj;
38        use crate::store::k8::default_convert_from_k8;
39
40        use super::SpuSpec;
41
42        impl K8ExtendedSpec for SpuSpec {
43            type K8Spec = Self;
44
45            fn convert_from_k8(
46                k8_obj: K8Obj<Self::K8Spec>,
47                multi_namespace_context: bool,
48            ) -> Result<MetadataStoreObject<Self, K8MetaItem>, K8ConvertError<Self::K8Spec>>
49            {
50                default_convert_from_k8(k8_obj, multi_namespace_context)
51            }
52
53            fn convert_status_from_k8(status: Self::Status) -> Self::Status {
54                status
55            }
56
57            fn into_k8(self) -> Self::K8Spec {
58                self
59            }
60        }
61    }
62}
63
64mod custom_metadata {
65
66    use std::io::Error;
67    use std::io::ErrorKind;
68    use std::fmt;
69
70    use tracing::trace;
71
72    use fluvio_protocol::Encoder;
73    use fluvio_protocol::Decoder;
74    use fluvio_protocol::Version;
75    use fluvio_protocol::bytes::{Buf, BufMut};
76
77    use crate::core::{Spec, Removable, Creatable};
78    use crate::extended::{ObjectType, SpecExt};
79
80    use super::*;
81
82    /// this is not real spec but is there to allow passing of parameters
83    impl Spec for CustomSpuSpec {
84        const LABEL: &'static str = "CustomSpu";
85        type IndexKey = String;
86        type Status = SpuStatus;
87        type Owner = SpuSpec;
88    }
89
90    impl SpecExt for CustomSpuSpec {
91        const OBJECT_TYPE: ObjectType = ObjectType::CustomSpu;
92    }
93
94    impl Removable for CustomSpuSpec {
95        type DeleteKey = CustomSpuKey;
96    }
97
98    impl Creatable for CustomSpuSpec {}
99
100    // This can be auto generated by enum derive later
101    #[derive(Debug)]
102    pub enum CustomSpuKey {
103        Name(String),
104        Id(i32),
105    }
106
107    impl fmt::Display for CustomSpuKey {
108        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
109            match self {
110                Self::Name(name) => write!(f, "{name}"),
111                Self::Id(id) => write!(f, "{id}"),
112            }
113        }
114    }
115
116    impl From<&CustomSpuKey> for String {
117        fn from(key: &CustomSpuKey) -> Self {
118            match key {
119                CustomSpuKey::Name(name) => name.to_owned(),
120                CustomSpuKey::Id(id) => id.to_string(),
121            }
122        }
123    }
124
125    impl CustomSpuKey {
126        fn type_string(&self) -> &'static str {
127            match self {
128                Self::Name(_) => "Name",
129                Self::Id(_) => "Id",
130            }
131        }
132    }
133
134    impl Default for CustomSpuKey {
135        fn default() -> Self {
136            Self::Id(0)
137        }
138    }
139
140    impl Encoder for CustomSpuKey {
141        fn write_size(&self, version: Version) -> usize {
142            let type_size = self.type_string().to_owned().write_size(version);
143
144            type_size
145                + match self {
146                    Self::Name(s) => s.write_size(version),
147                    Self::Id(s) => s.write_size(version),
148                }
149        }
150
151        // encode match
152        fn encode<T>(&self, dest: &mut T, version: Version) -> Result<(), Error>
153        where
154            T: BufMut,
155        {
156            self.type_string().to_owned().encode(dest, version)?;
157
158            match self {
159                Self::Name(s) => s.encode(dest, version)?,
160                Self::Id(s) => s.encode(dest, version)?,
161            }
162
163            Ok(())
164        }
165    }
166
167    impl Decoder for CustomSpuKey {
168        fn decode<T>(&mut self, src: &mut T, version: Version) -> Result<(), Error>
169        where
170            T: Buf,
171        {
172            let mut typ = "".to_owned();
173            typ.decode(src, version)?;
174            trace!("decoded type: {}", typ);
175
176            match typ.as_ref() {
177                "Name" => {
178                    let mut response = String::default();
179                    response.decode(src, version)?;
180                    *self = Self::Name(response);
181                    Ok(())
182                }
183
184                "Id" => {
185                    let mut response: i32 = 9;
186                    response.decode(src, version)?;
187                    *self = Self::Id(response);
188                    Ok(())
189                }
190
191                // Unexpected type
192                _ => Err(Error::new(
193                    ErrorKind::InvalidData,
194                    format!("invalid spec type {typ}"),
195                )),
196            }
197        }
198    }
199}