1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use crate::{
    data::instance::{ScalewayInstance, ScalewayInstanceRoot},
    ScalewayApi, ScalewayError,
};

pub struct ScalewayListInstanceBuilder {
    api: ScalewayApi,
    params: Vec<(&'static str, String)>,
}

impl ScalewayListInstanceBuilder {
    pub fn new(api: ScalewayApi) -> Self {
        ScalewayListInstanceBuilder {
            api,
            params: vec![],
        }
    }

    /// A positive integer lower or equal to 100 to select the number of items to return.
    pub fn per_page(mut self, count: u32) -> ScalewayListInstanceBuilder {
        self.params.push(("per_page", count.to_string()));
        self
    }

    /// A positive integer to choose the page to return.
    pub fn page(mut self, count: u32) -> ScalewayListInstanceBuilder {
        self.params.push(("page", count.to_string()));
        self
    }

    /// List only Instances of this Organization ID.
    pub fn organization(mut self, organization: &str) -> ScalewayListInstanceBuilder {
        self.params.push(("organization", organization.to_string()));
        self
    }

    /// List only Instances of this Project ID.
    pub fn project(mut self, project: &str) -> ScalewayListInstanceBuilder {
        self.params.push(("project", project.to_string()));
        self
    }

    /// Filter Instances by name (eg. "server1" will return "server100" and "server1" but not "foo").
    pub fn name(mut self, name: &str) -> ScalewayListInstanceBuilder {
        self.params.push(("name", name.to_string()));
        self
    }

    /// List Instances by private_ip. (IP address)
    pub fn private_ip(mut self, private_ip: &str) -> ScalewayListInstanceBuilder {
        self.params.push(("private_ip", private_ip.to_string()));
        self
    }

    /// List Instances that are not attached to a public IP.
    pub fn without_ip(mut self, without_ip: bool) -> ScalewayListInstanceBuilder {
        self.params.push(("without_ip", without_ip.to_string()));
        self
    }

    /// List Instances of this commercial type.
    pub fn commercial_type(mut self, commercial_type: &str) -> ScalewayListInstanceBuilder {
        self.params
            .push(("commercial_type", commercial_type.to_string()));
        self
    }

    /// List Instances in this state.
    pub fn state(mut self, state: &str) -> ScalewayListInstanceBuilder {
        self.params.push(("state", state.to_string()));
        self
    }

    /// List Instances with these exact tags (to filter with several tags, use commas to separate them).
    pub fn tags(mut self, tags: &str) -> ScalewayListInstanceBuilder {
        self.params.push(("tags", tags.to_string()));
        self
    }

    /// List Instances in this Private Network.
    pub fn private_network(mut self, private_network: &str) -> ScalewayListInstanceBuilder {
        self.params
            .push(("private_network", private_network.to_string()));
        self
    }

    /// Define the order of the returned servers.
    /// Default: creation_date_desc
    /// Possible values: creation_date_desc, creation_date_asc, modification_date_desc, modification_date_asc
    pub fn order(mut self, order: &str) -> ScalewayListInstanceBuilder {
        self.params.push(("order", order.to_string()));
        self
    }

    /// List Instances from the given Private Networks (use commas to separate them).
    pub fn private_networks(mut self, private_networks: &str) -> ScalewayListInstanceBuilder {
        self.params
            .push(("private_networks", private_networks.to_string()));
        self
    }

    /// List Instances associated with the given private NIC MAC address.
    pub fn private_nic_mac_address(
        mut self,
        private_nic_mac_address: &str,
    ) -> ScalewayListInstanceBuilder {
        self.params.push((
            "private_nic_mac_address",
            private_nic_mac_address.to_string(),
        ));
        self
    }

    /// List Instances from these server ids (use commas to separate them).
    pub fn servers(mut self, servers: &str) -> ScalewayListInstanceBuilder {
        self.params.push(("servers", servers.to_string()));
        self
    }

    #[cfg(feature = "blocking")]
    pub fn run(self) -> Result<ScalewayInstance, ScalewayError> {
        let url = &format!(
            "https://api.scaleway.com/instance/v1/zones/{zone}/servers",
            zone = self.api.zone
        );
        Ok(self
            .api
            .get(&url, self.params)?
            .json::<ScalewayInstanceRoot>()?
            .servers)
    }

    pub async fn run_async(self) -> Result<ScalewayInstance, ScalewayError> {
        let url = &format!(
            "https://api.scaleway.com/instance/v1/zones/{zone}/servers",
            zone = self.api.zone
        );
        Ok(self
            .api
            .get_async(&url, self.params)
            .await?
            .json::<ScalewayInstanceRoot>()
            .await?
            .server)
    }
}