dsh_api/
volume.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//! # Manage volumes
//!
//! Module that contains methods and functions to manage volumes.
//! * API methods - DshApiClient methods that directly call the API.
//! * Derived methods - DshApiClient methods that add extra capabilities
//!   but depend on the API methods.
//!
//! # API methods
//!
//! [`DshApiClient`] methods that directly call the DSH resource management API.
//!
//! * [`create_volume(id, configuration)`](DshApiClient::create_volume)
//! * [`delete_volume(id)`](DshApiClient::delete_volume)
//! * [`get_volume(id) -> volume_status`](DshApiClient::get_volume)
//! * [`get_volume_allocation_status(id) -> allocation_status`](DshApiClient::get_volume_allocation_status)
//! * [`get_volume_configuration(id) -> volume`](DshApiClient::get_volume_configuration)
//! * [`list_volume_ids() -> [id]`](DshApiClient::list_volume_ids)
//!
//! # Derived methods
//!
//! [`DshApiClient`] methods that add extra capabilities but do not directly call the
//! DSH resource management API. These derived methods depend on the API methods for this.
//!
//! * [`get_volume_with_usage(id) -> [volume_status, [usage]]`](DshApiClient::get_volume_with_usage)
//! * [`list_volumes_with_usage() -> [id, [usage]]`](DshApiClient::list_volumes_with_usage)
#![cfg_attr(feature = "actual", doc = "")]
#![cfg_attr(feature = "actual", doc = "## Actual configuration methods")]
#![cfg_attr(feature = "actual", doc = "* [`get_volume_actual_configuration(volume_id) -> Volume`](DshApiClient::get_volume_actual_configuration)")]

use crate::dsh_api_client::DshApiClient;
use crate::types::{AllocationStatus, Volume, VolumeStatus};
#[allow(unused_imports)]
use crate::DshApiError;
use crate::{app, application, DshApiResult, UsedBy};
use futures::try_join;

