Skip to main content

devops_armory/cloud/gcp/sql/
create.rs

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