diff --git a/sources/Cargo.lock b/sources/Cargo.lock index 826b31e5429..7a20ee712ae 100644 --- a/sources/Cargo.lock +++ b/sources/Cargo.lock @@ -1758,6 +1758,7 @@ dependencies = [ "libc", "serde", "serde_json", + "serde_plain", "toml", ] diff --git a/sources/models/Cargo.toml b/sources/models/Cargo.toml index ce679bbbc6d..3d8cfa2ab16 100644 --- a/sources/models/Cargo.toml +++ b/sources/models/Cargo.toml @@ -14,6 +14,7 @@ bottlerocket-release.workspace = true libc.workspace = true serde = { workspace = true, features = ["derive"] } serde_json.workspace = true +serde_plain.workspace = true toml.workspace = true # settings plugins diff --git a/sources/models/src/lib.rs b/sources/models/src/lib.rs index 8dd88bc4e78..7692545d47e 100644 --- a/sources/models/src/lib.rs +++ b/sources/models/src/lib.rs @@ -25,6 +25,7 @@ use bottlerocket_release::BottlerocketRelease; use bottlerocket_settings_models::model_derive::model; use bottlerocket_settings_plugin::BottlerocketSettings; use serde::{Deserialize, Serialize}; +use serde_plain::derive_fromstr_from_deserialize; use std::collections::HashMap; use bottlerocket_settings_models::modeled_types::SingleLineString; @@ -87,3 +88,55 @@ struct Report { name: String, description: String, } + +/// Strength represents whether the settings can be overridden by a setting generator or not. +#[derive(Deserialize, Serialize, Debug, Clone, Copy, PartialEq)] +#[serde(rename_all = "kebab-case")] +pub enum Strength { + Strong, + Weak, +} + +impl std::fmt::Display for Strength { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Strength::Strong => write!(f, "strong"), + Strength::Weak => write!(f, "weak"), + } + } +} + +derive_fromstr_from_deserialize!(Strength); + +/// Struct to hold the setting generator definition containing +/// command, strength, skip-if-populated +#[derive(Deserialize, Serialize, std::fmt::Debug, PartialEq)] +pub struct SettingsGenerator { + pub command: String, + pub strength: Strength, + #[serde(rename = "skip-if-populated")] + pub skip_if_populated: bool, +} + +impl SettingsGenerator { + pub fn is_weak(&self) -> bool { + self.strength == Strength::Weak + } + + pub fn with_command(command: String) -> Self { + SettingsGenerator { + command, + ..SettingsGenerator::default() + } + } +} + +impl Default for SettingsGenerator { + fn default() -> Self { + SettingsGenerator { + command: String::new(), + strength: Strength::Strong, + skip_if_populated: true, + } + } +}