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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#![doc(
  html_favicon_url = "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeD0iMHB4IiB5PSIwcHgiCiAgICAgdmlld0JveD0iMCAwIDE3MS4zIDE4Mi45IiBzdHlsZT0iZW5hYmxlLWJhY2tncm91bmQ6bmV3IDAgMCAxNzEuMyAxODIuOTsiIHhtbDpzcGFjZT0icHJlc2VydmUiPgogICAgPHN0eWxlPgoJCSNrcG5fbG9nbyB7CgkJCWZpbGw6IGJsYWNrOwoJCX0KCgkJQG1lZGlhIChwcmVmZXJzLWNvbG9yLXNjaGVtZTogZGFyaykgewoJCQkja3BuX2xvZ28gewoJCQkJZmlsbDogd2hpdGU7CgkJCX0KCQl9Cgk8L3N0eWxlPgogICAgPGcgaWQ9Imtwbl9sb2dvIj4KCQk8cGF0aCBkPSJNMTYxLjcsNzIuMWMtNS40LTUuNC0xNS4zLTExLjgtMzIuMi0xMS44Yy0zLjEsMC02LjIsMC4yLTkuMSwwLjZsLTAuOSwwLjFsMC4zLDAuOWMwLjgsMi42LDEuNCw1LjUsMS44LDguNGwwLjEsMC44CgkJCWwwLjgtMC4xYzIuNC0wLjMsNC43LTAuNCw3LTAuNGMxMy40LDAsMjEsNC44LDI1LDguOGM0LjIsNC4yLDYuNSw5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jItMjQuNWMwLTQuNS0yLjYtNi44LTcuOS02LjhDOTIsNzkuOCw5MS40LDc5LjksOTAuOSw3OS45eiIvPgoJCTxwYXRoIGQ9Ik0xMTkuMyw5My4xYzAuOC0yLDEuMy01LjMsMS42LTcuN2M4LjMtMC4zLDE0LjYsNC41LDE0LjYsMTEuMmMwLDUuNC0yLjgsMTAuMi0xNC4yLDE5LjljLTIuOS0xLTYuOC0yLjEtMTAuMS0yLjgKCQkJYzExLjItMTAuNSwxMy0xMy4zLDEzLTE2LjRDMTI0LjEsOTUuMSwxMjIuNSw5My42LDExOS4zLDkzLjF6Ii8+CgkJPHBhdGggZD0iTTg3LDEzMC4yYzguNCwwLDE3LDEuMSwyNS45LDMuOGwzLTEwYy0xMC0zLTE5LjgtNC4yLTI5LTQuMmMtOS4yLDAtMTguOSwxLjItMjksNC4ybDMsMTBDNzAsMTMxLjMsNzguNiwxMzAuMiw4NywxMzAuMnoiCgkJLz4KCQk8cmVjdCB4PSI4MC41IiB5PSI0OS4zIiB0cmFuc2Zvcm09Im1hdHJpeCgwLjcwNzIgLTAuNzA3MSAwLjcwNzEgMC43MDcyIC0xMy45OTkyIDc3Ljg3NDQpIiB3aWR0aD0iMTMuMSIKCQkJICBoZWlnaHQ9IjEzLjEiLz4KCTwvZz4KPC9zdmc+Cg=="
)]

