scaleway-rs 0.2.7

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

/// Builder for listing security groups in a zone.
///
/// Created by [`ScalewayApi::list_security_groups`](crate::ScalewayApi::list_security_groups).
/// Auto-paginates. Call [`run`](Self::run) or [`run_async`](Self::run_async) to execute.
pub struct ScalewayListSecurityGroupsBuilder {
    api: ScalewayApi,
    zone: String,
    params: Vec<(&'static str, String)>,
}

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

    /// Number of results per page (max 100).
    pub fn per_page(mut self, per_page: u32) -> Self {
        self.params.push(("per_page", per_page.to_string()));
        self
    }

    /// Filters by organization UUID.
    pub fn organization(mut self, organization: &str) -> Self {
        self.params.push(("organization", organization.to_string()));
        self
    }

    /// Filters by project UUID.
    pub fn project(mut self, project: &str) -> Self {
        self.params.push(("project", project.to_string()));
        self
    }

    /// Filters by name (substring match).
    pub fn name(mut self, name: &str) -> Self {
        self.params.push(("name", name.to_string()));
        self
    }

    /// Filters by tags (comma-separated list of exact tags).
    pub fn tags(mut self, tags: &str) -> Self {
        self.params.push(("tags", tags.to_string()));
        self
    }

    /// Fetches all matching security groups, auto-paginating (blocking).
    #[cfg(feature = "blocking")]
    pub fn run(self) -> Result<Vec<ScalewaySecurityGroupDetails>, ScalewayError> {
        let mut list = vec![];
        let mut page = 1;
        loop {
            let mut params = self.params.clone();
            params.push(("page", page.to_string()));
            let result = self
                .api
                .get(
                    &format!(
                        "https://api.scaleway.com/instance/v1/zones/{zone}/security_groups",
                        zone = self.zone
                    ),
                    params,
                )?
                .json::<ScalewaySecurityGroupDetailsListRoot>()?;
            if result.security_groups.is_empty() {
                break;
            }
            list.extend(result.security_groups.into_iter());
            page += 1;
        }
        Ok(list)
    }

    /// Fetches all matching security groups, auto-paginating.
    pub async fn run_async(self) -> Result<Vec<ScalewaySecurityGroupDetails>, ScalewayError> {
        let mut list = vec![];
        let mut page = 1;
        loop {
            let mut params = self.params.clone();
            params.push(("page", page.to_string()));
            let result = self
                .api
                .get_async(
                    &format!(
                        "https://api.scaleway.com/instance/v1/zones/{zone}/security_groups",
                        zone = self.zone
                    ),
                    params,
                )
                .await?
                .json::<ScalewaySecurityGroupDetailsListRoot>()
                .await?;
            if result.security_groups.is_empty() {
                break;
            }
            list.extend(result.security_groups.into_iter());
            page += 1;
        }
        Ok(list)
    }
}