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
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use super::super::node::schemas::Node;

/// Nodegroup represents a deserialized nodegroup body from an API response.
#[derive(Debug, Deserialize, Serialize)]
pub struct Nodegroup {
    /// Nodegroup identifier.
    pub id: String,

    /// Timestamp in UTC timezone of when the nodegroup has been created.
    pub created_at: DateTime<Utc>,

    /// Timestamp in UTC timezone of when the nodegroup has been updated.
    pub updated_at: Option<DateTime<Utc>>,

    /// Cluster identifier.
    pub cluster_id: String,

    /// OpenStack flavor identifier for all nodes in the nodegroup.
    pub flavor_id: String,

    /// Initial volume size in GB for each node.
    pub volume_gb: u32,

    /// Initial blockstorage volume type for each node.
    pub volume_type: String,

    /// Flag that represents if nodes use local volume.
    pub local_volume: bool,

    /// OpenStack availability zone for all nodes in the nodegroup.
    pub availability_zone: String,

    /// All nodes in the nodegroup.
    pub nodes: Vec<Node>,

    /// A map of user-defined Kubernetes labels for each node in the group.
    pub labels: HashMap<String, String>,
}

/// NodegroupRoot represents a root of a deserialized nodegroup.
#[derive(Debug, Deserialize, Serialize)]
pub struct NodegroupRoot {
    pub nodegroup: Nodegroup,
}

/// ListRoot represents a root of a list with deserialized nodegroups.
#[derive(Debug, Deserialize, Serialize)]
pub struct ListRoot {
    pub nodegroups: Vec<Nodegroup>,
}

/// Create options for a new nodegroup.
#[derive(Debug, Serialize)]
pub struct CreateOpts {
    count: u32,
    flavor_id: Option<String>,
    cpus: Option<u32>,
    ram_mb: Option<u32>,
    volume_gb: Option<u32>,
    volume_type: Option<String>,
    local_volume: bool,
    keypair_name: Option<String>,
    affinity_policy: Option<String>,
    availability_zone: String,
    labels: Option<HashMap<String, String>>,
}

impl CreateOpts {
    pub fn new(count: u32, local_volume: bool, availability_zone: &str) -> CreateOpts {
        CreateOpts {
            count,
            flavor_id: None,
            cpus: None,
            ram_mb: None,
            volume_gb: None,
            volume_type: None,
            local_volume,
            keypair_name: None,
            affinity_policy: None,
            availability_zone: String::from(availability_zone),
            labels: None,
        }
    }

    /// Add a reference to a pre-created flavor.
    /// It can be omitted in most cases.
    pub fn with_flavor_id(mut self, flavor_id: &str) -> CreateOpts {
        self.flavor_id = Some(String::from(flavor_id));
        self
    }

    /// Add a CPU count for each node.
    /// It can be omitted only in cases when flavor_id is set.
    pub fn with_cpus(mut self, cpus: u32) -> CreateOpts {
        self.cpus = Some(cpus);
        self
    }

    /// Add a RAM count in MB for each node.
    /// It can be omitted only in cases when flavor_id is set.
    pub fn with_ram_mb(mut self, ram_mb: u32) -> CreateOpts {
        self.ram_mb = Some(ram_mb);
        self
    }

    /// Add a volume size in GB for each node.
    /// It can be omitted only in cases when flavor_id is set and volume is local.
    pub fn with_volume_gb(mut self, volume_gb: u32) -> CreateOpts {
        self.volume_gb = Some(volume_gb);
        self
    }

    /// Add a blockstorage volume type for each node.
    /// It can be omitted only in cases when flavor_id is set and volume is local.
    pub fn with_volume_type(mut self, volume_type: &str) -> CreateOpts {
        self.volume_type = Some(String::from(volume_type));
        self
    }

    /// Add a name of the SSH key that will be added to all nodes.
    pub fn with_keypair_name(mut self, keypair_name: &str) -> CreateOpts {
        self.keypair_name = Some(String::from(keypair_name));
        self
    }

    /// Add an optional parameter to tune nodes affinity.
    pub fn with_affinity_policy(mut self, affinity_policy: &str) -> CreateOpts {
        self.affinity_policy = Some(String::from(affinity_policy));
        self
    }

    /// Add a map of user-defined Kubernetes labels for each node in the group.
    pub fn with_labels(mut self, labels: HashMap<String, String>) -> CreateOpts {
        self.labels = Some(labels);
        self
    }
}

/// CreateOptsRoot represents a root of nodegroup create options.
#[derive(Debug, Serialize)]
pub struct CreateOptsRoot<'a> {
    pub nodegroup: &'a CreateOpts,
}

/// Options for the nodegroup resize operation.
#[derive(Debug, Serialize)]
pub struct ResizeOpts {
    desired: u32,
}

impl ResizeOpts {
    pub fn new(desired: u32) -> ResizeOpts {
        ResizeOpts { desired }
    }
}

/// ResizeOptsRoot represents a root of nodegroup resize options.
#[derive(Debug, Serialize)]
pub struct ResizeOptsRoot<'a> {
    pub nodegroup: &'a ResizeOpts,
}

/// Options for the nodegroup update operation.
#[derive(Debug, Serialize)]
pub struct UpdateOpts {
    labels: Option<HashMap<String, String>>,
}

impl UpdateOpts {
    pub fn new() -> UpdateOpts {
        UpdateOpts { labels: None }
    }

    /// Update user-defined Kubernetes labels for each node in the group.
    pub fn with_labels(mut self, labels: HashMap<String, String>) -> UpdateOpts {
        self.labels = Some(labels);
        self
    }
}

impl Default for UpdateOpts {
    fn default() -> Self {
        UpdateOpts::new()
    }
}

/// UpdateOptsRoot represents a root of nodegroup update options.
#[derive(Debug, Serialize)]
pub struct UpdateOptsRoot<'a> {
    pub nodegroup: &'a UpdateOpts,
}