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
use std::collections::HashMap;
use std::fs::read_to_string;
use std::path::Path;
use tracing::debug;
use wick_interface_types::TypeDefinition;
use crate::error::ManifestError;
use crate::host_definition::HostConfig;
use crate::v1::ComponentMetadata;
use crate::{from_yaml, v0, v1, ComponentDefinition, Error, FlowOperation, Result};
#[derive(Debug, Clone, Default)]
#[must_use]
pub struct ComponentConfiguration {
source: Option<String>,
format: u32,
version: String,
host: HostConfig,
name: Option<String>,
types: Vec<TypeDefinition>,
labels: HashMap<String, String>,
import: HashMap<String, ComponentDefinition>,
operations: HashMap<String, FlowOperation>,
}
impl TryFrom<v0::HostManifest> for ComponentConfiguration {
type Error = ManifestError;
fn try_from(def: v0::HostManifest) -> Result<Self> {
let flows: Result<HashMap<String, FlowOperation>> = def
.network
.schematics
.iter()
.map(|val| Ok((val.name.clone(), val.try_into()?)))
.collect();
Ok(ComponentConfiguration {
source: None,
format: def.format,
version: def.version,
types: Default::default(),
host: def.host.try_into()?,
name: def.network.name,
import: def
.network
.collections
.iter()
.map(|val| Ok((val.namespace.clone(), val.try_into()?)))
.collect::<Result<HashMap<_, _>>>()?,
labels: def.network.labels,
operations: flows?,
})
}
}
impl TryFrom<v1::ComponentConfiguration> for ComponentConfiguration {
type Error = ManifestError;
fn try_from(def: v1::ComponentConfiguration) -> Result<Self> {
Ok(ComponentConfiguration {
source: None,
format: def.format,
types: def.types,
version: def.metadata.unwrap_or(ComponentMetadata::default()).version,
host: def.host.try_into()?,
name: def.name,
import: def
.import
.into_iter()
.map(|(k, v)| (k.clone(), (k, v).into()))
.collect(),
labels: def.labels,
operations: def
.operations
.into_iter()
.map(|op| Ok((op.name.clone(), op.try_into()?)))
.collect::<Result<_>>()?,
})
}
}
impl ComponentConfiguration {
pub fn load_from_file(path: impl AsRef<Path>) -> Result<ComponentConfiguration> {
let path = path.as_ref();
if !path.exists() {
return Err(Error::FileNotFound(path.to_string_lossy().into()));
}
debug!("Reading manifest from {}", path.to_string_lossy());
let contents = read_to_string(path)?;
let mut manifest = Self::from_yaml(&contents)?;
manifest.source = Some(path.to_string_lossy().to_string());
Ok(manifest)
}
pub fn load_from_bytes(source: Option<String>, bytes: &[u8]) -> Result<ComponentConfiguration> {
let contents = String::from_utf8_lossy(bytes);
let mut manifest = Self::from_yaml(&contents)?;
manifest.source = source;
Ok(manifest)
}
pub fn from_yaml(src: &str) -> Result<ComponentConfiguration> {
debug!("Trying to parse manifest as yaml");
let raw: serde_yaml::Value = from_yaml(src)?;
debug!("Yaml parsed successfully");
let raw_version = raw.get("format").ok_or(Error::NoFormat)?;
let version = raw_version
.as_i64()
.unwrap_or_else(|| -> i64 { raw_version.as_str().and_then(|s| s.parse::<i64>().ok()).unwrap_or(-1) });
let manifest = match version {
0 => Ok(from_yaml::<v0::HostManifest>(src)?.try_into()?),
1 => Ok(from_yaml::<v1::ComponentConfiguration>(src)?.try_into()?),
-1 => Err(Error::NoFormat),
_ => Err(Error::VersionError(version.to_string())),
};
debug!("Manifest: {:?}", manifest);
manifest
}
pub fn host(&self) -> &HostConfig {
&self.host
}
pub fn host_mut(&mut self) -> &mut HostConfig {
&mut self.host
}
#[must_use]
pub fn allow_latest(&self) -> bool {
self.host.allow_latest
}
#[must_use]
pub fn insecure_registries(&self) -> &Vec<String> {
&self.host.insecure_registries
}
#[must_use]
pub fn format(&self) -> u32 {
self.format
}
#[must_use]
pub fn version(&self) -> &str {
&self.version
}
#[must_use]
pub fn source(&self) -> &Option<String> {
&self.source
}
#[must_use]
pub fn operations(&self) -> &HashMap<String, FlowOperation> {
&self.operations
}
#[must_use]
pub fn name(&self) -> &Option<String> {
&self.name
}
#[must_use]
pub fn labels(&self) -> &HashMap<String, String> {
&self.labels
}
pub fn types(&self) -> &[TypeDefinition] {
&self.types
}
#[must_use]
pub fn components(&self) -> &HashMap<String, ComponentDefinition> {
&self.import
}
#[must_use]
pub fn component(&self, namespace: &str) -> Option<&ComponentDefinition> {
self.import.iter().find(|(k, _)| *k == namespace).map(|(_, v)| v)
}
#[must_use]
pub fn flow(&self, name: &str) -> Option<&FlowOperation> {
self.operations.iter().find(|(n, _)| name == *n).map(|(_, v)| v)
}
}
#[derive(Default, Debug, Clone)]
#[must_use]
pub struct ComponentConfigurationBuilder {
version: Option<String>,
base: Option<ComponentConfiguration>,
components: HashMap<String, ComponentDefinition>,
flows: HashMap<String, FlowOperation>,
}
impl ComponentConfigurationBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn with_base(definition: ComponentConfiguration) -> Self {
Self {
base: Some(definition),
..Default::default()
}
}
pub fn version(mut self, version: impl AsRef<str>) -> Self {
self.version = Some(version.as_ref().to_owned());
self
}
pub fn add_collection(mut self, name: impl AsRef<str>, collection: ComponentDefinition) -> Self {
self.components.insert(name.as_ref().to_owned(), collection);
self
}
pub fn add_flow(mut self, name: impl AsRef<str>, flow: FlowOperation) -> Self {
self.flows.insert(name.as_ref().to_owned(), flow);
self
}
pub fn build(self) -> ComponentConfiguration {
if let Some(mut def) = self.base {
for (name, collection) in self.components {
def.import.insert(name, collection);
}
for (name, flow) in self.flows {
def.operations.insert(name, flow);
}
def
} else {
ComponentConfiguration {
version: self.version.unwrap_or("0.0.0".to_owned()),
import: self.components,
operations: self.flows,
..Default::default()
}
}
}
}