scaleway-rs 0.2.4

A pure Rust scaleway API binding.
Documentation
use crate::{
    FlexibleIpType, ScalewayApi, ScalewayError, ScalewayFlexibleIp,
    data::flexible_ip::ScalewayFlexibleIpRoot,
};
use serde::Serialize;

pub struct ScalewayCreateFlexibleIpBuilder {
    api: ScalewayApi,
    zone: String,
    config: CreateFlexibleIpConfig,
}

#[derive(Serialize, Debug)]
struct CreateFlexibleIpConfig {
    #[serde(rename = "type")]
    ip_type: FlexibleIpType,
    #[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")]
    server: Option<String>,
}

impl ScalewayCreateFlexibleIpBuilder {
    pub fn new(api: ScalewayApi, zone: &str, ip_type: FlexibleIpType) -> Self {
        ScalewayCreateFlexibleIpBuilder {
            api,
            zone: zone.to_string(),
            config: CreateFlexibleIpConfig {
                ip_type,
                project: None,
                tags: None,
                server: None,
            },
        }
    }

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

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

    /// Server ID to attach the flexible IP to on creation.
    pub fn server(mut self, server_id: &str) -> Self {
        self.config.server = Some(server_id.to_string());
        self
    }

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

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