dsh_api 0.9.0

DSH resource management API client
Documentation
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! # Additional methods to manage topics
//!
//! Module that contains methods and functions to manage topics.
//!
//! # Generated methods
//!
//! [`DshApiClient`] methods that are generated from the `openapi` specification.
//!
//! * [`delete_topic_configuration(id)`](DshApiClient::delete_topic_configuration)
//! * [`get_topic(id) -> TopicStatus`](DshApiClient::get_topic)
//! * [`get_topic_actual(id) -> Topic`](DshApiClient::get_topic_actual)
//! * [`get_topic_configuration(id) -> Topic`](DshApiClient::get_topic_configuration)
//! * [`get_topic_ids() -> [id]`](DshApiClient::get_topic_ids)
//! * [`get_topic_status(id) -> AllocationStatus`](DshApiClient::get_topic_status)
//! * [`put_topic_configuration(id, body)`](DshApiClient::put_topic_configuration)
//!
//! # 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.
//!
//! * [`topic_dependant_applications(topic id) -> [application id, instances, [injection]]`](DshApiClient::topic_dependant_applications)
//! * [`topic_dependant_apps(topic id) -> [app id, [resource]]`](DshApiClient::topic_dependant_apps)
//! * [`topic_dependants(topic id) -> [id, [dependant]]`](DshApiClient::topic_dependants)
//! * [`topics_with_dependant_applications() -> [topic id, [application id, instances, [injection]]]`](DshApiClient::topics_with_dependant_applications)
//! * [`topics_with_dependant_apps() -> [topic id, [app id, [resource]]]`](DshApiClient::topics_with_dependant_apps)
//! * [`topics_with_dependants() -> [topic id, [dependant]]`](DshApiClient::topics_with_dependants)

use crate::app::{app_resources, apps_that_use_topic};
use crate::application_types::ApplicationValues;
use crate::dsh_api_client::DshApiClient;
use crate::error::DshApiResult;
use crate::parse::TopicString;
use crate::types::{AppCatalogApp, AppCatalogAppResourcesValue, Application, Topic};
use crate::{Dependant, DependantApp, DependantApplication};
use futures::try_join;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::{Display, Formatter};

/// # Describes an injection of a resource in an application
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
pub enum TopicInjection {
  /// Environment variable injection, where the value is the name of the environment variable.
  #[serde(rename = "env")]
  EnvVar { env_var_name: String },
  /// Topic injection, where the value is the name of the topic.
  #[serde(rename = "topic")]
  Topic { topic_name: String },
}

impl TopicInjection {
  pub(crate) fn env_var<T>(env_var: T) -> Self
  where
    T: Into<String>,
  {
    Self::EnvVar { env_var_name: env_var.into() }
  }

  pub(crate) fn topic<T>(topic_name: T) -> Self
  where
    T: Into<String>,
  {
    Self::Topic { topic_name: topic_name.into() }
  }
}

impl Display for TopicInjection {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    match self {
      TopicInjection::EnvVar { env_var_name } => write!(f, "{}", env_var_name),
      TopicInjection::Topic { topic_name } => write!(f, "{{ topic('{}') }}", topic_name),
    }
  }
}

/// # Additional method to manage Kafka topics
///
/// Module that contains methods to manage Kafka topics.
/// * Derived methods - DshApiClient methods that add extra capabilities
///   but depend on the API methods.
///
/// # 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.
///
/// * [`topic_dependant_applications(topic id) -> [application id, instances, [injection]]`](DshApiClient::topic_dependant_applications)
/// * [`topic_dependant_apps(topic id) -> [app id, [resource]]`](DshApiClient::topic_dependant_apps)
/// * [`topic_dependants(topic id) -> [id, [dependant]]`](DshApiClient::topic_dependants)
/// * [`topics_with_dependant_applications() -> [topic id, [application id, instances, [injection]]]`](DshApiClient::topics_with_dependant_applications)
/// * [`topics_with_dependant_apps() -> [topic id, [app id, [resource]]]`](DshApiClient::topics_with_dependant_apps)
/// * [`topics_with_dependants() -> [topic id, [dependant]]`](DshApiClient::topics_with_dependants)
impl DshApiClient {
  /// # Returns dependants of a topic
  ///
  /// # Parameters
  /// * `topic_id` - Identifies the scratch topic.
  ///
  /// Returns a sorted list of all topics together with the applications and apps that use them.
  pub async fn topic_dependant_applications(&self, topic_id: &str) -> DshApiResult<Vec<DependantApplication<TopicInjection>>> {
    let application_configuration_map = self.get_application_configuration_map().await?;
    let mut dependant_applications = Vec::<DependantApplication<TopicInjection>>::new();
    for application in topic_env_vars_from_applications(topic_id, &application_configuration_map) {
      dependant_applications.push(DependantApplication::new(
        application.id.to_string(),
        application.application.instances,
        application.values.iter().map(|(env_var, _)| TopicInjection::env_var(*env_var)).collect_vec(),
      ));
    }
    Ok(dependant_applications)
  }

