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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
//! # Additional methods to manage manifest
//!
//! Module that contains methods and functions to manage manifests.
//!
//! # Generated methods
//!
//! [`DshApiClient`] methods that are generated from the `openapi` specification.
//!
//! * [`get_appcatalog_manifests() -> [AppCatalogManifest]`](DshApiClient::get_appcatalog_manifests)
//!
//! # 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.
//!
//! * [`manifest(manifest id, version) -> [manifest]`](DshApiClient::manifest)
//! * [`manifest_latest_version(manifest id, draft) -> [manifest]`](DshApiClient::manifest_latest_version)
//! * [`manifest_all_versions(manifest id) -> [manifest]`](DshApiClient::manifest_all_versions)
//! * [`manifests() -> [manifest]`](DshApiClient::manifests)
//! * [`manifest_raw(manifest id, version) -> (raw, draft)`](DshApiClient::manifest_raw)
//! * [`manifest_raw_latest(manifest id, draft) -> (version, raw, draft)`](DshApiClient::manifest_raw_latest)
//! * [`manifest_ids() -> [manifest id]`](DshApiClient::manifest_ids)
//! * [`manifest_ids_versions() -> [(manifest id, [(version, draft)])]`](DshApiClient::manifest_ids_versions)
//! * [`manifest_all_versions() -> [(manifest id, [manifest])]`](DshApiClient::manifest_all_versions)
//! * [`manifests_latest_version(draft) -> [(manifest id, manifest)]`](DshApiClient::manifests_latest_version)
use crate::dsh_api_client::DshApiClient;
use crate::epoch_milliseconds_to_string;
use crate::error::DshApiResult;
use crate::types::AppCatalogManifest;
use crate::version::Version;
#[allow(unused_imports)]
use crate::DshApiError;
use itertools::Itertools;
use serde::de::Error;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::{from_str, Value};
use std::collections::{HashMap, HashSet};
use std::fmt::{Debug, Display, Formatter};
use std::str::FromStr;

/// # Additional methods to manage the app catalog manifests
///
/// Module that contains methods and functions to query the App Catalog for all manifest files.
///
/// # Derived methods
/// * [`manifest(manifest id, version) -> [manifest]`](DshApiClient::manifest)
/// * [`manifest_latest_version(manifest id, draft) -> [manifest]`](DshApiClient::manifest_latest_version)
/// * [`manifest_all_versions(manifest id) -> [manifest]`](DshApiClient::manifest_all_versions)
/// * [`manifests() -> [manifest]`](DshApiClient::manifests)
/// * [`manifest_raw(manifest id, version) -> (raw, draft)`](DshApiClient::manifest_raw)
/// * [`manifest_raw_latest(manifest id, draft) -> (version, raw, draft)`](DshApiClient::manifest_raw_latest)
/// * [`manifest_ids() -> [manifest id]`](DshApiClient::manifest_ids)
/// * [`manifest_ids_versions() -> [(manifest id, [(version, draft)])]`](DshApiClient::manifest_ids_versions)
/// * [`manifest_all_versions() -> [(manifest id, [manifest])]`](DshApiClient::manifest_all_versions)
/// * [`manifests_latest_version(draft) -> [(manifest id, manifest)]`](DshApiClient::manifests_latest_version)
impl DshApiClient {
  /// # Return manifest by id and version
  ///
  /// Returns the manifest with the specified id and version.
  ///
  /// # Parameters
  /// * `manifest_id` - Identifier of the requested manifest.
  /// * `manifest_version` - Version of the requested manifest.
  ///
  /// # Returns
  /// * `Ok<Manifest>` - manifest
  /// * `Err<`[`DshApiError::NotFound`]`>` - when the manifest could not be found
  /// * `Err<`[`DshApiError`]`>` - when the request could not be processed by the DSH
  pub async fn manifest(&self, manifest_id: &str, manifest_version: &Version) -> DshApiResult<Manifest> {
    self
      .manifests()
      .await?
      .iter()
      .find(|manifest| manifest.id == manifest_id && manifest.version == *manifest_version)
      .cloned()
      .ok_or(DshApiError::not_found())
  }

