-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
149 lines (127 loc) · 3.92 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
package provider
import (
"encoding/json"
"errors"
"fmt"
"os"
"reflect"
"github.com/pivotal-cf/brokerapi/domain"
)
type Config struct {
DeployEnv string
BrokerName string `json:"name"`
Cloud string `json:"cloud"`
ServiceNamePrefix string
APIToken string
Project string
Catalog Catalog `json:"catalog"`
}
type Catalog struct {
Services []Service `json:"services"`
}
type Service struct {
domain.Service
Plans []Plan `json:"plans"`
}
type Plan struct {
domain.ServicePlan
PlanSpecificConfig
}
type AivenServiceCommonConfig struct{}
type AivenServiceOpenSearchConfig struct {
OpenSearchVersion string `json:"opensearch_version"`
}
type AivenServiceInfluxDBConfig struct{}
type PlanSpecificConfig struct {
AivenPlan string `json:"aiven_plan"`
AivenServiceCommonConfig
AivenServiceOpenSearchConfig
AivenServiceInfluxDBConfig
}
func DecodeConfig(b []byte) (*Config, error) {
var config *Config
err := json.Unmarshal(b, &config)
if err != nil {
return config, err
}
aivenCloud, ok := os.LookupEnv("AIVEN_CLOUD")
if ok {
config.Cloud = aivenCloud
}
if config.Cloud == "" {
return config, errors.New("Config error: must provide cloud configuration. For example, 'aws-eu-west-1'")
}
if reflect.DeepEqual(config.Catalog, Catalog{}) {
return config, errors.New("Config error: no catalog found")
}
if len(config.Catalog.Services) == 0 {
return config, errors.New("Config error: at least one service must be configured")
}
for _, service := range config.Catalog.Services {
if len(service.Plans) == 0 {
return config, errors.New("Config error: at least one plan must be configured for service " + service.Name)
}
for _, plan := range service.Plans {
if plan.AivenPlan == "" {
return config, errors.New("Config error: every plan must specify an `aiven_plan`")
}
if service.Name == "opensearch" && plan.OpenSearchVersion == "" {
return config, errors.New("Config error: every opensearch plan must specify an `opensearch_version`")
}
}
}
config.BrokerName = os.Getenv("BROKER_NAME")
if config.BrokerName == "" {
return config, errors.New("Config error: must declare a Broker name")
}
config.DeployEnv = os.Getenv("DEPLOY_ENV")
if config.DeployEnv == "" {
return config, errors.New("Config error: must declare a Deploy Environment")
}
config.ServiceNamePrefix = config.DeployEnv
serviceNamePrefixFromEnv := os.Getenv("SERVICE_NAME_PREFIX")
if serviceNamePrefixFromEnv != "" {
config.ServiceNamePrefix = fmt.Sprintf("%s-%s", config.ServiceNamePrefix, serviceNamePrefixFromEnv)
}
// Aiven only allow 64 characters for the service name. The instanceID from Cloud Foundry
// is joined with a hyphen to the service name prefix. This gives us 27 characters to use.
if len(config.ServiceNamePrefix) > 27 {
return config, errors.New("Config error: service name prefix cannot be longer than 8 characters")
}
config.APIToken = os.Getenv("AIVEN_API_TOKEN")
if config.APIToken == "" {
return config, errors.New("Config error: must pass an Aiven API token")
}
config.Project = os.Getenv("AIVEN_PROJECT")
if config.Project == "" {
return config, errors.New("Config error: must declare an Aiven project name")
}
return config, nil
}
func (c *Config) FindPlan(serviceId, planId string) (*Plan, error) {
service, err := findServiceById(serviceId, &c.Catalog)
if err != nil {
return &Plan{}, err
}
plan, err := findPlanById(planId, service)
if err != nil {
return &Plan{}, err
}
return &plan, nil
}
func findServiceById(id string, catalog *Catalog) (Service, error) {
for _, service := range catalog.Services {
if service.ID == id {
return service, nil
}
}
return Service{}, errors.New("could not find service with id " + id)
}
func findPlanById(id string, service Service) (Plan, error) {
for _, plan := range service.Plans {
if plan.ID == id {
return plan, nil
}
}
return Plan{}, errors.New("could not find plan with id " + id)
}