aiven_rs/cloud/api.rs
1// MIT License
2//
3// Copyright (c) 2020 Ankur Srivastava
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23use crate::{client::HTTPClient, errors::AivenError, make_request};
24
25use crate::cloud::types;
26pub struct CloudApi {
27 http_client: HTTPClient,
28}
29
30impl CloudApi {
31 pub(crate) fn new(client: HTTPClient) -> Self {
32 Self {
33 http_client: client,
34 }
35 }
36 /// List available cloud platforms by project name
37 ///
38 /// # Examples
39 /// Basic usage:
40 ///
41 /// ```rust,no_run
42 /// use serde_json::json;
43 ///
44 /// # #[tokio::main]
45 /// # async fn main()-> Result<(), Box<dyn std::error::Error>>{
46 /// let client = aiven_rs::AivenClient::from_token("https://api.aiven.io", "v1", "aiven-token");
47 ///
48 /// let response = client
49 /// .cloud()
50 /// .list_by_project("my-project").await?;
51 /// # Ok(())
52 /// # }
53 /// ```
54 pub async fn list_by_project(&self, project: &str) -> Result<types::ResClouds, AivenError> {
55 let url = &format!("project/{}/clouds", project);
56 let response = make_request!(self, reqwest::Method::GET, &url)?;
57 Ok(response.json().await?)
58 }
59
60 /// List all available cloud platforms
61 ///
62 /// # Examples
63 /// Basic usage:
64 ///
65 /// ```rust,no_run
66 /// use serde_json::json;
67 ///
68 /// # #[tokio::main]
69 /// # async fn main()-> Result<(), Box<dyn std::error::Error>>{
70 /// let client = aiven_rs::AivenClient::from_token("https://api.aiven.io", "v1", "aiven-token");
71 ///
72 /// let response = client
73 /// .cloud()
74 /// .list_all().await?;
75 /// # Ok(())
76 /// # }
77 /// ```
78 pub async fn list_all(&self) -> Result<types::ResClouds, AivenError> {
79 let url = "clouds";
80 let response = make_request!(self, reqwest::Method::GET, &url)?;
81 Ok(response.json().await?)
82 }
83}
84
85#[cfg(test)]
86mod tests {
87 use crate::testutil;
88
89 #[tokio::test]
90 async fn test_cloud_list_all() {
91 let client = testutil::prepare_test_client();
92 let query_url = "/clouds";
93 let test_data = testutil::get_test_data("tests/testdata/cloud/list.json");
94 let _m = testutil::create_mock_server(query_url, &test_data, "GET");
95
96 match client.cloud().list_all().await {
97 Ok(response) => {
98 assert!(response.clouds.len() > 0, format!("{:?}", response));
99 }
100 Err(e) => assert!(false, format!("{:?}", e)),
101 }
102 }
103
104 #[tokio::test]
105 async fn test_cloud_list_by_project() {
106 let client = testutil::prepare_test_client();
107 let query_url = "/project/my-project/clouds";
108 let test_data = testutil::get_test_data("tests/testdata/cloud/list_by_project.json");
109 let _m = testutil::create_mock_server(query_url, &test_data, "GET");
110
111 match client.cloud().list_by_project("my-project").await {
112 Ok(response) => {
113 assert!(response.clouds.len() > 0, format!("{:?}", response));
114 }
115 Err(e) => assert!(false, format!("{:?}", e)),
116 }
117 }
118}