Skip to main content

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

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