forked from cloudfoundry/bosh-bootloader
/
ops_generator.go
169 lines (147 loc) · 3.71 KB
/
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
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
package azure
import (
"fmt"
"strings"
yaml "gopkg.in/yaml.v2"
"github.com/cloudfoundry/bosh-bootloader/storage"
"github.com/cloudfoundry/bosh-bootloader/terraform"
)
type OpsGenerator struct {
terraformManager terraformManager
}
type terraformManager interface {
GetOutputs() (terraform.Outputs, error)
}
type op struct {
Type string
Path string
Value interface{}
}
type lb struct {
Name string
CloudProperties cloudProperties `yaml:"cloud_properties"`
}
type cloudProperties struct {
ApplicationGateway string `yaml:"application_gateway,omitempty"`
SecurityGroup string `yaml:"security_group,omitempty"`
LoadBalancer string `yaml:"load_balancer,omitempty"`
}
type network struct {
Name string
Subnets []networkSubnet
Type string
}
type networkSubnet struct {
AZs []string
Gateway string
Range string
Reserved []string
DNS []string
CloudProperties subnetCloudProperties `yaml:"cloud_properties"`
}
type subnetCloudProperties struct {
VirtualNetworkName string `yaml:"virtual_network_name"`
SubnetName string `yaml:"subnet_name"`
SecurityGroup string `yaml:"security_group,omitempty"`
}
var marshal func(interface{}) ([]byte, error) = yaml.Marshal
func NewOpsGenerator(terraformManager terraformManager) OpsGenerator {
return OpsGenerator{
terraformManager: terraformManager,
}
}
func (o OpsGenerator) GenerateVars(state storage.State) (string, error) {
terraformOutputs, err := o.terraformManager.GetOutputs()
if err != nil {
return "", fmt.Errorf("Get terraform outputs: %s", err)
}
varsYAML := map[string]interface{}{}
for k, v := range terraformOutputs.Map {
varsYAML[k] = v
}
varsBytes, err := marshal(varsYAML)
if err != nil {
return "", err
}
return string(varsBytes), nil
}
func (o OpsGenerator) Generate(state storage.State) (string, error) {
subnet := networkSubnet{
AZs: []string{"z1", "z2", "z3"},
Gateway: "((internal_gw))",
Range: "((subnet_cidr))",
Reserved: []string{"((jumpbox__internal_ip))", "((director__internal_ip))", "((internal_gw))/30"},
DNS: []string{"8.8.8.8", "168.63.129.16"},
CloudProperties: subnetCloudProperties{
VirtualNetworkName: "((vnet_name))",
SubnetName: "((subnet_name))",
SecurityGroup: "((default_security_group))",
},
}
cloudConfigOps := []op{
{
Type: "replace",
Path: "/networks/-",
Value: network{
Name: "default",
Subnets: []networkSubnet{subnet},
Type: "manual",
},
},
{
Type: "replace",
Path: "/networks/-",
Value: network{
Name: "private",
Subnets: []networkSubnet{subnet},
Type: "manual",
},
},
}
switch state.LB.Type {
case "cf":
lbOps := []op{
{
Type: "replace",
Path: "/vm_extensions/-",
Value: lb{
Name: "cf-router-network-properties",
CloudProperties: cloudProperties{
ApplicationGateway: "((cf_app_gateway_name))",
SecurityGroup: "((cf_security_group))",
},
},
},
{
Type: "replace",
Path: "/vm_extensions/-",
Value: lb{Name: "cf-tcp-router-network-properties"},
},
{
Type: "replace",
Path: "/vm_extensions/-",
Value: lb{Name: "diego-ssh-proxy-network-properties"},
}}
cloudConfigOps = append(cloudConfigOps, lbOps...)
case "concourse":
lbOp := op{
Type: "replace",
Path: "/vm_extensions/-",
Value: lb{
Name: "lb",
CloudProperties: cloudProperties{
LoadBalancer: "((concourse_lb_name))",
},
},
}
cloudConfigOps = append(cloudConfigOps, lbOp)
}
cloudConfigOpsYAML, err := marshal(cloudConfigOps)
if err != nil {
return "", err
}
return strings.Join([]string{
BaseOps,
string(cloudConfigOpsYAML),
}, "\n"), nil
}