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

mod deprecated;

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)
    }

    #[must_use]
    pub fn region(mut self, region: impl ToString) -> Self {
        self.spec.region = Some(region.to_string());
        self
    }

    #[must_use]
    pub fn sshkey(mut self, sshkey: impl ToString) -> Self {
        self.spec.ssh_key_name = Some(sshkey.to_string());
        self
    }

    #[must_use]
    pub fn vpc_cidr_block(mut self, cidr_block: impl ToString) -> Self {
        let cidr_block = cidr_block.to_string();
        let mut network = self.spec.network.unwrap_or_default();
        let mut vpc = network.vpc.unwrap_or_default();
        vpc.cidr_block = Some(cidr_block);
        network.vpc = Some(vpc);
        self.spec.network = Some(network);

        // let vpc_cidr_block = vpc_cidr_block.to_string();
        // let network = self.spec.network.unwrap_or_default();
        // let vpc = network.vpc.unwrap_or_default();
        // let vpc = VPCSpec {
        //     cidr_block: Some(vpc_cidr_block),
        //     ..vpc
        // };
        // let network = NetworkSpec {
        //     vpc: Some(vpc),
        //     ..network
        // };

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

    pub fn add_subnet(&mut self, availability_zone: &str, cidr_block: &str, is_public: bool) {
        let network = self.spec.network.take().unwrap_or_default();
        let network::Subnets(mut subnets) = network.subnets.unwrap_or_default();
        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.network = Some(network);
    }

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

    pub fn bastion(self, enabled: bool) -> Self {
        Self {
            spec: AWSClusterSpec {
                bastion: Some(Bastion {
                    enabled: Some(enabled),
                    allowed_cidr_blocks: vec!["0.0.0.0/0".to_string()],
                    ..Bastion::default()
                }),
                ..self.spec
            },
            ..self
        }
    }
}