-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
162 lines (129 loc) · 4.1 KB
/
metadata.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
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
package aws
import (
"context"
"sync"
"github.com/aws/aws-sdk-go/aws/session"
typesaws "github.com/bailey84j/terraform_installer/pkg/types/aws"
"github.com/pkg/errors"
)
// Metadata holds additional metadata for InstallConfig resources that
// does not need to be user-supplied (e.g. because it can be retrieved
// from external APIs).
type Metadata struct {
session *session.Session
availabilityZones []string
privateSubnets map[string]Subnet
publicSubnets map[string]Subnet
vpc string
instanceTypes map[string]InstanceType
Region string `json:"region,omitempty"`
Subnets []string `json:"subnets,omitempty"`
Services []typesaws.ServiceEndpoint `json:"services,omitempty"`
mutex sync.Mutex
}
// NewMetadata initializes a new Metadata object.
func NewMetadata(region string, subnets []string, services []typesaws.ServiceEndpoint) *Metadata {
return &Metadata{Region: region, Subnets: subnets, Services: services}
}
// Session holds an AWS session which can be used for AWS API calls
// during asset generation.
func (m *Metadata) Session(ctx context.Context) (*session.Session, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
return m.unlockedSession(ctx)
}
func (m *Metadata) unlockedSession(ctx context.Context) (*session.Session, error) {
if m.session == nil {
var err error
m.session, err = GetSessionWithOptions(WithRegion(m.Region))
if err != nil {
return nil, errors.Wrap(err, "creating AWS session")
}
}
return m.session, nil
}
// AvailabilityZones retrieves a list of availability zones for the configured region.
func (m *Metadata) AvailabilityZones(ctx context.Context) ([]string, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
if len(m.availabilityZones) == 0 {
session, err := m.unlockedSession(ctx)
if err != nil {
return nil, err
}
m.availabilityZones, err = availabilityZones(ctx, session, m.Region)
if err != nil {
return nil, errors.Wrap(err, "creating AWS session")
}
}
return m.availabilityZones, nil
}
// PrivateSubnets retrieves subnet metadata indexed by subnet ID, for
// subnets that the cloud-provider logic considers to be private
// (i.e. not public).
func (m *Metadata) PrivateSubnets(ctx context.Context) (map[string]Subnet, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
err := m.populateSubnets(ctx)
if err != nil {
return nil, err
}
return m.privateSubnets, nil
}
// PublicSubnets retrieves subnet metadata indexed by subnet ID, for
// subnets that the cloud-provider logic considers to be public
// (e.g. with suitable routing for hosting public load balancers).
func (m *Metadata) PublicSubnets(ctx context.Context) (map[string]Subnet, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
err := m.populateSubnets(ctx)
if err != nil {
return nil, err
}
return m.publicSubnets, nil
}
func (m *Metadata) populateSubnets(ctx context.Context) error {
if len(m.publicSubnets) > 0 || len(m.privateSubnets) > 0 {
return nil
}
if len(m.Subnets) == 0 {
return errors.New("no subnets configured")
}
session, err := m.unlockedSession(ctx)
if err != nil {
return err
}
m.vpc, m.privateSubnets, m.publicSubnets, err = subnets(ctx, session, m.Region, m.Subnets)
return err
}
// VPC retrieves the VPC ID containing PublicSubnets and PrivateSubnets.
func (m *Metadata) VPC(ctx context.Context) (string, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
if m.vpc == "" {
if len(m.Subnets) == 0 {
return "", errors.New("cannot calculate VPC without configured subnets")
}
err := m.populateSubnets(ctx)
if err != nil {
return "", err
}
}
return m.vpc, nil
}
// InstanceTypes retrieves instance type metadata indexed by InstanceType for the configured region.
func (m *Metadata) InstanceTypes(ctx context.Context) (map[string]InstanceType, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
if len(m.instanceTypes) == 0 {
session, err := m.unlockedSession(ctx)
if err != nil {
return nil, err
}
m.instanceTypes, err = instanceTypes(ctx, session, m.Region)
if err != nil {
return nil, errors.Wrap(err, "listing instance types")
}
}
return m.instanceTypes, nil
}