Skip to main content

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

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