oxide_api/sshkeys.rs
1use anyhow::Result;
2
3use crate::Client;
4
5pub struct Sshkeys {
6 pub client: Client,
7}
8
9impl Sshkeys {
10 #[doc(hidden)]
11 pub fn new(client: Client) -> Self {
12 Sshkeys { client }
13 }
14
15 /**
16 * List SSH public keys.
17 *
18 * This function performs a `GET` to the `/session/me/sshkeys` endpoint.
19 *
20 * Lists SSH public keys for the currently authenticated user.
21 *
22 * **Parameters:**
23 *
24 * * `limit: u32` -- Maximum number of items returned by a single call.
25 * * `page_token: &str` -- Token returned by previous call to retrieve the subsequent page.
26 * * `sort_by: crate::types::NameSortMode` -- Supported set of sort modes for scanning by name only
27 *
28 * Currently, we only support scanning in ascending order.
29 */
30 pub async fn get_page(
31 &self,
32 limit: u32,
33 page_token: &str,
34 sort_by: crate::types::NameSortMode,
35 ) -> Result<Vec<crate::types::SshKey>> {
36 let mut query_args: Vec<(String, String)> = Default::default();
37 if !limit.to_string().is_empty() {
38 query_args.push(("limit".to_string(), limit.to_string()));
39 }
40 if !page_token.is_empty() {
41 query_args.push(("page_token".to_string(), page_token.to_string()));
42 }
43 if !sort_by.to_string().is_empty() {
44 query_args.push(("sort_by".to_string(), sort_by.to_string()));
45 }
46 let query_ = serde_urlencoded::to_string(&query_args).unwrap();
47 let url = format!("/session/me/sshkeys?{}", query_);
48
49 let resp: crate::types::SshKeyResultsPage = self.client.get(&url, None).await?;
50
51 // Return our response data.
52 Ok(resp.items)
53 }
54
55 /**
56 * List SSH public keys.
57 *
58 * This function performs a `GET` to the `/session/me/sshkeys` endpoint.
59 *
60 * As opposed to `get`, this function returns all the pages of the request at once.
61 *
62 * Lists SSH public keys for the currently authenticated user.
63 */
64 pub async fn get_all(
65 &self,
66 sort_by: crate::types::NameSortMode,
67 ) -> Result<Vec<crate::types::SshKey>> {
68 let mut query_args: Vec<(String, String)> = Default::default();
69 if !sort_by.to_string().is_empty() {
70 query_args.push(("sort_by".to_string(), sort_by.to_string()));
71 }
72 let query_ = serde_urlencoded::to_string(&query_args).unwrap();
73 let url = format!("/session/me/sshkeys?{}", query_);
74
75 let mut resp: crate::types::SshKeyResultsPage = self.client.get(&url, None).await?;
76
77 let mut items = resp.items;
78 let mut page = resp.next_page;
79
80 // Paginate if we should.
81 while !page.is_empty() {
82 if !url.contains('?') {
83 resp = self
84 .client
85 .get(&format!("{}?page={}", url, page), None)
86 .await?;
87 } else {
88 resp = self
89 .client
90 .get(&format!("{}&page={}", url, page), None)
91 .await?;
92 }
93
94 items.append(&mut resp.items);
95
96 if !resp.next_page.is_empty() && resp.next_page != page {
97 page = resp.next_page.to_string();
98 } else {
99 page = "".to_string();
100 }
101 }
102
103 // Return our response data.
104 Ok(items)
105 }
106
107 /**
108 * Create an SSH public key.
109 *
110 * This function performs a `POST` to the `/session/me/sshkeys` endpoint.
111 *
112 * Create an SSH public key for the currently authenticated user.
113 */
114 pub async fn post(&self, body: &crate::types::SshKeyCreate) -> Result<crate::types::SshKey> {
115 let url = "/session/me/sshkeys".to_string();
116 self.client
117 .post(&url, Some(reqwest::Body::from(serde_json::to_vec(body)?)))
118 .await
119 }
120
121 /**
122 * Fetch an SSH public key.
123 *
124 * This function performs a `GET` to the `/session/me/sshkeys/{ssh_key_name}` endpoint.
125 *
126 * Fetch an SSH public key associated with the currently authenticated user.
127 *
128 * **Parameters:**
129 *
130 * * `ssh_key_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
131 */
132 pub async fn get_key(&self, ssh_key_name: &str) -> Result<crate::types::SshKey> {
133 let url = format!(
134 "/session/me/sshkeys/{}",
135 crate::progenitor_support::encode_path(ssh_key_name),
136 );
137
138 self.client.get(&url, None).await
139 }
140
141 /**
142 * Delete an SSH public key.
143 *
144 * This function performs a `DELETE` to the `/session/me/sshkeys/{ssh_key_name}` endpoint.
145 *
146 * Delete an SSH public key associated with the currently authenticated user.
147 *
148 * **Parameters:**
149 *
150 * * `ssh_key_name: &str` -- Names must begin with a lower case ASCII letter, be composed exclusively of lowercase ASCII, uppercase ASCII, numbers, and '-', and may not end with a '-'. Names cannot be a UUID though they may contain a UUID.
151 */
152 pub async fn delete_key(&self, ssh_key_name: &str) -> Result<()> {
153 let url = format!(
154 "/session/me/sshkeys/{}",
155 crate::progenitor_support::encode_path(ssh_key_name),
156 );
157
158 self.client.delete(&url, None).await
159 }
160}