wadm_types/
lib.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
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
use std::collections::{BTreeMap, HashMap};

use schemars::JsonSchema;
use serde::{de, Deserialize, Serialize};
use utoipa::ToSchema;

pub mod api;
#[cfg(feature = "wit")]
pub mod bindings;
#[cfg(feature = "wit")]
pub use bindings::*;
pub mod validation;

/// The default weight for a spread
pub const DEFAULT_SPREAD_WEIGHT: usize = 100;
/// The expected OAM api version
pub const OAM_VERSION: &str = "core.oam.dev/v1beta1";
/// The currently supported kind for OAM manifests.
// NOTE(thomastaylor312): If we ever end up supporting more than one kind, we should use an enum for
// this
pub const APPLICATION_KIND: &str = "Application";
/// The version key, as predefined by the [OAM
/// spec](https://github.com/oam-dev/spec/blob/master/metadata.md#annotations-format)
pub const VERSION_ANNOTATION_KEY: &str = "version";
/// The description key, as predefined by the [OAM
/// spec](https://github.com/oam-dev/spec/blob/master/metadata.md#annotations-format)
pub const DESCRIPTION_ANNOTATION_KEY: &str = "description";
/// The annotation key for shared applications
pub const SHARED_ANNOTATION_KEY: &str = "experimental.wasmcloud.dev/shared";
/// The identifier for the builtin spreadscaler trait type
pub const SPREADSCALER_TRAIT: &str = "spreadscaler";
/// The identifier for the builtin daemonscaler trait type
pub const DAEMONSCALER_TRAIT: &str = "daemonscaler";
/// The identifier for the builtin linkdef trait type
pub const LINK_TRAIT: &str = "link";
/// The string used for indicating a latest version. It is explicitly forbidden to use as a version
/// for a manifest
pub const LATEST_VERSION: &str = "latest";
/// The default link name
pub const DEFAULT_LINK_NAME: &str = "default";

/// Manifest file based on the Open Application Model (OAM) specification for declaratively managing wasmCloud applications
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Manifest {
    /// The OAM version of the manifest
    #[serde(rename = "apiVersion")]
    pub api_version: String,
    /// The kind or type of manifest described by the spec
    pub kind: String,
    /// Metadata describing the manifest
    pub metadata: Metadata,
    /// The specification for this manifest
    pub spec: Specification,
}

impl Manifest {
    /// Returns a reference to the current version
    pub fn version(&self) -> &str {
        self.metadata
            .annotations
            .get(VERSION_ANNOTATION_KEY)
            .map(|v| v.as_str())
            .unwrap_or_default()
    }

    /// Returns a reference to the current description if it exists
    pub fn description(&self) -> Option<&str> {
        self.metadata
            .annotations
            .get(DESCRIPTION_ANNOTATION_KEY)
            .map(|v| v.as_str())
    }

    /// Indicates if the manifest is shared, meaning it can be used by multiple applications
    pub fn shared(&self) -> bool {
        self.metadata
            .annotations
            .get(SHARED_ANNOTATION_KEY)
            .is_some_and(|v| v.parse::<bool>().unwrap_or(false))
    }

    /// Returns the components in the manifest
    pub fn components(&self) -> impl Iterator<Item = &Component> {
        self.spec.components.iter()
    }

    /// Helper function to find shared components that are missing from the given list of
    /// deployed applications
    pub fn missing_shared_components(&self, deployed_apps: &[&Manifest]) -> Vec<&Component> {
        self.spec
            .components
            .iter()
            .filter(|shared_component| {
                match &shared_component.properties {
                    Properties::Capability {
                        properties:
                            CapabilityProperties {
                                image: None,
                                application: Some(shared_app),
                                ..
                            },
                    }
                    | Properties::Component {
                        properties:
                            ComponentProperties {
                                image: None,
                                application: Some(shared_app),
                                ..
                            },
                    } => {
                        if deployed_apps.iter().filter(|a| a.shared()).any(|m| {
                            m.metadata.name == shared_app.name
                                && m.components().any(|c| {
                                    c.name == shared_app.component
                                    // This compares just the enum variant, not the actual properties
                                    // For example, if we reference a shared component that's a capability,
                                    // we want to make sure the deployed component is a capability.
                                    && std::mem::discriminant(&c.properties)
                                        == std::mem::discriminant(&shared_component.properties)
                                })
                        }) {
                            false
                        } else {
                            true
                        }
                    }
                    _ => false,
                }
            })
            .collect()
    }

