digitalocean_api/api/
action.rs

1use super::{ApiLinks, ApiMeta};
2use super::{HasPagination, HasResponse, HasValue};
3use crate::method::{Get, List};
4use crate::request::ActionRequest;
5use crate::request::Request;
6use crate::{ROOT_URL, STATIC_URL_ERROR};
7use chrono::{DateTime, Utc};
8use getset::{Getters, Setters};
9use serde::Deserialize;
10use serde::Serialize;
11use url::Url;
12
13const ACTIONS_SEGMENT: &str = "actions";
14
15/// Actions are records of events that have occurred on the resources in your
16/// account. These can be things like rebooting a Droplet, or transferring an
17/// image to a new region.
18///
19/// An action object is created every time one of these actions is initiated.
20/// The action object contains information about the current status of the
21/// action, start and complete timestamps, and the associated resource type
22/// and ID.
23///
24/// Every action that creates an action object is available through this
25/// endpoint. Completed actions are not removed from this list and are always
26/// available for querying.
27///
28/// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#actions)
29#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
30#[get = "pub"]
31pub struct Action {
32    /// A unique identifier for each Droplet action event. This is used to
33    /// reference a specific action that was requested.
34    id: usize,
35
36    /// The current status of the action. The value of this attribute will be
37    /// "in-progress", "completed", or "errored".
38    status: String,
39
40    /// The type of action that the event is executing (reboot, power_off,
41    /// etc.).
42    started_at: DateTime<Utc>,
43
44    /// A time value given in ISO8601 combined date and time format that
45    /// represents when the action was completed.
46    completed_at: Option<DateTime<Utc>>,
47
48    /// A unique identifier for the resource that the action is associated
49    /// with.
50    resource_id: usize,
51
52    /// The type of resource that the action is associated with.
53    resource_type: String,
54
55    // /// (deprecated) A slug representing the region where the action occurred.
56    // #[get = "pub"]
57    // #[deprecated(since = "0.0.1", note = "DigitalOcean has deprecated this.")]
58    // region: Option<Region>,
59    /// A slug representing the region where the action occurred.
60    region_slug: Option<String>,
61}
62
63impl Action {
64    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-action)
65    pub fn get(id: usize) -> ActionRequest<Get, Action> {
66        let mut url = ROOT_URL.clone();
67        url.path_segments_mut()
68            .expect(STATIC_URL_ERROR)
69            .push(ACTIONS_SEGMENT)
70            .push(&id.to_string());
71
72        Request::new(url)
73    }
74
75    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#list-all-actions)
76    pub fn list() -> ActionRequest<List, Vec<Action>> {
77        let mut url = ROOT_URL.clone();
78        url.path_segments_mut()
79            .expect(STATIC_URL_ERROR)
80            .push(ACTIONS_SEGMENT);
81
82        Request::new(url)
83    }
84}
85
86/// Response type returned from Digital Ocean.
87#[derive(Deserialize, Serialize, Debug, Clone)]
88pub struct ActionResponse {
89    action: Action,
90}
91
92impl HasValue for ActionResponse {
93    type Value = Action;
94
95    fn value(self) -> Action {
96        self.action
97    }
98}
99
100impl HasResponse for Action {
101    type Response = ActionResponse;
102}
103
104/// Response type returned from Digital Ocean.
105#[derive(Deserialize, Serialize, Debug, Clone)]
106pub struct ActionListResponse {
107    actions: Vec<Action>,
108    links: ApiLinks,
109    meta: ApiMeta,
110}
111
112impl HasResponse for Vec<Action> {
113    type Response = ActionListResponse;
114}
115
116impl HasPagination for ActionListResponse {
117    fn next_page(&self) -> Option<Url> {
118        self.links.next()
119    }
120}
121
122impl HasValue for ActionListResponse {
123    type Value = Vec<Action>;
124
125    fn value(self) -> Vec<Action> {
126        self.actions
127    }
128}