scaleway-rs 0.2.8

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

/// Builder for updating a flexible IP address.
///
/// Created by [`ScalewayApi::update_flexible_ip`](crate::ScalewayApi::update_flexible_ip).
/// Call [`run`](Self::run) or [`run_async`](Self::run_async) to execute.
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
    }

    /// Applies the update and returns the modified flexible IP (blocking).
    ///
    /// See [`run_async`](Self::run_async) for the async version.
    #[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)
    }

    /// Applies the update and returns the modified flexible IP.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), scaleway_rs::ScalewayError> {
    /// let api = scaleway_rs::ScalewayApi::new("my-secret-token");
    /// let ip = api
    ///     .update_flexible_ip("fr-par-1", "ip-uuid")
    ///     .reverse("my-server.example.com")
    ///     .tags(vec!["prod".to_string()])
    ///     .run_async()
    ///     .await?;
    /// println!("Updated IP: {}", ip.address);
    /// # Ok(())
    /// # }
    /// ```
    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)
    }
}