    /// Returns only the WebAssembly components in the manifest
    pub fn wasm_components(&self) -> impl Iterator<Item = &Component> {
        self.components()
            .filter(|c| matches!(c.properties, Properties::Component { .. }))
    }

    /// Returns only the provider components in the manifest
    pub fn capability_providers(&self) -> impl Iterator<Item = &Component> {
        self.components()
            .filter(|c| matches!(c.properties, Properties::Capability { .. }))
    }

    /// Returns a map of component names to components in the manifest
    pub fn component_lookup(&self) -> HashMap<&String, &Component> {
        self.components()
            .map(|c| (&c.name, c))
            .collect::<HashMap<&String, &Component>>()
    }

    /// Returns only links in the manifest
    pub fn links(&self) -> impl Iterator<Item = &Trait> {
        self.components()
            .flat_map(|c| c.traits.as_ref())
            .flatten()
            .filter(|t| t.is_link())
    }

    /// Returns only policies in the manifest
    pub fn policies(&self) -> impl Iterator<Item = &Policy> {
        self.spec.policies.iter()
    }

    /// Returns a map of policy names to policies in the manifest
    pub fn policy_lookup(&self) -> HashMap<&String, &Policy> {
        self.spec
            .policies
            .iter()
            .map(|p| (&p.name, p))
            .collect::<HashMap<&String, &Policy>>()
    }
}

/// The metadata describing the manifest
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, JsonSchema)]
pub struct Metadata {
    /// The name of the manifest. This must be unique per lattice
    pub name: String,
    /// Optional data for annotating this manifest see <https://github.com/oam-dev/spec/blob/master/metadata.md#annotations-format>
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub annotations: BTreeMap<String, String>,
    /// Optional data for labeling this manifest, see <https://github.com/oam-dev/spec/blob/master/metadata.md#label-format>
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub labels: BTreeMap<String, String>,
}

/// A representation of an OAM specification
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, JsonSchema)]
pub struct Specification {
    /// The list of components for describing an application
    pub components: Vec<Component>,

    /// The list of policies describing an application. This is for providing application-wide
    /// setting such as configuration for a secrets backend, how to render Kubernetes services,
    /// etc. It can be omitted if no policies are needed for an application.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub policies: Vec<Policy>,
}

/// A policy definition
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, JsonSchema)]
pub struct Policy {
    /// The name of this policy
    pub name: String,
    /// The properties for this policy
    pub properties: BTreeMap<String, String>,
    /// The type of the policy
    #[serde(rename = "type")]
    pub policy_type: String,
}

/// A component definition
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, JsonSchema)]
// TODO: figure out why this can't be uncommented
// #[serde(deny_unknown_fields)]
pub struct Component {
    /// The name of this component
    pub name: String,
    /// The properties for this component
    // NOTE(thomastaylor312): It would probably be better for us to implement a custom deserialze
    // and serialize that combines this and the component type. This is good enough for first draft
    #[serde(flatten)]
    pub properties: Properties,
    /// A list of various traits assigned to this component
    #[serde(skip_serializing_if = "Option::is_none")]
    pub traits: Option<Vec<Trait>>,
}

impl Component {
    fn secrets(&self) -> Vec<SecretProperty> {
        let mut secrets = Vec::new();
        if let Some(traits) = self.traits.as_ref() {
            let l: Vec<SecretProperty> = traits
                .iter()
                .filter_map(|t| {
                    if let TraitProperty::Link(link) = &t.properties {
                        let mut tgt_iter = link.target.secrets.clone();
                        if let Some(src) = &link.source {
                            tgt_iter.extend(src.secrets.clone());
                        }
                        Some(tgt_iter)
                    } else {
                        None
                    }
                })
                .flatten()
                .collect();
            secrets.extend(l);
        };

        match &self.properties {
            Properties::Component { properties } => {
                secrets.extend(properties.secrets.clone());
            }
            Properties::Capability { properties } => secrets.extend(properties.secrets.clone()),
        };
        secrets
    }

    /// Returns only links in the component
    fn links(&self) -> impl Iterator<Item = &Trait> {
        self.traits.iter().flatten().filter(|t| t.is_link())
    }
}

