-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
170 lines (139 loc) · 4.2 KB
/
config.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
170
package broker
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"github.com/pivotal-cf/brokerapi"
)
var (
ErrNoSuchPlan = errors.New("no plan found")
)
type BindParameters struct{}
const (
DefaultHost = "0.0.0.0"
)
type PlanConfig struct {
InstanceType string `json:"instance_type"`
ReplicasPerNodeGroup int64 `json:"replicas_per_node_group"`
ShardCount int64 `json:"shard_count"`
SnapshotRetentionLimit int64 `json:"snapshot_retention_limit"`
AutomaticFailoverEnabled bool `json:"automatic_failover_enabled"`
MultiAZEnabled bool `json:"multi_az_enabled"`
Parameters map[string]string `json:"parameters"`
Engine string `json:"engine"`
EngineVersion string `json:"engine_version"`
CacheParameterGroupFamily string `json:"cache_parameter_group_family"`
}
type Config struct {
LogLevel string `json:"log_level"`
Username string `json:"username"`
Password string `json:"password"`
Region string `json:"region"`
BrokerName string `json:"broker_name"`
CacheSubnetGroupName string `json:"cache_subnet_group_name"`
VpcSecurityGroupIds []string `json:"vpc_security_group_ids"`
Catalog brokerapi.CatalogResponse `json:"catalog"`
PlanConfigs map[string]PlanConfig `json:"plan_configs"`
KmsKeyID string `json:"kms_key_id"`
SecretsManagerPath string `json:"secrets_manager_path"`
Host string `json:"host"`
TLS *TLSConfig `json:"tls"`
}
func (c Config) GetPlanConfig(planID string) (PlanConfig, error) {
plan, ok := c.PlanConfigs[planID]
if !ok {
return PlanConfig{}, ErrNoSuchPlan
}
return plan, nil
}
func LoadConfig(configFile string) (config Config, err error) {
if configFile == "" {
return config, errors.New("Must provide a config file")
}
file, err := os.Open(configFile)
if err != nil {
return config, err
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
if err != nil {
return config, err
}
if err = json.Unmarshal(bytes, &config); err != nil {
return config, err
}
if err = config.Validate(); err != nil {
return config, fmt.Errorf("Validating config contents: %s", err)
}
if config.Host == "" {
config.Host = DefaultHost
}
return config, nil
}
func (c Config) Validate() error {
if c.LogLevel == "" {
return errors.New("Must provide a non-empty log_level")
}
if c.Username == "" {
return errors.New("Must provide a non-empty username")
}
if c.Password == "" {
return errors.New("Must provide a non-empty password")
}
if c.Region == "" {
return errors.New("Must provide a non-empty region")
}
if c.BrokerName == "" {
return errors.New("Must provide a non-empty broker_name")
}
if c.CacheSubnetGroupName == "" {
return errors.New("Must provide a cache_subnet_group_name")
}
if len(c.VpcSecurityGroupIds) < 1 {
return errors.New("Must provide at least one VPC security group ID")
}
if c.KmsKeyID == "" {
return errors.New("Must provide a non-empty kms_key_id")
}
if c.SecretsManagerPath == "" {
return errors.New("Must provide a non-empty secrets_manager_path")
}
for _, s := range c.Catalog.Services {
for _, p := range s.Plans {
if !c.hasPlanConfig(p.ID) {
return fmt.Errorf("Plan with ID %s has no PlanConfig", p.ID)
}
}
}
for k := range c.PlanConfigs {
if !c.hasPlan(k) {
return fmt.Errorf("PlanConfig %v not found in catalog", k)
}
}
if c.TLS != nil {
err := c.TLS.Validate()
if err != nil {
return fmt.Errorf("TLS Validation failed: %v", err)
}
}
return nil
}
func (c Config) hasPlanConfig(id string) bool {
_, ok := c.PlanConfigs[id]
return ok
}
func (c Config) TLSEnabled() bool {
return c.TLS != nil
}
func (c Config) hasPlan(id string) bool {
for _, s := range c.Catalog.Services {
for _, p := range s.Plans {
if p.ID == id {
return true
}
}
}
return false
}