-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
142 lines (126 loc) · 3.22 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
package broker
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"strings"
"code.cloudfoundry.org/lager"
"github.com/pivotal-cf/brokerapi/domain"
"github.com/pivotal-cf/brokerapi/domain/apiresponses"
)
const (
DefaultPort = "3000"
DefaultLogLevel = "debug"
)
type Config struct {
API API
Catalog Catalog
Provider []byte
}
func NewConfig(source io.Reader) (Config, error) {
config := Config{}
bytes, err := ioutil.ReadAll(source)
if err != nil {
return config, err
}
api := API{}
if err = json.Unmarshal(bytes, &api); err != nil {
return config, err
}
basicAuthUsername, ok := os.LookupEnv("AIVEN_USERNAME")
if ok {
api.BasicAuthUsername = basicAuthUsername
}
basicAuthPassword, ok := os.LookupEnv("AIVEN_PASSWORD")
if ok {
api.BasicAuthPassword = basicAuthPassword
}
port, ok := os.LookupEnv("PORT")
if ok {
api.Port = port
} else {
if api.Port == "" {
api.Port = DefaultPort
}
}
if api.LogLevel == "" {
api.LogLevel = "debug"
}
api.LagerLogLevel, err = api.ConvertLogLevel()
if err != nil {
return config, err
}
catalog := Catalog{}
if err = json.Unmarshal(bytes, &catalog); err != nil {
return config, err
}
config = Config{
API: api,
Catalog: catalog,
Provider: bytes,
}
err = config.Validate()
return config, err
}
func (c Config) Validate() error {
if c.API.BasicAuthUsername == "" {
return fmt.Errorf("Config error: basic auth username required")
}
if c.API.BasicAuthPassword == "" {
return fmt.Errorf("Config error: basic auth password required")
}
if reflect.DeepEqual(c.Catalog, Catalog{}) {
return fmt.Errorf("Config error: catalog required")
}
if len(c.Catalog.Catalog.Services) == 0 {
return fmt.Errorf("Config error: at least one service is required")
}
for _, service := range c.Catalog.Catalog.Services {
if len(service.Plans) == 0 {
return fmt.Errorf("Config error: no plans found for service %s", service.Name)
}
}
return nil
}
type API struct {
BasicAuthUsername string `json:"basic_auth_username"`
BasicAuthPassword string `json:"basic_auth_password"`
Port string `json:"port"`
LogLevel string `json:"log_level"`
LagerLogLevel lager.LogLevel
}
func (api API) ConvertLogLevel() (lager.LogLevel, error) {
logLevels := map[string]lager.LogLevel{
"DEBUG": lager.DEBUG,
"INFO": lager.INFO,
"ERROR": lager.ERROR,
"FATAL": lager.FATAL,
}
logLevel, ok := logLevels[strings.ToUpper(api.LogLevel)]
if !ok {
return lager.DEBUG, fmt.Errorf("Config error: log level %s does not map to a Lager log level", api.LogLevel)
}
return logLevel, nil
}
type Catalog struct {
Catalog apiresponses.CatalogResponse `json:"catalog"`
}
func findServiceByID(catalog Catalog, serviceID string) (domain.Service, error) {
for _, service := range catalog.Catalog.Services {
if service.ID == serviceID {
return service, nil
}
}
return domain.Service{}, fmt.Errorf("Error: service %s not found in the catalog", serviceID)
}
func findPlanByID(service domain.Service, planID string) (domain.ServicePlan, error) {
for _, plan := range service.Plans {
if plan.ID == planID {
return plan, nil
}
}
return domain.ServicePlan{}, fmt.Errorf("Error: plan %s not found in service %s", planID, service.ID)
}