/// Properties that can be defined for a component
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, JsonSchema)]
#[serde(tag = "type")]
pub enum Properties {
    #[serde(rename = "component", alias = "actor")]
    Component { properties: ComponentProperties },
    #[serde(rename = "capability")]
    Capability { properties: CapabilityProperties },
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ComponentProperties {
    /// The image reference to use. Required unless the component is a shared component
    /// that is defined in another shared application.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image: Option<String>,
    /// Information to locate a component within a shared application. Cannot be specified
    /// if the image is specified.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub application: Option<SharedApplicationComponentProperties>,
    /// The component ID to use for this component. If not supplied, it will be generated
    /// as a combination of the [Metadata::name] and the image reference.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// Named configuration to pass to the component. The component will be able to retrieve
    /// these values at runtime using `wasi:runtime/config.`
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub config: Vec<ConfigProperty>,
    /// Named secret references to pass to the component. The component will be able to retrieve
    /// these values at runtime using `wasmcloud:secrets/store`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub secrets: Vec<SecretProperty>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Default, ToSchema, JsonSchema)]
pub struct ConfigDefinition {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub config: Vec<ConfigProperty>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub secrets: Vec<SecretProperty>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash, ToSchema, JsonSchema)]
pub struct SecretProperty {
    /// The name of the secret. This is used by a reference by the component or capability to
    /// get the secret value as a resource.
    pub name: String,
    /// The properties of the secret that indicate how to retrieve the secret value from a secrets
    /// backend and which backend to actually query.
    pub properties: SecretSourceProperty,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash, ToSchema, JsonSchema)]
pub struct SecretSourceProperty {
    /// The policy to use for retrieving the secret.
    pub policy: String,
    /// The key to use for retrieving the secret from the backend.
    pub key: String,
    /// The field to use for retrieving the secret from the backend. This is optional and can be
    /// used to retrieve a specific field from a secret.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub field: Option<String>,
    /// The version of the secret to retrieve. If not supplied, the latest version will be used.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct CapabilityProperties {
    /// The image reference to use. Required unless the component is a shared component
    /// that is defined in another shared application.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image: Option<String>,
    /// Information to locate a component within a shared application. Cannot be specified
    /// if the image is specified.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub application: Option<SharedApplicationComponentProperties>,
    /// The component ID to use for this provider. If not supplied, it will be generated
    /// as a combination of the [Metadata::name] and the image reference.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// Named configuration to pass to the provider. The merged set of configuration will be passed
    /// to the provider at runtime using the provider SDK's `init()` function.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub config: Vec<ConfigProperty>,
    /// Named secret references to pass to the t. The provider will be able to retrieve
    /// these values at runtime using `wasmcloud:secrets/store`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub secrets: Vec<SecretProperty>,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, JsonSchema)]
pub struct SharedApplicationComponentProperties {
    /// The name of the shared application
    pub name: String,
    /// The name of the component in the shared application
    pub component: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Trait {
    /// The type of trait specified. This should be a unique string for the type of scaler. As we
    /// plan on supporting custom scalers, these traits are not enumerated
    #[serde(rename = "type")]
    pub trait_type: String,
    /// The properties of this trait
    pub properties: TraitProperty,
}

impl Trait {
    /// Helper that creates a new linkdef type trait with the given properties
    pub fn new_link(props: LinkProperty) -> Trait {
        Trait {
            trait_type: LINK_TRAIT.to_owned(),
            properties: TraitProperty::Link(props),
        }
    }

    /// Check if a trait is a link
    pub fn is_link(&self) -> bool {
        self.trait_type == LINK_TRAIT
    }

    /// Check if a trait is a scaler
    pub fn is_scaler(&self) -> bool {
        self.trait_type == SPREADSCALER_TRAIT || self.trait_type == DAEMONSCALER_TRAIT
    }

    /// Helper that creates a new spreadscaler type trait with the given properties
    pub fn new_spreadscaler(props: SpreadScalerProperty) -> Trait {
        Trait {
            trait_type: SPREADSCALER_TRAIT.to_owned(),
            properties: TraitProperty::SpreadScaler(props),
        }
    }

    pub fn new_daemonscaler(props: SpreadScalerProperty) -> Trait {
        Trait {
            trait_type: DAEMONSCALER_TRAIT.to_owned(),
            properties: TraitProperty::SpreadScaler(props),
        }
    }
}

/// Properties for defining traits
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, JsonSchema)]
#[serde(untagged)]
#[allow(clippy::large_enum_variant)]
pub enum TraitProperty {
    Link(LinkProperty),
    SpreadScaler(SpreadScalerProperty),
    // TODO(thomastaylor312): This is still broken right now with deserializing. If the incoming
    // type specifies instances, it matches with spreadscaler first. So we need to implement a custom
    // parser here
    Custom(serde_json::Value),
}

