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
107
use crate::{data::volume::ScalewayVolumeRoot, ScalewayApi, ScalewayError, ScalewayVolume};
use serde::Serialize;
/// Builder for updating a block storage volume.
///
/// Created by [`ScalewayApi::update_volume`](crate::ScalewayApi::update_volume).
/// Call [`run`](Self::run) or [`run_async`](Self::run_async) to execute.
pub struct ScalewayUpdateVolumeBuilder {
api: ScalewayApi,
zone: String,
volume_id: String,
config: UpdateVolumeConfig,
}
#[derive(Serialize, Debug)]
struct UpdateVolumeConfig {
#[serde(skip_serializing_if = "Option::is_none")]
name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
size: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
tags: Option<Vec<String>>,
}
impl ScalewayUpdateVolumeBuilder {
pub fn new(api: ScalewayApi, zone: &str, volume_id: &str) -> Self {
ScalewayUpdateVolumeBuilder {
api,
zone: zone.to_string(),
volume_id: volume_id.to_string(),
config: UpdateVolumeConfig {
name: None,
size: None,
tags: None,
},
}
}
/// New name for the volume.
pub fn name(mut self, name: &str) -> Self {
self.config.name = Some(name.to_string());
self
}
/// New size in bytes (can only be increased).
pub fn size(mut self, size: u64) -> Self {
self.config.size = Some(size);
self
}
/// Tags to apply to the volume.
pub fn tags(mut self, tags: Vec<String>) -> Self {
self.config.tags = Some(tags);
self
}
/// Applies the update and returns the modified volume (blocking).
///
/// See [`run_async`](Self::run_async) for the async version.
#[cfg(feature = "blocking")]
pub fn run(self) -> Result<ScalewayVolume, ScalewayError> {
let url = format!(
"https://api.scaleway.com/instance/v1/zones/{zone}/volumes/{volume_id}",
zone = self.zone,
volume_id = self.volume_id
);
Ok(self
.api
.patch_json(&url, self.config)?
.json::<ScalewayVolumeRoot>()?
.volume)
}
/// Applies the update and returns the modified volume.
///
/// # Example
///
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), scaleway_rs::ScalewayError> {
/// let api = scaleway_rs::ScalewayApi::new("my-secret-token");
/// // Rename a volume and grow it to 100 GiB
/// let volume = api
/// .update_volume("fr-par-1", "volume-uuid")
/// .name("data-vol-primary")
/// .size(100 * 1024 * 1024 * 1024)
/// .run_async()
/// .await?;
/// println!("Volume size: {} bytes", volume.size);
/// # Ok(())
/// # }
/// ```
pub async fn run_async(self) -> Result<ScalewayVolume, ScalewayError> {
let url = format!(
"https://api.scaleway.com/instance/v1/zones/{zone}/volumes/{volume_id}",
zone = self.zone,
volume_id = self.volume_id
);
Ok(self
.api
.patch_json_async(&url, self.config)
.await?
.json::<ScalewayVolumeRoot>()
.await?
.volume)
}
}