1use std::sync::atomic::Ordering;
2
3use crate::{Layer, Stack, COUNT};
4
5#[derive(Clone, Debug, Default, PartialEq, Eq)]
6pub struct Vpc<'a> {
7 pub name: &'a str,
8 pub max_azs: u32,
9}
10
11impl Stack for Vpc<'_> {
12 fn run(_me: &mut Layer<Self>) {}
13
14 fn setup(me: &mut Layer<Self>) {
15 let id = COUNT.fetch_add(1, Ordering::SeqCst);
16
17 let expr = format!(
18 r#"
19 if (stacks['{id}'] == null) {{
20 stacks['{id}'] = new ec2.Vpc(this, '{}', {{
21 maxAzs: {}
22 }});
23 }}
24
25 return stacks['{id}']
26 "#,
27 me.name, me.max_azs,
28 );
29 me.expr = Some(expr.clone());
30 }
31
32 fn initialize(me: &mut Layer<Self>) {
33 me.exprs
34 .borrow_mut()
35 .push(me.expr.as_ref().unwrap().clone());
36 }
37}
38
39#[derive(Clone)]
40pub struct Instance<'a> {
41 pub name: &'a str,
42 pub vpc: &'a Layer<Vpc<'a>>,
43}
44
45impl Stack for Instance<'_> {
46 fn run(_me: &mut Layer<Self>) {}
47
48 fn initialize(me: &mut Layer<Self>) {
49 let id = COUNT.fetch_add(1, Ordering::SeqCst);
50 let vpc = me.vpc.expr.as_ref().unwrap().clone();
51
52 me.parent_exprs.borrow_mut().push(format!(
53 r#"
54 if (stacks['{id}'] == null) {{
55 stacks['{id}'] = new ec2.Instance(this, '{}', {{
56 instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.MICRO),
57 machineImage: ec2.MachineImage.latestAmazonLinux2(),
58 vpc: (() => {{ {vpc} }})()
59 }});
60 }}
61
62 return stacks['{id}']
63 "#,
64 me.name,
65 ));
66 }
67}