impl From<LinkProperty> for TraitProperty {
    fn from(value: LinkProperty) -> Self {
        Self::Link(value)
    }
}

impl From<SpreadScalerProperty> for TraitProperty {
    fn from(value: SpreadScalerProperty) -> Self {
        Self::SpreadScaler(value)
    }
}

// impl From<serde_json::Value> for TraitProperty {
//     fn from(value: serde_json::Value) -> Self {
//         Self::Custom(value)
//     }
// }

/// Properties for the config list associated with components, providers, and links
///
/// ## Usage
/// Defining a config block, like so:
/// ```yaml
/// source_config:
/// - name: "external-secret-kv"
/// - name: "default-port"
///   properties:
///      port: "8080"
/// ```
///
/// Will result in two config scalers being created, one with the name `basic-kv` and one with the
/// name `default-port`. Wadm will not resolve collisions with configuration names between manifests.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct ConfigProperty {
    /// Name of the config to ensure exists
    pub name: String,
    /// Optional properties to put with the configuration. If the properties are
    /// omitted in the manifest, wadm will assume that the configuration is externally managed
    /// and will not attempt to create it, only reporting the status as failed if not found.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub properties: Option<HashMap<String, String>>,
}

/// This impl is a helper to help compare a `Vec<String>` to a `Vec<ConfigProperty>`
impl PartialEq<ConfigProperty> for String {
    fn eq(&self, other: &ConfigProperty) -> bool {
        self == &other.name
    }
}

/// Properties for links
#[derive(Debug, Serialize, Clone, PartialEq, Eq, ToSchema, JsonSchema, Default)]
#[serde(deny_unknown_fields)]
pub struct LinkProperty {
    /// WIT namespace for the link
    pub namespace: String,
    /// WIT package for the link
    pub package: String,
    /// WIT interfaces for the link
    pub interfaces: Vec<String>,
    /// Configuration to apply to the source of the link
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<ConfigDefinition>,
    /// Configuration to apply to the target of the link
    pub target: TargetConfig,
    /// The name of this link
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    #[serde(default, skip_serializing)]
    #[deprecated(since = "0.13.0")]
    pub source_config: Option<Vec<ConfigProperty>>,

    #[serde(default, skip_serializing)]
    #[deprecated(since = "0.13.0")]
    pub target_config: Option<Vec<ConfigProperty>>,
}

impl<'de> Deserialize<'de> for LinkProperty {
    fn deserialize<D>(d: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let json = serde_json::value::Value::deserialize(d)?;
        let mut target = TargetConfig::default();
        let mut source = None;

        // Handling the old configuration -- translate to a TargetConfig
        if let Some(t) = json.get("target") {
            if t.is_string() {
                let name = t.as_str().unwrap();
                let mut tgt = vec![];
                if let Some(tgt_config) = json.get("target_config") {
                    tgt = serde_json::from_value(tgt_config.clone()).map_err(de::Error::custom)?;
                }
                target = TargetConfig {
                    name: name.to_string(),
                    config: tgt,
                    secrets: vec![],
                };
            } else {
                // Otherwise handle normally
                target =
                    serde_json::from_value(json["target"].clone()).map_err(de::Error::custom)?;
            }
        }

        if let Some(s) = json.get("source_config") {
            let src: Vec<ConfigProperty> =
                serde_json::from_value(s.clone()).map_err(de::Error::custom)?;
            source = Some(ConfigDefinition {
                config: src,
                secrets: vec![],
            });
        }

        // If the source block is present then it takes priority
        if let Some(s) = json.get("source") {
            source = Some(serde_json::from_value(s.clone()).map_err(de::Error::custom)?);
        }

        // Validate that the required keys are all present
        if json.get("namespace").is_none() {
            return Err(de::Error::custom("namespace is required"));
        }

        if json.get("package").is_none() {
            return Err(de::Error::custom("package is required"));
        }

        if json.get("interfaces").is_none() {
            return Err(de::Error::custom("interfaces is required"));
        }

        Ok(LinkProperty {
            namespace: json["namespace"].as_str().unwrap().to_string(),
            package: json["package"].as_str().unwrap().to_string(),
            interfaces: json["interfaces"]
                .as_array()
                .unwrap()
                .iter()
                .map(|v| v.as_str().unwrap().to_string())
                .collect(),
            source,
            target,
            name: json.get("name").map(|v| v.as_str().unwrap().to_string()),
            ..Default::default()
        })
    }
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Default, ToSchema, JsonSchema)]
pub struct TargetConfig {
    /// The target this link applies to. This should be the name of a component in the manifest
    pub name: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub config: Vec<ConfigProperty>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub secrets: Vec<SecretProperty>,
}

