statehub_api/v0/cluster/
impls.rs

1//
2// Copyright (c) 2021 RepliXio Ltd. All rights reserved.
3// Use is subject to license terms.
4//
5
6use std::str;
7
8use thiserror::Error;
9
10use super::*;
11
12impl Cluster {
13    pub fn all_locations(&self) -> Vec<Location> {
14        let aws = self.locations.aws.iter().map(|aws| aws.region.into());
15        let azure = self.locations.azure.iter().map(|azure| azure.region.into());
16        aws.chain(azure).collect()
17    }
18}
19
20impl fmt::Display for Cluster {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        f.debug_struct("Cluster").field("name", &self.name).finish()
23    }
24}
25
26impl From<String> for ClusterName {
27    fn from(name: String) -> Self {
28        Self(name)
29    }
30}
31
32impl From<&str> for ClusterName {
33    fn from(text: &str) -> Self {
34        text.to_string().into()
35    }
36}
37
38impl fmt::Display for ClusterName {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        self.0.fmt(f)
41    }
42}
43
44impl str::FromStr for ClusterName {
45    type Err = Infallible;
46
47    fn from_str(text: &str) -> Result<Self, Self::Err> {
48        Ok(text.into())
49    }
50}
51
52impl AsRef<str> for ClusterName {
53    fn as_ref(&self) -> &str {
54        self.0.as_ref()
55    }
56}
57
58impl ops::Deref for ClusterName {
59    type Target = str;
60
61    fn deref(&self) -> &Self::Target {
62        self.0.deref()
63    }
64}
65
66impl PartialEq<&str> for ClusterName {
67    fn eq(&self, other: &&str) -> bool {
68        self.0.eq(other)
69    }
70}
71
72impl PartialEq<String> for ClusterName {
73    fn eq(&self, other: &String) -> bool {
74        self.0.eq(other)
75    }
76}
77
78impl From<AwsRegion> for ClusterLocationAws {
79    fn from(region: AwsRegion) -> Self {
80        Self {
81            region,
82            account_principal: None,
83        }
84    }
85}
86
87impl From<AzureRegion> for ClusterLocationAzure {
88    fn from(region: AzureRegion) -> Self {
89        Self { region }
90    }
91}
92
93impl From<&[Location]> for ClusterLocations {
94    fn from(locations: &[Location]) -> Self {
95        let mut aws = vec![];
96        let mut azure = vec![];
97        for location in locations {
98            match location {
99                Location::Aws(region) => aws.push((*region).into()),
100                Location::Azure(region) => azure.push((*region).into()),
101                // Location::Gcp(region) => gcp.push(region),
102            }
103        }
104        Self { aws, azure }
105    }
106}
107
108impl Provider {
109    pub(crate) fn as_str(&self) -> &'static str {
110        match self {
111            Self::Eks => "eks",
112            Self::Aks => "aks",
113            Self::Kops => "kops",
114            Self::Generic => "generic",
115        }
116    }
117}
118
119impl fmt::Display for Provider {
120    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121        let text = self.as_str();
122        if f.alternate() {
123            text.to_uppercase().fmt(f)
124        } else {
125            text.fmt(f)
126        }
127    }
128}
129
130#[derive(Debug, Error)]
131#[error(r#"Invalid K8s provider "{provider}"#)]
132pub struct InvalidProvider {
133    pub provider: String,
134}
135
136impl InvalidProvider {
137    pub(crate) fn new(provider: &str) -> Self {
138        let provider = provider.to_string();
139        Self { provider }
140    }
141}
142
143impl str::FromStr for Provider {
144    type Err = InvalidProvider;
145
146    fn from_str(s: &str) -> Result<Self, Self::Err> {
147        match s {
148            "eks" => Ok(Self::Eks),
149            "aks" => Ok(Self::Aks),
150            "kops" => Ok(Self::Kops),
151            "genetic" => Ok(Self::Generic),
152            other => Err(InvalidProvider::new(other)),
153        }
154    }
155}