  /// # Returns dependants of a topic
  ///
  /// # Parameters
  /// * `topic_id` - Identifies the scratch topic.
  ///
  /// Returns a sorted list of all topics together with the applications and apps that use them.
  pub async fn topic_dependant_apps(&self, topic_id: &str) -> DshApiResult<Vec<DependantApp>> {
    let appcatalogapp_configuration_map = self.get_appcatalogapp_configuration_map().await?;
    let mut dependant_apps = Vec::<DependantApp>::new();
    for (app_id, _, resource_ids) in apps_that_use_topic(topic_id, &appcatalogapp_configuration_map) {
      dependant_apps.push(DependantApp::new(
        app_id.to_string(),
        resource_ids.iter().map(|resource_id| resource_id.to_string()).collect_vec(),
      ));
    }
    Ok(dependant_apps)
  }

  /// # Returns dependants of a topic
  ///
  /// # Parameters
  /// * `topic_id` - Identifies the scratch topic.
  ///
  /// Returns a sorted list of all topics together with the applications and apps that use them.
  pub async fn topic_dependants(&self, topic_id: &str) -> DshApiResult<Vec<Dependant<TopicInjection>>> {
    let (application_configuration_map, appcatalogapp_configuration_map) = try_join!(self.get_application_configuration_map(), self.get_appcatalogapp_configuration_map())?;
    let mut dependants = Vec::<Dependant<TopicInjection>>::new();
    for application in topic_injections_from_applications(topic_id, &application_configuration_map) {
      dependants.push(Dependant::service(
        application.id,
        application.application.instances,
        application.values.iter().cloned().collect_vec(),
      ));
    }
    for (app_id, _, resource_ids) in apps_that_use_topic(topic_id, &appcatalogapp_configuration_map) {
      dependants.push(Dependant::app(
        app_id.to_string(),
        resource_ids.iter().map(|resource_id| resource_id.to_string()).collect_vec(),
      ));
    }
    dependants.sort();
    Ok(dependants)
  }

  /// # Returns all topics with dependant applications
  ///
  /// Returns a sorted list of all topics together with the applications that use them.
  pub async fn topics_with_dependant_applications(&self) -> DshApiResult<Vec<(String, Vec<DependantApplication<TopicInjection>>)>> {
    let (topic_ids, application_configuration_map) = try_join!(self.get_topic_ids(), self.get_application_configuration_map())?;
    let mut topics = Vec::<(String, Vec<DependantApplication<TopicInjection>>)>::new();
    for topic_id in topic_ids {
      let mut dependant_applications: Vec<DependantApplication<TopicInjection>> = vec![];
      for application in topic_env_vars_from_applications(topic_id.as_str(), &application_configuration_map) {
        dependant_applications.push(DependantApplication::new(
          application.id.to_string(),
          application.application.instances,
          application.values.iter().map(|(env_var, _)| TopicInjection::env_var(*env_var)).collect_vec(),
        ));
      }
      topics.push((topic_id, dependant_applications));
    }
    Ok(topics)
  }

  /// # Returns all topics with dependant apps
  ///
  /// Returns a sorted list of all topics together with the apps that use them.
  pub async fn topics_with_dependant_apps(&self) -> DshApiResult<Vec<(String, Vec<DependantApp>)>> {
    let (topic_ids, apps) = try_join!(self.get_topic_ids(), self.get_appcatalogapp_configuration_map())?;
    let mut topics = Vec::<(String, Vec<DependantApp>)>::new();
    for topic_id in topic_ids {
      let mut dependant_apps: Vec<DependantApp> = vec![];
      for (app_id, _, resource_ids) in apps_that_use_topic(topic_id.as_str(), &apps) {
        dependant_apps.push(DependantApp::new(
          app_id.to_string(),
          resource_ids.iter().map(|resource_id| resource_id.to_string()).collect_vec(),
        ));
      }
      topics.push((topic_id, dependant_apps));
    }
    Ok(topics)
  }

