1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use super::*;

impl AWSMachineTemplate {
    pub fn with_type(name: &str, instance_type: &str) -> Self {
        let machine_spec = AWSMachineSpec::with_type(instance_type);
        let template = AWSMachineTemplateResource::new(machine_spec);
        let spec = AWSMachineTemplateSpec::new(template);
        Self::new(name, spec)
    }

    pub fn t3small(name: &str) -> Self {
        Self::with_type(name, "t3.small")
    }

    pub fn m5large(name: &str) -> Self {
        Self::with_type(name, "m5.large")
    }

    pub fn m5xlarge(name: &str) -> Self {
        Self::with_type(name, "m5.xlarge")
    }

    pub fn m5dxlarge(name: &str) -> Self {
        Self::with_type(name, "m5d.xlarge")
    }

    pub fn m5d4xlarge(name: &str) -> Self {
        Self::with_type(name, "m5d.4xlarge")
    }

    pub fn m5d16large(name: &str) -> Self {
        Self::with_type(name, "m5d.16xlarge")
    }

    pub fn m6gdxlarge(name: &str) -> Self {
        Self::with_type(name, "m6gd.xlarge")
    }

    pub fn m6gd4xlarge(name: &str) -> Self {
        Self::with_type(name, "m6gd.4xlarge")
    }

    pub fn m6ilarge(name: &str) -> Self {
        Self::with_type(name, "m6i.large")
    }

    pub fn m6ixlarge(name: &str) -> Self {
        Self::with_type(name, "m6i.xlarge")
    }

    #[must_use]
    pub fn ssh_key_name(mut self, name: &str) -> Self {
        self.spec.template.spec.ssh_key_name = Some(name.to_string());
        self
    }

    #[must_use]
    pub fn iam_instance_profile(mut self, iam_instance_profile: &str) -> Self {
        self.spec.template.spec.iam_instance_profile = Some(iam_instance_profile.to_string());
        self
    }

    #[must_use]
    pub fn public_ip(mut self, yes: bool) -> Self {
        self.spec.template.spec.public_ip = Some(yes);
        self
    }

    #[must_use]
    pub fn ami(mut self, id: &str) -> Self {
        let id = id.to_string();
        let mut ami = self.spec.template.spec.ami.unwrap_or_default();
        ami.id = Some(id);
        self.spec.template.spec.ami = Some(ami);
        self
    }

    #[must_use]
    pub fn root_volume(mut self, root_volume: Volume) -> Self {
        self.spec.template.spec.root_volume = Some(root_volume);
        self
    }

    #[must_use]
    pub fn root_volume_size(self, size: i64) -> Self {
        let root_volume = Volume::gp3(size);
        self.root_volume(root_volume)
    }

    #[must_use]
    pub fn non_root_volumes(mut self, volumes: impl IntoIterator<Item = Volume>) -> Self {
        self.spec.template.spec.non_root_volumes = volumes.into_iter().collect();
        self
    }

    #[must_use]
    pub fn namespace(self, namespace: &str) -> Self {
        Self {
            metadata: kube::core::ObjectMeta {
                namespace: Some(namespace.to_string()),
                ..self.metadata
            },
            ..self
        }
    }

    pub fn additional_security_group(self, arn: &str) -> Self {
        let mut additional_security_groups = self.spec.template.spec.additional_security_groups;
        additional_security_groups.push(types::AWSResourceReference {
            arn: Some(arn.to_string()),
            id: None,
            filters: vec![],
        });
        Self {
            spec: AWSMachineTemplateSpec {
                template: AWSMachineTemplateResource {
                    spec: AWSMachineSpec {
                        additional_security_groups,
                        ..self.spec.template.spec
                    },
                    ..self.spec.template
                },
            },
            ..self
        }
    }
}

impl AWSMachineTemplateSpec {
    fn new(template: AWSMachineTemplateResource) -> Self {
        Self { template }
    }
}

impl AWSMachineTemplateResource {
    fn new(spec: AWSMachineSpec) -> Self {
        Self {
            spec,
            ..Self::default()
        }
    }
}

impl AWSMachineSpec {
    fn with_type(instnce_type: &str) -> Self {
        let instance_type = instnce_type.to_string();
        Self {
            instance_type,
            ..Self::default()
            // provider_id: todo!(),
            // instance_id: todo!(),
            // image_lookup_format: todo!(),
            // image_lookup_org: todo!(),
            // image_lookup_base_os: todo!(),
            // instance_type: todo!(),
            // additional_tags: todo!(),
            // iam_instance_profile: todo!(),
            // public_ip: todo!(),
            // additional_security_groups: todo!(),
            // failure_domain: todo!(),
            // subnet: todo!(),
            // ssh_key_name: todo!(),
            // network_interfaces: todo!(),
            // uncompressed_user_data: todo!(),
            // tenancy: todo!(),
        }
    }

    pub fn ssh_key_name(self, ssh_key_name: impl ToString) -> Self {
        let ssh_key_name = ssh_key_name.to_string().into();
        Self {
            ssh_key_name,
            ..self
        }
    }
}