scaleway_rs/builder/
list_instance_builder.rs

1use crate::{
2    data::instance::{ScalewayInstance, ScalewayInstancesRoot},
3    ScalewayApi, ScalewayError,
4};
5
6pub struct ScalewayListInstanceBuilder {
7    api: ScalewayApi,
8    zone: String,
9    params: Vec<(&'static str, String)>,
10}
11
12impl ScalewayListInstanceBuilder {
13    pub fn new(api: ScalewayApi, zone: &str) -> Self {
14        ScalewayListInstanceBuilder {
15            api,
16            zone: zone.to_string(),
17            params: vec![],
18        }
19    }
20
21    /// A positive integer lower or equal to 100 to select the number of items to return.
22    pub fn per_page(mut self, count: u32) -> ScalewayListInstanceBuilder {
23        self.params.push(("per_page", count.to_string()));
24        self
25    }
26
27    /// A positive integer to choose the page to return.
28    pub fn page(mut self, count: u32) -> ScalewayListInstanceBuilder {
29        self.params.push(("page", count.to_string()));
30        self
31    }
32
33    /// List only Instances of this Organization ID.
34    pub fn organization(mut self, organization: &str) -> ScalewayListInstanceBuilder {
35        self.params.push(("organization", organization.to_string()));
36        self
37    }
38
39    /// List only Instances of this Project ID.
40    pub fn project(mut self, project: &str) -> ScalewayListInstanceBuilder {
41        self.params.push(("project", project.to_string()));
42        self
43    }
44
45    /// Filter Instances by name (eg. "server1" will return "server100" and "server1" but not "foo").
46    pub fn name(mut self, name: &str) -> ScalewayListInstanceBuilder {
47        self.params.push(("name", name.to_string()));
48        self
49    }
50
51    /// List Instances by private_ip. (IP address)
52    pub fn private_ip(mut self, private_ip: &str) -> ScalewayListInstanceBuilder {
53        self.params.push(("private_ip", private_ip.to_string()));
54        self
55    }
56
57    /// List Instances that are not attached to a public IP.
58    pub fn without_ip(mut self, without_ip: bool) -> ScalewayListInstanceBuilder {
59        self.params.push(("without_ip", without_ip.to_string()));
60        self
61    }
62
63    /// List Instances of this commercial type.
64    pub fn commercial_type(mut self, commercial_type: &str) -> ScalewayListInstanceBuilder {
65        self.params
66            .push(("commercial_type", commercial_type.to_string()));
67        self
68    }
69
70    /// List Instances in this state.
71    pub fn state(mut self, state: &str) -> ScalewayListInstanceBuilder {
72        self.params.push(("state", state.to_string()));
73        self
74    }
75
76    /// List Instances with these exact tags (to filter with several tags, use commas to separate them).
77    pub fn tags(mut self, tags: &str) -> ScalewayListInstanceBuilder {
78        self.params.push(("tags", tags.to_string()));
79        self
80    }
81
82    /// List Instances in this Private Network.
83    pub fn private_network(mut self, private_network: &str) -> ScalewayListInstanceBuilder {
84        self.params
85            .push(("private_network", private_network.to_string()));
86        self
87    }
88
89    /// Define the order of the returned servers.
90    /// Default: creation_date_desc
91    /// Possible values: creation_date_desc, creation_date_asc, modification_date_desc, modification_date_asc
92    pub fn order(mut self, order: &str) -> ScalewayListInstanceBuilder {
93        self.params.push(("order", order.to_string()));
94        self
95    }
96
97    /// List Instances from the given Private Networks (use commas to separate them).
98    pub fn private_networks(mut self, private_networks: &str) -> ScalewayListInstanceBuilder {
99        self.params
100            .push(("private_networks", private_networks.to_string()));
101        self
102    }
103
104    /// List Instances associated with the given private NIC MAC address.
105    pub fn private_nic_mac_address(
106        mut self,
107        private_nic_mac_address: &str,
108    ) -> ScalewayListInstanceBuilder {
109        self.params.push((
110            "private_nic_mac_address",
111            private_nic_mac_address.to_string(),
112        ));
113        self
114    }
115
116    /// List Instances from these server ids (use commas to separate them).
117    pub fn servers(mut self, servers: &str) -> ScalewayListInstanceBuilder {
118        self.params.push(("servers", servers.to_string()));
119        self
120    }
121
122    #[cfg(feature = "blocking")]
123    pub fn run(self) -> Result<Vec<ScalewayInstance>, ScalewayError> {
124        let mut list = vec![];
125        let mut page = 1;
126        loop {
127            let url = &format!(
128                "https://api.scaleway.com/instance/v1/zones/{zone}/servers",
129                zone = self.zone
130            );
131            let mut params = self.params.clone();
132            params.push(("page", page.to_string()));
133            let result = self
134                .api
135                .get(&url, params)?
136                .json::<ScalewayInstancesRoot>()?;
137            if result.servers.len() == 0 {
138                break;
139            }
140            list.extend(result.servers.into_iter());
141            page += 1;
142        }
143        Ok(list)
144    }
145
146    pub async fn run_async(self) -> Result<Vec<ScalewayInstance>, ScalewayError> {
147        let mut list = vec![];
148        let mut page = 1;
149        loop {
150            let url = &format!(
151                "https://api.scaleway.com/instance/v1/zones/{zone}/servers",
152                zone = self.zone
153            );
154            let mut params = self.params.clone();
155            params.push(("page", page.to_string()));
156            let result = self
157                .api
158                .get_async(&url, params)
159                .await?
160                .json::<ScalewayInstancesRoot>()
161                .await?;
162            if result.servers.len() == 0 {
163                break;
164            }
165            list.extend(result.servers.into_iter());
166            page += 1;
167        }
168        Ok(list)
169    }
170}