1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use crate::{data::flexible_ip::ScalewayFlexibleIpRoot, ScalewayApi, ScalewayError, ScalewayFlexibleIp};
use serde::Serialize;
/// Builder for updating a flexible IP address.
///
/// Created by [`ScalewayApi::update_flexible_ip`](crate::ScalewayApi::update_flexible_ip).
/// Call [`run`](Self::run) or [`run_async`](Self::run_async) to execute.
pub struct ScalewayUpdateFlexibleIpBuilder {
api: ScalewayApi,
zone: String,
ip_id: String,
config: UpdateFlexibleIpConfig,
}
#[derive(Serialize, Debug)]
struct UpdateFlexibleIpConfig {
#[serde(skip_serializing_if = "Option::is_none")]
reverse: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
tags: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
server: Option<String>,
}
impl ScalewayUpdateFlexibleIpBuilder {
pub fn new(api: ScalewayApi, zone: &str, ip_id: &str) -> Self {
ScalewayUpdateFlexibleIpBuilder {
api,
zone: zone.to_string(),
ip_id: ip_id.to_string(),
config: UpdateFlexibleIpConfig {
reverse: None,
tags: None,
server: None,
},
}
}
/// Set the reverse DNS for this IP.
pub fn reverse(mut self, reverse: &str) -> Self {
self.config.reverse = Some(reverse.to_string());
self
}
/// Tags to apply to the flexible IP.
pub fn tags(mut self, tags: Vec<String>) -> Self {
self.config.tags = Some(tags);
self
}
/// Server ID to attach this IP to. Pass an empty string to detach.
pub fn server(mut self, server_id: &str) -> Self {
self.config.server = Some(server_id.to_string());
self
}
/// Applies the update and returns the modified flexible IP (blocking).
///
/// See [`run_async`](Self::run_async) for the async version.
#[cfg(feature = "blocking")]
pub fn run(self) -> Result<ScalewayFlexibleIp, ScalewayError> {
let url = format!(
"https://api.scaleway.com/instance/v1/zones/{zone}/ips/{ip_id}",
zone = self.zone,
ip_id = self.ip_id
);
Ok(self
.api
.patch_json(&url, self.config)?
.json::<ScalewayFlexibleIpRoot>()?
.ip)
}
/// Applies the update and returns the modified flexible IP.
///
/// # Example
///
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), scaleway_rs::ScalewayError> {
/// let api = scaleway_rs::ScalewayApi::new("my-secret-token");
/// let ip = api
/// .update_flexible_ip("fr-par-1", "ip-uuid")
/// .reverse("my-server.example.com")
/// .tags(vec!["prod".to_string()])
/// .run_async()
/// .await?;
/// println!("Updated IP: {}", ip.address);
/// # Ok(())
/// # }
/// ```
pub async fn run_async(self) -> Result<ScalewayFlexibleIp, ScalewayError> {
let url = format!(
"https://api.scaleway.com/instance/v1/zones/{zone}/ips/{ip_id}",
zone = self.zone,
ip_id = self.ip_id
);
Ok(self
.api
.patch_json_async(&url, self.config)
.await?
.json::<ScalewayFlexibleIpRoot>()
.await?
.ip)
}
}