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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//
// Copyright (c) 2021 RepliXio Ltd. All rights reserved.
// Use is subject to license terms.
//

use std::cmp;
use std::iter;

use super::*;

impl fmt::Display for State {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            f.debug_struct("State")
                .field("name", &self.name.0)
                .field("aws", &self.locations.aws)
                .field("azure", &self.locations.azure)
                .finish()
        } else {
            self.name.fmt(f)
        }
    }
}

impl From<String> for StateName {
    fn from(name: String) -> Self {
        Self(name)
    }
}

impl From<&str> for StateName {
    fn from(text: &str) -> Self {
        text.to_string().into()
    }
}

impl fmt::Display for StateName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl str::FromStr for StateName {
    type Err = Infallible;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        Ok(text.into())
    }
}

impl AsRef<str> for StateName {
    fn as_ref(&self) -> &str {
        self.0.as_ref()
    }
}

impl ops::Deref for StateName {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.0.deref()
    }
}

impl PartialEq<&str> for StateName {
    fn eq(&self, other: &&str) -> bool {
        self.0.eq(other)
    }
}

impl State {
    #[cfg(test)]
    pub fn new(name: impl AsRef<str>) -> Self {
        let name = StateName::from(name.as_ref());
        Self {
            id: Uuid::default(),
            name,
            created: Utc::now(),
            modified: Utc::now(),
            storage_class: None,
            locations: StateLocations::default(),
            owner: None,
            provisioning_status: ProvisioningStatus::default(),
            allowed_clusters: None,
            condition: Condition::Green,
        }
    }

    pub fn is_available_in(&self, location: &Location) -> bool {
        self.locations.contains(location)
    }

    pub fn all_locations(&self) -> Vec<Location> {
        let aws = self.locations.aws.iter().map(|aws| aws.region.into());
        let azure = self.locations.azure.iter().map(|azure| azure.region.into());
        aws.chain(azure).collect()
    }

    pub fn collect_volumes(&self) -> HashMap<String, HashMap<Location, &VolumeLocation>> {
        let aws = self.locations.aws.iter().flat_map(|location| {
            location
                .volumes
                .iter()
                .map(move |volume| (volume, location.region.into()))
        });
        let azure = self.locations.azure.iter().flat_map(|location| {
            location
                .volumes
                .iter()
                .map(move |volume| (volume, location.region.into()))
        });

        let mut volumes = HashMap::<_, HashMap<_, _>>::new();
        for (volume, location) in aws.chain(azure) {
            volumes
                .entry(volume.name.clone())
                .or_default()
                .insert(location, volume);
        }
        volumes
    }

    pub fn count_volumes(&self) -> usize {
        let aws = self
            .locations
            .aws
            .iter()
            .map(|aws| aws.volumes.len())
            .max()
            .unwrap_or_default();
        let azure = self
            .locations
            .azure
            .iter()
            .map(|azure| azure.volumes.len())
            .max()
            .unwrap_or_default();
        cmp::max(aws, azure)
    }
}

impl StateLocationStatus {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Ok => "ok",
            Self::Provisioning => "provisioning",
            Self::Recovering => "recovering",
            Self::Deleting => "deleting",
            Self::Error => "error",
        }
    }

    pub fn is_deleting(&self) -> bool {
        *self == Self::Deleting
    }

    pub fn is_final(&self) -> bool {
        match self {
            Self::Ok => true,
            Self::Provisioning => false,
            Self::Recovering => false,
            Self::Deleting => false,
            Self::Error => true,
        }
    }
}

impl Default for VolumeBindingMode {
    fn default() -> Self {
        Self::WaitForFirstConsumer
    }
}

impl From<AwsRegion> for CreateStateLocationAwsDto {
    fn from(region: AwsRegion) -> Self {
        Self { region }
    }
}

impl From<AzureRegion> for CreateStateLocationAzureDto {
    fn from(region: AzureRegion) -> Self {
        Self { region }
    }
}

impl iter::FromIterator<Location> for CreateStateLocationsDto {
    fn from_iter<T>(iter: T) -> Self
    where
        T: IntoIterator<Item = Location>,
    {
        iter.into_iter().fold(Self::default(), |mut dto, location| {
            match location {
                Location::Aws(region) => dto.aws.push(region.into()),
                Location::Azure(region) => dto.azure.push(region.into()),
                // Location::Gcp(region) => dto.gcp.push(region),
            }
            dto
        })
    }
}

impl StateLocations {
    pub fn contains(&self, location: &Location) -> bool {
        match location {
            Location::Aws(region) => self.aws.iter().any(|aws| aws.region == *region),
            Location::Azure(region) => self.azure.iter().any(|azure| azure.region == *region),
        }
    }
}

impl Default for Condition {
    fn default() -> Self {
        Self::Green
    }
}

impl Default for ProvisioningStatus {
    fn default() -> Self {
        Self::Ready
    }
}

impl StorageClass {
    pub fn new_if_not_default(
        state: &str,
        storage_class: Option<String>,
        fs_type: Option<VolumeFileSystem>,
    ) -> Option<Self> {
        if storage_class.is_none() && fs_type.is_none() {
            return None;
        }

        let name = storage_class.unwrap_or_else(|| state.to_string());
        let fs_type = fs_type.unwrap_or_default().to_string();

        let storage_class = Self {
            name,
            fs_type,
            ..Self::default()
        };

        Some(storage_class)
    }
}