-
Notifications
You must be signed in to change notification settings - Fork 420
/
loader.go
75 lines (63 loc) · 1.88 KB
/
loader.go
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package manifest
import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/copilot-cli/internal/pkg/aws/ec2"
)
type subnetIDsGetter interface {
SubnetIDs(filters ...ec2.Filter) ([]string, error)
}
type loader interface {
load() error
}
// DynamicWorkloadManifest represents a dynamically populated workload manifest.
type DynamicWorkloadManifest struct {
mft workloadManifest
// Clients required to dynamically populate.
newSubnetIDsGetter func(*session.Session) subnetIDsGetter
}
func newDynamicWorkloadManifest(mft workloadManifest) *DynamicWorkloadManifest {
return &DynamicWorkloadManifest{
mft: mft,
newSubnetIDsGetter: func(s *session.Session) subnetIDsGetter {
return ec2.New(s)
},
}
}
// Manifest returns the manifest content.
func (s *DynamicWorkloadManifest) Manifest() any {
return s.mft
}
// ApplyEnv returns the workload manifest with environment overrides.
// If the environment passed in does not have any overrides then it returns itself.
func (s DynamicWorkloadManifest) ApplyEnv(envName string) (DynamicWorkload, error) {
mft, err := s.mft.applyEnv(envName)
if err != nil {
return nil, err
}
s.mft = mft
return &s, nil
}
// RequiredEnvironmentFeatures returns environment features that are required for this manifest.
func (s *DynamicWorkloadManifest) RequiredEnvironmentFeatures() []string {
return s.mft.requiredEnvironmentFeatures()
}
// Load dynamically populates all fields in the manifest.
func (s *DynamicWorkloadManifest) Load(sess *session.Session) error {
loaders := []loader{
&dynamicSubnets{
cfg: s.mft.subnets(),
client: s.newSubnetIDsGetter(sess),
},
}
return loadAll(loaders)
}
func loadAll(loaders []loader) error {
for _, loader := range loaders {
if err := loader.load(); err != nil {
return err
}
}
return nil
}