-
Notifications
You must be signed in to change notification settings - Fork 8
/
databases.go
337 lines (294 loc) · 13.6 KB
/
databases.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package scalingo
import (
"context"
"net/http"
"time"
"gopkg.in/errgo.v1"
httpclient "github.com/Scalingo/go-scalingo/v7/http"
"github.com/Scalingo/go-utils/errors/v2"
)
// DatabasesService is the interface gathering all the methods related to
// database addon configuration updates
type DatabasesService interface {
DatabaseShow(ctx context.Context, app, addonID string) (Database, error)
DatabaseEnableFeature(ctx context.Context, app, addonID, feature string) (DatabaseEnableFeatureResponse, error)
DatabaseDisableFeature(ctx context.Context, app, addonID, feature string) (DatabaseDisableFeatureResponse, error)
DatabaseUpdatePeriodicBackupsConfig(ctx context.Context, app, addonID string, params DatabaseUpdatePeriodicBackupsConfigParams) (Database, error)
DatabaseUpdateMaintenanceWindow(ctx context.Context, app, addonID string, params MaintenanceWindowParams) (Database, error)
DatabaseListMaintenance(ctx context.Context, app, addonID string, opts PaginationOpts) ([]*Maintenance, PaginationMeta, error)
DatabaseShowMaintenance(ctx context.Context, app, addonID, maintenanceID string) (Maintenance, error)
}
// DatabaseStatus is a string representing the status of a database deployment
type DatabaseStatus string
const (
// DatabaseStatusCreating is set when the database is being started before
// it's operational
DatabaseStatusCreating DatabaseStatus = "creating"
// DatabaseStatusRunning is the standard status of a database when everything
// is operational
DatabaseStatusRunning DatabaseStatus = "running"
// DatabaseStatusMigrating is set when a component of the database is being
// migrated by Scalingo infrastructure
DatabaseStatusMigrating DatabaseStatus = "migrating"
// DatabaseStatusUpdating is set the plan of the database is being changed
DatabaseStatusUpdating DatabaseStatus = "updating"
// DatabaseStatusUpgrading is set when a database version upgrade is being
// applied on the database
DatabaseStatusUpgrading DatabaseStatus = "upgrading"
// DatabaseStatusStopped is set when the database has been stopped (suspended
// after free trial or when an account has been suspended)
DatabaseStatusStopped DatabaseStatus = "stopped"
)
// DatabaseFeature represents the state of application of a database feature
type DatabaseFeature struct {
Name string `json:"name"`
Status DatabaseFeatureStatus `json:"status"`
}
// Database contains the metadata and configuration of a database deployment
type Database struct {
ID string `json:"id"`
CreatedAt time.Time `json:"created_at"`
ResourceID string `json:"resource_id"`
AppName string `json:"app_name"`
EncryptionAtRest bool `json:"encryption_at_rest"`
Features []DatabaseFeature `json:"features"`
Plan string `json:"plan"`
Status DatabaseStatus `json:"status"`
TypeID string `json:"type_id"`
TypeName string `json:"type_name"`
VersionID string `json:"version_id"`
MongoReplSetName string `json:"mongo_repl_set_name"`
Instances []Instance `json:"instances"`
NextVersionID string `json:"next_version_id"`
ReadableVersion string `json:"readable_version"`
Hostname string `json:"hostname"`
CurrentOperationID string `json:"current_operation_id"`
Cluster bool `json:"cluster"`
PeriodicBackupsEnabled bool `json:"periodic_backups_enabled"`
PeriodicBackupsScheduledAt []int `json:"periodic_backups_scheduled_at"` // Hours of the day of the periodic backups (UTC)
MaintenanceWindow MaintenanceWindow `json:"maintenance_window"`
}
// InstanceStatus is a type of string representing the status of an Instance
type InstanceStatus string
const (
// InstanceStatusBooting is set when an instance is starting for the first
// time
InstanceStatusBooting InstanceStatus = "booting"
// InstanceStatusRunning is the default status when the instance is working
// normally
InstanceStatusRunning InstanceStatus = "running"
// InstanceStatusRestarting is set when an instance is restarting (during a
// plan change, at the end of an upgrade or a migration)
InstanceStatusRestarting InstanceStatus = "restarting"
// InstanceStatusMigrating is set when an instance is being migrated by the
// Scalingo infrastructure
InstanceStatusMigrating InstanceStatus = "migrating"
// InstanceStatusUpgrading is set when an instance version is being changed,
// part of a Database upgrade
InstanceStatusUpgrading InstanceStatus = "upgrading"
// InstanceStatusStopped is set when an instance has been stopped
InstanceStatusStopped InstanceStatus = "stopped"
// InstanceStatusRemoving is set during the deletion of an Instance (business
// to starter downgrade or database deletion)
InstanceStatusRemoving InstanceStatus = "removing"
)
// InstanceType is a type of string representing the type of the Instance inside a Database
type InstanceType string
const (
// InstanceTypeDBNode instances represent database instances holding data
InstanceTypeDBNode InstanceType = "db-node"
// InstanceTypeUtility instances are those running service used for running a
// service which is neither holding data nor routing requests utilities as
// stated by its Name
InstanceTypeUtility InstanceType = "utility"
// InstanceTypeHAProxy are instances running a HAProxy reverse proxy in order
// to route requests to the InstanceTypeDBNodes
InstanceTypeHAProxy InstanceType = "haproxy"
)
// Instance contains the metadata of an Instance which is one component of a
// Database deployment.
type Instance struct {
ID string `json:"id"`
Hostname string `json:"hostname"`
Port int `json:"port"`
Status InstanceStatus `json:"status"`
Type InstanceType `json:"type"`
Features []string `json:"features"`
PrivateIP string `json:"private_ip"`
}
// DatabaseRes is the returned response from DatabaseShow
type DatabaseRes struct {
Database Database `json:"database"`
}
// DatabaseShow returns the Database info of the given app/addonID
func (c *Client) DatabaseShow(ctx context.Context, app, addonID string) (Database, error) {
var res DatabaseRes
err := c.DBAPI(app, addonID).ResourceGet(ctx, "databases", addonID, nil, &res)
if err != nil {
return Database{}, errgo.Notef(err, "fail to get the database")
}
return res.Database, nil
}
// DatabaseUpdatePeriodicBackupsConfigParams contains the parameters which can
// be tweaked to update how periodic backups are triggered.
type DatabaseUpdatePeriodicBackupsConfigParams struct {
ScheduledAt *int `json:"periodic_backups_scheduled_at,omitempty"`
Enabled *bool `json:"periodic_backups_enabled,omitempty"`
}
// DatabaseUpdatePeriodicBackupsConfig updates the configuration of periodic
// backups for a given database addon
func (c *Client) DatabaseUpdatePeriodicBackupsConfig(ctx context.Context, app, addonID string, params DatabaseUpdatePeriodicBackupsConfigParams) (Database, error) {
var dbRes DatabaseRes
err := c.DBAPI(app, addonID).ResourceUpdate(ctx, "databases", addonID, map[string]DatabaseUpdatePeriodicBackupsConfigParams{
"database": params,
}, &dbRes)
if err != nil {
return Database{}, errgo.Notef(err, "fail to update periodic backups settings")
}
return dbRes.Database, nil
}
// DatabaseEnableFeatureParams contains the feature which has to be enabled
type DatabaseEnableFeatureParams struct {
Feature DatabaseFeature `json:"feature"`
}
// DatabaseFeatureStatus is a type of string representing the advancement of
// the application of a database feature
type DatabaseFeatureStatus string
const (
// DatabaseFeatureStatusActivated is set when the feature has been enabled with success
DatabaseFeatureStatusActivated DatabaseFeatureStatus = "ACTIVATED"
// DatabaseFeatureStatusPending is set when the feature is being enabled
DatabaseFeatureStatusPending DatabaseFeatureStatus = "PENDING"
// DatabaseFeatureStatusFailed is set when the feature failed to get enabeld
DatabaseFeatureStatusFailed DatabaseFeatureStatus = "FAILED"
)
// DatabaseEnableFeatureResponse is the response structure from DatabaseEnableFeature
type DatabaseEnableFeatureResponse struct {
Name string `json:"name"`
Status DatabaseFeatureStatus `json:"status"`
Message string `json:"message"`
}
// DatabaseEnableFeature enable a feature on a given database addon.
func (c *Client) DatabaseEnableFeature(ctx context.Context, app, addonID, feature string) (DatabaseEnableFeatureResponse, error) {
payload := DatabaseEnableFeatureParams{
Feature: DatabaseFeature{
Name: feature,
},
}
res := DatabaseEnableFeatureResponse{}
err := c.DBAPI(app, addonID).DoRequest(ctx, &httpclient.APIRequest{
Method: "POST",
Endpoint: "/databases/" + addonID + "/features",
Expected: httpclient.Statuses{http.StatusOK},
Params: payload,
}, &res)
if err != nil {
return res, errgo.Notef(err, "fail to enable database feature %v", feature)
}
return res, nil
}
// DatabaseDisableFeatureResponse is the response body of DatabaseDisableFeature
type DatabaseDisableFeatureResponse struct {
Message string `json:"message"`
}
// DatabaseDisableFeature disables a feature on a given database addon
func (c *Client) DatabaseDisableFeature(ctx context.Context, app, addonID, feature string) (DatabaseDisableFeatureResponse, error) {
res := DatabaseDisableFeatureResponse{}
err := c.DBAPI(app, addonID).DoRequest(ctx, &httpclient.APIRequest{
Method: "DELETE",
Endpoint: "/databases/" + addonID + "/features",
Expected: httpclient.Statuses{http.StatusOK},
Params: map[string]string{"feature": feature},
}, &res)
if err != nil {
return res, errgo.Notef(err, "fail to disable database feature %v", feature)
}
return res, nil
}
type DbmsAttributes struct {
PasswordEncryption string `json:"password_encryption"`
}
type DatabaseUser struct {
Name string `json:"name"`
ReadOnly bool `json:"read_only"`
Protected bool `json:"protected"`
Password string `json:"password,omitempty"`
DbmsAttributes *DbmsAttributes `json:"dbms_attributes,omitempty"`
}
type DatabaseCreateUserParam struct {
DatabaseID string `json:"database_id"`
Name string `json:"name"`
ReadOnly bool `json:"read_only"`
Password string `json:"password,omitempty"`
PasswordConfirmation string `json:"password_confirmation,omitempty"`
}
type databaseCreateUserPayload struct {
DatabaseUser DatabaseCreateUserParam `json:"database_user"`
}
// DatabaseUserResponse is the response body of database create user
type DatabaseUserResponse struct {
DatabaseUser DatabaseUser `json:"database_user"`
}
// DatabaseCreateUser creates an user to the given database addon
func (c *Client) DatabaseCreateUser(ctx context.Context, app, addonID string, user DatabaseCreateUserParam) (DatabaseUser, error) {
res := DatabaseUserResponse{}
payload := databaseCreateUserPayload{
DatabaseUser: user,
}
err := c.DBAPI(app, addonID).SubresourceAdd(ctx, "databases", addonID, "users", payload, &res)
if err != nil {
return res.DatabaseUser, errors.Wrapf(ctx, err, "create a user on database %s", addonID)
}
return res.DatabaseUser, nil
}
type DatabaseUpdateUserParam struct {
DatabaseID string `json:"database_id"`
Password string `json:"password,omitempty"`
PasswordConfirmation string `json:"password_confirmation,omitempty"`
}
type databaseUpdateUserPayload struct {
DatabaseUser DatabaseUpdateUserParam `json:"database_user"`
}
// DatabaseUpdateUser updates a user to the given database addon
func (c *Client) DatabaseUpdateUser(ctx context.Context, app, addonID, username string, databaseUserParams DatabaseUpdateUserParam) (DatabaseUser, error) {
res := DatabaseUserResponse{}
payload := databaseUpdateUserPayload{
DatabaseUser: databaseUserParams,
}
err := c.DBAPI(app, addonID).SubresourceUpdate(ctx, "databases", addonID, "users", username, payload, &res)
if err != nil {
return res.DatabaseUser, errors.Wrapf(ctx, err, "update a user on database %s", addonID)
}
return res.DatabaseUser, nil
}
// DatabaseUsersResponse is the response body of database list users
type DatabaseUsersResponse struct {
DatabaseUsers []DatabaseUser `json:"database-users"`
}
// DatabaseListUsers list the users of the given database addon
func (c *Client) DatabaseListUsers(ctx context.Context, app, addonID string) ([]DatabaseUser, error) {
res := DatabaseUsersResponse{}
err := c.DBAPI(app, addonID).SubresourceList(ctx, "databases", addonID, "users", nil, &res)
if err != nil {
return res.DatabaseUsers, errors.Wrap(ctx, err, "get database list of users")
}
return res.DatabaseUsers, nil
}
// DatabaseDeleteUser delete an user from the given database addon
func (c *Client) DatabaseDeleteUser(ctx context.Context, app, addonID, userName string) error {
return c.DBAPI(app, addonID).SubresourceDelete(ctx, "databases", addonID, "users", userName)
}
// DatabaseUserResetPassword resets the password for a user for given database addon
func (c *Client) DatabaseUserResetPassword(ctx context.Context, app, addonID, username string) (DatabaseUser, error) {
res := DatabaseUserResponse{}
req := &httpclient.APIRequest{
Method: "POST",
Endpoint: "/databases/" + addonID + "/users/" + username + "/reset_password",
Expected: httpclient.Statuses{http.StatusOK},
}
err := c.DBAPI(app, addonID).DoRequest(ctx, req, &res)
if err != nil {
return DatabaseUser{}, err
}
return res.DatabaseUser, nil
}