Skip to main content

devops_armory/cloud/gcp/sql/database/
create.rs

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