/
template_generator.go
133 lines (111 loc) · 3.05 KB
/
template_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
package gcp
import (
"fmt"
"strings"
"github.com/cloudfoundry/bosh-bootloader/storage"
"github.com/gobuffalo/packr/v2"
)
const templatesPath = "./templates"
type templates struct {
vars string
jumpbox string
boshDirector string
cfLB string
cfDNS string
concourseLB string
}
type TemplateGenerator struct {
box *packr.Box
}
func NewTemplateGenerator() TemplateGenerator {
return TemplateGenerator{
box: packr.New("gcp-templates", templatesPath),
}
}
func (t TemplateGenerator) Generate(state storage.State) string {
tmpls := t.readTemplates()
template := strings.Join([]string{tmpls.vars, tmpls.boshDirector, tmpls.jumpbox}, "\n")
switch state.LB.Type {
case "concourse":
template = strings.Join([]string{template, tmpls.concourseLB}, "\n")
case "cf":
zoneList := state.GCP.Zones
instanceGroups := t.GenerateInstanceGroups(zoneList)
backendService := t.GenerateBackendService(zoneList)
template = strings.Join([]string{template, tmpls.cfLB, instanceGroups, backendService}, "\n")
if state.LB.Domain != "" {
template = strings.Join([]string{template, tmpls.cfDNS}, "\n")
}
}
return template
}
func (t TemplateGenerator) GenerateBackendService(zoneList []string) string {
if len(zoneList) > 2 {
zoneList = zoneList[:2]
}
backendBase := `resource "google_compute_backend_service" "router-lb-backend-service" {
name = "${var.env_id}-router-lb"
port_name = "https"
protocol = "HTTPS"
timeout_sec = 900
enable_cdn = false
%s
health_checks = ["${google_compute_health_check.cf-public-health-check.self_link}"]
}
`
var backends string
for i := 0; i < len(zoneList); i++ {
backends = fmt.Sprintf(`%s
backend {
group = "${google_compute_instance_group.router-lb-%d.self_link}"
}
`, backends, i)
}
return fmt.Sprintf(backendBase, backends)
}
func (t TemplateGenerator) GenerateInstanceGroups(zoneList []string) string {
var groups []string
for i, zone := range zoneList {
groups = append(groups, fmt.Sprintf(`resource "google_compute_instance_group" "router-lb-%[1]d" {
name = "${var.env_id}-router-lb-%[1]d-%[2]s"
description = "terraform generated instance group that is multi-zone for https loadbalancing"
zone = "%[2]s"
named_port {
name = "https"
port = "443"
}
}
`, i, zone))
}
return strings.Join(groups, "\n")
}
func (t TemplateGenerator) readTemplates() templates {
listings := map[string]string{
"vars.tf": "",
"jumpbox.tf": "",
"bosh_director.tf": "",
"cf_lb.tf": "",
"cf_dns.tf": "",
"concourse_lb.tf": "",
}
var errors []error
for item := range listings {
content, err := t.box.FindString(item)
if err != nil {
errors = append(errors, err)
continue
}
listings[item] = content
}
if errors != nil {
panic(errors)
}
return templates{
vars: listings["vars.tf"],
jumpbox: listings["jumpbox.tf"],
boshDirector: listings["bosh_director.tf"],
cfLB: listings["cf_lb.tf"],
cfDNS: listings["cf_dns.tf"],
concourseLB: listings["concourse_lb.tf"],
}
}