  /// # Returns all topics with dependant applications and apps
  ///
  /// Returns a sorted list of all topics together with the applications and apps that use them.
  pub async fn topics_with_dependants(&self) -> DshApiResult<Vec<(String, Vec<Dependant<TopicInjection>>)>> {
    let (topic_ids, application_configuration_map, appcatalogapp_configuration_map) = try_join!(
      self.get_topic_ids(),
      self.get_application_configuration_map(),
      self.get_appcatalogapp_configuration_map()
    )?;
    let mut topics = Vec::<(String, Vec<Dependant<TopicInjection>>)>::new();
    for topic_id in topic_ids {
      let mut dependants: Vec<Dependant<TopicInjection>> = vec![];
      for application in topic_env_vars_from_applications(topic_id.as_str(), &application_configuration_map) {
        dependants.push(Dependant::service(
          application.id,
          application.application.instances,
          application.values.iter().map(|(env_var, _)| TopicInjection::env_var(*env_var)).collect_vec(),
        ));
      }
      for (app_id, _, resource_ids) in apps_that_use_topic(topic_id.as_str(), &appcatalogapp_configuration_map) {
        dependants.push(Dependant::app(
          app_id.to_string(),
          resource_ids.iter().map(|resource_id| resource_id.to_string()).collect_vec(),
        ));
      }
      topics.push((topic_id, dependants));
    }
    Ok(topics)
  }
}

/// # Get application environment variables referencing topic
///
/// Get all environment variables referencing topic `topic_id` from an `Application`.
/// When the application does not reference the topic, an empty list will be returned.
///
/// # Parameters
/// * `topic_name` - Identifies the topic to look for.
/// * `application` - Reference to the `Application`.
///
/// # Returns
/// `Vec<&str>` - List of all environment variables referencing topic `topic_id`
///
/// The list is sorted by environment variable key.
pub fn topic_env_vars_from_application<'a>(topic_name: &str, application: &'a Application) -> Vec<(&'a str, TopicString<'a>)> {
  let mut env_var_keys: Vec<(&str, TopicString)> = application
    .env
    .iter()
    .filter_map(|(env_key, env_value)| {
      TopicString::try_from(env_value.as_str())
        .ok()
        .and_then(|topic| if topic.name() == topic_name { Some((env_key.as_str(), topic)) } else { None })
    })
    .collect_vec();
  env_var_keys.sort_by_key(|(env_key, _)| *env_key);
  env_var_keys
}

/// # Get applications environment variables referencing topics
///
/// Get all environment variables referencing topic `topic_id` from multiple `Application`s.
/// Applications are only included if they reference topic `topic_id` at least once.
///
/// # Parameters
/// * `topic_id` - Identifier of the topic to look for.
/// * `applications` - Hashmap containing id/application pairs.
///
/// # Returns
/// `Vec<ApplicationValue<&str>>` - List of application values containing:
/// * application id,
/// * application reference,
/// * sorted list of environment variable keys that reference topic `topic_id`.
///
/// The list is sorted by application id.
pub fn topic_env_vars_from_applications<'a>(topic_id: &str, applications: &'a HashMap<String, Application>) -> Vec<ApplicationValues<'a, (&'a str, TopicString<'a>)>> {
  let mut application_injections: Vec<ApplicationValues<(&'a str, TopicString<'a>)>> = vec![];
  for (application_id, application) in applications {
    let environment_variable_keys = topic_env_vars_from_application(topic_id, application);
    if !environment_variable_keys.is_empty() {
      application_injections.push(ApplicationValues::new(application_id, application, environment_variable_keys));
    }
  }
  application_injections.sort();
  application_injections
}

/// # Get application environment variables referencing topic
///
/// Get all environment variables referencing topic `topic_id` from an `Application`.
/// When the application does not reference the topic, an empty list will be returned.
///
/// # Parameters
/// * `topic_name` - Identifies the topic to look for.
/// * `application` - Reference to the `Application`.
///
/// # Returns
/// `Vec<&str>` - List of all topic injections referencing topic `topic_id`
///
/// The list is sorted by environment variable key.
pub fn topic_injections_from_application(topic_name: &str, application: &Application) -> Vec<TopicInjection> {
  let mut topic_injections: Vec<TopicInjection> = vec![];
  for (env_key, env_value) in &application.env {
    if let Ok(topic) = TopicString::try_from(env_value.as_str()) {
      if topic.name() == topic_name {
        topic_injections.push(TopicInjection::env_var(env_key))
      }
    }
  }
  for application_topic in &application.topics {
    if let Ok(topic) = TopicString::try_from(application_topic.as_str()) {
      if topic.name() == topic_name {
        topic_injections.push(TopicInjection::topic(application_topic))
      }
    }
  }
  topic_injections.sort();
  topic_injections
}

