digitalocean_api/api/
floating_ip.rs1use super::{ApiLinks, ApiMeta};
2use super::{Droplet, Region};
3use super::{HasPagination, HasResponse, HasValue};
4use crate::method::{Create, Delete, Get, List};
5use crate::request::FloatingIpRequest;
6use crate::request::Request;
7use crate::{ROOT_URL, STATIC_URL_ERROR};
8use getset::{Getters, Setters};
9use serde::Deserialize;
10use serde::Serialize;
11use std::fmt::Display;
12use std::net::IpAddr;
13use url::Url;
14
15const FLOATING_IP_SEGMENT: &str = "floating_ips";
16
17#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
26#[get = "pub"]
27pub struct FloatingIp {
28 ip: IpAddr,
31
32 region: Region,
35
36 droplet: Option<Droplet>,
40}
41
42impl FloatingIp {
43 pub fn list() -> FloatingIpRequest<List, Vec<FloatingIp>> {
45 let mut url = ROOT_URL.clone();
46 url.path_segments_mut()
47 .expect(STATIC_URL_ERROR)
48 .push(FLOATING_IP_SEGMENT);
49
50 Request::new(url)
51 }
52
53 pub fn for_droplet(id: usize) -> FloatingIpRequest<Create, FloatingIp> {
55 let mut url = ROOT_URL.clone();
56 url.path_segments_mut()
57 .expect(STATIC_URL_ERROR)
58 .push(FLOATING_IP_SEGMENT);
59
60 let mut req = Request::new(url);
61 req.set_body(json!({
62 "droplet_id": id,
63 }));
64 req
65 }
66
67 pub fn for_region<S>(id: S) -> FloatingIpRequest<Create, FloatingIp>
69 where
70 S: AsRef<str> + Display + Serialize,
71 {
72 let mut url = ROOT_URL.clone();
73 url.path_segments_mut()
74 .expect(STATIC_URL_ERROR)
75 .push(FLOATING_IP_SEGMENT);
76
77 let mut req = Request::new(url);
78 req.set_body(json!({
79 "region": id,
80 }));
81 req
82 }
83
84 pub fn get<I: Into<IpAddr>>(id: I) -> FloatingIpRequest<Get, FloatingIp> {
86 let mut url = ROOT_URL.clone();
87 url.path_segments_mut()
88 .expect(STATIC_URL_ERROR)
89 .push(FLOATING_IP_SEGMENT)
90 .push(&id.into().to_string());
91
92 Request::new(url)
93 }
94
95 pub fn delete<I: Into<IpAddr>>(id: I) -> FloatingIpRequest<Delete, ()> {
97 let mut url = ROOT_URL.clone();
98 url.path_segments_mut()
99 .expect(STATIC_URL_ERROR)
100 .push(FLOATING_IP_SEGMENT)
101 .push(&id.into().to_string());
102
103 Request::new(url)
104 }
105}
106
107#[derive(Deserialize, Serialize, Debug, Clone)]
109pub struct FloatingIpResponse {
110 floating_ip: FloatingIp,
111}
112
113impl HasResponse for FloatingIp {
114 type Response = FloatingIpResponse;
115}
116
117impl HasValue for FloatingIpResponse {
118 type Value = FloatingIp;
119
120 fn value(self) -> FloatingIp {
121 self.floating_ip
122 }
123}
124
125#[derive(Deserialize, Serialize, Debug, Clone)]
127pub struct FloatingIpListResponse {
128 floating_ips: Vec<FloatingIp>,
129 links: ApiLinks,
130 meta: ApiMeta,
131}
132
133impl HasResponse for Vec<FloatingIp> {
134 type Response = FloatingIpListResponse;
135}
136
137impl HasPagination for FloatingIpListResponse {
138 fn next_page(&self) -> Option<Url> {
139 self.links.next()
140 }
141}
142
143impl HasValue for FloatingIpListResponse {
144 type Value = Vec<FloatingIp>;
145
146 fn value(self) -> Vec<FloatingIp> {
147 self.floating_ips
148 }
149}