Skip to content

Commit 20fa735

Browse files
feat: support templating other types in default
1 parent 5ba7128 commit 20fa735

4 files changed

Lines changed: 251 additions & 49 deletions

File tree

agent-control/src/agent_type/render.rs

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,20 @@ fn global_defaults_to_variables(global_defaults: HashMap<String, serde_yaml::Val
114114
global_defaults
115115
.into_iter()
116116
.map(|(key, value)| {
117-
// Convert the YAML value to a string representation
118-
let string_value = match value {
119-
serde_yaml::Value::String(s) => s,
120-
other => serde_yaml::to_string(&other)
121-
.unwrap_or_else(|_| String::new())
122-
.trim()
123-
.to_string(),
117+
// Create the appropriate Variable type based on the YAML value type
118+
let var = match value {
119+
serde_yaml::Value::String(s) => Variable::new_final_string_variable(s),
120+
serde_yaml::Value::Bool(b) => Variable::new_final_bool_variable(b),
121+
serde_yaml::Value::Number(n) => Variable::new_final_number_variable(n),
122+
serde_yaml::Value::Mapping(m) => {
123+
let map: HashMap<String, serde_yaml::Value> = m
124+
.into_iter()
125+
.filter_map(|(k, v)| k.as_str().map(|s| (s.to_string(), v)))
126+
.collect();
127+
Variable::new_final_mapping_variable(map)
128+
}
129+
other => Variable::new_final_yaml_variable(other),
124130
};
125-
let var = Variable::new_final_string_variable(string_value);
126131
(Namespace::Default.namespaced_name(&key), var)
127132
})
128133
.collect()
@@ -673,15 +678,22 @@ variables:
673678
description: "registry url"
674679
type: string
675680
required: false
676-
default: "${nr-default:registry_url}"
681+
default: "${nr-default:registry_url}/test-repository"
682+
enable_file_logging:
683+
description: "enable file logging"
684+
type: bool
685+
required: false
686+
default: ${nr-default:enable_file_logging}
677687
deployment:
678688
linux:
689+
enable_file_logging: ${nr-var:enable_file_logging}
679690
executables:
680691
- id: first
681692
path: /opt/first
682693
args:
683694
- "${nr-var:registry}"
684695
windows:
696+
enable_file_logging: ${nr-var:enable_file_logging}
685697
executables:
686698
- id: first
687699
path: /opt/first
@@ -693,10 +705,16 @@ deployment:
693705
let values = testing_values("");
694706
let attributes = testing_agent_attributes(&agent_id);
695707

696-
let global_defaults = HashMap::from([(
697-
"registry_url".to_string(),
698-
serde_yaml::to_value("random-registry-url").unwrap(),
699-
)]);
708+
let global_defaults = HashMap::from([
709+
(
710+
"registry_url".to_string(),
711+
serde_yaml::to_value("test-registry-url").unwrap(),
712+
),
713+
(
714+
"enable_file_logging".to_string(),
715+
serde_yaml::to_value(true).unwrap(),
716+
),
717+
]);
700718

701719
let renderer =
702720
TemplateRenderer::default().with_agent_control_variables(HashMap::new().into_iter());
@@ -711,14 +729,15 @@ deployment:
711729
)
712730
.unwrap();
713731
assert_eq!(
714-
rendered::Args(vec!("random-registry-url".to_string())),
715-
extract_runtime_by_environment(runtime_config)
732+
rendered::Args(vec!("test-registry-url/test-repository".to_string())),
733+
extract_runtime_by_environment(runtime_config.clone())
716734
.executables
717735
.first()
718736
.unwrap()
719737
.args
720738
.clone()
721739
);
740+
assert!(extract_runtime_by_environment(runtime_config.clone()).enable_file_logging);
722741
}
723742

724743
#[test]
@@ -736,15 +755,22 @@ variables:
736755
description: "registry url"
737756
type: string
738757
required: false
739-
default: "${nr-default:registry_url}"
758+
default: "${nr-default:registry_url}/test-repository"
759+
enable_file_logging:
760+
description: "enable file logging"
761+
type: bool
762+
required: false
763+
default: ${nr-default:enable_file_logging}
740764
deployment:
741765
linux:
766+
enable_file_logging: ${nr-var:enable_file_logging}
742767
executables:
743768
- id: first
744769
path: /opt/first
745770
args:
746771
- "${nr-var:registry}"
747772
windows:
773+
enable_file_logging: ${nr-var:enable_file_logging}
748774
executables:
749775
- id: first
750776
path: /opt/first
@@ -756,15 +782,27 @@ deployment:
756782
let values = testing_values("");
757783
let attributes = testing_agent_attributes(&agent_id);
758784

