use super::{ApiLinks, ApiMeta};
use super::{HasPagination, HasResponse, HasValue};
use crate::method::{Delete, Get, List};
use crate::request::Request;
use crate::request::SnapshotRequest;
use crate::{ROOT_URL, STATIC_URL_ERROR};
use chrono::{DateTime, Utc};
use getset::{Getters, Setters};
use url::Url;
const SNAPSHOT_SEGMENT: &str = "snapshots";
#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
#[get = "pub"]
pub struct Snapshot {
id: String,
name: String,
created_at: DateTime<Utc>,
regions: Vec<String>,
resource_id: String,
resource_type: String,
min_disk_size: usize,
size_gigabytes: f64,
}
impl Snapshot {
pub fn list() -> SnapshotRequest<List, Vec<Snapshot>> {
let mut url = ROOT_URL.clone();
url.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(SNAPSHOT_SEGMENT);
Request::new(url)
}
pub fn droplets() -> SnapshotRequest<List, Vec<Snapshot>> {
let mut url = ROOT_URL.clone();
url.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(SNAPSHOT_SEGMENT);
url.query_pairs_mut()
.append_pair("resource_type", "droplet");
Request::new(url)
}
pub fn volumes() -> SnapshotRequest<List, Vec<Snapshot>> {
let mut url = ROOT_URL.clone();
url.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(SNAPSHOT_SEGMENT);
url.query_pairs_mut().append_pair("resource_type", "volume");
Request::new(url)
}
pub fn get(id: usize) -> SnapshotRequest<Get, Snapshot> {
let mut url = ROOT_URL.clone();
url.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(SNAPSHOT_SEGMENT)
.push(&id.to_string());
Request::new(url)
}
pub fn delete(id: usize) -> SnapshotRequest<Delete, ()> {
let mut url = ROOT_URL.clone();
url.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(SNAPSHOT_SEGMENT)
.push(&id.to_string());
Request::new(url)
}
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct SnapshotListResponse {
snapshots: Vec<Snapshot>,
links: ApiLinks,
meta: ApiMeta
}
impl HasResponse for Vec<Snapshot> {
type Response = SnapshotListResponse;
}
impl HasPagination for SnapshotListResponse {
fn next_page(&self) -> Option<Url> {
self.links.next()
}
}
impl HasValue for SnapshotListResponse {
type Value = Vec<Snapshot>;
fn value(self) -> Vec<Snapshot> {
self.snapshots
}
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct SnapshotResponse {
snapshot: Snapshot
}
impl HasResponse for Snapshot {
type Response = SnapshotResponse;
}
impl HasValue for SnapshotResponse {
type Value = Snapshot;
fn value(self) -> Snapshot {
self.snapshot
}
}