-
Notifications
You must be signed in to change notification settings - Fork 3
/
provider.go
209 lines (182 loc) · 5.77 KB
/
provider.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package provider
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/alphagov/paas-aiven-broker/provider/aiven"
"github.com/pivotal-cf/brokerapi"
)
const AIVEN_BASE_URL string = "https://api.aiven.io"
const SERVICE_TYPE string = "elasticsearch"
type AivenProvider struct {
Client aiven.Client
Config *Config
}
func New(configJSON []byte) (*AivenProvider, error) {
config, err := DecodeConfig(configJSON)
if err != nil {
return nil, err
}
client := aiven.NewHttpClient(AIVEN_BASE_URL, config.APIToken, config.Project)
return &AivenProvider{
Client: client,
Config: config,
}, nil
}
func (ap *AivenProvider) Provision(ctx context.Context, provisionData ProvisionData) (dashboardURL, operationData string, err error) {
plan, err := ap.Config.FindPlan(provisionData.Service.ID, provisionData.Plan.ID)
if err != nil {
return "", "", err
}
ipFilter, err := ParseIPWhitelist(os.Getenv("IP_WHITELIST"))
if err != nil {
return "", "", err
}
createServiceInput := &aiven.CreateServiceInput{
Cloud: ap.Config.Cloud,
Plan: plan.AivenPlan,
ServiceName: buildServiceName(ap.Config.ServiceNamePrefix, provisionData.InstanceID),
ServiceType: SERVICE_TYPE,
UserConfig: aiven.UserConfig{
ElasticsearchVersion: plan.ElasticsearchVersion,
IPFilter: ipFilter,
},
}
_, err = ap.Client.CreateService(createServiceInput)
return dashboardURL, operationData, err
}
func (ap *AivenProvider) Deprovision(ctx context.Context, deprovisionData DeprovisionData) (operationData string, err error) {
err = ap.Client.DeleteService(&aiven.DeleteServiceInput{
ServiceName: buildServiceName(ap.Config.ServiceNamePrefix, deprovisionData.InstanceID),
})
if err != nil {
if err == aiven.ErrInstanceDoesNotExist {
return "", brokerapi.ErrInstanceDoesNotExist
}
}
return "", err
}
type Credentials struct {
URI string `json:"uri"`
Hostname string `json:"hostname"`
Port string `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
}
func (ap *AivenProvider) Bind(ctx context.Context, bindData BindData) (binding brokerapi.Binding, err error) {
user := bindData.BindingID
password, err := ap.Client.CreateServiceUser(&aiven.CreateServiceUserInput{
ServiceName: buildServiceName(ap.Config.ServiceNamePrefix, bindData.InstanceID),
Username: user,
})
if err != nil {
return brokerapi.Binding{}, err
}
host, port, err := ap.Client.GetServiceConnectionDetails(&aiven.GetServiceInput{
ServiceName: buildServiceName(ap.Config.ServiceNamePrefix, bindData.InstanceID),
})
if err != nil {
return brokerapi.Binding{}, err
}
credentials := Credentials{
URI: buildURI(user, password, host, port),
Hostname: host,
Port: port,
Username: user,
Password: password,
}
return brokerapi.Binding{
Credentials: credentials,
}, nil
}
func buildURI(user, password, host, port string) string {
uri := &url.URL{
Scheme: "https",
User: url.UserPassword(user, password),
Host: fmt.Sprintf("%s:%s", host, port),
}
return uri.String()
}
func (ap *AivenProvider) Unbind(ctx context.Context, unbindData UnbindData) (err error) {
_, err = ap.Client.DeleteServiceUser(&aiven.DeleteServiceUserInput{
ServiceName: buildServiceName(ap.Config.ServiceNamePrefix, unbindData.InstanceID),
Username: unbindData.BindingID,
})
return err
}
func (ap *AivenProvider) Update(ctx context.Context, updateData UpdateData) (operationData string, err error) {
plan, err := ap.Config.FindPlan(updateData.Details.ServiceID, updateData.Details.PlanID)
if err != nil {
return "", err
}
ipFilter, err := ParseIPWhitelist(os.Getenv("IP_WHITELIST"))
if err != nil {
return "", err
}
_, err = ap.Client.UpdateService(&aiven.UpdateServiceInput{
ServiceName: buildServiceName(ap.Config.ServiceNamePrefix, updateData.InstanceID),
Plan: plan.AivenPlan,
UserConfig: aiven.UserConfig{
ElasticsearchVersion: plan.ElasticsearchVersion,
IPFilter: ipFilter,
},
})
switch err := err.(type) {
case aiven.ErrInvalidUpdate:
return "", brokerapi.NewFailureResponseBuilder(
err,
http.StatusUnprocessableEntity,
"plan-change-not-supported",
).WithErrorKey("PlanChangeNotSupported").Build()
default:
return "", err
}
return "", nil
}
func (ap *AivenProvider) LastOperation(ctx context.Context, lastOperationData LastOperationData) (state brokerapi.LastOperationState, description string, err error) {
status, updateTime, err := ap.Client.GetServiceStatus(&aiven.GetServiceInput{
ServiceName: buildServiceName(ap.Config.ServiceNamePrefix, lastOperationData.InstanceID),
})
if err != nil {
return "", "", err
}
if updateTime.After(time.Now().Add(-1 * 60 * time.Second)) {
return brokerapi.InProgress, "Preparing to apply update", nil
}
lastOperationState, description := providerStatesMapping(status)
return lastOperationState, description, nil
}
func ParseIPWhitelist(ips string) ([]string, error) {
if ips == "" {
return []string{}, nil
}
outIPs := []string{}
for _, ip := range strings.Split(ips, ",") {
if len(strings.Split(ip, ".")) != 4 {
return []string{}, fmt.Errorf("malformed whitelist IP: %v", ip)
}
outIPs = append(outIPs, ip)
}
return outIPs, nil
}
func buildServiceName(prefix, guid string) string {
return strings.ToLower(prefix + "-" + guid)
}
func providerStatesMapping(status aiven.ServiceStatus) (brokerapi.LastOperationState, string) {
switch status {
case aiven.Running:
return brokerapi.Succeeded, "Last operation succeeded"
case aiven.Rebuilding:
return brokerapi.InProgress, "Rebuilding"
case aiven.Rebalancing:
return brokerapi.InProgress, "Rebalancing"
case aiven.PowerOff:
return brokerapi.Failed, "Last operation failed: service is powered off"
default:
return brokerapi.InProgress, fmt.Sprintf("Unknown state: %s", status)
}
}