use super::action::Action;
use super::droplet::Droplet;
use crate::method::{Create, Get, List};
use crate::request::{DropletActionRequest, DropletRequest};
use crate::STATIC_URL_ERROR;
use serde::Serialize;
use std::fmt::Display;
const DROPLET_ACTIONS_SEGMENT: &str = "actions";
impl DropletRequest<Get, Droplet> {
pub fn actions(mut self) -> DropletActionRequest<List, Vec<Action>> {
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.transmute()
}
pub fn enable_backups(mut self) -> DropletActionRequest<Create, Action> {
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": "enable_backups",
}));
self.transmute()
}
pub fn disable_backups(mut self) -> DropletActionRequest<Create, Action> {
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": "disable_backups",
}));
self.transmute()
}
pub fn reboot(mut self) -> DropletActionRequest<Create, Action> {
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": "reboot",
}));
self.transmute()
}
pub fn power_cycle(mut self) -> DropletActionRequest<Create, Action> {
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": "power_cycle",
}));
self.transmute()
}
pub fn shutdown(mut self) -> DropletActionRequest<Create, Action> {
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": "shutdown",
}));
self.transmute()
}
pub fn power(mut self, val: bool) -> DropletActionRequest<Create, Action> {
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": if val { "power_on" } else { "power_off" },
}));
self.transmute()
}
pub fn restore<D: Display>(mut self, image: D) -> DropletActionRequest<Create, Action> {
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": "restore",
"image": format!("{}", image),
}));
self.transmute()
}
pub fn password_reset(mut self) -> DropletActionRequest<Create, Action> {
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": "password_reset",
}));
self.transmute()
}
pub fn resize<S>(mut self, size: S, disk: bool) -> DropletActionRequest<Create, Action>
where
S: AsRef<str> + Serialize + Display,
{
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": "resize",
"disk": disk,
"size": size.as_ref(),
}));
self.transmute()
}
pub fn rebuild<S>(mut self, image: S) -> DropletActionRequest<Create, Action>
where
S: AsRef<str> + Serialize + Display,
{
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": "rebuild",
"image": image.as_ref(),
}));
self.transmute()
}
pub fn rename<S>(mut self, name: S) -> DropletActionRequest<Create, Action>
where
S: AsRef<str> + Serialize + Display,
{
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": "rename",
"name": name.as_ref(),
}));
self.transmute()
}
pub fn kernel(mut self, kernel: usize) -> DropletActionRequest<Create, Action> {
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": "change_kernel",
"kernel": kernel,
}));
self.transmute()
}
pub fn enable_ipv6(mut self) -> DropletActionRequest<Create, Action> {
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": "enable_ipv6",
}));
self.transmute()
}
pub fn enable_private_networking(mut self) -> DropletActionRequest<Create, Action> {
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": "enable_private_networking",
}));
self.transmute()
}
pub fn snapshot<S>(mut self, name: S) -> DropletActionRequest<Create, Action>
where
S: AsRef<str> + Serialize + Display,
{
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT);
self.set_body(json!({
"type": "snapshot",
"name": name.as_ref(),
}));
self.transmute()
}
pub fn action(mut self, id: usize) -> DropletActionRequest<Get, Action> {
self.url_mut()
.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(DROPLET_ACTIONS_SEGMENT)
.push(&id.to_string());
self.transmute()
}
}