use super::{ApiLinks, ApiMeta};
use super::{HasPagination, HasResponse, HasValue};
use {ROOT_URL, STATIC_URL_ERROR};
use chrono::{DateTime, Utc};
use method::{Delete, Get, List};
use request::Request;
use request::SnapshotRequest;
use url::Url;
const SNAPSHOT_SEGMENT: &'static str = "snapshots";
#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
pub struct Snapshot {
#[get = "pub"]
id: String,
#[get = "pub"]
name: String,
#[get = "pub"]
created_at: DateTime<Utc>,
#[get = "pub"]
regions: Vec<String>,
#[get = "pub"]
resource_id: String,
#[get = "pub"]
resource_type: String,
#[get = "pub"]
min_disk_size: usize,
#[get = "pub"]
size_gigabytes: usize,
}
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
}
}