  /// # Return latest version of manifest by id
  ///
  /// Returns the latest version of the manifest with the specified id.
  ///
  /// # Parameters
  /// * `manifest_id` - Identifier of the requested manifest.
  /// * `allow_draft_version` - Whether the returned manifest can be a draft manifest or not.
  ///
  /// # Returns
  /// * `Ok<Manifest>` - Manifest.
  /// * `Err<`[`DshApiError::NotFound`]`>` - When no manifest could be found.
  /// * `Err<`[`DshApiError`]`>` - When the request could not be processed.
  pub async fn manifest_latest_version(&self, manifest_id: &str, allow_draft_version: bool) -> DshApiResult<Manifest> {
    match self.manifest_all_versions(manifest_id).await {
      Ok(manifests) => match manifests.into_iter().filter(|manifest| !manifest.draft || allow_draft_version).next_back() {
        Some(latest_manifest) => Ok(latest_manifest),
        None => Err(DshApiError::not_found()),
      },
      Err(_) => Err(DshApiError::not_found()),
    }
  }

  /// # Return all versions of manifest by id
  ///
  /// Returns a list of all versions of an app catalog manifest with the provided manifest_id.
  ///
  /// # Parameters
  /// * `manifest_id` - Identifier of the requested manifest.
  ///
  /// # Returns
  /// * `Ok<Vec<manifest>>` - List of version/manifest pairs sorted by version.
  /// * `Err<`[`DshApiError::NotFound`]`>` - When the manifest could not be found.
  /// * `Err<`[`DshApiError`]`>` - When the request could not be processed.
  pub async fn manifest_all_versions(&self, manifest_id: &str) -> DshApiResult<Vec<Manifest>> {
    let mut manifests: Vec<Manifest> = self.manifests().await?.into_iter().filter(|manifest| manifest.id == manifest_id).collect_vec();
    if manifests.is_empty() {
      Err(DshApiError::not_found())
    } else {
      manifests.sort_by(|manifest_a, manifest_b| manifest_a.version.cmp(&manifest_b.version));
      Ok(manifests)
    }
  }

  /// # Return all manifests
  ///
  /// # Returns
  /// * `Ok<Vec<Manifest>>` - Vector containing Manifest objects in unspecified order.
  /// * `Err<`[`DshApiError`]`>` - When the request could not be processed.
  pub async fn manifests(&self) -> DshApiResult<Vec<Manifest>> {
    self.get_appcatalog_manifests().await?.iter().map(Manifest::try_from).try_collect()
  }

  /// # Return raw manifest by id and version
  ///
  /// Returns the raw manifest as a json formatted string.
  ///
  /// # Parameters
  /// * `manifest_id` - Identifier of the requested manifest.
  /// * `manifest_version` - Version of the requested manifest.
  ///
  /// # Returns
  /// * `Ok<(manifest, draft)>` - Manifest as a json formatted string.
  /// * `Err<`[`DshApiError::NotFound`]`>` - When the manifest could not be found.
  /// * `Err<`[`DshApiError`]`>` - When the request could not be processed.
  pub async fn manifest_raw(&self, manifest_id: &str, manifest_version: &Version) -> DshApiResult<(String, bool)> {
    for app_catalog_manifest in self.get_appcatalog_manifests().await?.iter() {
      let payload = from_str::<HashMap<String, Value>>(app_catalog_manifest.payload.as_str())?;
      if payload.get("id").is_some_and(|payload_id| payload_id.as_str().unwrap() == manifest_id)
        && payload
          .get("version")
          .is_some_and(|version_value| Version::from_str(version_value.as_str().unwrap()).unwrap() == *manifest_version)
      {
        return Ok((serde_json::to_string_pretty(&payload)?, app_catalog_manifest.draft));
      }
    }
    Err(DshApiError::not_found())
  }

