Skip to content

Commit 95ba5ce

Browse files
committed
refactor: remove the possibility of duplicate mapping keys
1 parent 2a8e204 commit 95ba5ce

File tree

5 files changed

+123
-192
lines changed

5 files changed

+123
-192
lines changed

agent-control/src/bin/main_config_migrate.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use newrelic_agent_control::agent_control::config_repository::store::AgentControlConfigStore;
22
use newrelic_agent_control::config_migrate::cli::Cli;
33
use newrelic_agent_control::config_migrate::migration::agent_config_getter::AgentConfigGetter;
4-
use newrelic_agent_control::config_migrate::migration::config::MigrationConfig;
4+
use newrelic_agent_control::config_migrate::migration::config::{MappingType, MigrationConfig};
55
use newrelic_agent_control::config_migrate::migration::converter::ConfigConverter;
66
use newrelic_agent_control::config_migrate::migration::migrator::{ConfigMigrator, MigratorError};
77
use newrelic_agent_control::config_migrate::migration::persister::legacy_config_renamer::LegacyConfigRenamer;
@@ -35,11 +35,15 @@ fn main() -> Result<(), Box<dyn Error>> {
3535
debug!("Checking configurations for {}", cfg.agent_type_fqn);
3636
match config_migrator.migrate(&cfg) {
3737
Ok(_) => {
38-
for (_, dir_path) in cfg.dirs_map {
39-
legacy_config_renamer.rename_path(dir_path.path.as_path())?;
40-
}
41-
for (_, file_path) in cfg.files_map {
42-
legacy_config_renamer.rename_path(file_path.as_path())?;
38+
for (_, mapping_type) in cfg.filesystem_mappings {
39+
match mapping_type {
40+
MappingType::Dir(dir_path) => {
41+
legacy_config_renamer.rename_path(dir_path.dir_path.as_path())?
42+
}
43+
MappingType::File(file_path) => {
44+
legacy_config_renamer.rename_path(file_path.as_path())?
45+
}
46+
}
4347
}
4448
debug!("Classic config files and paths renamed");
4549
}

agent-control/src/config_migrate/migration/config.rs

Lines changed: 47 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,9 @@ impl Hash for AgentTypeFieldFQN {
8282
}
8383
}
8484

85-
pub struct FileMap {
86-
pub file_path: FilePath,
87-
pub agent_type_fqn: AgentTypeID,
88-
}
89-
90-
pub struct DirMap {
91-
pub file_path: FilePath,
92-
pub agent_type_fqn: AgentTypeID,
93-
}
94-
9585
#[derive(Clone, Debug, PartialEq, Deserialize)]
9686
pub struct DirInfo {
97-
pub path: FilePath,
87+
pub dir_path: FilePath,
9888
pub extensions: Vec<String>,
9989
}
10090

@@ -107,9 +97,6 @@ impl DirInfo {
10797
}
10898
}
10999

110-
pub type FilesMap = HashMap<AgentTypeFieldFQN, FilePath>;
111-
pub type DirsMap = HashMap<AgentTypeFieldFQN, DirInfo>;
112-
113100
#[derive(Debug, PartialEq, Clone, Deserialize)]
114101
pub struct MigrationConfig {
115102
pub configs: Vec<MigrationAgentConfig>,
@@ -148,36 +135,35 @@ impl MigrationConfig {
148135
pub struct MigrationAgentConfig {
149136
#[serde(deserialize_with = "AgentTypeID::deserialize_fqn")]
150137
pub agent_type_fqn: AgentTypeID,
151-
pub files_map: FilesMap,
152-
pub dirs_map: DirsMap,
138+
pub filesystem_mappings: HashMap<AgentTypeFieldFQN, MappingType>,
153139
pub next: Option<AgentTypeID>,
154140
}
155141

156-
impl MigrationAgentConfig {
157-
pub fn get_file(&self, fqn_to_check: AgentTypeFieldFQN) -> Option<FilePath> {
158-
for (fqn, path) in self.files_map.iter() {
159-
if *fqn == fqn_to_check {
160-
return Some(path.clone());
161-
}
162-
}
163-
None
164-
}
142+
#[derive(Debug, PartialEq, Clone, Deserialize)]
143+
#[serde(untagged)]
144+
pub enum MappingType {
145+
File(PathBuf),
146+
Dir(DirInfo),
147+
}
165148

166-
pub fn get_dir(&self, fqn_to_check: AgentTypeFieldFQN) -> Option<DirInfo> {
167-
for (fqn, dir_info) in self.dirs_map.iter() {
168-
if *fqn == fqn_to_check {
169-
return Some(dir_info.clone());
170-
}
171-
}
172-
None
149+
impl From<DirInfo> for MappingType {
150+
fn from(value: DirInfo) -> Self {
151+
MappingType::Dir(value)
152+
}
153+
}
154+
impl<P: Into<PathBuf>> From<P> for MappingType {
155+
fn from(value: P) -> Self {
156+
MappingType::File(value.into())
173157
}
174158
}
175159

176160
#[cfg(test)]
177161
mod tests {
178162

179163
use crate::agent_type::agent_type_id::AgentTypeID;
180-
use crate::config_migrate::migration::config::{DirInfo, FilePath, MigrationConfig};
164+
use crate::config_migrate::migration::config::{
165+
DirInfo, FilePath, MappingType, MigrationConfig,
166+
};
181167
use crate::config_migrate::migration::defaults::NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING;
182168

183169
#[test]
@@ -186,68 +172,60 @@ mod tests {
186172
configs:
187173
-
188174
agent_type_fqn: newrelic/com.newrelic.infrastructure:0.0.2
189-
files_map:
175+
filesystem_mappings:
190176
config_agent: /etc/newrelic-infra.yml
191-
dirs_map:
192177
config_ohis:
193-
path: /etc/newrelic-infra/integrations.d
178+
dir_path: /etc/newrelic-infra/integrations.d
194179
extensions:
195180
- "yaml"
196181
- "yml"
197182
logging:
198-
path: /etc/newrelic-infra/logging.d
183+
dir_path: /etc/newrelic-infra/logging.d
199184
extensions:
200185
- "yaml"
201186
- "yml"
202187
-
203188
agent_type_fqn: newrelic/com.newrelic.another:1.0.0
204-
files_map:
189+
filesystem_mappings:
205190
config_another: /etc/another.yml
206-
dirs_map:
207191
-
208192
agent_type_fqn: newrelic/com.newrelic.infrastructure:1.0.1
209-
files_map:
193+
filesystem_mappings:
210194
config_agent: /etc/newrelic-infra.yml
211-
dirs_map:
212195
config_integrations:
213-
path: /etc/newrelic-infra/integrations.d
196+
dir_path: /etc/newrelic-infra/integrations.d
214197
extensions:
215198
- "yaml"
216199
- "yml"
217-
218200
config_logging:
219-
path: /etc/newrelic-infra/logging.d
201+
dir_path: /etc/newrelic-infra/logging.d
220202
extensions:
221203
- "yaml"
222204
- "yml"
223205
224206
-
225207
agent_type_fqn: francisco-partners/com.newrelic.another:0.0.2
226-
files_map:
208+
filesystem_mappings:
227209
config_another: /etc/another.yml
228-
dirs_map:
229210
-
230211
agent_type_fqn: newrelic/com.newrelic.infrastructure:0.1.2
231-
files_map:
212+
filesystem_mappings:
232213
config_agent: /etc/newrelic-infra.yml
233-
dirs_map:
234214
config_integrations:
235-
path: /etc/newrelic-infra/integrations.d
215+
dir_path: /etc/newrelic-infra/integrations.d
236216
extensions:
237217
- "yaml"
238218
- "yml"
239-
240219
config_logging:
241-
path: /etc/newrelic-infra/logging.d
220+
dir_path: /etc/newrelic-infra/logging.d
242221
extensions:
243222
- "yaml"
244223
- "yml"
245224
246225
-
247226
agent_type_fqn: newrelic/com.newrelic.another:0.0.1
248-
files_map:
227+
filesystem_mappings:
249228
config_another: /etc/another.yml
250-
dirs_map:
251229
"#;
252230

253231
let expected_fqns_in_order = [
@@ -306,7 +284,7 @@ configs: []
306284
String::from("yml"),
307285
String::from("otro"),
308286
],
309-
path: FilePath::from("some/path"),
287+
dir_path: FilePath::from("some/path"),
310288
};
311289

312290
assert!(dir_info.valid_filename("something.yaml"));
@@ -322,13 +300,21 @@ configs: []
322300
let migration_config: MigrationConfig =
323301
MigrationConfig::parse(NEWRELIC_INFRA_AGENT_TYPE_CONFIG_MAPPING).unwrap();
324302

325-
for config in migration_config.configs {
326-
for dir_map in config.dirs_map {
327-
assert!(dir_map.1.valid_filename("something.yaml"));
328-
assert!(dir_map.1.valid_filename("something.yml"));
329-
assert!(!dir_map.1.valid_filename("something.yml.sample"));
330-
assert!(!dir_map.1.valid_filename("something.yaml.sample"));
331-
assert!(!dir_map.1.valid_filename("something.yoml"));
303+
for config in migration_config.configs.into_iter() {
304+
let dir_mappings =
305+
config
306+
.filesystem_mappings
307+
.into_iter()
308+
.filter_map(|(_, v)| match v {
309+
MappingType::Dir(dir) => Some(dir),
310+
_ => None,
311+
});
312+
for dir_map in dir_mappings {
313+
assert!(dir_map.valid_filename("something.yaml"));
314+
assert!(dir_map.valid_filename("something.yml"));
315+
assert!(!dir_map.valid_filename("something.yml.sample"));
316+
assert!(!dir_map.valid_filename("something.yaml.sample"));
317+
assert!(!dir_map.valid_filename("something.yoml"));
332318
}
333319
}
334320
}

0 commit comments

Comments
 (0)