//! # DSH resource management API
//!
//! This crate contains functions and definitions that provide support for using the functions
//! of the DSH resource management API. The crate was originally developed as part of the
//! [`dsh`](https://github.com/kpn-dsh/dsh) command line tool,
//! but has now been promoted to a separate library.
//!
//! # [`DshApiClient`]
//!
//! The crate consists basically of the struct [`DshApiClient`],
//! which has many associated methods.
//! In order to use these methods, you first need to acquire an instance of the struct.
//! This is a two-step process.
//!
//! * First you need to get an instance of
//!   [`DshApiClientFactory`](dsh_api_client_factory::DshApiClientFactory) or [`DshApiPlatformClientFactory`](dsh_api_client_factory::DshApiPlatformClientFactory).
//! * Once you have the client factory instance, you can call its
//!   [`client()`](dsh_api_client_factory::DshApiClientFactory::client) method to create the
//!   [`DshApiClient`].
//!
//! You can now call the client's methods to interact with the DSH resource management API.
//!
//! # [`types`]
//!
//! For their parameters and return values the methods and functions in the crate
//! make use of rust `struct`s that where generated from the DSH resource management API
//! Openapi specification (version 1.10.0).
//!
//! The generated types are defined as follows:
//!
//! ```rust
//! # use serde::{Deserialize, Serialize};
//! #[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
//! pub struct Secret {
//!   pub name: String,
//!   pub value: String,
//! }
//! ```
//!
//! * All types are `pub`.
//! * All fields are `pub`.
//! * All types have derived implementations of the [`Clone`], [`Debug`], [`Deserialize`],
//!   [`PartialEq`] and [`Serialize`] traits.
//! * Some [selected types](default) also have an implementation of the [`Default`] trait.
//! * Some [selected types](display) also have an implementation of the [`Display`] trait.
//! * Some [selected types](new) also have a `new` function.
//!
//! # Examples
//!
//! ## Minimal example
//!
//! The first minimal example will print a list of all the applications that are deployed
//! in a tenant environment. This example requires that the tenant's name,
//! platform and API password are configured via [environment variables](dsh_api_client_factory).
//!
//! ```ignore
//! use dsh_api::dsh_api_client_factory::DshApiClientFactory;
//!
//! # use dsh_api::error::DshApiResult;
//! # async fn hide() -> DshApiResult<()> {
//! let client = DshApiClientFactory::default().client().await?;
//! for (application_id, application) in client.list_applications()? {
//!   println!("{} -> {}", application_id, application);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## More elaborate example
//!
//! In the next, more elaborate example, these tenant parameters are given explicitly.
//! This example will list all the applications in the tenant environment that have been
//! configured to require a token in order to access the Kafka broker.
//! This is accomplished via the `find_applications()`
//! methods, that returns a list of all applications for which the provided predicate
//! evaluates to `true`.
//!
//! ```ignore
//! # use dsh_api::dsh_api_client_factory::DshApiClientFactory;
//! # use dsh_api::dsh_api_tenant::DshApiTenant;
//! # use dsh_api::platform::DshPlatform;
//! # use dsh_api::types::Application;
//! # use dsh_api::error::DshApiResult;
//! # async fn hide() -> DshApiResult<()> {
//! let tenant = DshApiTenant::new("my-tenant", DshPlatform::try_from("np-aws-lz-dsh")?);
//! let password = "...";
//! let client_factory = DshApiClientFactory::create(tenant, password)?;
//! let client = client_factory.client().await?;
//! let predicate = |application: &Application| application.needs_token;
//! let applications = client.find_applications(&predicate).await?;
//! for (_, application) in applications {
//!   println!("{}", application);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Features
//!
//! By enabling/disabling the features described below you have control over what's included
//! in your library and what's not.
//! All features are disabled by default.
//!
//! The following features are defined:
//!
//! * `generic` - Enables the generic methods, which allows calling all api operations by name.
//! * `manage` -  Enables the manage modules [`stream`] and [`tenant`], which support creating
//!   managed streams and tenants. This feature is only useful when you have the proper
//!   authorizations for these capabilities.
//! * `robot` - Enables the
//!   [`post_robot_generate_secret()`](DshApiClient::post_robot_generate_secret) operation, which
//!   will generate a new robot password, invalidating the old password.

/// # Types generated from openapi file
#[allow(clippy::clone_on_copy)]
#[allow(clippy::derivable_impls)]
#[allow(clippy::large_enum_variant)]
pub mod types {
  include!(concat!(env!("OUT_DIR"), "/types.rs"));
}

include!(concat!(env!("OUT_DIR"), "/methods.rs"));

/// Openapi specification version 1.11.1
pub static OPENAPI_SPEC: &str = include_str!(concat!(env!("OUT_DIR"), "/openapi.json"));

