Skip to main content

dsh_api/
proxy.rs

1//! # Additional methods to manage proxies
2//!
3//! Module that contains methods and functions to manage proxies.
4//!
5//! # Generated methods
6//! [`DshApiClient`] methods that are generated from the `openapi` specification.
7//!
8//! * [`delete_kafkaproxy_configuration(id)`](DshApiClient::delete_kafkaproxy_configuration)
9//! * [`get_kafkaproxy_actual(id) -> KafkaProxy`](DshApiClient::get_kafkaproxy_actual)
10//! * [`get_kafkaproxy_configuration(id) -> KafkaProxy`](DshApiClient::get_kafkaproxy_configuration)
11//! * [`get_kafkaproxy_ids() -> Vec<String>`](DshApiClient::get_kafkaproxy_ids)
12//! * [`get_kafkaproxy_status(id) -> AllocationStatus`](DshApiClient::get_kafkaproxy_status)
13//! * [`put_kafkaproxy_configuration(id, proxy)`](DshApiClient::put_kafkaproxy_configuration)
14//!
15//! # Derived methods
16//! [`DshApiClient`] methods that add extra capabilities but do not directly call the
17//! DSH resource management API. These derived methods depend on the API methods for this.
18//!
19//! * [`proxies() -> Vec<(String, KafkaProxy)>`](DshApiClient::proxies)
20//! * [`proxies_dependant_on_certificate(id) -> Vec<DependantProxy>`](DshApiClient::proxies_dependant_on_certificate)
21//! * [`proxies_dependant_on_secret(id) -> Vec<DependantProxy>`](DshApiClient::proxies_dependant_on_secret)
22
23use crate::dsh_api_client::DshApiClient;
24use crate::error::DshApiResult;
25use crate::types::KafkaProxy;
26use crate::DependantProxy;
27#[allow(unused_imports)]
28use crate::DshApiError;
29use futures::future::try_join_all;
30use itertools::Itertools;
31
32/// # Additional methods to manage proxies
33///
34/// Module that contains methods and functions to manage proxies.
35/// * Derived methods - DshApiClient methods that add extra capabilities
36///   but depend on the API methods.
37///
38/// # Derived methods
39///
40/// [`DshApiClient`] methods that add extra capabilities but do not directly call the
41/// DSH resource management API. These derived methods depend on the API methods for this.
42///
43/// * [`proxies() -> Vec<(String, KafkaProxy)>`](DshApiClient::proxies)
44/// * [`proxies_dependant_on_certificate(id) -> Vec<DependantProxy>`](DshApiClient::proxies_dependant_on_certificate)
45/// * [`proxies_dependant_on_secret(id) -> Vec<DependantProxy>`](DshApiClient::proxies_dependant_on_secret)
46impl DshApiClient {
47  /// # Return proxy configurations
48  ///
49  /// # Returns
50  /// * `Ok<(String, KafkaProxy)>` - Tuple containing the proxy ids and proxy configurations.
51  /// * `Err<`[`DshApiError`]`>` - When the request could not be processed.
52  pub async fn proxies(&self) -> DshApiResult<Vec<(String, KafkaProxy)>> {
53    let proxy_ids = self.get_kafkaproxy_ids().await?;
54    let proxies = try_join_all(proxy_ids.iter().map(|proxy_id| self.get_kafkaproxy_configuration(proxy_id))).await?;
55    Ok(proxy_ids.into_iter().zip(proxies).collect_vec())
56  }
57
58  /// # Get all proxies that depend on a certificate
59  ///
60  /// # Parameters
61  /// * `certificate_id` - Identifier of the certificate.
62  ///
63  /// # Returns
64  /// * `Ok<Vec<DependantApp>>` - Apps that depend on the certificate.
65  /// * `Err<`[`DshApiError`]`>` - When the request could not be processed by the DSH.
66  pub async fn proxies_dependant_on_certificate(&self, certificate_id: &str) -> DshApiResult<Vec<DependantProxy>> {
67    Ok(
68      proxies_that_use_certificate(certificate_id, &self.proxies().await?)
69        .into_iter()
70        .map(|(proxy_id, proxy)| DependantProxy::new(proxy_id.to_string(), proxy.instances.get()))
71        .collect_vec(),
72    )
73  }
74
75  /// # Get all apps that depend on a secret
76  ///
77  /// # Parameters
78  /// * `secret_name` - Name of the secret.
79  ///
80  /// # Returns
81  /// * `Ok<Vec<DependantApp>>` - Apps that depend on the secret.
82  /// * `Err<`[`DshApiError`]`>` - When the request could not be processed by the DSH.
83  pub async fn proxies_dependant_on_secret(&self, secret_name: &str) -> DshApiResult<Vec<DependantProxy>> {
84    Ok(
85      proxies_that_use_secret(secret_name, &self.proxies().await?)
86        .into_iter()
87        .map(|(proxy_id, proxy)| DependantProxy::new(proxy_id.to_string(), proxy.instances.get()))
88        .collect_vec(),
89    )
90  }
91}
92
93/// Find proxies that use a certificate
94///
95/// # Parameters
96/// * `certificate_id` - Identifier of the certificate to look for.
97/// * `proxies` - List of all proxies.
98///
99/// # Returns
100/// `Vec<(proxy_id, proxy)>` - Vector of proxies that use the certificate:
101/// * `proxy_id` - Proxy id of the proxy that uses the certificates.
102/// * `proxy` - Reference to the proxy.
103pub fn proxies_that_use_certificate<'a>(certificate_id: &str, proxies: &'a [(String, KafkaProxy)]) -> Vec<(&'a str, &'a KafkaProxy)> {
104  proxies
105    .iter()
106    .filter(|(_, proxy)| proxy.certificate == certificate_id)
107    .map(|(proxy_id, proxy)| (proxy_id.as_str(), proxy))
108    .collect_vec()
109}
110
111/// Find proxies that use a secret
112///
113/// # Parameters
114/// * `secret_name` - Name of the secret to look for.
115/// * `proxies` - List of all proxies.
116///
117/// # Returns
118/// `Vec<(proxy_id, proxy)>` - Vector of proxies that use the secret:
119/// * `proxy_id` - Proxy id of the proxy that uses the secret.
120/// * `proxy` - Reference to the proxy.
121pub fn proxies_that_use_secret<'a>(secret_name: &str, proxies: &'a [(String, KafkaProxy)]) -> Vec<(&'a str, &'a KafkaProxy)> {
122  proxies
123    .iter()
124    .filter(|(_, proxy)| proxy.secret_name_ca_chain == secret_name)
125    .map(|(proxy_id, proxy)| (proxy_id.as_str(), proxy))
126    .collect_vec()
127}