tfparser_core/ir/
environment.rs1use std::{path::Path, sync::Arc};
12
13use serde::{Deserialize, Serialize};
14use typed_builder::TypedBuilder;
15
16use crate::ir::{AccountId, Map, Region};
17
18#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TypedBuilder)]
20#[non_exhaustive]
21#[serde(rename_all = "camelCase")]
22#[builder(field_defaults(setter(into)))]
23pub struct Environment {
24 pub name: Arc<str>,
26
27 #[builder(default)]
29 pub aws_account_id: Option<AccountId>,
30
31 #[builder(default)]
33 pub aws_region: Option<Region>,
34
35 #[builder(default)]
38 pub aws_profile: Option<Arc<str>>,
39
40 #[serde(with = "crate::ir::path_serde::arc_path")]
43 pub source_file: Arc<Path>,
44
45 #[builder(default)]
48 pub locals: Map,
49}
50
51#[cfg(test)]
52#[allow(clippy::unwrap_used)]
53mod tests {
54 use std::path::PathBuf;
55
56 use super::*;
57 use crate::ir::{AccountId, Region};
58
59 #[test]
60 fn test_should_build_minimal_environment() {
61 let env = Environment::builder()
62 .name(Arc::<str>::from("staging"))
63 .source_file(Arc::<Path>::from(PathBuf::from(
64 "terraform/environments/staging.terragrunt.hcl",
65 )))
66 .build();
67 assert_eq!(&*env.name, "staging");
68 assert!(env.locals.is_empty());
69 assert!(env.aws_account_id.is_none());
70 }
71
72 #[test]
73 fn test_should_carry_validated_aws_metadata() {
74 let env = Environment::builder()
75 .name(Arc::<str>::from("production"))
76 .aws_account_id(Some(AccountId::new("100000000001").unwrap()))
77 .aws_region(Some(Region::new("us-west-2").unwrap()))
78 .aws_profile(Some(Arc::<str>::from("primary")))
79 .source_file(Arc::<Path>::from(PathBuf::from("a.hcl")))
80 .build();
81 assert_eq!(
82 env.aws_account_id.as_ref().map(AccountId::as_str),
83 Some("100000000001")
84 );
85 assert_eq!(
86 env.aws_region.as_ref().map(Region::as_str),
87 Some("us-west-2")
88 );
89 }
90
91 #[test]
92 fn test_should_serde_round_trip_environment() {
93 let env = Environment::builder()
94 .name(Arc::<str>::from("staging"))
95 .aws_account_id(Some(AccountId::new("100000000001").unwrap()))
96 .aws_region(Some(Region::new("us-west-2").unwrap()))
97 .source_file(Arc::<Path>::from(PathBuf::from("a.hcl")))
98 .build();
99 let json = serde_json::to_string(&env).unwrap();
100 let back: Environment = serde_json::from_str(&json).unwrap();
101 assert_eq!(env, back);
102 }
103}