digitalocean_api/api/
droplet_action.rs

1use super::action::Action;
2use super::droplet::Droplet;
3use crate::method::{Create, Get, List};
4use crate::request::{DropletActionRequest, DropletRequest};
5use crate::STATIC_URL_ERROR;
6use serde::Serialize;
7use std::fmt::Display;
8
9const DROPLET_ACTIONS_SEGMENT: &str = "actions";
10
11impl DropletRequest<Get, Droplet> {
12    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#list-actions-for-a-droplet)
13    pub fn actions(mut self) -> DropletActionRequest<List, Vec<Action>> {
14        self.url_mut()
15            .path_segments_mut()
16            .expect(STATIC_URL_ERROR)
17            .push(DROPLET_ACTIONS_SEGMENT);
18
19        self.transmute()
20    }
21
22    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#enable-backups)
23    pub fn enable_backups(mut self) -> DropletActionRequest<Create, Action> {
24        self.url_mut()
25            .path_segments_mut()
26            .expect(STATIC_URL_ERROR)
27            .push(DROPLET_ACTIONS_SEGMENT);
28
29        self.set_body(json!({
30            "type": "enable_backups",
31        }));
32
33        self.transmute()
34    }
35
36    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#disable-backups)
37    pub fn disable_backups(mut self) -> DropletActionRequest<Create, Action> {
38        self.url_mut()
39            .path_segments_mut()
40            .expect(STATIC_URL_ERROR)
41            .push(DROPLET_ACTIONS_SEGMENT);
42
43        self.set_body(json!({
44            "type": "disable_backups",
45        }));
46
47        self.transmute()
48    }
49
50    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#reboot-a-droplet)
51    pub fn reboot(mut self) -> DropletActionRequest<Create, Action> {
52        self.url_mut()
53            .path_segments_mut()
54            .expect(STATIC_URL_ERROR)
55            .push(DROPLET_ACTIONS_SEGMENT);
56
57        self.set_body(json!({
58            "type": "reboot",
59        }));
60
61        self.transmute()
62    }
63
64    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#power-cycle-a-droplet)
65    pub fn power_cycle(mut self) -> DropletActionRequest<Create, Action> {
66        self.url_mut()
67            .path_segments_mut()
68            .expect(STATIC_URL_ERROR)
69            .push(DROPLET_ACTIONS_SEGMENT);
70
71        self.set_body(json!({
72            "type": "power_cycle",
73        }));
74
75        self.transmute()
76    }
77
78    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#shutdown-a-droplet)
79    pub fn shutdown(mut self) -> DropletActionRequest<Create, Action> {
80        self.url_mut()
81            .path_segments_mut()
82            .expect(STATIC_URL_ERROR)
83            .push(DROPLET_ACTIONS_SEGMENT);
84
85        self.set_body(json!({
86            "type": "shutdown",
87        }));
88
89        self.transmute()
90    }
91
92    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#power-off-a-droplet)
93    pub fn power(mut self, val: bool) -> DropletActionRequest<Create, Action> {
94        self.url_mut()
95            .path_segments_mut()
96            .expect(STATIC_URL_ERROR)
97            .push(DROPLET_ACTIONS_SEGMENT);
98
99        self.set_body(json!({
100            "type": if val { "power_on" } else { "power_off" },
101        }));
102
103        self.transmute()
104    }
105
106    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#restore-a-droplet)
107    pub fn restore<D: Display>(mut self, image: D) -> DropletActionRequest<Create, Action> {
108        self.url_mut()
109            .path_segments_mut()
110            .expect(STATIC_URL_ERROR)
111            .push(DROPLET_ACTIONS_SEGMENT);
112
113        self.set_body(json!({
114            "type": "restore",
115            "image": format!("{}", image),
116        }));
117
118        self.transmute()
119    }
120
121    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#password-reset-a-droplet)
122    pub fn password_reset(mut self) -> DropletActionRequest<Create, Action> {
123        self.url_mut()
124            .path_segments_mut()
125            .expect(STATIC_URL_ERROR)
126            .push(DROPLET_ACTIONS_SEGMENT);
127
128        self.set_body(json!({
129            "type": "password_reset",
130        }));
131
132        self.transmute()
133    }
134
135    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#resize-a-droplet)
136    pub fn resize<S>(mut self, size: S, disk: bool) -> DropletActionRequest<Create, Action>
137    where
138        S: AsRef<str> + Serialize + Display,
139    {
140        self.url_mut()
141            .path_segments_mut()
142            .expect(STATIC_URL_ERROR)
143            .push(DROPLET_ACTIONS_SEGMENT);
144
145        self.set_body(json!({
146            "type": "resize",
147            "disk": disk,
148            "size": size.as_ref(),
149        }));
150
151        self.transmute()
152    }
153
154    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#resize-a-droplet)
155    pub fn rebuild<S>(mut self, image: S) -> DropletActionRequest<Create, Action>
156    where
157        S: AsRef<str> + Serialize + Display,
158    {
159        self.url_mut()
160            .path_segments_mut()
161            .expect(STATIC_URL_ERROR)
162            .push(DROPLET_ACTIONS_SEGMENT);
163
164        self.set_body(json!({
165            "type": "rebuild",
166            "image": image.as_ref(),
167        }));
168
169        self.transmute()
170    }
171
172    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#rename-a-droplet)
173    pub fn rename<S>(mut self, name: S) -> DropletActionRequest<Create, Action>
174    where
175        S: AsRef<str> + Serialize + Display,
176    {
177        self.url_mut()
178            .path_segments_mut()
179            .expect(STATIC_URL_ERROR)
180            .push(DROPLET_ACTIONS_SEGMENT);
181
182        self.set_body(json!({
183            "type": "rename",
184            "name": name.as_ref(),
185        }));
186
187        self.transmute()
188    }
189
190    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#change-the-kernel)
191    pub fn kernel(mut self, kernel: usize) -> DropletActionRequest<Create, Action> {
192        self.url_mut()
193            .path_segments_mut()
194            .expect(STATIC_URL_ERROR)
195            .push(DROPLET_ACTIONS_SEGMENT);
196
197        self.set_body(json!({
198            "type": "change_kernel",
199            "kernel": kernel,
200        }));
201
202        self.transmute()
203    }
204
205    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#enable-ipv6)
206    pub fn enable_ipv6(mut self) -> DropletActionRequest<Create, Action> {
207        self.url_mut()
208            .path_segments_mut()
209            .expect(STATIC_URL_ERROR)
210            .push(DROPLET_ACTIONS_SEGMENT);
211
212        self.set_body(json!({
213            "type": "enable_ipv6",
214        }));
215
216        self.transmute()
217    }
218
219    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#enable-private-networking)
220    pub fn enable_private_networking(mut self) -> DropletActionRequest<Create, Action> {
221        self.url_mut()
222            .path_segments_mut()
223            .expect(STATIC_URL_ERROR)
224            .push(DROPLET_ACTIONS_SEGMENT);
225
226        self.set_body(json!({
227            "type": "enable_private_networking",
228        }));
229
230        self.transmute()
231    }
232
233    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#snapshot-a-droplet)
234    pub fn snapshot<S>(mut self, name: S) -> DropletActionRequest<Create, Action>
235    where
236        S: AsRef<str> + Serialize + Display,
237    {
238        self.url_mut()
239            .path_segments_mut()
240            .expect(STATIC_URL_ERROR)
241            .push(DROPLET_ACTIONS_SEGMENT);
242
243        self.set_body(json!({
244            "type": "snapshot",
245            "name": name.as_ref(),
246        }));
247
248        self.transmute()
249    }
250
251    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#retrieve-a-droplet-action)
252    pub fn action(mut self, id: usize) -> DropletActionRequest<Get, Action> {
253        self.url_mut()
254            .path_segments_mut()
255            .expect(STATIC_URL_ERROR)
256            .push(DROPLET_ACTIONS_SEGMENT)
257            .push(&id.to_string());
258
259        self.transmute()
260    }
261}
262
263// TODO: https://developers.digitalocean.com/documentation/v2/#acting-on-tagged-droplets