digitalocean_api/api/
floating_ip.rs

1use 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/// Floating IP objects represent a publicly-accessible static IP addresses
18/// that can be mapped to one of your Droplets. They can be used to create
19/// highly available setups or other configurations requiring movable
20/// addresses.
21///
22/// Floating IPs are bound to a specific region.
23///
24/// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#floating-ips)
25#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
26#[get = "pub"]
27pub struct FloatingIp {
28    /// The public IP address of the Floating IP. It also serves as its
29    /// identifier.
30    ip: IpAddr,
31
32    /// The region that the Floating IP is reserved to. When you query a
33    /// Floating IP, the entire region object will be returned.
34    region: Region,
35
36    /// The Droplet that the Floating IP has been assigned to. When you query
37    /// a Floating IP, if it is assigned to a Droplet, the entire Droplet
38    /// object will be returned. If it is not assigned, the value will be null.
39    droplet: Option<Droplet>,
40}
41
42impl FloatingIp {
43    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#list-all-floating-ips)
44    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    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#create-a-new-floating-ip-assigned-to-a-droplet)
54    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    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#create-a-new-floating-ip-reserved-to-a-region)
68    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    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-floating-ip)
85    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    /// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-floating-ip)
96    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/// Response type returned from Digital Ocean.
108#[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/// Response type returned from Digital Ocean.
126#[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}