scaleway-rs 0.2.8

A pure Rust scaleway API binding.
Documentation
use crate::{data::volume::ScalewayVolumeRoot, ScalewayApi, ScalewayError, ScalewayVolume};
use serde::Serialize;

/// Builder for updating a block storage volume.
///
/// Created by [`ScalewayApi::update_volume`](crate::ScalewayApi::update_volume).
/// Call [`run`](Self::run) or [`run_async`](Self::run_async) to execute.
pub struct ScalewayUpdateVolumeBuilder {
    api: ScalewayApi,
    zone: String,
    volume_id: String,
    config: UpdateVolumeConfig,
}

#[derive(Serialize, Debug)]
struct UpdateVolumeConfig {
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    size: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tags: Option<Vec<String>>,
}

impl ScalewayUpdateVolumeBuilder {
    pub fn new(api: ScalewayApi, zone: &str, volume_id: &str) -> Self {
        ScalewayUpdateVolumeBuilder {
            api,
            zone: zone.to_string(),
            volume_id: volume_id.to_string(),
            config: UpdateVolumeConfig {
                name: None,
                size: None,
                tags: None,
            },
        }
    }

    /// New name for the volume.
    pub fn name(mut self, name: &str) -> Self {
        self.config.name = Some(name.to_string());
        self
    }

    /// New size in bytes (can only be increased).
    pub fn size(mut self, size: u64) -> Self {
        self.config.size = Some(size);
        self
    }

    /// Tags to apply to the volume.
    pub fn tags(mut self, tags: Vec<String>) -> Self {
        self.config.tags = Some(tags);
        self
    }

    /// Applies the update and returns the modified volume (blocking).
    ///
    /// See [`run_async`](Self::run_async) for the async version.
    #[cfg(feature = "blocking")]
    pub fn run(self) -> Result<ScalewayVolume, ScalewayError> {
        let url = format!(
            "https://api.scaleway.com/instance/v1/zones/{zone}/volumes/{volume_id}",
            zone = self.zone,
            volume_id = self.volume_id
        );
        Ok(self
            .api
            .patch_json(&url, self.config)?
            .json::<ScalewayVolumeRoot>()?
            .volume)
    }

    /// Applies the update and returns the modified volume.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), scaleway_rs::ScalewayError> {
    /// let api = scaleway_rs::ScalewayApi::new("my-secret-token");
    /// // Rename a volume and grow it to 100 GiB
    /// let volume = api
    ///     .update_volume("fr-par-1", "volume-uuid")
    ///     .name("data-vol-primary")
    ///     .size(100 * 1024 * 1024 * 1024)
    ///     .run_async()
    ///     .await?;
    /// println!("Volume size: {} bytes", volume.size);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn run_async(self) -> Result<ScalewayVolume, ScalewayError> {
        let url = format!(
            "https://api.scaleway.com/instance/v1/zones/{zone}/volumes/{volume_id}",
            zone = self.zone,
            volume_id = self.volume_id
        );
        Ok(self
            .api
            .patch_json_async(&url, self.config)
            .await?
            .json::<ScalewayVolumeRoot>()
            .await?
            .volume)
    }
}