Skip to main content

devops_armory/cloud/gcp/project/
billing.rs

1use std::time::Duration;
2
3use super::models::BillingInfo;
4
5/// Add billing info to GCP project
6/// Token and billing account are required
7pub async fn add_billing_to_gcp_project(
8    token: String,
9    project: String,
10    billing_account: String
11) -> Result<(), std::io::Error> {
12
13    let billing_info_body: BillingInfo = BillingInfo { 
14        billingAccountName: billing_account
15    }; 
16
17    let client = awc::Client::default();
18    let request = client.put(format!("https://cloudbilling.googleapis.com/v1/projects/{project}/billingInfo"))
19        .bearer_auth(token)
20        .insert_header(("Content-Type", "application/json"))
21        .timeout(Duration::from_secs(30))
22        .send_json(&billing_info_body)
23        .await
24        .expect("Request: PUT Update billing info could not been sent");
25
26    let mut req = request;
27    let req_status = req.status().as_u16();
28    let respone = req.body().await.unwrap_or_default();
29
30    match req_status {
31
32        200 => {
33            println!("Request has been successfull: Status: {:?}, {:?}", req_status, respone);
34        },
35        400 => {
36            println!("Bad Request. Check URL parameters or body: {:?}", respone);
37        },
38        403 => {
39            println!("You don't have access to perform such request: {:?}", respone);
40        }
41        404 => {
42            println!("Requested resource does not exists: {:?}", respone);
43        },
44        409 => {
45            println!("Requested resource already exists! {:?}", respone)
46        }
47        _ => {
48            println!("Request status mismatch. Check response: {:?}", respone);
49        }
50
51    }
52
53    Ok(())
54
55}
56