scaleway-rs 0.2.8

A pure Rust scaleway API binding.
Documentation
use crate::ScalewayApi;
use crate::ScalewayError;
use crate::data::security_group::{ScalewaySecurityGroupRule, ScalewaySecurityGroupRulesRoot};

/// Builder for listing rules in a security group.
///
/// Created by [`ScalewayApi::list_security_group_rules`](crate::ScalewayApi::list_security_group_rules).
/// Call [`run`](Self::run) or [`run_async`](Self::run_async) to execute.
///
/// **Note:** The API always returns the full rule list regardless of pagination parameters.
pub struct ScalewayListSecurityGroupRulesBuilder {
    api: ScalewayApi,
    zone: String,
    security_group_id: String,
    params: Vec<(&'static str, String)>,
}

impl ScalewayListSecurityGroupRulesBuilder {
    pub fn new(api: ScalewayApi, zone: &str, security_group_id: &str) -> Self {
        ScalewayListSecurityGroupRulesBuilder {
            api,
            zone: zone.to_string(),
            security_group_id: security_group_id.to_string(),
            params: vec![],
        }
    }

    /// Requests a specific page size (note: the API currently ignores this for rules).
    pub fn per_page(mut self, per_page: u32) -> Self {
        self.params.push(("per_page", per_page.to_string()));
        self
    }

    /// Fetches all rules in the security group (blocking).
    #[cfg(feature = "blocking")]
    pub fn run(self) -> Result<Vec<ScalewaySecurityGroupRule>, ScalewayError> {
        // it seems paging does not work for security group rules
        // no matter what i give for "page" i always get the full list
        let result = self.api.get(
            &format!(
                "https://api.scaleway.com/instance/v1/zones/{zone}/security_groups/{sg_id}/rules",
                zone = self.zone,
                sg_id = self.security_group_id
            ),
            self.params,
        )?;
        let result = result.json::<ScalewaySecurityGroupRulesRoot>()?;
        Ok(result.rules)
    }

    /// Fetches all rules in the security group.
    pub async fn run_async(self) -> Result<Vec<ScalewaySecurityGroupRule>, ScalewayError> {
        // it seems paging does not work for security group rules
        // no matter what i give for "page" i always get the full list
        let result = self
            .api
            .get_async(
                &format!(
                    "https://api.scaleway.com/instance/v1/zones/{zone}/security_groups/{sg_id}/rules",
                    zone = self.zone,
                    sg_id = self.security_group_id
                ),
                self.params,
            )
            .await?;
        let result = result.json::<ScalewaySecurityGroupRulesRoot>().await?;
        Ok(result.rules)
    }
}