/// # Manage volumes
///
/// Module that contains methods and functions to manage volumes.
/// * API methods - DshApiClient methods that directly call the API.
/// * Derived methods - DshApiClient methods that add extra capabilities
///   but depend on the API methods.
///
/// # API methods
///
/// [`DshApiClient`] methods that directly call the DSH resource management API.
///
/// * [`create_volume(id, configuration)`](DshApiClient::create_volume)
/// * [`delete_volume(id)`](DshApiClient::delete_volume)
/// * [`get_volume(id) -> volume_status`](DshApiClient::get_volume)
/// * [`get_volume_allocation_status(id) -> allocation_status`](DshApiClient::get_volume_allocation_status)
/// * [`get_volume_configuration(id) -> volume`](DshApiClient::get_volume_configuration)
/// * [`list_volume_ids() -> [id]`](DshApiClient::list_volume_ids)
///
/// # Derived methods
///
/// [`DshApiClient`] methods that add extra capabilities but do not directly call the
/// DSH resource management API. These derived methods depend on the API methods for this.
///
/// * [`get_volume_with_usage(id) -> [volume_status, [usage]]`](DshApiClient::get_volume_with_usage)
/// * [`list_volumes_with_usage() -> [id, [usage]]`](DshApiClient::list_volumes_with_usage)
#[cfg_attr(feature = "actual", doc = "")]
#[cfg_attr(feature = "actual", doc = "## Actual configuration methods")]
#[cfg_attr(feature = "actual", doc = "* [`get_volume_actual_configuration(volume_id) -> Volume`](DshApiClient::get_volume_actual_configuration)")]
impl DshApiClient<'_> {
  /// # Create volume
  ///
  /// API function: `PUT /allocation/{tenant}/volume/{id}/configuration`
  ///
  /// # Parameters
  /// * `volume_id` - name of the created volume
  /// * `configuration` - configuration for the created volume
  ///
  /// # Returns
  /// * `Ok(())` - when DSH has properly received the request
  ///              (note that this does not mean that the volume has been successfully created)
  /// * `Err<`[`DshApiError`]`>` - when the request could not be processed by the DSH
  pub async fn create_volume(&self, volume_id: &str, configuration: &Volume) -> DshApiResult<()> {
    self
      .process(
        self
          .generated_client
          .put_volume_configuration_by_tenant_by_id(self.tenant_name(), volume_id, self.token(), configuration)
          .await,
      )
      .map(|(_, result)| result)
  }

  /// # Delete volume
  ///
  /// API function: `DELETE /allocation/{tenant}/volume/{id}/configuration`
  ///
  /// # Parameters
  /// * `volume_id` - name of the volume to delete
  ///
  /// # Returns
  /// * `Ok(())` - when DSH has properly received the request
  ///              (note that this does not mean that the volume has been successfully deleted)
  /// * `Err<`[`DshApiError`]`>` - when the request could not be processed by the DSH
  pub async fn delete_volume(&self, volume_id: &str) -> DshApiResult<()> {
    self
      .process(
        self
          .generated_client
          .delete_volume_configuration_by_tenant_by_id(self.tenant_name(), volume_id, self.token())
          .await,
      )
      .map(|(_, result)| result)
  }

  /// # Return volume
  ///
  /// API function: `GET /allocation/{tenant}/volume/{id}`
  ///
  /// # Parameters
  /// * `volume_id` - name of the requested volume
  ///
  /// # Returns
  /// * `Ok<`[`VolumeStatus`]`>` - volume status
  /// * `Err<`[`DshApiError`]`>` - when the request could not be processed by the DSH
  pub async fn get_volume(&self, volume_id: &str) -> DshApiResult<VolumeStatus> {
    self
      .process(self.generated_client.get_volume_by_tenant_by_id(self.tenant_name(), volume_id, self.token()).await)
      .map(|(_, result)| result)
  }

  /// # Return volume allocation status
  ///
  /// API function: `GET /allocation/{tenant}/volume/{id}/status`
  ///
  /// # Parameters
  /// * `volume_id` - name of the requested volume
  ///
  /// # Returns
  /// * `Ok<`[`AllocationStatus`]`>` - volume allocation status
  /// * `Err<`[`DshApiError`]`>` - when the request could not be processed by the DSH
  pub async fn get_volume_allocation_status(&self, volume_id: &str) -> DshApiResult<AllocationStatus> {
    self
      .process(
        self
          .generated_client
          .get_volume_status_by_tenant_by_id(self.tenant_name(), volume_id, self.token())
          .await,
      )
      .map(|(_, result)| result)
  }

  /// # Return volume configuration
  ///
  /// API function: `GET /allocation/{tenant}/volume/{id}/configuration`
  ///
  /// # Parameters
  /// * `volume_id` - name of the requested volume
  ///
  /// # Returns
  /// * `Ok<`[`Volume`]`>` - volume configuration
  /// * `Err<`[`DshApiError`]`>` - when the request could not be processed by the DSH
  pub async fn get_volume_configuration(&self, volume_id: &str) -> DshApiResult<Volume> {
    self
      .process(
        self
          .generated_client
          .get_volume_configuration_by_tenant_by_id(self.tenant_name(), volume_id, self.token())
          .await,
      )
      .map(|(_, result)| result)
  }

  /// # Return actual volume configuration
  ///
  /// API function: `GET /allocation/{tenant}/volume/{id}/actual`
  ///
  /// # Parameters
  /// * `volume_id` - name of the requested volume
  ///
  /// # Returns
  /// * `Ok<`[`Volume`]`>` - volume configuration
  /// * `Err<`[`DshApiError`]`>` - when the request could not be processed by the DSH
  #[cfg(feature = "actual")]
  pub async fn get_volume_actual_configuration(&self, volume_id: &str) -> DshApiResult<Volume> {
    self
      .process(
        self
          .generated_client
          .get_volume_actual_by_tenant_by_id(self.tenant_name(), volume_id, self.token())
          .await,
      )
      .map(|(_, result)| result)
  }

  /// # Return sorted list of volume names
  ///
  /// API function: `GET /allocation/{tenant}/volume`
  ///
  /// # Returns
  /// * `Ok<Vec<String>>` - list of volume names
  /// * `Err<`[`DshApiError`]`>` - when the request could not be processed by the DSH
  pub async fn list_volume_ids(&self) -> DshApiResult<Vec<String>> {
    let mut volume_ids: Vec<String> = self
      .process(self.generated_client.get_volume_by_tenant(self.tenant_name(), self.token()).await)
      .map(|(_, result)| result)
      .map(|secret_ids| secret_ids.iter().map(|secret_id| secret_id.to_string()).collect())?;
    volume_ids.sort();
    Ok(volume_ids)
  }

  /// # Get volume with usage
  ///
  /// Returns configuration and usage for a given volume.
  ///
  /// # Parameters
  /// * `volume_id` - name of the requested volume
  ///
  /// # Returns
  /// * `Ok<(VolumeStatus, Vec<UsedBy>)>` - volume status and usage.
  /// * `Err<`[`DshApiError`]`>` - when the request could not be processed by the DSH
  pub async fn get_volume_with_usage(&self, volume_id: &str) -> DshApiResult<(VolumeStatus, Vec<UsedBy>)> {
    let (volume_status, applications, apps) = try_join!(self.get_volume(volume_id), self.get_applications(), self.get_app_configurations())?;
    let mut usages: Vec<UsedBy> = vec![];
    for (application_id, application, injections) in application::find_applications_that_use_volume(volume_id, &applications) {
      usages.push(UsedBy::Application(application_id, application.instances, injections));
    }
    for (app_id, _, resource_ids) in app::find_apps_that_use_volume(volume_id, &apps) {
      usages.push(UsedBy::App(app_id, resource_ids));
    }
    Ok((volume_status, usages))
  }

  /// # List all volumes with usage
  ///
  /// Returns a list of all volumes together with the apps and applications that use them.
  ///
  /// # Returns
  /// * `Ok<Vec<(String, Vec<UsedBy>>>` - list of tuples
  ///   containing the secret id and a vector of usages, which can be empty.
  /// * `Err<`[`DshApiError`]`>` - when the request could not be processed by the DSH
  pub async fn list_volumes_with_usage(&self) -> DshApiResult<Vec<(String, Vec<UsedBy>)>> {
    let (volume_ids, applications, apps) = try_join!(self.list_volume_ids(), self.get_applications(), self.get_app_configurations())?;
    let mut volumes_with_usage: Vec<(String, Vec<UsedBy>)> = vec![];
    for volume_id in volume_ids {
      let mut usages: Vec<UsedBy> = vec![];
      for (application_id, application, injections) in application::find_applications_that_use_volume(volume_id.as_str(), &applications) {
        usages.push(UsedBy::Application(application_id, application.instances, injections));
      }
      for (app_id, _, resource_ids) in app::find_apps_that_use_volume(volume_id.as_str(), &apps) {
        usages.push(UsedBy::App(app_id, resource_ids));
      }
      volumes_with_usage.push((volume_id, usages));
    }
    Ok(volumes_with_usage)
  }
}