digitalocean_api/api/
snapshot.rs1use 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#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
23#[get = "pub"]
24pub struct Snapshot {
25 id: String,
27
28 name: String,
30
31 created_at: DateTime<Utc>,
34
35 regions: Vec<String>,
38
39 resource_id: String,
42
43 resource_type: String,
45
46 min_disk_size: usize,
48
49 size_gigabytes: f64,
51}
52
53impl Snapshot {
54 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 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 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 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 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#[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#[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}