digitalocean/api/
ssh_key.rs

1use super::{ApiLinks, ApiMeta};
2use super::{HasPagination, HasResponse, HasValue};
3use crate::method::{Create, Delete, Get, List, Update};
4use crate::request::Request;
5use crate::request::SshKeyRequest;
6use crate::{ROOT_URL, STATIC_URL_ERROR};
7use getset::{Getters, Setters};
8use serde::Serialize;
9use std::fmt::Display;
10use url::Url;
11
12const ACCOUNT_SEGMENT: &str = "account";
13const KEYS_SEGMENT: &str = "keys";
14
15/// DigitalOcean allows you to add SSH public keys to the interface so that you
16/// can embed your public key into a Droplet at the time of creation. Only the
17/// public key is required to take advantage of this functionality.
18///
19/// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#ssh-keys)
20#[derive(Deserialize, Serialize, Debug, Clone, Getters, Setters)]
21#[get = "pub"]
22pub struct SshKey {
23	/// This is a unique identification number for the key. This can be used
24	/// to reference a specific SSH key when you wish to embed a key into a
25	/// Droplet.
26	///
27	/// *Note:* This is a `String` to allow for `id` and `fingerprint` to be
28	/// used in `Get`, `Update`, and `Delete` calls like the API describes.
29	id: usize,
30
31	/// This attribute contains the fingerprint value that is generated from
32	/// the public key. This is a unique identifier that will differentiate
33	/// it from other keys using a format that SSH recognizes.
34	fingerprint: String,
35
36	/// This attribute contains the entire public key string that was uploaded.
37	/// This is what is embedded into the root user's authorized_keys file if
38	/// you choose to include this SSH key during Droplet creation.
39	public_key: String,
40
41	/// This is the human-readable display name for the given SSH key. This
42	/// is used to easily identify the SSH keys when they are displayed.
43	name: String,
44}
45
46impl SshKey {
47	/// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#create-a-new-key)
48	pub fn create<N>(name: N, public_key: N) -> SshKeyRequest<Create, SshKey>
49		where N: AsRef<str> + Serialize + Display, {
50		let mut url = ROOT_URL.clone();
51		url.path_segments_mut()
52			.expect(STATIC_URL_ERROR)
53			.push(ACCOUNT_SEGMENT)
54			.push(KEYS_SEGMENT);
55
56		let mut req = Request::new(url);
57		req.set_body(json!({
58            "name": name,
59            "public_key": public_key,
60        }));
61		req
62	}
63
64	/// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#list-all-keys)
65	pub fn list() -> SshKeyRequest<List, Vec<SshKey>> {
66		let mut url = ROOT_URL.clone();
67		url.path_segments_mut()
68			.expect(STATIC_URL_ERROR)
69			.push(ACCOUNT_SEGMENT)
70			.push(KEYS_SEGMENT);
71
72		Request::new(url)
73	}
74
75	/// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-key)
76	pub fn get<S: Serialize + Display>(id: S) -> SshKeyRequest<Get, SshKey> {
77		let mut url = ROOT_URL.clone();
78		url.path_segments_mut()
79			.expect(STATIC_URL_ERROR)
80			.push(ACCOUNT_SEGMENT)
81			.push(KEYS_SEGMENT)
82			.push(&format!("{}", id));
83
84		Request::new(url)
85	}
86
87	/// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#retrieve-an-existing-key)
88	pub fn update<S: Serialize + Display>(id: S) -> SshKeyRequest<Update, SshKey> {
89		let mut url = ROOT_URL.clone();
90		url.path_segments_mut()
91			.expect(STATIC_URL_ERROR)
92			.push(ACCOUNT_SEGMENT)
93			.push(KEYS_SEGMENT)
94			.push(&format!("{}", id));
95
96		Request::new(url)
97	}
98
99	/// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#delete-a-domain)
100	pub fn delete<S: Serialize + Display>(id: S) -> SshKeyRequest<Delete, ()> {
101		let mut url = ROOT_URL.clone();
102		url.path_segments_mut()
103			.expect(STATIC_URL_ERROR)
104			.push(ACCOUNT_SEGMENT)
105			.push(KEYS_SEGMENT)
106			.push(&format!("{}", id));
107
108		Request::new(url)
109	}
110}
111
112impl SshKeyRequest<Update, SshKey> {
113	/// The name to give the new SSH key in your account.
114	///
115	/// [Digital Ocean Documentation.](https://developers.digitalocean.com/documentation/v2/#domain-records)
116	pub fn name<S: AsRef<str> + Display + Serialize>(mut self, val: S) -> Self {
117		self.body_mut()["name"] = json!(val);
118		self
119	}
120}
121
122/// Response type returned from Digital Ocean.
123#[derive(Deserialize, Serialize, Debug, Clone)]
124pub struct SshKeyListResponse {
125	ssh_keys: Vec<SshKey>,
126	links: ApiLinks,
127	meta: ApiMeta
128}
129
130impl HasResponse for Vec<SshKey> {
131	type Response = SshKeyListResponse;
132}
133
134impl HasPagination for SshKeyListResponse {
135	fn next_page(&self) -> Option<Url> {
136		self.links.next()
137	}
138}
139
140impl HasValue for SshKeyListResponse {
141	type Value = Vec<SshKey>;
142
143	fn value(self) -> Vec<SshKey> {
144		self.ssh_keys
145	}
146}
147
148/// Response type returned from Digital Ocean.
149#[derive(Deserialize, Serialize, Debug, Clone)]
150pub struct SshKeyResponse {
151	ssh_key: SshKey
152}
153
154impl HasResponse for SshKey {
155	type Response = SshKeyResponse;
156}
157
158impl HasValue for SshKeyResponse {
159	type Value = SshKey;
160
161	fn value(self) -> SshKey {
162		self.ssh_key
163	}
164}