use super::{ApiLinks, ApiMeta};
use super::{Droplet, Region};
use super::{HasPagination, HasResponse, HasValue};
use crate::method::{Create, Delete, Get, List};
use crate::request::FloatingIpRequest;
use crate::request::Request;
use crate::{ROOT_URL, STATIC_URL_ERROR};
use getset::{Getters, Setters};
use serde::Serialize;
use std::fmt::Display;
use std::net::IpAddr;
use url::Url;
const FLOATING_IP_SEGMENT: &str = "floating_ips";
#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
#[get = "pub"]
pub struct FloatingIp {
ip: IpAddr,
region: Region,
droplet: Option<Droplet>
}
impl FloatingIp {
pub fn list() -> FloatingIpRequest<List, Vec<FloatingIp>> {
let mut url = ROOT_URL.clone();
url.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(FLOATING_IP_SEGMENT);
Request::new(url)
}
pub fn for_droplet(id: usize) -> FloatingIpRequest<Create, FloatingIp> {
let mut url = ROOT_URL.clone();
url.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(FLOATING_IP_SEGMENT);
let mut req = Request::new(url);
req.set_body(json!({
"droplet_id": id,
}));
req
}
pub fn for_region<S>(id: S) -> FloatingIpRequest<Create, FloatingIp>
where
S: AsRef<str> + Display + Serialize {
let mut url = ROOT_URL.clone();
url.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(FLOATING_IP_SEGMENT);
let mut req = Request::new(url);
req.set_body(json!({
"region": id,
}));
req
}
pub fn get<I: Into<IpAddr>>(id: I) -> FloatingIpRequest<Get, FloatingIp> {
let mut url = ROOT_URL.clone();
url.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(FLOATING_IP_SEGMENT)
.push(&id.into().to_string());
Request::new(url)
}
pub fn delete<I: Into<IpAddr>>(id: I) -> FloatingIpRequest<Delete, ()> {
let mut url = ROOT_URL.clone();
url.path_segments_mut()
.expect(STATIC_URL_ERROR)
.push(FLOATING_IP_SEGMENT)
.push(&id.into().to_string());
Request::new(url)
}
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct FloatingIpResponse {
floating_ip: FloatingIp
}
impl HasResponse for FloatingIp {
type Response = FloatingIpResponse;
}
impl HasValue for FloatingIpResponse {
type Value = FloatingIp;
fn value(self) -> FloatingIp {
self.floating_ip
}
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct FloatingIpListResponse {
floating_ips: Vec<FloatingIp>,
links: ApiLinks,
meta: ApiMeta
}
impl HasResponse for Vec<FloatingIp> {
type Response = FloatingIpListResponse;
}
impl HasPagination for FloatingIpListResponse {
fn next_page(&self) -> Option<Url> {
self.links.next()
}
}
impl HasValue for FloatingIpListResponse {
type Value = Vec<FloatingIp>;
fn value(self) -> Vec<FloatingIp> {
self.floating_ips
}
}