Skip to main content

web_arena_indigo/
ssh.rs

1//! SSH key APIs.
2
3use crate::http::HttpClient;
4use crate::{WebArenaIndigoApi, WebArenaIndigoApiError};
5use serde::{Deserialize, Serialize};
6use serde_json::json;
7
8pub struct SshApi<'a> {
9    indigo: &'a WebArenaIndigoApi,
10}
11
12impl<'a> SshApi<'a> {
13    pub(crate) fn new(api: &'a WebArenaIndigoApi) -> Self {
14        SshApi { indigo: api }
15    }
16
17    /// `GET /webarenaIndigo/v1/vm/sshkey`
18    pub async fn ssh_key_list(&self) -> Result<SshKeyListResponse, WebArenaIndigoApiError> {
19        HttpClient::get(
20            self.indigo.throttle(),
21            self.indigo.access_token(),
22            &self.indigo.endpoint("/webarenaIndigo/v1/vm/sshkey"),
23        )
24        .await
25    }
26
27    /// `GET /webarenaIndigo/v1/vm/sshkey/active/status`
28    pub async fn active_ssh_key_list(
29        &self,
30    ) -> Result<ActiveSshKeyListResponse, WebArenaIndigoApiError> {
31        HttpClient::get(
32            self.indigo.throttle(),
33            self.indigo.access_token(),
34            &self
35                .indigo
36                .endpoint("/webarenaIndigo/v1/vm/sshkey/active/status"),
37        )
38        .await
39    }
40
41    /// `POST /webarenaIndigo/v1/vm/sshkey`
42    pub async fn create_ssh_key(
43        &self,
44        name: &str,
45        public_key: &str,
46    ) -> Result<CreateSshKeyResponse, WebArenaIndigoApiError> {
47        HttpClient::post(
48            self.indigo.throttle(),
49            self.indigo.access_token(),
50            &self.indigo.endpoint("/webarenaIndigo/v1/vm/sshkey"),
51            &json!({
52                "sshName": name,
53                "sshKey": public_key,
54            }),
55        )
56        .await
57    }
58
59    /// `GET /webarenaIndigo/v1/vm/sshkey/{id}`
60    pub async fn retrieve_ssh_key(
61        &self,
62        ssh_key_id: u32,
63    ) -> Result<RetrieveSshKeyResponse, WebArenaIndigoApiError> {
64        HttpClient::get(
65            self.indigo.throttle(),
66            self.indigo.access_token(),
67            &self
68                .indigo
69                .endpoint(&format!("/webarenaIndigo/v1/vm/sshkey/{}", ssh_key_id)),
70        )
71        .await
72    }
73
74    /// `PUT /webarenaIndigo/v1/vm/sshkey/{id}`
75    pub async fn update_ssh_key(
76        &self,
77        ssh_key_id: u32,
78        request: UpdateSshKeyRequest,
79    ) -> Result<UpdateSshKeyResponse, WebArenaIndigoApiError> {
80        HttpClient::put(
81            self.indigo.throttle(),
82            self.indigo.access_token(),
83            &self
84                .indigo
85                .endpoint(&format!("/webarenaIndigo/v1/vm/sshkey/{}", ssh_key_id)),
86            &request,
87        )
88        .await
89    }
90
91    /// `DELETE /webarenaIndigo/v1/vm/sshkey/{id}`
92    pub async fn destroy_ssh_key(
93        &self,
94        ssh_key_id: u32,
95    ) -> Result<DestroySshKeyResponse, WebArenaIndigoApiError> {
96        HttpClient::delete(
97            self.indigo.throttle(),
98            self.indigo.access_token(),
99            &self
100                .indigo
101                .endpoint(&format!("/webarenaIndigo/v1/vm/sshkey/{}", ssh_key_id)),
102        )
103        .await
104    }
105}
106
107#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct SshKeyListResponse {
109    pub success: bool,
110    pub total: u32,
111    #[serde(rename = "sshkeys")]
112    pub ssh_keys: Vec<SshKey>,
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize)]
116pub struct SshKey {
117    pub id: u32,
118    pub service_id: String,
119    pub user_id: u32,
120    pub name: String,
121    #[serde(rename = "sshkey")]
122    pub ssh_key: String,
123    pub status: String,
124    pub created_at: String,
125    pub updated_at: String,
126}
127
128#[derive(Debug, Clone, Serialize, Deserialize)]
129pub struct ActiveSshKeyListResponse {
130    pub success: bool,
131    pub total: u32,
132    #[serde(rename = "sshkeys")]
133    pub ssh_keys: Vec<SshKey>,
134}
135
136#[derive(Debug, Clone, Serialize, Deserialize)]
137pub struct CreateSshKeyResponse {
138    pub success: bool,
139    pub message: String,
140    #[serde(rename = "sshKey")]
141    pub ssh_key: SshKey,
142}
143
144#[derive(Debug, Clone, Serialize, Deserialize)]
145pub struct RetrieveSshKeyResponse {
146    pub success: bool,
147    #[serde(rename = "sshKey")]
148    pub ssh_key: Vec<SshKey>,
149}
150
151#[derive(Debug, Clone, Serialize, Deserialize)]
152pub struct UpdateSshKeyResponse {
153    pub success: bool,
154    pub message: String,
155}
156
157#[derive(Debug, Clone, Serialize)]
158pub struct UpdateSshKeyRequest {
159    #[serde(rename = "sshName")]
160    pub ssh_name: String,
161    #[serde(rename = "sshKey")]
162    pub ssh_key: String,
163    #[serde(rename = "sshKeyStatus")]
164    pub ssh_key_status: SshKeyStatus,
165}
166
167#[derive(Debug, Clone, Copy, Default, Serialize)]
168pub enum SshKeyStatus {
169    #[default]
170    #[serde(rename = "ACTIVE")]
171    Active,
172    #[serde(rename = "DEACTIVE")]
173    Deactive,
174}
175
176#[derive(Debug, Clone, Serialize, Deserialize)]
177pub struct DestroySshKeyResponse {
178    pub success: bool,
179    pub message: String,
180}