impl PartialEq<TargetConfig> for String {
    fn eq(&self, other: &TargetConfig) -> bool {
        self == &other.name
    }
}

/// Properties for spread scalers
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct SpreadScalerProperty {
    /// Number of instances to spread across matching requirements
    #[serde(alias = "replicas")]
    pub instances: usize,
    /// Requirements for spreading those instances
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub spread: Vec<Spread>,
}

/// Configuration for various spreading requirements
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, ToSchema, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct Spread {
    /// The name of this spread requirement
    pub name: String,
    /// An arbitrary map of labels to match on for scaling requirements
    #[serde(skip_serializing_if = "BTreeMap::is_empty")]
    pub requirements: BTreeMap<String, String>,
    /// An optional weight for this spread. Higher weights are given more precedence
    #[serde(skip_serializing_if = "Option::is_none")]
    pub weight: Option<usize>,
}

impl Default for Spread {
    fn default() -> Self {
        Spread {
            name: "default".to_string(),
            requirements: BTreeMap::default(),
            weight: None,
        }
    }
}

#[cfg(test)]
mod test {
    use std::io::BufReader;
    use std::path::Path;

    use anyhow::Result;

    use super::*;

    pub(crate) fn deserialize_yaml(filepath: impl AsRef<Path>) -> Result<Manifest> {
        let file = std::fs::File::open(filepath)?;
        let reader = BufReader::new(file);
        let yaml_string: Manifest = serde_yaml::from_reader(reader)?;
        Ok(yaml_string)
    }

    pub(crate) fn deserialize_json(filepath: impl AsRef<Path>) -> Result<Manifest> {
        let file = std::fs::File::open(filepath)?;
        let reader = BufReader::new(file);
        let json_string: Manifest = serde_json::from_reader(reader)?;
        Ok(json_string)
    }

    #[test]
    fn test_oam_deserializer() {
        let res = deserialize_json("../../oam/simple1.json");
        match res {
            Ok(parse_results) => parse_results,
            Err(error) => panic!("Error {:?}", error),
        };

        let res = deserialize_yaml("../../oam/simple1.yaml");
        match res {
            Ok(parse_results) => parse_results,
            Err(error) => panic!("Error {:?}", error),
        };
    }

    #[test]
    #[ignore] // see TODO in TraitProperty enum
    fn test_custom_traits() {
        let manifest = deserialize_yaml("../../oam/custom.yaml").expect("Should be able to parse");
        let component = manifest
            .spec
            .components
            .into_iter()
            .find(|comp| matches!(comp.properties, Properties::Component { .. }))
            .expect("Should be able to find component");
        let traits = component.traits.expect("Should have Vec of traits");
        assert!(
            traits
                .iter()
                .any(|t| matches!(t.properties, TraitProperty::Custom(_))),
            "Should have found custom property trait: {traits:?}"
        );
    }

    #[test]
    fn test_config() {
        let manifest = deserialize_yaml("../../oam/config.yaml").expect("Should be able to parse");
        let props = match &manifest.spec.components[0].properties {
            Properties::Component { properties } => properties,
            _ => panic!("Should have found capability component"),
        };

        assert_eq!(props.config.len(), 1, "Should have found a config property");
        let config_property = props.config.first().expect("Should have a config property");
        assert!(config_property.name == "component_config");
        assert!(config_property
            .properties
            .as_ref()
            .is_some_and(|p| p.get("lang").is_some_and(|v| v == "EN-US")));

        let props = match &manifest.spec.components[1].properties {
            Properties::Capability { properties } => properties,
            _ => panic!("Should have found capability component"),
        };

        assert_eq!(props.config.len(), 1, "Should have found a config property");
        let config_property = props.config.first().expect("Should have a config property");
        assert!(config_property.name == "provider_config");
        assert!(config_property
            .properties
            .as_ref()
            .is_some_and(|p| p.get("default-port").is_some_and(|v| v == "8080")));
        assert!(config_property.properties.as_ref().is_some_and(|p| p
            .get("cache_file")
            .is_some_and(|v| v == "/tmp/mycache.json")));
    }

