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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright 2018-2019 Dmitry Tantsur <divius.inside@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Block device mapping for the Compute API.

use super::super::common;
use super::super::session::Session;
use super::super::Result;

use serde::ser::{Serialize, SerializeStruct, Serializer};

protocol_enum! {
    #[doc = "A destination type for a block device."]
    enum BlockDeviceDestinationType {
        #[doc = "Local ephemeral device."]
        Local = "local",

        #[doc = "Attached remote volume."]
        Volume = "volume"
    }
}

/// A source of a block device.
#[derive(Clone, Debug)]
pub enum BlockDeviceSource {
    /// A device from an image.
    Image(common::ImageRef),

    /// A device from a volume.
    Volume(common::VolumeRef),

    /// A device from a snapshot.
    Snapshot(common::SnapshotRef),
}

impl BlockDeviceSource {
    #[inline]
    fn source_type(&self) -> &'static str {
        match self {
            BlockDeviceSource::Image(..) => "image",
            BlockDeviceSource::Volume(..) => "volume",
            BlockDeviceSource::Snapshot(..) => "snapshot",
        }
    }

    #[inline]
    fn uuid(&self) -> &str {
        match self {
            BlockDeviceSource::Image(image) => image.as_ref(),
            BlockDeviceSource::Volume(volume) => volume.as_ref(),
            BlockDeviceSource::Snapshot(snapshot) => snapshot.as_ref(),
        }
    }
}

impl common::IntoVerified for BlockDeviceSource {
    fn into_verified(self, session: &Session) -> Result<Self> {
        Ok(match self {
            BlockDeviceSource::Image(inner) => {
                BlockDeviceSource::Image(inner.into_verified(session)?)
            }
            BlockDeviceSource::Volume(inner) => {
                BlockDeviceSource::Volume(inner.into_verified(session)?)
            }
            BlockDeviceSource::Snapshot(inner) => {
                BlockDeviceSource::Snapshot(inner.into_verified(session)?)
            }
        })
    }
}

/// A block device to attach to a server.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct BlockDevice {
    /// Boot index of the device if it's intended to be bootable.
    ///
    /// # Note
    ///
    /// Not all backends support values other than `None` and `Some(0)`.
    pub boot_index: Option<u16>,

    /// Whether to delete the created volume on termination (defaults to `false`).
    pub delete_on_termination: bool,

    /// A type of the destination: local disk or persistent volume.
    pub destination_type: BlockDeviceDestinationType,

    /// Format of the target device if it needs to be formatted.
    pub guest_format: Option<String>,

    /// The size (in GiB) of the created volume (if any).
    ///
    /// # Note
    ///
    /// This is only mandatory when creating `source` is `None`.
    pub size_gib: Option<u32>,

    /// A source for this block device (if any).
    pub source: Option<BlockDeviceSource>,
}

impl BlockDevice {
    /// Create a block device from the specified source.
    pub fn new(
        source: BlockDeviceSource,
        destination_type: BlockDeviceDestinationType,
    ) -> BlockDevice {
        BlockDevice {
            boot_index: None,
            delete_on_termination: false,
            destination_type,
            guest_format: None,
            size_gib: None,
            source: Some(source),
        }
    }

    /// Create a swap device.
    pub fn swap(size_gib: u32) -> BlockDevice {
        BlockDevice {
            boot_index: None,
            delete_on_termination: false,
            destination_type: BlockDeviceDestinationType::Local,
            guest_format: Some("swap".into()),
            size_gib: Some(size_gib),
            source: None,
        }
    }

    /// Attach an image.
    ///
    /// This is used for the entry referring to the image that the instance is being booted with.
    /// Boot index 0 is used for it.
    ///
    /// Use `from_new_volume` to create a volume from any image.
    pub fn from_image<I>(image: I) -> BlockDevice
    where
        I: Into<common::ImageRef>,
    {
        BlockDevice {
            boot_index: Some(0),
            delete_on_termination: false,
            destination_type: BlockDeviceDestinationType::Local,
            guest_format: None,
            size_gib: None,
            source: Some(BlockDeviceSource::Image(image.into())),
        }
    }

    /// Attach a remote volume.
    ///
    /// The volume will be the first bootable device if `is_boot_device` is `true`.
    pub fn from_volume<V>(volume: V, is_boot_device: bool) -> BlockDevice
    where
        V: Into<common::VolumeRef>,
    {
        BlockDevice {
            boot_index: if is_boot_device { Some(0) } else { None },
            delete_on_termination: false,
            destination_type: BlockDeviceDestinationType::Volume,
            guest_format: None,
            size_gib: None,
            source: Some(BlockDeviceSource::Volume(volume.into())),
        }
    }

    /// Create a new empty volume.
    pub fn from_empty_volume(size_gib: u32) -> BlockDevice {
        BlockDevice {
            boot_index: None,
            delete_on_termination: false,
            destination_type: BlockDeviceDestinationType::Volume,
            guest_format: None,
            size_gib: Some(size_gib),
            source: None,
        }
    }

    /// Create a volume from an image.
    ///
    /// The volume will be the first bootable device if `is_boot_device` is `true`.
    pub fn from_new_volume<I>(image: I, size_gib: u32, is_boot_device: bool) -> BlockDevice
    where
        I: Into<common::ImageRef>,
    {
        BlockDevice {
            boot_index: if is_boot_device { Some(0) } else { None },
            delete_on_termination: false,
            destination_type: BlockDeviceDestinationType::Volume,
            guest_format: None,
            size_gib: Some(size_gib),
            source: Some(BlockDeviceSource::Image(image.into())),
        }
    }

    #[inline]
    fn non_null_field_count(&self) -> usize {
        let mut count = 4;
        if self.source.is_some() {
            count += 1;
        }
        if self.guest_format.is_some() {
            count += 1;
        }
        if self.size_gib.is_some() {
            count += 1
        }
        count
    }
}

impl common::IntoVerified for BlockDevice {
    fn into_verified(self, session: &Session) -> Result<Self> {
        Ok(if let Some(source) = self.source {
            BlockDevice {
                source: Some(source.into_verified(session)?),
                ..self
            }
        } else {
            // No source - nothing to verify.
            self
        })
    }
}

impl common::IntoVerified for Vec<BlockDevice> {
    fn into_verified(self, session: &Session) -> Result<Self> {
        let mut result = Vec::with_capacity(self.len());
        for item in self {
            result.push(item.into_verified(session)?);
        }
        Ok(result)
    }
}

impl Serialize for BlockDevice {
    fn serialize<S>(&self, serializer: S) -> ::std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut bd = serializer.serialize_struct("BlockDevice", self.non_null_field_count())?;
        bd.serialize_field("boot_index", &self.boot_index)?;
        bd.serialize_field("delete_on_termination", &self.delete_on_termination)?;
        bd.serialize_field("destination_type", &self.destination_type)?;
        if let Some(ref guest_format) = self.guest_format {
            bd.serialize_field("guest_format", guest_format)?;
        }
        if let Some(ref source) = self.source {
            bd.serialize_field("source_type", source.source_type())?;
            bd.serialize_field("uuid", source.uuid())?;
        } else {
            bd.serialize_field("source_type", "blank")?;
        }
        if let Some(volume_size) = self.size_gib {
            bd.serialize_field("volume_size", &volume_size)?;
        }
        bd.end()
    }
}