Skip to main content

devops_armory/cloud/gcp/compute/
get.rs

1use std::time::Duration;
2
3use crate::cloud::gcp::compute::models::GetVMInstanceInfo;
4
5/// Get GCP instance info
6/// Token, project name need to be provided
7pub async fn get_gcp_vm_info(
8    token: String,
9    project_name: String,
10    zone_name: String,
11    instance_name: String
12) -> Result<GetVMInstanceInfo, std::io::Error> {
13
14    let client = awc::Client::default();
15    let request = client
16        .get(format!("https://compute.googleapis.com/compute/v1/projects/{project_name}/zones/{zone_name}/instances/{instance_name}"))
17        .bearer_auth(token)
18        .insert_header(("Content-Type", "application/json"))
19        .timeout(Duration::from_secs(30))
20        .send()
21        .await
22        .expect("Request GET instance info could not been sent")
23        .json::<GetVMInstanceInfo>()
24        .await
25        .unwrap_or_default();
26
27    Ok(request)
28
29}
30
31// Debug - raw bytes
32pub async fn get_gcp_vm_info_raw(
33    token: String,
34    project_name: String,
35    zone_name: String,
36    instance_name: String
37) -> Result<(), std::io::Error> {
38
39    let client = awc::Client::default();
40    let request = client
41        .get(format!("https://compute.googleapis.com/compute/v1/projects/{project_name}/zones/{zone_name}/instances/{instance_name}"))
42        .bearer_auth(token)
43        .insert_header(("Content-Type", "application/json"))
44        .timeout(Duration::from_secs(30))
45        .send()
46        .await
47        .expect("Request GET instance info could not been sent");
48
49    let mut req = request;
50    let req_status = req.status().as_u16();
51    let respone = req.body().await.unwrap_or_default();
52
53    match req_status {
54        200 => {
55            println!("Request has been successfull: Status: {:?}, {:?}", req_status, respone);
56        },
57        400 => {
58            println!("Bad Request. Check URL parameters or body: {:?}", respone);
59        },
60        403 => {
61            println!("You don't have access to perform such request: {:?}", respone);
62        }
63        404 => {
64            println!("Requested resource does not exists: {:?}", respone);
65        },
66        409 => {
67            println!("Requested resource already exists! {:?}", respone)
68        }
69        _ => {
70            println!("Request status mismatch. Check response: {:?}", respone);
71        }
72    }
73
74    Ok(())
75
76}