    #[test]
    fn test_component_matching() {
        let manifest = deserialize_yaml("../../oam/simple2.yaml").expect("Should be able to parse");
        assert_eq!(
            manifest
                .spec
                .components
                .iter()
                .filter(|component| matches!(component.properties, Properties::Component { .. }))
                .count(),
            1,
            "Should have found 1 component property"
        );
        assert_eq!(
            manifest
                .spec
                .components
                .iter()
                .filter(|component| matches!(component.properties, Properties::Capability { .. }))
                .count(),
            2,
            "Should have found 2 capability properties"
        );
    }

    #[test]
    fn test_trait_matching() {
        let manifest = deserialize_yaml("../../oam/simple2.yaml").expect("Should be able to parse");
        // Validate component traits
        let traits = manifest
            .spec
            .components
            .clone()
            .into_iter()
            .find(|component| matches!(component.properties, Properties::Component { .. }))
            .expect("Should find component component")
            .traits
            .expect("Should have traits object");
        assert_eq!(traits.len(), 1, "Should have 1 trait");
        assert!(
            matches!(traits[0].properties, TraitProperty::SpreadScaler(_)),
            "Should have spreadscaler properties"
        );
        // Validate capability component traits
        let traits = manifest
            .spec
            .components
            .into_iter()
            .find(|component| {
                matches!(
                    &component.properties,
                    Properties::Capability {
                        properties: CapabilityProperties { image, .. }
                    } if image.clone().expect("image to be present") == "wasmcloud.azurecr.io/httpserver:0.13.1"
                )
            })
            .expect("Should find capability component")
            .traits
            .expect("Should have traits object");
        assert_eq!(traits.len(), 1, "Should have 1 trait");
        assert!(
            matches!(traits[0].properties, TraitProperty::Link(_)),
            "Should have link property"
        );
        if let TraitProperty::Link(ld) = &traits[0].properties {
            assert_eq!(ld.source.as_ref().unwrap().config, vec![]);
            assert_eq!(ld.target.name, "userinfo".to_string());
        } else {
            panic!("trait property was not a link definition");
        }
    }

