digitalocean_api/api/
snapshot.rs

1use super::{ApiLinks, ApiMeta};
2use super::{HasPagination, HasResponse, HasValue};
3use crate::method::{Delete, Get, List};
4use crate::request::Request;
5use crate::request::SnapshotRequest;
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 SNAPSHOT_SEGMENT: &str = "snapshots";
14
15/// Snapshots are saved instances of a Droplet or a volume, which is reflected
16/// in the `resource_type` attribute. In order to avoid problems with
17/// compressing filesystems, each defines a `min_disk_size` attribute which is
18/// the minimum size of the Droplet or volume disk when creating a new resource
19/// from the saved snapshot.
20///
21/// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#snapshots)
22#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
23#[get = "pub"]
24pub struct Snapshot {
25    /// The unique identifier for the snapshot.
26    id: String,
27
28    /// A human-readable name for the snapshot.
29    name: String,
30
31    /// A time value given in ISO8601 combined date and time format that
32    /// represents when the snapshot was created.
33    created_at: DateTime<Utc>,
34
35    /// An array of the regions that the image is available in. The regions
36    /// are represented by their identifying slug values.
37    regions: Vec<String>,
38
39    /// A unique identifier for the resource that the action is associated
40    /// with.
41    resource_id: String,
42
43    /// The type of resource that the action is associated with.
44    resource_type: String,
45
46    /// The minimum size in GB required for a volume or Droplet to use this snapshot.
47    min_disk_size: usize,
48
49    /// The billable size of the snapshot in gigabytes.
50    size_gigabytes: f64,
51}
52
53impl Snapshot {
54    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#list-all-snapshots)
55    pub fn list() -> SnapshotRequest<List, Vec<Snapshot>> {
56        let mut url = ROOT_URL.clone();
57        url.path_segments_mut()
58            .expect(STATIC_URL_ERROR)
59            .push(SNAPSHOT_SEGMENT);
60
61        Request::new(url)
62    }
63
64    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#list-all-droplet-snapshots)
65    pub fn droplets() -> SnapshotRequest<List, Vec<Snapshot>> {
66        let mut url = ROOT_URL.clone();
67        url.path_segments_mut()
68            .expect(STATIC_URL_ERROR)
69            .push(SNAPSHOT_SEGMENT);
70
71        url.query_pairs_mut()
72            .append_pair("resource_type", "droplet");
73
74        Request::new(url)
75    }
76
77    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#list-all-volume-snapshots)
78    pub fn volumes() -> SnapshotRequest<List, Vec<Snapshot>> {
79        let mut url = ROOT_URL.clone();
80        url.path_segments_mut()
81            .expect(STATIC_URL_ERROR)
82            .push(SNAPSHOT_SEGMENT);
83
84        url.query_pairs_mut().append_pair("resource_type", "volume");
85
86        Request::new(url)
87    }
88
89    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-snapshot-by-id)
90    pub fn get(id: usize) -> SnapshotRequest<Get, Snapshot> {
91        let mut url = ROOT_URL.clone();
92        url.path_segments_mut()
93            .expect(STATIC_URL_ERROR)
94            .push(SNAPSHOT_SEGMENT)
95            .push(&id.to_string());
96
97        Request::new(url)
98    }
99
100    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#delete-a-snapshot)
101    pub fn delete(id: usize) -> SnapshotRequest<Delete, ()> {
102        let mut url = ROOT_URL.clone();
103        url.path_segments_mut()
104            .expect(STATIC_URL_ERROR)
105            .push(SNAPSHOT_SEGMENT)
106            .push(&id.to_string());
107
108        Request::new(url)
109    }
110}
111
112/// Response type returned from Digital Ocean.
113#[derive(Deserialize, Serialize, Debug, Clone)]
114pub struct SnapshotListResponse {
115    snapshots: Vec<Snapshot>,
116    links: ApiLinks,
117    meta: ApiMeta,
118}
119
120impl HasResponse for Vec<Snapshot> {
121    type Response = SnapshotListResponse;
122}
123
124impl HasPagination for SnapshotListResponse {
125    fn next_page(&self) -> Option<Url> {
126        self.links.next()
127    }
128}
129
130impl HasValue for SnapshotListResponse {
131    type Value = Vec<Snapshot>;
132
133    fn value(self) -> Vec<Snapshot> {
134        self.snapshots
135    }
136}
137
138/// Response type returned from Digital Ocean.
139#[derive(Deserialize, Serialize, Debug, Clone)]
140pub struct SnapshotResponse {
141    snapshot: Snapshot,
142}
143
144impl HasResponse for Snapshot {
145    type Response = SnapshotResponse;
146}
147
148impl HasValue for SnapshotResponse {
149    type Value = Snapshot;
150
151    fn value(self) -> Snapshot {
152        self.snapshot
153    }
154}