scaleway-rs 0.2.7

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

pub struct ScalewayCreateVolumeBuilder {
    api: ScalewayApi,
    zone: String,
    config: CreateVolumeConfig,
}

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

impl ScalewayCreateVolumeBuilder {
    pub fn new(api: ScalewayApi, zone: &str, name: &str, size: u64, volume_type: &str) -> Self {
        ScalewayCreateVolumeBuilder {
            api,
            zone: zone.to_string(),
            config: CreateVolumeConfig {
                name: name.to_string(),
                size,
                volume_type: volume_type.to_string(),
                project: None,
                tags: None,
                from_snapshot: None,
            },
        }
    }

    /// Project ID to associate the volume with.
    pub fn project(mut self, project_id: &str) -> Self {
        self.config.project = Some(project_id.to_string());
        self
    }

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

    /// Snapshot ID to create the volume from.
    pub fn from_snapshot(mut self, snapshot_id: &str) -> Self {
        self.config.from_snapshot = Some(snapshot_id.to_string());
        self
    }

    #[cfg(feature = "blocking")]
    pub fn run(self) -> Result<ScalewayVolume, ScalewayError> {
        let url = format!(
            "https://api.scaleway.com/instance/v1/zones/{zone}/volumes",
            zone = self.zone
        );
        Ok(self
            .api
            .post(&url, self.config)?
            .json::<ScalewayVolumeRoot>()?
            .volume)
    }

    pub async fn run_async(self) -> Result<ScalewayVolume, ScalewayError> {
        let url = format!(
            "https://api.scaleway.com/instance/v1/zones/{zone}/volumes",
            zone = self.zone
        );
        Ok(self
            .api
            .post_async(&url, self.config)
            .await?
            .json::<ScalewayVolumeRoot>()
            .await?
            .volume)
    }
}