    #[test]
    fn test_oam_serializer() {
        let mut spread_vec: Vec<Spread> = Vec::new();
        let spread_item = Spread {
            name: "eastcoast".to_string(),
            requirements: BTreeMap::from([("zone".to_string(), "us-east-1".to_string())]),
            weight: Some(80),
        };
        spread_vec.push(spread_item);
        let spread_item = Spread {
            name: "westcoast".to_string(),
            requirements: BTreeMap::from([("zone".to_string(), "us-west-1".to_string())]),
            weight: Some(20),
        };
        spread_vec.push(spread_item);
        let mut trait_vec: Vec<Trait> = Vec::new();
        let spreadscalerprop = SpreadScalerProperty {
            instances: 4,
            spread: spread_vec,
        };
        let trait_item = Trait::new_spreadscaler(spreadscalerprop);
        trait_vec.push(trait_item);
        let linkdefprop = LinkProperty {
            target: TargetConfig {
                name: "webcap".to_string(),
                ..Default::default()
            },
            namespace: "wasi".to_string(),
            package: "http".to_string(),
            interfaces: vec!["incoming-handler".to_string()],
            source: Some(ConfigDefinition {
                config: {
                    vec![ConfigProperty {
                        name: "http".to_string(),
                        properties: Some(HashMap::from([("port".to_string(), "8080".to_string())])),
                    }]
                },
                ..Default::default()
            }),
            name: Some("default".to_string()),
            ..Default::default()
        };
        let trait_item = Trait::new_link(linkdefprop);
        trait_vec.push(trait_item);
        let mut component_vec: Vec<Component> = Vec::new();
        let component_item = Component {
            name: "userinfo".to_string(),
            properties: Properties::Component {
                properties: ComponentProperties {
                    image: Some("wasmcloud.azurecr.io/fake:1".to_string()),
                    application: None,
                    id: None,
                    config: vec![],
                    secrets: vec![],
                },
            },
            traits: Some(trait_vec),
        };
        component_vec.push(component_item);
        let component_item = Component {
            name: "webcap".to_string(),
            properties: Properties::Capability {
                properties: CapabilityProperties {
                    image: Some("wasmcloud.azurecr.io/httpserver:0.13.1".to_string()),
                    application: None,
                    id: None,
                    config: vec![],
                    secrets: vec![],
                },
            },
            traits: None,
        };
        component_vec.push(component_item);

        let mut spread_vec: Vec<Spread> = Vec::new();
        let spread_item = Spread {
            name: "haslights".to_string(),
            requirements: BTreeMap::from([("zone".to_string(), "enabled".to_string())]),
            weight: Some(DEFAULT_SPREAD_WEIGHT),
        };
        spread_vec.push(spread_item);
        let spreadscalerprop = SpreadScalerProperty {
            instances: 1,
            spread: spread_vec,
        };
        let mut trait_vec: Vec<Trait> = Vec::new();
        let trait_item = Trait::new_spreadscaler(spreadscalerprop);
        trait_vec.push(trait_item);
        let component_item = Component {
            name: "ledblinky".to_string(),
            properties: Properties::Capability {
                properties: CapabilityProperties {
                    image: Some("wasmcloud.azurecr.io/ledblinky:0.0.1".to_string()),
                    application: None,
                    id: None,
                    config: vec![],
                    secrets: vec![],
                },
            },
            traits: Some(trait_vec),
        };
        component_vec.push(component_item);

        let spec = Specification {
            components: component_vec,
            policies: vec![],
        };
        let metadata = Metadata {
            name: "my-example-app".to_string(),
            annotations: BTreeMap::from([
                (VERSION_ANNOTATION_KEY.to_string(), "v0.0.1".to_string()),
                (
                    DESCRIPTION_ANNOTATION_KEY.to_string(),
                    "This is my app".to_string(),
                ),
            ]),
            labels: BTreeMap::from([(
                "prefix.dns.prefix/name-for_a.123".to_string(),
                "this is a valid label".to_string(),
            )]),
        };
        let manifest = Manifest {
            api_version: OAM_VERSION.to_owned(),
            kind: APPLICATION_KIND.to_owned(),
            metadata,
            spec,
        };
        let serialized_json =
            serde_json::to_vec(&manifest).expect("Should be able to serialize JSON");

        let serialized_yaml = serde_yaml::to_string(&manifest)
            .expect("Should be able to serialize YAML")
            .into_bytes();

        // Test the round trip back in
        let json_manifest: Manifest = serde_json::from_slice(&serialized_json)
            .expect("Should be able to deserialize JSON roundtrip");
        let yaml_manifest: Manifest = serde_yaml::from_slice(&serialized_yaml)
            .expect("Should be able to deserialize YAML roundtrip");

        // Make sure the manifests don't contain any custom traits (to test that we aren't parsing
        // the tagged enum poorly)
        assert!(
            !json_manifest
                .spec
                .components
                .into_iter()
                .any(|component| component
                    .traits
                    .unwrap_or_default()
                    .into_iter()
                    .any(|t| matches!(t.properties, TraitProperty::Custom(_)))),
            "Should have found custom properties"
        );

        assert!(
            !yaml_manifest
                .spec
                .components
                .into_iter()
                .any(|component| component
                    .traits
                    .unwrap_or_default()
                    .into_iter()
                    .any(|t| matches!(t.properties, TraitProperty::Custom(_)))),
            "Should have found custom properties"
        );
    }

    #[test]
    fn test_deprecated_fields_not_set() {
        let manifest = deserialize_yaml("../../oam/simple2.yaml").expect("Should be able to parse");
        // Validate component traits
        let traits = manifest
            .spec
            .components
            .clone()
            .into_iter()
            .filter(|component| matches!(component.name.as_str(), "webcap"))
            .find(|component| matches!(component.properties, Properties::Capability { .. }))
            .expect("Should find component component")
            .traits
            .expect("Should have traits object");
        assert_eq!(traits.len(), 1, "Should have 1 trait");
        if let TraitProperty::Link(ld) = &traits[0].properties {
            assert_eq!(ld.source.as_ref().unwrap().config, vec![]);
            #[allow(deprecated)]
            let source_config = &ld.source_config;
            assert_eq!(source_config, &None);
        } else {
            panic!("trait property was not a link definition");
        };
    }
}