forked from cloudfoundry/bosh-bootloader
/
cloudformation_ops_generator.go
137 lines (118 loc) · 3.37 KB
/
cloudformation_ops_generator.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
package aws
import (
"fmt"
"strings"
"github.com/cloudfoundry/bosh-bootloader/aws/cloudformation"
"github.com/cloudfoundry/bosh-bootloader/aws/ec2"
"github.com/cloudfoundry/bosh-bootloader/storage"
)
type CloudFormationOpsGenerator struct {
availabilityZoneRetriever ec2.AvailabilityZoneRetriever
infrastructureManager infrastructureManager
}
type infrastructureManager interface {
Describe(stackName string) (cloudformation.Stack, error)
}
func NewCloudFormationOpsGenerator(availabilityZoneRetriever ec2.AvailabilityZoneRetriever, infrastructureManager infrastructureManager) CloudFormationOpsGenerator {
return CloudFormationOpsGenerator{
availabilityZoneRetriever: availabilityZoneRetriever,
infrastructureManager: infrastructureManager,
}
}
func (a CloudFormationOpsGenerator) Generate(state storage.State) (string, error) {
ops, err := a.generateCloudFormationAWSOps(state)
if err != nil {
return "", err
}
cloudConfigOpsYAML, err := marshal(ops)
if err != nil {
return "", err
}
return strings.Join(
[]string{
BaseOps,
string(cloudConfigOpsYAML),
},
"\n",
), nil
}
func (a CloudFormationOpsGenerator) generateCloudFormationAWSOps(state storage.State) ([]op, error) {
azs, err := a.availabilityZoneRetriever.RetrieveAvailabilityZones(state.AWS.Region)
if err != nil {
return []op{}, err
}
ops := []op{}
for i, awsAZ := range azs {
op := createOp("replace", "/azs/-", az{
Name: fmt.Sprintf("z%d", i+1),
CloudProperties: azCloudProperties{
AvailabilityZone: awsAZ,
},
})
ops = append(ops, op)
}
stack, err := a.infrastructureManager.Describe(state.Stack.Name)
if err != nil {
return []op{}, err
}
subnets := []networkSubnet{}
for i := range azs {
subnet, err := generateNetworkSubnet(
fmt.Sprintf("z%d", i+1),
stack.Outputs[fmt.Sprintf("InternalSubnet%dCIDR", i+1)],
stack.Outputs[fmt.Sprintf("InternalSubnet%dName", i+1)],
stack.Outputs["InternalSecurityGroup"],
)
if err != nil {
return []op{}, err
}
subnets = append(subnets, subnet)
}
ops = append(ops, createOp("replace", "/networks/-", network{
Name: "private",
Subnets: subnets,
Type: "manual",
}))
ops = append(ops, createOp("replace", "/networks/-", network{
Name: "default",
Subnets: subnets,
Type: "manual",
}))
if value := stack.Outputs["CFRouterLoadBalancer"]; value != "" {
ops = append(ops, createOp("replace", "/vm_extensions/-", lb{
Name: "router-lb",
CloudProperties: lbCloudProperties{
ELBs: []string{value},
SecurityGroups: []string{
stack.Outputs["CFRouterInternalSecurityGroup"],
stack.Outputs["InternalSecurityGroup"],
},
},
}))
}
if value := stack.Outputs["CFSSHProxyLoadBalancer"]; value != "" {
ops = append(ops, createOp("replace", "/vm_extensions/-", lb{
Name: "ssh-proxy-lb",
CloudProperties: lbCloudProperties{
ELBs: []string{value},
SecurityGroups: []string{
stack.Outputs["CFSSHProxyInternalSecurityGroup"],
stack.Outputs["InternalSecurityGroup"],
},
},
}))
}
if value := stack.Outputs["ConcourseLoadBalancer"]; value != "" {
ops = append(ops, createOp("replace", "/vm_extensions/-", lb{
Name: "lb",
CloudProperties: lbCloudProperties{
ELBs: []string{value},
SecurityGroups: []string{
stack.Outputs["ConcourseInternalSecurityGroup"],
stack.Outputs["InternalSecurityGroup"],
},
},
}))
}
return ops, nil
}