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
use crate::{
    data::image::{ScalewayImage, ScalewayImageRoot},
    ScalewayApi, ScalewayError,
};

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

impl ScalewayListInstanceImagesBuilder {
    pub fn new(api: ScalewayApi, zone: &str) -> Self {
        ScalewayListInstanceImagesBuilder {
            api,
            zone: zone.to_string(),
            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) -> ScalewayListInstanceImagesBuilder {
        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) -> ScalewayListInstanceImagesBuilder {
        self.params.push(("page", count.to_string()));
        self
    }

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

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

    /// Filter images by public attribute
    pub fn public(mut self, public: bool) -> ScalewayListInstanceImagesBuilder {
        self.params.push(("public", public.to_string()));
        self
    }

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

    /// Filter images by arch
    pub fn arch(mut self, arch: &str) -> ScalewayListInstanceImagesBuilder {
        self.params.push(("arch", arch.to_string()));
        self
    }

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

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

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