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
use super::*;

impl AWSClusterSpec {
    pub fn region(&self) -> Option<&str> {
        self.region.as_deref()
    }

    pub fn network(&self) -> Option<&NetworkSpec> {
        self.network.as_ref()
    }

    pub fn vpc(&self) -> Option<&VPCSpec> {
        self.network().and_then(|network| network.vpc.as_ref())
    }

    pub fn vpc_id(&self) -> Option<&str> {
        self.vpc().and_then(|vpc| vpc.id.as_deref())
    }
}

impl AWSClusterStatus {
    pub fn ready(&self) -> bool {
        self.ready
    }
    pub fn network(&self) -> Option<&NetworkStatus> {
        self.network_status.as_ref()
    }
}

impl AWSCluster {
    pub fn with_name(name: &str) -> Self {
        let spec = AWSClusterSpec::default();
        Self::new(name, spec)
    }

    pub fn set_region(self, region: &str) -> Self {
        Self {
            spec: AWSClusterSpec {
                region: Some(region.to_string()),
                ..self.spec
            },
            ..self
        }
    }

    pub fn set_sshkey(self, sshkey: &str) -> Self {
        Self {
            spec: AWSClusterSpec {
                ssh_key_name: Some(sshkey.to_string()),
                ..self.spec
            },
            ..self
        }
    }

    pub fn set_vpc_cidr_block(self, vpc_cidr_block: &str) -> Self {
        let network = self.spec.network.unwrap_or_default();
        let vpc = network.vpc.unwrap_or_default();
        let vpc = VPCSpec {
            cidr_block: Some(vpc_cidr_block.to_string()),
            ..vpc
        };
        let network = NetworkSpec {
            vpc: Some(vpc),
            ..network
        };

        Self {
            spec: AWSClusterSpec {
                network: Some(network),
                ..self.spec
            },
            ..self
        }
    }

    pub fn add_subnet(self, availability_zone: &str, cidr_block: &str, is_public: bool) -> Self {
        let network = self.spec.network.unwrap_or_default();
        let mut subnets = network.subnets.unwrap_or_default().0;
        subnets.push(network::SubnetSpec {
            availability_zone: Some(availability_zone.to_string()),
            cidr_block: Some(cidr_block.to_string()),
            is_public: Some(is_public),
            ..Default::default()
        });
        let network = NetworkSpec {
            subnets: Some(network::Subnets(subnets)),
            ..network
        };
        Self {
            spec: AWSClusterSpec {
                network: Some(network),
                ..self.spec
            },
            ..self
        }
    }
}