forked from cloudfoundry/bosh-bootloader
/
create_lbs.go
159 lines (133 loc) · 3.81 KB
/
create_lbs.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
package commands
import (
"errors"
"fmt"
"github.com/cloudfoundry/bosh-bootloader/flags"
"github.com/cloudfoundry/bosh-bootloader/storage"
)
type CreateLBs struct {
createLBsCmd CreateLBsCmd
boshManager boshManager
certificateValidator certificateValidator
logger logger
stateValidator stateValidator
}
type CreateLBsCmd interface {
Execute(createLBsConfig CreateLBsConfig, state storage.State) error
}
type CreateLBsConfig struct {
AWS AWSCreateLBsConfig
GCP GCPCreateLBsConfig
}
var LBNotFound error = errors.New("no load balancer has been found for this bbl environment")
func NewCreateLBs(createLBsCmd CreateLBsCmd, logger logger, stateValidator stateValidator, certificateValidator certificateValidator, boshManager boshManager) CreateLBs {
return CreateLBs{
createLBsCmd: createLBsCmd,
boshManager: boshManager,
logger: logger,
stateValidator: stateValidator,
certificateValidator: certificateValidator,
}
}
func (c CreateLBs) CheckFastFails(subcommandFlags []string, state storage.State) error {
config, err := parseFlags(subcommandFlags, state.IAAS, state.LB.Type)
if err != nil {
return err
}
if err := c.stateValidator.Validate(); err != nil {
return fmt.Errorf("Validate state: %s", err)
}
if !lbExists(getLBType(config)) {
return errors.New("--type is required")
}
if !(state.IAAS == "gcp" && getLBType(config) == "concourse") {
err = c.certificateValidator.Validate("create-lbs", getCertPath(config), getKeyPath(config), getChainPath(config))
if err != nil {
return fmt.Errorf("Validate certificate: %s", err)
}
}
if getLBType(config) == "concourse" && getDomain(config) != "" {
return errors.New("--domain is not implemented for concourse load balancers. Remove the --domain flag and try again.")
}
if !state.NoDirector {
err := fastFailBOSHVersion(c.boshManager)
if err != nil {
return err
}
}
return nil
}
func (c CreateLBs) Execute(args []string, state storage.State) error {
config, err := parseFlags(args, state.IAAS, state.LB.Type)
if err != nil {
return err
}
err = c.createLBsCmd.Execute(config, state)
if err != nil {
return err
}
return nil
}
func parseFlags(subcommandFlags []string, iaas string, existingLBType string) (CreateLBsConfig, error) {
lbFlags := flags.New("create-lbs")
config := CreateLBsConfig{}
switch iaas {
case "aws":
lbFlags.String(&config.AWS.LBType, "type", existingLBType)
lbFlags.String(&config.AWS.CertPath, "cert", "")
lbFlags.String(&config.AWS.KeyPath, "key", "")
lbFlags.String(&config.AWS.ChainPath, "chain", "")
lbFlags.String(&config.AWS.Domain, "domain", "")
case "gcp":
lbFlags.String(&config.GCP.LBType, "type", existingLBType)
lbFlags.String(&config.GCP.CertPath, "cert", "")
lbFlags.String(&config.GCP.KeyPath, "key", "")
lbFlags.String(&config.GCP.Domain, "domain", "")
}
if err := lbFlags.Parse(subcommandFlags); err != nil {
return config, err
}
return config, nil
}
func getLBType(config CreateLBsConfig) string {
if config.AWS.LBType != "" {
return config.AWS.LBType
}
if config.GCP.LBType != "" {
return config.GCP.LBType
}
return ""
}
func getCertPath(config CreateLBsConfig) string {
if config.AWS.CertPath != "" {
return config.AWS.CertPath
}
if config.GCP.CertPath != "" {
return config.GCP.CertPath
}
return ""
}
func getKeyPath(config CreateLBsConfig) string {
if config.AWS.KeyPath != "" {
return config.AWS.KeyPath
}
if config.GCP.KeyPath != "" {
return config.GCP.KeyPath
}
return ""
}
func getChainPath(config CreateLBsConfig) string {
if config.AWS.ChainPath != "" {
return config.AWS.ChainPath
}
return ""
}
func getDomain(config CreateLBsConfig) string {
if config.AWS.Domain != "" {
return config.AWS.Domain
}
if config.GCP.Domain != "" {
return config.GCP.Domain
}
return ""
}