Skip to main content

devops_armory/cloud/gcp/dns/
update.rs

1use std::time::Duration;
2
3use super::models::CreateDNSRecord;
4
5/// Update DNS records
6/// Need to provide project, managed zone and token to successfully send request
7/// RRdatas contains list of values
8pub async fn update_record_set(
9    project: String,
10    managed_zone: String,
11    token: String,
12    dns_name: String,
13    dns_type: String,
14    propagation_ttl: i32,
15    dns_rrdatas: Vec<String>,
16    dns_signatureRrdatas: Option<Vec<String>>,
17    dns_kind: String
18) -> Result<(), std::io::Error> {
19
20    let dns_data = CreateDNSRecord {
21            name: dns_name.clone(),
22            r#type: dns_type.clone(),
23            ttl: propagation_ttl,
24            rrdatas: dns_rrdatas,
25            signatureRrdatas: None,
26            kind: dns_kind
27    };
28
29    let client = awc::Client::default();
30    let request = client.patch(format!("https://dns.googleapis.com/dns/v1/projects/{project}/managedZones/{managed_zone}/rrsets/{dns_name}/{dns_type}"))
31        .bearer_auth(&token)
32        .insert_header(("Content-Type", "application/json"))
33        .timeout(Duration::from_secs(30))
34        .send_json(&dns_data)
35        .await
36        .unwrap();
37
38    let mut req = request;
39    let req_status = req.status().as_u16();
40    let respone = req.body().await.unwrap_or_default();
41
42    match req_status {
43
44        200 => {
45            println!("Request has been successfull: Status: {:?}, {:?}", req_status, respone);
46        },
47        400 => {
48            println!("Bad Request. Check URL parameters or body: {:?}", respone);
49        },
50        403 => {
51            println!("You don't have access to perform such request: {:?}", respone);
52        }
53        404 => {
54            println!("Requested resource does not exists: {:?}", respone);
55        },
56        409 => {
57            println!("Requested resource already exists! {:?}", respone)
58        }
59        _ => {
60            println!("Request status mismatch. Check response: {:?}", respone);
61        }
62
63    }
64
65    Ok(())
66
67}
68