  /// # Return raw manifest latest version
  ///
  /// Returns the latest version of raw manifest as a json formatted string.
  ///
  /// # Parameters
  /// * `manifest_id` - Identifier of the requested manifest.
  /// * `allow_draft_version` - Whether the returned raw manifest can be a draft manifest or not.
  ///
  /// # Returns
  /// * `Ok<(version, manifest, draft)>` - Tuple consisting of version, manifest as a json
  ///   formatted string and whether the manifest is a draft.
  /// * `Err<`[`DshApiError::NotFound`]`>` - When the manifest could not be found.
  /// * `Err<`[`DshApiError`]`>` - When the request could not be processed.
  pub async fn manifest_raw_latest(&self, manifest_id: &str, allow_draft_version: bool) -> DshApiResult<(Version, String, bool)> {
    let mut raw_manifests: Vec<(Version, bool, HashMap<String, Value>)> = self
      .get_appcatalog_manifests()
      .await?
      .iter()
      .filter(|manifest| !manifest.draft || allow_draft_version)
      .filter_map(|manifest| match from_str::<HashMap<String, Value>>(manifest.payload.as_str()) {
        Ok(payload) => {
          if payload.get("id").is_some_and(|payload_id| payload_id.as_str().unwrap() == manifest_id) {
            match payload.get("version").map(|version_value| Version::from_str(version_value.as_str().unwrap())) {
              Some(Ok(version)) => Some((version, manifest.draft, payload)),
              _ => None,
            }
          } else {
            None
          }
        }
        Err(_) => None,
      })
      .collect_vec();
    raw_manifests.sort_by(|(version_a, _, _), (version_b, _, _)| version_a.cmp(version_b));
    match raw_manifests.last() {
      Some((last_version, draft, last_payload)) => Ok((last_version.clone(), serde_json::to_string_pretty(&last_payload)?, *draft)),
      None => Err(DshApiError::not_found()),
    }
  }

  /// # Return sorted list of all App Catalog manifest ids
  ///
  /// # Returns
  /// * `Ok<Vec<String>>` - Vector containing all manifest ids sorted.
  /// * `Err<`[`DshApiError`]`>` - When the request could not be processed.
  pub async fn manifest_ids(&self) -> DshApiResult<Vec<String>> {
    let mut ids: Vec<String> = self
      .manifests()
      .await?
      .iter()
      .map(|manifest| manifest.id.clone())
      .collect::<HashSet<_>>()
      .into_iter()
      .collect();
    ids.sort();
    Ok(ids)
  }

  /// # Return list of all available versions of all manifests
  ///
  /// # Returns
  /// * `Ok<Vec<(id, Vec<(version, draft)>)>>` - Vector containing pairs of manifest ids and
  ///   lists of versions, sorted by id and version.
  /// * `Err<`[`DshApiError`]`>` - When the request could not be processed.
  pub async fn manifest_ids_versions(&self) -> DshApiResult<Vec<(String, Vec<(Version, bool)>)>> {
    let mut id_versions_map: HashMap<String, Vec<(Version, bool)>> = HashMap::new();
    for manifest in self.manifests().await? {
      id_versions_map
        .entry(manifest.id)
        .and_modify(|versions| versions.push((manifest.version.clone(), manifest.draft)))
        .or_insert(vec![(manifest.version, manifest.draft)]);
    }
    let mut id_versions_pairs: Vec<(String, Vec<(Version, bool)>)> = id_versions_map.iter().map(|(id, versions)| (id.to_string(), versions.clone())).collect();
    id_versions_pairs.sort_by(|(id_a, _), (id_b, _)| id_a.cmp(id_b));
    for (_, versions) in id_versions_pairs.iter_mut() {
      versions.sort();
    }
    Ok(id_versions_pairs)
  }

  /// # Return list of all manifests with all available manifest versions
  ///
  /// # Returns
  /// * `Ok<Vec<(id, Vec<manifest>)>>` - vector containing pairs of ids and versions,
  ///   sorted by id and manifest version
  /// * `Err<`[`DshApiError`]`>` - when the request could not be processed by the DSH
  pub async fn manifests_all_versions(&self) -> DshApiResult<Vec<(String, Vec<Manifest>)>> {
    let mut id_manifests: Vec<(String, Vec<Manifest>)> = self
      .manifests()
      .await?
      .into_iter()
      .map(|manifest| (manifest.id.clone(), manifest))
      .into_group_map()
      .into_iter()
      .collect_vec()
      .into_iter()
      .map(|(manifest_id, mut manifests)| {
        manifests.sort_by(|manifest_a, manifest_b| manifest_a.version.cmp(&manifest_b.version));
        (manifest_id, manifests)
      })
      .collect_vec();
    id_manifests.sort_by(|(id_a, _), (id_b, _)| id_a.cmp(id_b));
    Ok(id_manifests)
  }