759-
let global_defaults = HashMap::from([(
760-
"registry_url".to_string(),
761-
serde_yaml::to_value("${nr-env:REGISTRY_URL}").unwrap(),
762-
)]);
785+
let global_defaults = HashMap::from([
786+
(
787+
"registry_url".to_string(),
788+
serde_yaml::to_value("${nr-env:REGISTRY_URL}").unwrap(),
789+
),
790+
(
791+
"enable_file_logging".to_string(),
792+
serde_yaml::to_value("${nr-env:ENABLE_FILE_LOGGING}").unwrap(),
793+
),
794+
]);
763795

764-
let secrets = HashMap::from([(
765-
Namespace::EnvironmentVariable.namespaced_name("REGISTRY_URL"),
766-
Variable::new_final_string_variable("random-registry-url".to_string()),
767-
)]);
796+
let secrets = HashMap::from([
797+
(
798+
Namespace::EnvironmentVariable.namespaced_name("REGISTRY_URL"),
799+
Variable::new_final_string_variable("test-registry-url".to_string()),
800+
),
801+
(
802+
Namespace::EnvironmentVariable.namespaced_name("ENABLE_FILE_LOGGING"),
803+
Variable::new_final_bool_variable(true),
804+
),
805+
]);
768806

769807
let renderer =
770808
TemplateRenderer::default().with_agent_control_variables(HashMap::new().into_iter());
@@ -779,14 +817,15 @@ deployment:
779817
)
780818
.unwrap();
781819
assert_eq!(
782-
rendered::Args(vec!("random-registry-url".to_string())),
783-
extract_runtime_by_environment(runtime_config)
820+
rendered::Args(vec!("test-registry-url/test-repository".to_string())),
821+
extract_runtime_by_environment(runtime_config.clone())
784822
.executables
785823
.first()
786824
.unwrap()
787825
.args
788826
.clone()
789827
);
828+
assert!(extract_runtime_by_environment(runtime_config.clone()).enable_file_logging);
790829
}
791830

792831
// Agent Type and Values definitions

agent-control/src/agent_type/variable.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,50 @@ impl Variable {
7070
}
7171
}
7272

73+
pub fn new_final_bool_variable(final_value: bool) -> Self {
74+
Self {
75+
description: String::new(),
76+
variable_type: VariableType::Bool(Fields {
77+
required: false,
78+
default: None,
79+
final_value: Some(final_value),
80+
}),
81+
}
82+
}
83+
84+
pub fn new_final_number_variable(final_value: serde_yaml::Number) -> Self {
85+
Self {
86+
description: String::new(),
87+
variable_type: VariableType::Number(Fields {
88+
required: false,
89+
default: None,
90+
final_value: Some(final_value),
91+
}),
92+
}
93+
}
94+
95+
pub fn new_final_mapping_variable(final_value: HashMap<String, serde_yaml::Value>) -> Self {
96+
Self {
97+
description: String::new(),
98+
variable_type: VariableType::MapStringYaml(Fields {
99+
required: false,
100+
default: None,
101+
final_value: Some(final_value),
102+
}),
103+
}
104+
}
105+
106+
pub fn new_final_yaml_variable(final_value: serde_yaml::Value) -> Self {
107+
Self {
108+
description: String::new(),
109+
variable_type: VariableType::Yaml(Fields {
110+
required: false,
111+
default: None,
112+
final_value: Some(final_value),
113+
}),
114+
}
115+
}
116+
73117
pub fn is_required(&self) -> bool {
74118
self.variable_type.is_required()
75119
}
@@ -164,6 +208,7 @@ mod tests {
164208

165209
#[test]
166210
fn variable_definition_tree_deserialize() {
211+
use super::fields::DefaultValue;
167212
let value = r#"
168213
foo:
169214
bar:
@@ -188,7 +233,7 @@ foo:
188233
variable_type: VariableTypeDefinition::String(StringFieldsDefinition {
189234
inner: FieldsDefinition {
190235
required: false,
191-
default: Some("a".to_string()),
236+
default: Some(DefaultValue::Value("a".to_string())),
192237
},
193238
variants: VariantsConfig {
194239
ac_config_field: Some("foo.bar.var_name".to_string()),

0 commit comments

Comments
 (0)