|
1 | 1 | #![allow(missing_docs)] |
2 | 2 |
|
3 | | -use std::{fmt, fs::remove_dir_all, path::PathBuf}; |
| 3 | +use std::{collections::HashMap, fmt, fs::remove_dir_all, path::PathBuf}; |
4 | 4 |
|
5 | 5 | use clap::Parser; |
6 | 6 | use colored::*; |
7 | 7 | use exitcode::ExitCode; |
| 8 | +use vector_vrl_metrics::MetricsStorage; |
8 | 9 |
|
9 | 10 | use crate::{ |
10 | | - config::{self, Config, ConfigDiff, loading::ConfigBuilderLoader}, |
| 11 | + config::{self, Config, ConfigDiff, TransformContext, loading::ConfigBuilderLoader}, |
| 12 | + schema::Definition, |
11 | 13 | topology::{ |
12 | 14 | self, |
13 | 15 | builder::{TopologyPieces, TopologyPiecesBuilder}, |
@@ -117,13 +119,13 @@ pub async fn validate(opts: &Opts, color: bool) -> ExitCode { |
117 | 119 | None => return exitcode::CONFIG, |
118 | 120 | }; |
119 | 121 |
|
120 | | - if !opts.no_environment { |
121 | | - if let Some(tmp_directory) = create_tmp_directory(&mut config, &mut fmt) { |
122 | | - validated &= validate_environment(opts, &config, &mut fmt).await; |
123 | | - remove_tmp_directory(tmp_directory); |
124 | | - } else { |
125 | | - validated = false; |
126 | | - } |
| 122 | + if opts.no_environment { |
| 123 | + validated &= validate_transforms_no_environment(&config, &mut fmt).await; |
| 124 | + } else if let Some(tmp_directory) = create_tmp_directory(&mut config, &mut fmt) { |
| 125 | + validated &= validate_environment(opts, &config, &mut fmt).await; |
| 126 | + remove_tmp_directory(tmp_directory); |
| 127 | + } else { |
| 128 | + validated = false; |
127 | 129 | } |
128 | 130 |
|
129 | 131 | if validated { |
@@ -180,6 +182,72 @@ pub fn validate_config(opts: &Opts, fmt: &mut Formatter) -> Option<Config> { |
180 | 182 | Some(config) |
181 | 183 | } |
182 | 184 |
|
| 185 | +async fn validate_transforms_no_environment(config: &Config, fmt: &mut Formatter) -> bool { |
| 186 | + let enrichment_tables = vector_lib::enrichment::TableRegistry::default(); |
| 187 | + let metrics_storage = MetricsStorage::default(); |
| 188 | + let mut definition_cache = HashMap::new(); |
| 189 | + let mut errors = Vec::new(); |
| 190 | + |
| 191 | + for (key, transform) in config.transforms() { |
| 192 | + let input_definitions = topology::schema::input_definitions( |
| 193 | + &transform.inputs, |
| 194 | + config, |
| 195 | + enrichment_tables.clone(), |
| 196 | + &mut definition_cache, |
| 197 | + ) |
| 198 | + .unwrap_or_default(); |
| 199 | + |
| 200 | + let merged_schema_definition = input_definitions |
| 201 | + .iter() |
| 202 | + .map(|(_, definition)| definition.clone()) |
| 203 | + .reduce(Definition::merge) |
| 204 | + .unwrap_or_else(Definition::any); |
| 205 | + |
| 206 | + let schema_definitions = transform |
| 207 | + .inner |
| 208 | + .outputs( |
| 209 | + &TransformContext { |
| 210 | + enrichment_tables: enrichment_tables.clone(), |
| 211 | + metrics_storage: metrics_storage.clone(), |
| 212 | + schema: config.schema, |
| 213 | + ..Default::default() |
| 214 | + }, |
| 215 | + &input_definitions, |
| 216 | + ) |
| 217 | + .into_iter() |
| 218 | + .map(|output| { |
| 219 | + let definitions = output.schema_definitions(config.schema.enabled); |
| 220 | + (output.port, definitions) |
| 221 | + }) |
| 222 | + .collect(); |
| 223 | + |
| 224 | + let context = TransformContext { |
| 225 | + key: Some(key.clone()), |
| 226 | + globals: config.global.clone(), |
| 227 | + enrichment_tables: enrichment_tables.clone(), |
| 228 | + metrics_storage: metrics_storage.clone(), |
| 229 | + schema_definitions, |
| 230 | + merged_schema_definition, |
| 231 | + schema: config.schema, |
| 232 | + skip_environment_checks: true, |
| 233 | + ..Default::default() |
| 234 | + }; |
| 235 | + |
| 236 | + if let Err(error) = transform.inner.build(&context).await { |
| 237 | + errors.push(format!("Transform \"{key}\": {error}")); |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + if errors.is_empty() { |
| 242 | + fmt.success("Transform configuration"); |
| 243 | + true |
| 244 | + } else { |
| 245 | + fmt.title("Transform errors"); |
| 246 | + fmt.sub_error(errors); |
| 247 | + false |
| 248 | + } |
| 249 | +} |
| 250 | + |
183 | 251 | async fn validate_environment(opts: &Opts, config: &Config, fmt: &mut Formatter) -> bool { |
184 | 252 | let diff = ConfigDiff::initial(config); |
185 | 253 |
|
|
0 commit comments