Skip to main content

devops_armory/cloud/gcp/vpc/subnet/
update.rs

1use std::time::Duration;
2
3use super::models::{
4    VpcSubnetUpdate,
5    SecondaryIpRanges
6};
7
8/// Update VPC Subnet
9/// Project ID, token, need to be provided
10pub async fn update_vpc_subnetwork(
11    token: String,
12    project: String,
13    subnet_name: String,
14    subnet_region: String,
15    subnet_sec_ip_ranges: Vec<SecondaryIpRanges>,
16    subnet_fingerprint: String,
17) -> Result<(), std::io::Error> {
18
19    let vpc_subnet_body: VpcSubnetUpdate = VpcSubnetUpdate { 
20        secondaryIpRanges: subnet_sec_ip_ranges,
21        fingerprint: subnet_fingerprint
22    }; 
23
24    let client = awc::Client::default();
25    let request = client.patch(format!("https://compute.googleapis.com/compute/v1/projects/{project}/regions/{subnet_region}/subnetworks/{subnet_name}"))
26        .bearer_auth(token)
27        .insert_header(("Content-Type", "application/json"))
28        .timeout(Duration::from_secs(30))
29        .send_json(&vpc_subnet_body)
30        .await
31        .expect("Request: Update VPC subnet could not been sent");
32
33    let mut req = request;
34    let req_status = req.status().as_u16();
35    let respone = req.body().await.unwrap_or_default();
36
37    match req_status {
38        200 => {
39            println!("Request has been successfull: Status: {:?}, {:?}", req_status, respone);
40        },
41        400 => {
42            println!("Bad Request. Check URL parameters or body: {:?}", respone);
43        },
44        403 => {
45            println!("You don't have access to perform such request: {:?}", respone);
46        }
47        404 => {
48            println!("Requested resource does not exists: {:?}", respone);
49        },
50        409 => {
51            println!("Requested resource already exists! {:?}", respone)
52        }
53        _ => {
54            println!("Request status mismatch. Check response: {:?}", respone);
55        }
56    }
57
58    Ok(())
59
60}
61