/// Specification of default platforms
pub static DEFAULT_PLATFORMS: &str = include_str!("../default-platforms.json");

use crate::error::DshApiError;
use crate::version::Version;
use chrono::{TimeZone, Utc};
use itertools::Itertools;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::fmt::{Debug, Display, Formatter};
use std::sync::LazyLock;

pub mod app;
pub mod application;
pub mod application_types;
pub mod bucket;
pub mod certificate;
pub mod database;
pub mod default;
pub mod display;
pub mod dsh_api_client;
pub mod dsh_api_client_factory;
pub mod dsh_api_tenant;
pub mod dsh_jwt;
pub mod error;
#[cfg(feature = "generic")]
pub mod generic;
pub mod manifest;
pub mod new;
pub mod nodepool;
pub mod parse;
pub mod platform;
pub mod proxy;
pub mod query_processor;
pub mod secret;
#[cfg(feature = "manage")]
pub mod stream;
#[cfg(feature = "manage")]
pub mod tenant;
pub mod token_fetcher;
pub mod topic;
pub mod version;
pub mod vhost;
pub mod volume;

/// # Returns the version of the lib crate
///
/// ## Example
///
/// ```
/// # use dsh_api::version::Version;
/// assert_eq!(dsh_api::crate_version(), &Version::new(0, 9, 0, None));
/// ```
pub fn crate_version() -> &'static Version {
  static CRATE_VERSION: LazyLock<Version> = LazyLock::new(|| Version::new(0, 9, 0, None));
  &CRATE_VERSION
}

/// # Returns the version of the openapi spec
///
/// Version number of the openapi file that the crate has been generated from.
///
/// ## Example
///
/// ```
/// # use dsh_api::version::Version;
/// assert_eq!(dsh_api::openapi_version(), &Version::new(1, 11, 1, None));
/// ```
pub fn openapi_version() -> &'static Version {
  DshApiClient::api_version()
}

/// # Indicates access rights
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum AccessRights {
  /// Indicates read access to a resource
  Read,
  /// Indicates read and write access to a resource
  ReadWrite,
  /// Indicates write access to a resource
  Write,
}

/// # Describes an app dependency
///
/// There are a number of methods that return whether a certain resource (e.g. a secret,
/// a volume or an environment variable) is used by a dependant app.
/// This struct represents one usage of the resource by an app.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct DependantApp {
  /// App identifier
  pub app_id: String,
  /// Resources that the app depends on
  pub resources: Vec<String>,
}

impl DependantApp {
  pub fn new(app_id: String, resources: Vec<String>) -> Self {
    Self { app_id, resources }
  }
}

impl Display for DependantApp {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}: {}", self.app_id, self.resources.join(", "))
  }
}

impl PartialOrd<Self> for DependantApp {
  /// Ordering uses `app_id` only
  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
    Some(self.cmp(other))
  }
}

impl Ord for DependantApp {
  /// Ordering uses `app_id` only
  fn cmp(&self, other: &Self) -> Ordering {
    self.app_id.cmp(&other.app_id)
  }
}

/// # Describes an application dependency
///
/// There are a number of methods that return whether a certain resource (e.g. a secret,
/// a volume or an environment variable) is used by a dependant application.
/// This struct represents one usage of the resource by an application.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct DependantApplication<T> {
  /// Identifies the dependant application.
  pub application_id: String,
  /// Number of instances of the dependant application.
  pub instances: u64,
  /// Injections that the dependencies originate from.
  pub injections: Vec<T>,
}

impl<T> DependantApplication<T> {
  pub fn new(application_id: String, instances: u64, injections: Vec<T>) -> Self {
    Self { application_id, instances, injections }
  }
}

impl<T> Display for DependantApplication<T>
where
  T: Display,
{
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", self.application_id)?;
    if !self.injections.is_empty() {
      write!(f, ": {}", self.injections.iter().map(|inj| inj.to_string()).collect_vec().join(", "))?
    }
    Ok(())
  }
}

impl<T: Ord> PartialOrd<Self> for DependantApplication<T> {
  /// Ordering uses `application_id` only
  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
    Some(self.cmp(other))
  }
}

