digitalocean_api/api/
volume_action.rs

1use super::action::Action;
2use super::volume::Volume;
3use crate::method::{Create, Get, List};
4use crate::request::Request;
5use crate::request::{VolumeActionRequest, VolumeRequest};
6use crate::{ROOT_URL, STATIC_URL_ERROR};
7use serde::Serialize;
8use std::fmt::Display;
9
10const VOLUMES_SEGMENT: &str = "volumes";
11const VOLUME_ACTIONS_SEGMENT: &str = "actions";
12
13impl Volume {
14    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#attach-a-block-storage-volume-to-a-droplet-by-name)
15    pub fn attach<S>(volume_name: S, droplet: usize) -> VolumeActionRequest<Create, Action>
16    where
17        S: AsRef<str> + Serialize + Display,
18    {
19        let mut url = ROOT_URL.clone();
20        url.path_segments_mut()
21            .expect(STATIC_URL_ERROR)
22            .push(VOLUMES_SEGMENT);
23
24        let mut req = Request::new(url);
25        req.set_body(json!({
26            "type": "attach",
27            "volume_name": volume_name,
28            "droplet_id": droplet,
29        }));
30        req
31    }
32
33    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#remove-a-block-storage-volume-from-a-droplet-by-name)
34    pub fn detach<S>(volume_name: S, droplet: usize) -> VolumeActionRequest<Create, Action>
35    where
36        S: AsRef<str> + Serialize + Display,
37    {
38        let mut url = ROOT_URL.clone();
39        url.path_segments_mut()
40            .expect(STATIC_URL_ERROR)
41            .push(VOLUMES_SEGMENT);
42
43        let mut req = Request::new(url);
44        req.set_body(json!({
45            "type": "detach",
46            "volume_name": volume_name,
47            "droplet_id": droplet,
48        }));
49        req
50    }
51}
52
53impl VolumeRequest<Get, Volume> {
54    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#attach-a-block-storage-volume-to-a-droplet)
55    pub fn attach(mut self, droplet: usize) -> VolumeActionRequest<Create, Action> {
56        self.url_mut()
57            .path_segments_mut()
58            .expect(STATIC_URL_ERROR)
59            .push(VOLUME_ACTIONS_SEGMENT);
60
61        self.set_body(json!({
62            "type": "attach",
63            "droplet_id": droplet,
64        }));
65
66        self.transmute()
67    }
68
69    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#remove-a-block-storage-volume-from-a-droplet)
70    pub fn detach(mut self, droplet: usize) -> VolumeActionRequest<Create, Action> {
71        self.url_mut()
72            .path_segments_mut()
73            .expect(STATIC_URL_ERROR)
74            .push(VOLUME_ACTIONS_SEGMENT);
75
76        self.set_body(json!({
77            "type": "detach",
78            "droplet_id": droplet,
79        }));
80
81        self.transmute()
82    }
83
84    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#resize-a-volume)
85    pub fn resize(mut self, size: usize) -> VolumeActionRequest<Create, Action> {
86        self.url_mut()
87            .path_segments_mut()
88            .expect(STATIC_URL_ERROR)
89            .push(VOLUME_ACTIONS_SEGMENT);
90
91        self.set_body(json!({
92            "type": "resize",
93            "size_gigabytes": size,
94        }));
95
96        self.transmute()
97    }
98
99    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#list-all-actions-for-a-volume)
100    pub fn actions(mut self) -> VolumeActionRequest<List, Vec<Action>> {
101        self.url_mut()
102            .path_segments_mut()
103            .expect(STATIC_URL_ERROR)
104            .push(VOLUME_ACTIONS_SEGMENT);
105
106        self.transmute()
107    }
108
109    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-volume-action)
110    pub fn action(mut self, id: usize) -> VolumeActionRequest<Get, Action> {
111        self.url_mut()
112            .path_segments_mut()
113            .expect(STATIC_URL_ERROR)
114            .push(VOLUME_ACTIONS_SEGMENT)
115            .push(&id.to_string());
116
117        self.transmute()
118    }
119}