  /// # Return list of all latest versions of App Catalog manifests
  ///
  /// # Parameters
  /// * allow_draft - whether the returned latest manifests can be a draft manifests or not
  ///
  /// # Returns
  /// * `Ok<Vec<(id, manifest)>>` - vector containing tuples of ids, versions and
  ///   manifests, sorted by id
  /// * `Err<`[`DshApiError`]`>` - when the request could not be processed by the DSH
  pub async fn manifests_latest_version(&self, allow_draft: bool) -> DshApiResult<Vec<(String, Manifest)>> {
    let manifests_grouped_by_id: Vec<(String, Vec<Manifest>)> = self
      .manifests()
      .await?
      .into_iter()
      .map(|manifest| (manifest.id.clone(), manifest))
      .collect_vec()
      .into_iter()
      .into_group_map()
      .into_iter()
      .collect_vec();
    let mut latest_manifests: Vec<(String, Manifest)> = manifests_grouped_by_id
      .into_iter()
      .filter_map(|(id, manifests)| {
        manifests
          .into_iter()
          .filter(|manifest| !manifest.draft || allow_draft)
          .max_by_key(|manifest| manifest.version.clone())
          .map(|manifest| (id, manifest))
      })
      .collect_vec();
    latest_manifests.sort_by(|(id_a, _), (id_b, _)| id_a.cmp(id_b));
    Ok(latest_manifests)
  }
}

/// Describes a manifest from the app catalog
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Manifest {
  #[serde(skip_deserializing)]
  pub draft: bool,
  #[serde(skip_deserializing)]
  pub last_modified: String,
  pub id: String,
  pub name: String,
  pub version: Version,
  pub vendor: String,
  pub kind: Option<String>,
  #[serde(rename = "apiVersion")]
  pub api_version: Option<String>,
  pub description: Option<String>,
  #[serde(rename = "moreInfo")]
  pub more_info: Option<String>,
  pub contact: String,
  pub configuration: Option<Configuration>,
  #[serde(deserialize_with = "deserialize_resource_map")]
  pub resources: HashMap<String, Resource>,
}

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

impl TryFrom<&AppCatalogManifest> for Manifest {
  type Error = DshApiError;

  fn try_from(app_catalog_manifest: &AppCatalogManifest) -> Result<Self, Self::Error> {
    from_str::<Manifest>(app_catalog_manifest.payload.as_str())
      .map(|payload| Manifest { draft: app_catalog_manifest.draft, last_modified: epoch_milliseconds_to_string(app_catalog_manifest.last_modified as i64), ..payload })
      .map_err(DshApiError::from)
  }
}

/// Configuration of a manifest
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Configuration {
  #[serde(rename = "$schema")]
  pub schema: String,
  #[serde(rename = "type")]
  pub kind: String,
  pub properties: HashMap<String, Property>,
}

/// Property of a manifest configuration
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Property {
  pub description: String,
  #[serde(rename = "type")]
  pub kind: PropertyKind,
  #[serde(rename = "enum")]
  pub enumeration: Option<Vec<String>>,
  pub default: Option<String>,
}

impl Display for Property {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    fn display_enumeration(enumeration: &[String], default: Option<&String>) -> String {
      enumeration
        .iter()
        .map(
          |enumeration_value| {
            if default.is_some_and(|default_value| default_value == enumeration_value) {
              format!("{}*", enumeration_value)
            } else {
              enumeration_value.to_string()
            }
          },
        )
        .join("|")
    }

    match self.kind {
      PropertyKind::DnsZone => write!(f, "dns-zone"),
      PropertyKind::Number => {
        if let Some(enumeration) = &self.enumeration {
          write!(f, "number:{}", display_enumeration(enumeration, Option::from(&self.default)))
        } else if let Some(default_value) = &self.default {
          write!(f, "number:default={}", default_value)
        } else {
          write!(f, "number")
        }
      }
      PropertyKind::String => {
        if let Some(enumeration) = &self.enumeration {
          write!(f, "string:{}", display_enumeration(enumeration, Option::from(&self.default)))
        } else if let Some(default_value) = &self.default {
          write!(f, "string:default=\"{}\"", default_value)
        } else {
          write!(f, "string")
        }
      }
    }
  }
}