impl<T: Ord> Ord for DependantApplication<T> {
  /// Ordering uses `application_id` only
  fn cmp(&self, other: &Self) -> Ordering {
    self.application_id.cmp(&other.application_id)
  }
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum CertificateSecretKind {
  CertChainSecret,
  KeySecret,
  PassphraseSecret,
}

impl Display for CertificateSecretKind {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::CertChainSecret => write!(f, "cert-chain-secret"),
      Self::KeySecret => write!(f, "key-secret"),
      Self::PassphraseSecret => write!(f, "passphrase-secret"),
    }
  }
}

/// # Describes a certificate dependency
///
/// There are a number of methods that return whether a certain resource (e.g. a secret) is
/// used by a certificate. This struct represents one usage of the resource by a certificate.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct DependantCertificate {
  /// Identifies the dependant certificate
  pub certificate_id: String,
  /// Kind of secret for the Certificate dependency.
  pub secret_kind: CertificateSecretKind,
}

impl DependantCertificate {
  pub fn new(certificate_id: String, secret_kind: CertificateSecretKind) -> Self {
    Self { certificate_id, secret_kind }
  }
}

impl Display for DependantCertificate {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}:{}", self.certificate_id, self.secret_kind)
  }
}

impl PartialOrd<Self> for DependantCertificate {
  /// Ordering uses `certificate_id` only
  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
    Some(self.cmp(other))
  }
}

impl Ord for DependantCertificate {
  /// Ordering uses `certificate_id` only
  fn cmp(&self, other: &Self) -> Ordering {
    self.certificate_id.cmp(&other.certificate_id)
  }
}

/// # Describes a proxy dependency
///
/// There are a number of methods that return whether a certain resource (e.g. a secret,
/// or a certificate) is used by a proxy.
/// This struct represents one usage of the resource by a proxy.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct DependantProxy {
  /// Identifies the dependant proxy
  pub proxy_id: String,
  /// Number of instances of the dependant application
  pub instances: u64,
}

impl DependantProxy {
  pub fn new(proxy_id: String, instances: u64) -> Self {
    Self { proxy_id, instances }
  }
}

impl Display for DependantProxy {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", self.proxy_id)
  }
}

impl PartialOrd<Self> for DependantProxy {
  /// Ordering uses `proxy_id` only
  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
    Some(self.cmp(other))
  }
}

impl Ord for DependantProxy {
  /// Ordering uses `proxy_id` only
  fn cmp(&self, other: &Self) -> Ordering {
    self.proxy_id.cmp(&other.proxy_id)
  }
}

/// # Describes a Trifonius dependency
///
/// There are a number of methods that return whether a certain resource (e.g. a secret,
/// a volume or an environment variable) is used by a dependant application.
/// This struct represents one usage of the resource by an application.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct DependantTrifonius<T> {
  /// Identifies the dependant application.
  pub trifonius_id: String,
  /// Number of instances of the dependant application.
  pub instances: u64,
  /// Injections that the dependencies originate from.
  pub injections: Vec<T>,
}

static TRIFONIUS_ID_REGEX_NEW: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^([a-z])-([a-z0-9]{8})-([a-z])-([a-z0-9]{8})-([a-z])-(.+)$").unwrap());
static TRIFONIUS_ID_REGEX_OLD: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"^([a-z])-([a-z0-9]{6})-([a-z])-([a-z0-9]{6})-([a-z])-([a-z0-9]{6})-(.+)$").unwrap());