/// # Get applications environment variables referencing topics
///
/// Get all environment variables referencing topic `topic_id` from multiple `Application`s.
/// Applications are only included if they reference topic `topic_id` at least once.
///
/// # Parameters
/// * `topic_id` - Identifier of the topic to look for.
/// * `applications` - Hashmap containing id/application pairs.
///
/// # Returns
/// `Vec<ApplicationValue<&str>>` - List of application values containing:
/// * application id,
/// * application reference,
/// * sorted list of environment variable keys that reference topic `topic_id`.
///
/// The list is sorted by application id.
pub fn topic_injections_from_applications<'a>(topic_id: &str, applications: &'a HashMap<String, Application>) -> Vec<ApplicationValues<'a, TopicInjection>> {
  let mut application_injections: Vec<ApplicationValues<TopicInjection>> = vec![];
  for (application_id, application) in applications {
    let environment_variable_keys = topic_injections_from_application(topic_id, application);
    if !environment_variable_keys.is_empty() {
      application_injections.push(ApplicationValues::new(application_id, application, environment_variable_keys));
    }
  }
  application_injections.sort();
  application_injections
}

/// Get topic resources from `AppCatalogApp`
///
/// # Parameters
/// * `app` - app to get the topic resources from
///
/// # Returns
/// Either `None` when the `app` does not have any topic resources,
/// or a `Some` that contains tuples describing the topic resources:
/// * resource id
/// * reference to the `Topic`
pub fn topic_resources_from_app(app: &AppCatalogApp) -> Vec<(&str, &Topic)> {
  app_resources(app, &|resource_value| match resource_value {
    AppCatalogAppResourcesValue::Topic(topic) => Some(topic),
    _ => None,
  })
}

/// # Check whether `topic_id` in used in an `Application`
///
/// # Parameters
/// * `topic_id` - id of the topic to look for
/// * `application` - reference to the `Application`
///
/// # Returns
/// * `true` - topic `topic_id` is used in `application`
/// * `false` - topic `topic_id` is not used in `application`
pub fn topic_used_in_application(topic_id: &str, application: &Application) -> bool {
  application.topics.iter().any(|id| id == topic_id)
}

/// # Get all `Applications` that use scratch topic
///
/// Get all `Applications` that use the scratch topic with `topic_id`.
///
/// # Parameters
/// * `topic_id` - id of the scratch topic to look for
/// * `applications` - hashmap containing id/application pairs
///
/// # Returns
/// `Vec<(&str, &application)>` - list of tuples containing:
/// * application id
/// * application reference
///
/// The list is sorted by application id.
pub fn topic_used_in_applications<'a>(topic_id: &str, applications: &'a HashMap<String, Application>) -> Vec<(&'a str, &'a Application)> {
  let mut application_tuples: Vec<(&str, &Application)> = applications
    .iter()
    .filter_map(|(application_id, application)| if topic_used_in_application(topic_id, application) { Some((application_id.as_str(), application)) } else { None })
    .collect_vec();
  application_tuples.sort_by(|(id_a, _), (id_b, _)| id_a.cmp(id_b));
  application_tuples
}

/// # Get all scratch topic ids from an `Application`
///
/// # Parameters
/// * `application` - reference to the `Application`
///
/// # Returns
/// `Vec<&str>` - sorted list of the ids of the scratch topics used in `application`.
pub fn topics_from_application(application: &Application) -> Vec<&str> {
  application.topics.iter().map(|topic_id| topic_id.as_str()).collect_vec()
}

/// # Get scratch topic ids from applications
///
/// Get all scratch topic ids from all `Application`s.
/// Application will only be included of they contain at least one scratch topic.
///
/// # Parameters
/// * `applications` - hashmap containing id/application pairs
///
/// # Returns
/// `Vec<ApplicationValues<&str>>` - list of tuples containing:
/// * application id
/// * application reference
/// * lists of scratch topic ids used in the applications, sorted by id
///
/// The list will be sorted by application id.
pub fn topics_from_applications(applications: &HashMap<String, Application>) -> Vec<ApplicationValues<&str>> {
  let mut application_tuples = applications
    .iter()
    .filter_map(|(application_id, application)| {
      let mut application_topics = topics_from_application(application);
      if !application_topics.is_empty() {
        application_topics.sort();
        Some(ApplicationValues::new(application_id, application, application_topics))
      } else {
        None
      }
    })
    .collect_vec();
  application_tuples.sort();
  application_tuples
}