-
Notifications
You must be signed in to change notification settings - Fork 180
/
state_query.go
97 lines (82 loc) · 2.51 KB
/
state_query.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
package commands
import (
"fmt"
"github.com/cloudfoundry/bosh-bootloader/storage"
)
const (
EnvIDCommand = "env-id"
JumpboxAddressCommand = "jumpbox-address"
DirectorUsernameCommand = "director-username"
DirectorPasswordCommand = "director-password"
DirectorAddressCommand = "director-address"
DirectorCACertCommand = "director-ca-cert"
EnvIDPropertyName = "environment id"
JumpboxAddressPropertyName = "jumpbox address"
DirectorUsernamePropertyName = "director username"
DirectorPasswordPropertyName = "director password"
DirectorAddressPropertyName = "director address"
DirectorCACertPropertyName = "director ca cert"
)
type StateQuery struct {
logger logger
stateValidator stateValidator
terraformManager terraformManager
propertyName string
}
type getPropertyFunc func(storage.State) string
func NewStateQuery(logger logger, stateValidator stateValidator, terraformManager terraformManager, propertyName string) StateQuery {
return StateQuery{
logger: logger,
stateValidator: stateValidator,
terraformManager: terraformManager,
propertyName: propertyName,
}
}
func (s StateQuery) CheckFastFails(subcommandFlags []string, state storage.State) error {
err := s.stateValidator.Validate()
if err != nil {
return err
}
return nil
}
func (s StateQuery) Execute(subcommandFlags []string, state storage.State) error {
var (
propertyValue string
err error
)
switch s.propertyName {
case JumpboxAddressPropertyName:
propertyValue, err = s.getEIP(state)
if err != nil {
return err
}
case DirectorAddressPropertyName:
propertyValue, err = s.getDirectorAddress(state)
if err != nil {
return err
}
case DirectorUsernamePropertyName:
propertyValue = state.BOSH.DirectorUsername
case DirectorPasswordPropertyName:
propertyValue = state.BOSH.DirectorPassword
case DirectorCACertPropertyName:
propertyValue = state.BOSH.DirectorSSLCA
case EnvIDPropertyName:
propertyValue = state.EnvID
}
if propertyValue == "" {
return fmt.Errorf("Could not retrieve %s, please make sure you are targeting the proper state dir.", s.propertyName)
}
s.logger.Println(propertyValue)
return nil
}
func (s StateQuery) getDirectorAddress(state storage.State) (string, error) {
return state.BOSH.DirectorAddress, nil
}
func (s StateQuery) getEIP(state storage.State) (string, error) {
terraformOutputs, err := s.terraformManager.GetOutputs()
if err != nil {
return "", err
}
return terraformOutputs.GetString("external_ip"), nil
}