/// Indicates the manifest configuration property type
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum PropertyKind {
  #[serde(rename = "dns-zone")]
  DnsZone,
  #[serde(rename = "number")]
  Number,
  #[serde(rename = "string")]
  String,
}

impl Display for PropertyKind {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    match &self {
      PropertyKind::DnsZone => write!(f, "dns-zone"),
      PropertyKind::Number => write!(f, "number"),
      PropertyKind::String => write!(f, "string"),
    }
  }
}

/// Resource that a manifest depends upon
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum Resource {
  Application { application: Box<ApplicationResource> },
  Bucket { bucket: Box<BucketResource> },
  Certificate { certificate: Box<CertificateResource> },
  Database { database: Box<DatabaseResource> },
  Secret { secret: Box<SecretResource> },
  Topic { topic: Box<TopicResource> },
  Vhost { vhost: Box<VhostResource> },
  Volume { volume: Box<VolumeResource> },
}

impl Display for Resource {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::Application { application } => Display::fmt(&application, f),
      Self::Bucket { bucket } => Display::fmt(&bucket, f),
      Self::Certificate { certificate } => Display::fmt(&certificate, f),
      Self::Database { database } => Display::fmt(&database, f),
      Self::Secret { secret } => Display::fmt(&secret, f),
      Self::Topic { topic } => Display::fmt(&topic, f),
      Self::Vhost { vhost } => Display::fmt(&vhost, f),
      Self::Volume { volume } => Display::fmt(&volume, f),
    }
  }
}

/// Manifest configuration property data type
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(try_from = "Value", into = "Value")]
pub enum Numerical {
  Float { value: f64 },
  Integer { value: i64 },
  Template { template: String },
}

impl Display for Numerical {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::Float { value } => write!(f, "{}", value),
      Self::Integer { value } => write!(f, "{}", value),
      Self::Template { template } => write!(f, "{}", template),
    }
  }
}

/// Manifest application dependency
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ApplicationResource {
  pub cpus: Numerical,
  pub env: HashMap<String, String>,
  #[serde(rename = "exposedPorts")]
  pub exposed_ports: Option<HashMap<String, ExposedPort>>,
  pub image: String,
  #[serde(rename = "imageConsole")]
  pub image_console: Option<String>,
  pub instances: Numerical,
  pub mem: Numerical,
  pub metrics: Option<Metrics>,
  pub name: String,
  #[serde(rename = "needsToken")]
  pub needs_token: bool,
  pub secrets: Option<Vec<Secret>>,
  #[serde(rename = "singleInstance")]
  pub single_instance: bool,
  pub user: String,
}

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

/// Manifest bucket dependency
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct BucketResource {
  pub encrypted: bool,
  pub name: String,
  pub versioned: bool,
}

impl Display for BucketResource {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", self.name)?;
    if self.encrypted {
      write!(f, ":encrypted")?;
    }
    if self.versioned {
      write!(f, ":versioned")?;
    }
    Ok(())
  }
}

/// Manifest certificate dependency (plain string since this dependency is undocumented)
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct CertificateResource {
  pub unformatted_representation: String,
}

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

/// Manifest database dependency
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct DatabaseResource {
  pub cpus: Numerical,
  pub extensions: Vec<String>,
  pub instances: Numerical,
  pub mem: Numerical,
  pub name: String,
  #[serde(rename = "snapshotInterval")]
  pub snapshot_interval: Numerical,
  pub version: String,
  #[serde(rename = "volumeSize")]
  pub volume_size: Numerical,
}

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

/// Manifest secret dependency (plain string since this dependency is undocumented)
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct SecretResource {
  pub unformatted_representation: String,
}

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

/// Manifest topic dependency
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct TopicResource {
  #[serde(rename = "kafkaProperties")]
  pub kafka_properties: Option<HashMap<String, String>>,
  pub name: String,
  pub partitions: i64,
  #[serde(rename = "replicationFactor")]
  pub replication_factor: i64,
}

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

/// Manifest vhost dependency (plain string since this dependency is undocumented)
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct VhostResource {
  pub unformatted_representation: String,
}

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

/// Manifest volume dependency
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct VolumeResource {
  pub name: String,
  pub size: Numerical,
}

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

