-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement config.json migration mechanism
Signed-off-by: Christina Ying Wang <[email protected]>
- Loading branch information
Showing
4 changed files
with
327 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ mod generate; | |
mod join; | ||
mod leave; | ||
mod logger; | ||
mod migrate; | ||
mod random; | ||
mod remote; | ||
mod schema; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Config.json migration module | ||
// | ||
// Provides methods for migrating config.json fields based on remote directives | ||
// from /os/vX/config. Limits migrated fields based on os-config.json schema | ||
// whitelist. | ||
|
||
use crate::config_json::ConfigMap; | ||
use crate::remote::ConfigMigrationInstructions; | ||
use crate::schema::OsConfigSchema; | ||
use anyhow::Result; | ||
|
||
pub fn generate_config_json_migration( | ||
schema: &OsConfigSchema, | ||
migration_config: &ConfigMigrationInstructions, | ||
config_json: &ConfigMap, | ||
) -> Result<ConfigMap> { | ||
info!("Checking for config.json migrations..."); | ||
|
||
let mut new_config = config_json | ||
.iter() | ||
.map(|(k, v)| (k.clone(), v.clone())) | ||
.collect::<ConfigMap>(); | ||
|
||
let keys_to_update = migration_config.update.keys().cloned().collect::<Vec<_>>(); | ||
|
||
for key in keys_to_update.iter().chain(migration_config.delete.iter()) { | ||
if !schema.config.whitelist.contains(key) { | ||
info!("Key `{}` not in whitelist, skipping", key); | ||
continue; | ||
} | ||
|
||
// Key is marked for deletion | ||
if migration_config.delete.contains(key) { | ||
if config_json.contains_key(key) { | ||
info!("Key `{}` found, will delete", key); | ||
new_config.remove(key); | ||
} else { | ||
info!("Key `{}` not found for deletion, skipping", key); | ||
} | ||
} else if migration_config.update.contains_key(key) { | ||
// Key is marked for update | ||
// If key is in both delete & update, delete takes precedence. | ||
if let Some(future) = migration_config.update.get(key) { | ||
if !config_json.contains_key(key) { | ||
info!("Key `{}` not found, will insert", key); | ||
new_config.insert(key.to_string(), future.clone()); | ||
} else if let Some(current) = config_json.get(key) { | ||
if current != future { | ||
info!( | ||
"Key `{}` found with current value `{}`, will update to `{}`", | ||
key, current, future | ||
); | ||
new_config.insert(key.to_string(), future.clone()); | ||
} else { | ||
info!( | ||
"Key `{}` found with current value `{}` equal to update value `{}`, skipping", | ||
key, current, future | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(new_config) | ||
} | ||
|
||
mod tests { | ||
#[test] | ||
fn test_generate_config_json_migration() { | ||
let config_json = format!( | ||
r#" | ||
{{ | ||
"deadbeef": 1, | ||
"deadca1f": "2", | ||
"deadca2f": true, | ||
"deadca3f": "string1", | ||
"deadca5f": "to_delete", | ||
"on_both_lists": "on_both_lists" | ||
}} | ||
"# | ||
); | ||
|
||
let schema = format!( | ||
r#" | ||
{{ | ||
"services": [ | ||
], | ||
"config": {{ | ||
"whitelist": [ | ||
"deadbeef", | ||
"deadca1f", | ||
"deadca2f", | ||
"deadca3f", | ||
"deadca4f", | ||
"deadca5f", | ||
"on_both_lists" | ||
], | ||
"leave": [] | ||
}} | ||
}} | ||
"# | ||
); | ||
|
||
let configuration = unindent::unindent( | ||
r#" | ||
{ | ||
"update": { | ||
"deadbeef": 2, | ||
"deadca1f": "3", | ||
"deadca2f": false, | ||
"deadca3f": "string0", | ||
"deadca4f": "new_field", | ||
"not_on_whitelist1": "not_on_whitelist", | ||
"on_both_lists": "on_both_lists2" | ||
}, | ||
"delete": ["deadca5f", "not_on_whitelist2", "on_both_lists"] | ||
} | ||
"#, | ||
); | ||
|
||
let old_config = serde_json::from_str::<super::ConfigMap>(&config_json).unwrap(); | ||
|
||
let new_config = super::generate_config_json_migration( | ||
&serde_json::from_str(&schema).unwrap(), | ||
&serde_json::from_str(&configuration).unwrap(), | ||
&old_config, | ||
) | ||
.unwrap(); | ||
|
||
assert_eq!(new_config.get("deadbeef").unwrap(), 2); | ||
assert_eq!(new_config.get("deadca1f").unwrap(), "3"); | ||
assert_eq!(new_config.get("deadca2f").unwrap(), false); | ||
assert_eq!(new_config.get("deadca3f").unwrap(), "string0"); | ||
assert_eq!(new_config.get("deadca4f").unwrap(), "new_field"); | ||
assert!(new_config.get("deadca5f").is_none()); | ||
assert!(new_config.get("not_on_whitelist1").is_none()); | ||
assert!(new_config.get("not_on_whitelist2").is_none()); | ||
assert!(new_config.get("on_both_lists").is_none()); | ||
} | ||
} |
Oops, something went wrong.