/// Returns a string slice with the Trifonius prefix removed
///
/// If the string starts with a Trifonius prefix this function returns the substring after
/// the prefix, wrapped in `Some`. If the string does not start with a Trifonius prefix
/// `None` will be returned.
///
/// There are two types of Trifonius prefixes:
/// * `f-df87c48f-n-5104548e-p-sleep` (new syntax),
/// * `p-6aythn-v-a6hrkl-n-7bvetd-metadata-manager` (old syntax).
///
/// Although the old syntax will not be used anymore, it is still supported for older builds.
///
/// # Examples
///
/// ```
/// # use dsh_api::strip_trifonius_prefix;
/// assert_eq!(strip_trifonius_prefix("f-df87c48f-n-5104548e-p-sleep"), Some("sleep"));
/// ```
pub fn strip_trifonius_prefix(id: &str) -> Option<&str> {
  match TRIFONIUS_ID_REGEX_NEW.captures(id) {
    Some(captures_new) => captures_new.get(6).map(|matching| matching.as_str()),
    None => match TRIFONIUS_ID_REGEX_OLD.captures(id) {
      Some(captures_old) => captures_old.get(7).map(|matching| matching.as_str()),
      None => None,
    },
  }
}

impl<T> DependantTrifonius<T> {
  pub fn new(application_id: String, instances: u64, injections: Vec<T>) -> Self {
    Self { trifonius_id: application_id, instances, injections }
  }

  pub fn try_new(application_id: &str, instances: u64, injections: Vec<T>) -> DshApiResult<Self> {
    if let Some(trifonius_prefix) = strip_trifonius_prefix(application_id) {
      Ok(Self { trifonius_id: trifonius_prefix.to_string(), instances, injections })
    } else {
      Err(DshApiError::NotFound { message: None })
    }
  }
}

impl<T> Display for DependantTrifonius<T>
where
  T: Display,
{
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    write!(f, "tr:{}", self.trifonius_id)?;
    if !self.injections.is_empty() {
      write!(f, ": {}", self.injections.iter().map(|inj| inj.to_string()).collect_vec().join(", "))?
    }
    Ok(())
  }
}

impl<T: Ord> PartialOrd<Self> for DependantTrifonius<T> {
  /// Ordering uses `application_id` only
  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
    Some(self.cmp(other))
  }
}

impl<T: Ord> Ord for DependantTrifonius<T> {
  /// Ordering uses `application_id` only
  fn cmp(&self, other: &Self) -> Ordering {
    self.trifonius_id.cmp(&other.trifonius_id)
  }
}

/// # Describes a app or application dependency
///
/// There are a number of methods that return whether a certain resource (e.g. a secret,
/// a volume or an environment variable) is used by a dependant app or application.
/// This enum represents one usage of the resource by an app or application.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum Dependant<T> {
  /// Identifies an app dependent on the resource
  App { app: DependantApp },
  /// Identifies an application dependent on the resource
  Application { application: DependantApplication<T> },
  /// Identifies a certificate dependent on the resource
  Certificate { certificate: DependantCertificate },
  /// Identifies a proxy dependent on the resource
  Proxy { proxy: DependantProxy },
  /// Identifies a Trifonius dependent on the resource
  Trifonius { trifonius: DependantTrifonius<T> },
}

impl<T> Dependant<T> {
  pub fn app(app_id: String, resources: Vec<String>) -> Self {
    Dependant::App { app: DependantApp::new(app_id, resources) }
  }

  pub fn application(application_id: String, instances: u64, injections: Vec<T>) -> Self {
    Dependant::Application { application: DependantApplication::new(application_id, instances, injections) }
  }

  pub fn certificate(certificate_id: String, secret_kind: CertificateSecretKind) -> Self {
    Dependant::Certificate { certificate: DependantCertificate::new(certificate_id, secret_kind) }
  }

  pub fn proxy(proxy_id: String, instances: u64) -> Self {
    Dependant::Proxy { proxy: DependantProxy::new(proxy_id, instances) }
  }

  pub fn service(service_id: &str, instances: u64, injections: Vec<T>) -> Self {
    if let Some(trifonius_id) = strip_trifonius_prefix(service_id) {
      Self::trifonius(trifonius_id.to_string(), instances, injections)
    } else {
      Self::application(service_id.to_string(), instances, injections)
    }
  }

  pub fn trifonius(trifonius_id: String, instances: u64, injections: Vec<T>) -> Self {
    Dependant::Trifonius { trifonius: DependantTrifonius::new(trifonius_id, instances, injections) }
  }