/// Used in application resource
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct ExposedPort {
  pub auth: Option<String>,
  pub tls: Option<String>,
  pub vhost: String,
}

impl Display for ExposedPort {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    write!(f, "{}", self.vhost)?;
    if let Some(auth) = &self.auth {
      write!(f, ":{}", auth)?;
    }
    if let Some(tls) = &self.tls {
      write!(f, ":{}", tls)?;
    }
    Ok(())
  }
}

/// Used in application resource
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Injection {
  pub env: String,
}

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

/// Used in application resource
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Secret {
  pub injections: Vec<Injection>,
  pub name: String,
}

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

/// Used in application resource
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct Metrics {
  pub path: String,
  pub port: i64,
}

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

fn deserialize_resource_map<'de, D>(deserializer: D) -> Result<HashMap<String, Resource>, D::Error>
where
  D: Deserializer<'de>,
{
  HashMap::<String, Value>::deserialize(deserializer).and_then(|deserialized_map| {
    deserialized_map
      .iter()
      .map(|(key, value)| {
        let key_parts = key.split("/").collect_vec();
        match key_parts.get(2) {
          Some(resource_type) => match *resource_type {
            "application" => Resource::application(value),
            "bucket" => Resource::bucket(value),
            "certificate" => Resource::certificate(value),
            "database" => Resource::database(value),
            "secret" => Resource::secret(value),
            "topic" => Resource::topic(value),
            "vhost" => Resource::vhost(value),
            "volume" => Resource::volume(value),
            unknown_resource => Err(serde_json::Error::custom(format!("unknown resource type ({})", unknown_resource))),
          }
          .map(|resource| (key.to_string(), resource)),
          None => Err(serde_json::Error::custom(format!("illegal resource allocation ({})", key))),
        }
      })
      .try_collect()
      .map_err(D::Error::custom)
  })
}

impl Resource {
  fn application(value: &Value) -> Result<Resource, serde_json::Error> {
    ApplicationResource::deserialize(value).map(|application_resource| Resource::Application { application: Box::new(application_resource) })
  }

  fn bucket(value: &Value) -> Result<Resource, serde_json::Error> {
    BucketResource::deserialize(value).map(|bucket_resource| Resource::Bucket { bucket: Box::new(bucket_resource) })
  }

  fn certificate(value: &Value) -> Result<Resource, serde_json::Error> {
    Ok(Resource::Certificate { certificate: Box::new(CertificateResource { unformatted_representation: value.to_string() }) })
  }

  fn database(value: &Value) -> Result<Resource, serde_json::Error> {
    DatabaseResource::deserialize(value).map(|database_resource| Resource::Database { database: Box::new(database_resource) })
  }

  fn secret(value: &Value) -> Result<Resource, serde_json::Error> {
    Ok(Resource::Secret { secret: Box::new(SecretResource { unformatted_representation: value.to_string() }) })
  }

  fn topic(value: &Value) -> Result<Resource, serde_json::Error> {
    TopicResource::deserialize(value).map(|topic_resource| Resource::Topic { topic: Box::new(topic_resource) })
  }

  fn vhost(value: &Value) -> Result<Resource, serde_json::Error> {
    Ok(Resource::Vhost { vhost: Box::new(VhostResource { unformatted_representation: value.to_string() }) })
  }

  fn volume(value: &Value) -> Result<Resource, serde_json::Error> {
    VolumeResource::deserialize(value).map(|volume_resource| Resource::Volume { volume: Box::new(volume_resource) })
  }
}

impl TryFrom<Value> for Numerical {
  type Error = String;

  fn try_from(value: Value) -> Result<Self, Self::Error> {
    match value.as_i64() {
      Some(value) => Ok(Numerical::Integer { value }),
      None => match value.as_f64() {
        Some(value) => Ok(Numerical::Float { value }),
        None => match value.as_str() {
          Some(template) => Ok(Numerical::Template { template: template.to_string() }),
          None => Err(format!("could not parse '{}' value", value)),
        },
      },
    }
  }
}

impl From<Numerical> for Value {
  fn from(numerical: Numerical) -> Self {
    match numerical {
      Numerical::Float { value } => Value::from(value),
      Numerical::Integer { value } => Value::from(value),
      Numerical::Template { template } => Value::from(template),
    }
  }
}