-
Notifications
You must be signed in to change notification settings - Fork 2
/
dbaas.go
261 lines (224 loc) · 7.27 KB
/
dbaas.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Package dbaas implements the Selectel DBaaS v1 API
package dbaas
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
)
const (
// appName specifies an application name.
appName = "dbaas-go"
// appVersion specifies an application version.
appVersion = "0.1.0"
// userAgent contains a basic user agent that will be used in queries.
userAgent = appName + "/" + appVersion
)
// Status represents custom type for various DBaaS objects statuses.
type Status string
const (
StatusActive Status = "ACTIVE"
StatusDeleted Status = "DELETED"
StatusDegraded Status = "DEGRADED"
StatusDiskFull Status = "DISK_FULL"
StatusError Status = "ERROR"
StatusPendingCreate Status = "PENDING_CREATE"
StatusPendingUpdate Status = "PENDING_UPDATE"
StatusPendingDelete Status = "PENDING_DELETE"
StatusDown Status = "DOWN"
StatusResizing Status = "RESIZING"
)
// API stores details that are needed to work with Selectel DBaaS API.
type API struct {
HTTPClient *http.Client
Token string
Endpoint string
UserAgent string
}
// NewDBAASClient initializes a new DBaaS client for the V1 API.
func NewDBAASClient(token, endpoint string) (*API, error) {
return &API{
HTTPClient: http.DefaultClient,
Token: token,
Endpoint: endpoint,
UserAgent: userAgent,
}, nil
}
// NewDBAASClientV1WithCustomHTTP initializes a new DBaaS client for the V1 API using custom HTTP client.
// If custom HTTP client is nil - default HTTP client will be used.
func NewDBAASClientV1WithCustomHTTP(customHTTPClient *http.Client, token, endpoint string) (*API, error) {
if customHTTPClient == nil {
customHTTPClient = http.DefaultClient
}
return &API{
HTTPClient: customHTTPClient,
Token: token,
Endpoint: endpoint,
UserAgent: userAgent,
}, nil
}
// NewDBAASClientV1WithOpenstackCredentials initializes a new DBaaS client for the V1 API using openstack credentials.
// You need to provide identityEndpoint, region and serviceType to get correct service endpoint.
func NewDBAASClientV1WithOpenstackCredentials(token, identityEndpoint, region, serviceType string) (*API, error) {
opts := gophercloud.AuthOptions{
IdentityEndpoint: identityEndpoint,
TokenID: token,
}
provider, err := openstack.AuthenticatedClient(opts)
if err != nil {
return nil, fmt.Errorf("could not authenticate to openstack, %w", err)
}
endpointOpts := gophercloud.EndpointOpts{Region: region}
endpointOpts.ApplyDefaults(serviceType)
endpoint, err := provider.EndpointLocator(endpointOpts)
if err != nil {
return nil, fmt.Errorf("could not locate an endpoint, %w", err)
}
return &API{
HTTPClient: http.DefaultClient,
Token: token,
Endpoint: endpoint,
UserAgent: userAgent,
}, nil
}
// makeRequest makes a HTTP request and returns the body as a byte slice.
// Params will be serialized to JSON.
func (api *API) makeRequest(ctx context.Context, method, uri string, params interface{}) ([]byte, error) {
jsonBody, err := handleParams(params)
if err != nil {
return nil, err
}
var resp *http.Response
var respErr error
var reqBody io.Reader
var respBody []byte
if jsonBody != nil {
reqBody = bytes.NewReader(jsonBody)
}
resp, respErr = api.request(ctx, method, uri, reqBody)
if respErr != nil || resp.StatusCode >= http.StatusInternalServerError {
if respErr == nil {
respBody, err = io.ReadAll(resp.Body)
resp.Body.Close()
respErr = fmt.Errorf("could not read response body, %w", err)
fmt.Printf("Request: %s %s got an error response %d", method, uri, resp.StatusCode)
} else {
fmt.Printf("Error performing request: %s %s : %s \n", method, uri, respErr.Error())
}
} else {
respBody, err = io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("could not read response body, %w", err)
}
}
if respErr != nil {
return nil, respErr
}
if resp.StatusCode >= http.StatusBadRequest {
return nil, handleStatusCode(resp.StatusCode, respBody, uri)
}
return respBody, nil
}
// request makes a HTTP request to the given API endpoint, returning the raw
// *http.Response, or an error if one occurred.
// Authentication and optional headers will be added automatically.
func (api *API) request(ctx context.Context, method, uri string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, method, api.Endpoint+uri, body)
if err != nil {
return nil, fmt.Errorf("HTTP request creation failed, %w", err)
}
req.Header.Set("User-Agent", api.UserAgent)
req.Header.Set("X-Auth-Token", api.Token)
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := api.HTTPClient.Do(req)
if err != nil {
return nil, fmt.Errorf("HTTP request failed, %w", err)
}
return resp, nil
}
// handleParams converts body params to slice of bytes if they are not nil.
func handleParams(params interface{}) ([]byte, error) {
var jsonBody []byte
var err error
if params == nil {
return nil, nil
}
if paramBytes, ok := params.([]byte); ok {
jsonBody = paramBytes
} else {
jsonBody, err = json.Marshal(params)
if err != nil {
return nil, fmt.Errorf("error marshalling params to JSON, %w", err)
}
}
return jsonBody, nil
}
// handleStatusCode checks status code and returns corresponding error.
func handleStatusCode(statusCode int, body []byte, uri string) error {
if statusCode >= http.StatusInternalServerError {
return fmt.Errorf("http status %d: service failed.\n%v\n%v", statusCode, body, uri) //nolint
}
errBody := &DBaaSAPIError{}
err := json.Unmarshal(body, &errBody)
if err != nil {
return fmt.Errorf("can't unmarshal response:\n%s, %w", body, err)
}
return errBody
}
// setQueryParams updates uri string with query parameters.
func setQueryParams(uri string, params interface{}) (string, error) {
v := url.Values{}
var queryParams map[string]interface{}
jsonParams, err := json.Marshal(params)
if err != nil {
return "", fmt.Errorf("Error marshalling params to JSON, %w", err)
}
err = json.Unmarshal(jsonParams, &queryParams)
if err != nil {
return "", fmt.Errorf("Error during Unmarshal, %w", err)
}
for key, value := range queryParams {
v.Set(key, fmt.Sprintf("%v", value))
}
if len(v) > 0 {
uri = uri + "?" + v.Encode()
}
return uri, nil
}
// convertFieldToType converts interface to the corresponding type.
func convertFieldToType(fieldValue interface{}) interface{} {
switch fieldValue := fieldValue.(type) {
case string:
return convertFieldFromStringToType(fieldValue)
default:
return fieldValue
}
}
// convertFieldFromStringToType converts string to the type that it represents.
func convertFieldFromStringToType(fieldValue string) interface{} {
if val, err := strconv.Atoi(fieldValue); err == nil {
return val
} else if val, err := strconv.ParseFloat(fieldValue, 64); err == nil {
return val
} else if val, err := strconv.ParseBool(fieldValue); err == nil {
return val
}
return fieldValue
}
// convertConfigValues convert config map values to the corresponding types.
func convertConfigValues(configMap map[string]interface{}) map[string]interface{} {
config := make(map[string]interface{})
for paramName, paramValue := range configMap {
config[paramName] = convertFieldToType(paramValue)
}
return config
}