-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
226 lines (198 loc) · 7.07 KB
/
Copy pathmod.rs
File metadata and controls
226 lines (198 loc) · 7.07 KB
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
use crate::{
flow_definition::Reader,
flow_service::{auth::get_authorization_metadata, retry::create_channel_with_retry},
};
use tokio::time::Duration;
use tonic::{Extensions, Request, transport::Channel};
use tucana::{
aquila::{ModuleUpdateRequest, module_service_client::ModuleServiceClient},
shared::{Module, ModuleDefinition},
};
pub mod auth;
pub mod retry;
pub struct FlowUpdateService {
modules: Vec<Module>,
channel: Channel,
aquila_token: String,
definition_source: Option<String>,
}
pub struct ModuleDefinitionAppendix {
pub module_identifier: String,
pub definitions: Vec<ModuleDefinition>,
}
impl FlowUpdateService {
/// Create a new FlowUpdateService instance from an Aquila URL and a definition path.
///
/// This reads the definition files from the given path as modules and initializes the
/// service with those module definitions.
pub async fn from_url(
aquila_url: String,
definition_path: &str,
aquila_token: String,
connect_timeout: Duration,
request_timeout: Duration,
) -> Self {
let reader = Reader::configure(definition_path.to_string(), true, vec![], None);
let modules = match reader.read_modules() {
Ok(modules) => modules,
Err(error) => {
log::error!("Error occurred while reading definitions: {:?}", error);
panic!("Error occurred while reading definitions")
}
};
let channel =
create_channel_with_retry("Aquila", aquila_url, connect_timeout, request_timeout).await;
Self {
modules,
channel,
aquila_token,
definition_source: None,
}
}
pub fn with_appendix(mut self, appendices: Vec<ModuleDefinitionAppendix>) -> Self {
append_definitions_to_matching_modules(&mut self.modules, appendices);
self
}
pub fn with_definition_source(mut self, source: String) -> Self {
self.definition_source = Some(source);
self
}
pub async fn send(&mut self) {
let _ = self.send_with_status().await;
}
pub async fn send_with_status(&mut self) -> bool {
self.update().await
}
async fn update(&mut self) -> bool {
if self.modules.is_empty() {
log::info!("No Modules are present, aborting update.");
return true;
}
let mut modules = self.modules.clone();
if let Some(source) = &self.definition_source {
modules = modules
.into_iter()
.map(|module| apply_definition_source_to_module(module, source.clone()))
.collect::<Vec<_>>();
}
log::info!("Updating {} Modules.", self.modules.len());
let mut client = ModuleServiceClient::new(self.channel.clone());
let request = Request::from_parts(
get_authorization_metadata(&self.aquila_token),
Extensions::new(),
ModuleUpdateRequest { modules },
);
match client.update(request).await {
Ok(response) => {
let res = response.into_inner();
match res.success {
true => log::info!("Module definition update has been successful"),
false => log::warn!("Module definition update has been unsuccessful"),
};
res.success
}
Err(err) => {
log::error!("Module definition update failed. Reason: {:?}", err);
false
}
}
}
}
fn append_definitions_to_matching_modules(
modules: &mut [Module],
appendices: Vec<ModuleDefinitionAppendix>,
) {
for appendix in appendices {
for module in modules.iter_mut() {
if module.identifier == appendix.module_identifier {
module.definitions.extend(appendix.definitions.clone());
}
}
}
}
fn apply_definition_source_to_module(mut module: Module, source: String) -> Module {
for data_type in &mut module.definition_data_types {
data_type.definition_source = source.clone();
}
for flow_type in &mut module.flow_types {
flow_type.definition_source = Some(source.clone());
}
for runtime_flow_type in &mut module.runtime_flow_types {
runtime_flow_type.definition_source = Some(source.clone());
}
for function in &mut module.function_definitions {
function.definition_source = source.clone();
}
for runtime_function in &mut module.runtime_function_definitions {
runtime_function.definition_source = source.clone();
}
module
}
#[cfg(test)]
mod tests {
use super::*;
fn module(identifier: &str) -> Module {
Module {
identifier: identifier.to_string(),
name: Vec::new(),
description: Vec::new(),
documentation: String::new(),
author: String::new(),
icon: String::new(),
version: String::new(),
flow_types: Vec::new(),
runtime_flow_types: Vec::new(),
function_definitions: Vec::new(),
runtime_function_definitions: Vec::new(),
definition_data_types: Vec::new(),
configurations: Vec::new(),
definitions: Vec::new(),
}
}
fn definition(identifier: &str) -> ModuleDefinition {
ModuleDefinition {
flow_type_identifier: vec![identifier.to_string()],
value: None,
}
}
#[test]
fn appends_appendix_definitions_to_all_modules_with_same_identifier() {
let mut modules = vec![module("shared"), module("other"), module("shared")];
let appendices = vec![ModuleDefinitionAppendix {
module_identifier: "shared".to_string(),
definitions: vec![definition("flow")],
}];
append_definitions_to_matching_modules(&mut modules, appendices);
assert_eq!(modules[0].definitions.len(), 1);
assert_eq!(modules[1].definitions.len(), 0);
assert_eq!(modules[2].definitions.len(), 1);
}
#[test]
fn appends_multiple_matching_appendices() {
let mut modules = vec![module("shared")];
let appendices = vec![
ModuleDefinitionAppendix {
module_identifier: "shared".to_string(),
definitions: vec![definition("flow-a")],
},
ModuleDefinitionAppendix {
module_identifier: "shared".to_string(),
definitions: vec![definition("flow-b")],
},
ModuleDefinitionAppendix {
module_identifier: "other".to_string(),
definitions: vec![definition("flow-c")],
},
];
append_definitions_to_matching_modules(&mut modules, appendices);
assert_eq!(modules[0].definitions.len(), 2);
assert_eq!(
modules[0].definitions[0].flow_type_identifier,
vec!["flow-a".to_string()]
);
assert_eq!(
modules[0].definitions[1].flow_type_identifier,
vec!["flow-b".to_string()]
);
}
}