scaleway-rs 0.2.4

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

pub struct ScalewayUpdateFlexibleIpBuilder {
    api: ScalewayApi,
    zone: String,
    ip_id: String,
    config: UpdateFlexibleIpConfig,
}

#[derive(Serialize, Debug)]
struct UpdateFlexibleIpConfig {
    #[serde(skip_serializing_if = "Option::is_none")]
    reverse: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    tags: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    server: Option<String>,
}

impl ScalewayUpdateFlexibleIpBuilder {
    pub fn new(api: ScalewayApi, zone: &str, ip_id: &str) -> Self {
        ScalewayUpdateFlexibleIpBuilder {
            api,
            zone: zone.to_string(),
            ip_id: ip_id.to_string(),
            config: UpdateFlexibleIpConfig {
                reverse: None,
                tags: None,
                server: None,
            },
        }
    }

    /// Set the reverse DNS for this IP.
    pub fn reverse(mut self, reverse: &str) -> Self {
        self.config.reverse = Some(reverse.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 this IP to. Pass an empty string to detach.
    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/{ip_id}",
            zone = self.zone,
            ip_id = self.ip_id
        );
        Ok(self
            .api
            .patch_json(&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/{ip_id}",
            zone = self.zone,
            ip_id = self.ip_id
        );
        Ok(self
            .api
            .patch_json_async(&url, self.config)
            .await?
            .json::<ScalewayFlexibleIpRoot>()
            .await?
            .ip)
    }
}