  pub fn id(&self) -> &str {
    match self {
      Self::App { app } => &app.app_id,
      Self::Application { application } => &application.application_id,
      Self::Certificate { certificate } => &certificate.certificate_id,
      Self::Proxy { proxy } => &proxy.proxy_id,
      Self::Trifonius { trifonius } => &trifonius.trifonius_id,
    }
  }
}

impl<T: Display> Display for Dependant<T> {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::App { app } => Display::fmt(app, f),
      Self::Application { application } => Display::fmt(application, f),
      Self::Certificate { certificate } => Display::fmt(certificate, f),
      Self::Proxy { proxy } => Display::fmt(proxy, f),
      Self::Trifonius { trifonius } => Display::fmt(trifonius, f),
    }
  }
}

impl<T: PartialOrd> PartialOrd<Self> for Dependant<T> {
  /// Ordering uses `id` only
  fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
    Some(self.id().cmp(other.id()))
  }
}

impl<T: Ord> Ord for Dependant<T> {
  /// Ordering uses `id` only
  fn cmp(&self, other: &Self) -> Ordering {
    self.id().cmp(other.id())
  }
}

impl AccessRights {
  /// Checks whether read access is granted
  pub fn has_read_access(&self) -> bool {
    self == &Self::Read || self == &Self::ReadWrite
  }

  /// Checks whether write access is granted
  pub fn has_write_access(&self) -> bool {
    self == &Self::Write || self == &Self::ReadWrite
  }

  pub fn from(read_access: bool, write_access: bool) -> Option<Self> {
    match (read_access, write_access) {
      (false, false) => None,
      (false, true) => Some(AccessRights::Write),
      (true, false) => Some(AccessRights::Read),
      (true, true) => Some(AccessRights::ReadWrite),
    }
  }
}

impl Display for AccessRights {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::Read => write!(f, "read"),
      Self::ReadWrite => write!(f, "read/write"),
      Self::Write => write!(f, "write"),
    }
  }
}

// Environment variable used to specify the name of a file with an alternative list of platforms
pub(crate) const ENV_VAR_PLATFORMS_FILE_NAME: &str = "DSH_API_PLATFORMS_FILE";

// Environment variable used to define the target platform
pub(crate) const ENV_VAR_PLATFORM: &str = "DSH_API_PLATFORM";

// Environment variable used to define the client tenant
pub(crate) const ENV_VAR_TENANT: &str = "DSH_API_TENANT";

// Converts epoch timestamp in seconds to utc representation
pub(crate) fn epoch_seconds_to_string(timestamp: impl Into<i64>) -> String {
  Utc.timestamp_opt(timestamp.into(), 0).single().map(|ts| ts.to_string()).unwrap_or_default()
}

// Converts epoch timestamp in milliseconds to utc representation
pub(crate) fn epoch_milliseconds_to_string(timestamp: impl Into<i64>) -> String {
  epoch_seconds_to_string(timestamp.into() / 1000)
}

#[test]
fn test_epoch_seconds_to_string() {
  const REPRESENTATION: &str = "2000-01-01 00:00:00 UTC";
  assert_eq!(epoch_seconds_to_string(946684800_i64), REPRESENTATION);
  assert_eq!(epoch_seconds_to_string(946684800_u64 as i64), REPRESENTATION);
  assert_eq!(epoch_seconds_to_string(946684800_u128 as i64), REPRESENTATION);
  assert_eq!(epoch_seconds_to_string(946684800.0_f64 as i64), REPRESENTATION);
}

#[test]
fn test_epoch_milliseconds_to_string() {
  const REPRESENTATION: &str = "2000-01-01 00:00:00 UTC";
  assert_eq!(epoch_milliseconds_to_string(946684800000_i64), REPRESENTATION);
  assert_eq!(epoch_milliseconds_to_string(946684800000_u64 as i64), REPRESENTATION);
  assert_eq!(epoch_milliseconds_to_string(946684800000_u128 as i64), REPRESENTATION);
  assert_eq!(epoch_milliseconds_to_string(946684800000.0_f64 as i64), REPRESENTATION);
}