-
Notifications
You must be signed in to change notification settings - Fork 83
/
mongo.go
324 lines (271 loc) · 10.9 KB
/
mongo.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
package linodego
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/go-resty/resty/v2"
"github.com/linode/linodego/internal/parseabletime"
)
type MongoDatabaseTarget string
const (
MongoDatabaseTargetPrimary MongoDatabaseTarget = "primary"
MongoDatabaseTargetSecondary MongoDatabaseTarget = "secondary"
)
type MongoCompressionType string
const (
MongoCompressionNone MongoCompressionType = "none"
MongoCompressionSnappy MongoCompressionType = "snappy"
MongoCompressionZlib MongoCompressionType = "zlib"
)
type MongoStorageEngine string
const (
MongoStorageWiredTiger MongoStorageEngine = "wiredtiger"
MongoStorageMmapv1 MongoStorageEngine = "mmapv1"
)
// A MongoDatabase is a instance of Linode Mongo Managed Databases
type MongoDatabase struct {
ID int `json:"id"`
Status DatabaseStatus `json:"status"`
Label string `json:"label"`
Region string `json:"region"`
Type string `json:"type"`
Engine string `json:"engine"`
Version string `json:"version"`
Encrypted bool `json:"encrypted"`
AllowList []string `json:"allow_list"`
Peers []string `json:"peers"`
Port int `json:"port"`
ReplicaSet string `json:"replica_set"`
SSLConnection bool `json:"ssl_connection"`
ClusterSize int `json:"cluster_size"`
Hosts DatabaseHost `json:"hosts"`
CompressionType MongoCompressionType `json:"compression_type"`
StorageEngine MongoStorageEngine `json:"storage_engine"`
Updates DatabaseMaintenanceWindow `json:"updates"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
}
func (d *MongoDatabase) UnmarshalJSON(b []byte) error {
type Mask MongoDatabase
p := struct {
*Mask
Created *parseabletime.ParseableTime `json:"created"`
Updated *parseabletime.ParseableTime `json:"updated"`
}{
Mask: (*Mask)(d),
}
if err := json.Unmarshal(b, &p); err != nil {
return err
}
d.Created = (*time.Time)(p.Created)
d.Updated = (*time.Time)(p.Updated)
return nil
}
// MongoCreateOptions fields are used when creating a new Mongo Database
type MongoCreateOptions struct {
Label string `json:"label"`
Region string `json:"region"`
Type string `json:"type"`
Engine string `json:"engine"`
AllowList []string `json:"allow_list,omitempty"`
ClusterSize int `json:"cluster_size,omitempty"`
Encrypted bool `json:"encrypted,omitempty"`
SSLConnection bool `json:"ssl_connection,omitempty"`
CompressionType MongoCompressionType `json:"compression_type,omitempty"`
StorageEngine MongoStorageEngine `json:"storage_engine,omitempty"`
}
// MongoUpdateOptions fields are used when altering the existing Mongo Database
type MongoUpdateOptions struct {
Label string `json:"label,omitempty"`
AllowList *[]string `json:"allow_list,omitempty"`
Updates *DatabaseMaintenanceWindow `json:"updates,omitempty"`
}
// MongoDatabaseSSL is the SSL Certificate to access the Linode Managed Mongo Database
type MongoDatabaseSSL struct {
CACertificate []byte `json:"ca_certificate"`
}
// MongoDatabaseCredential is the Root Credentials to access the Linode Managed Database
type MongoDatabaseCredential struct {
Username string `json:"username"`
Password string `json:"password"`
}
type MongoDatabasesPagedResponse struct {
*PageOptions
Data []MongoDatabase `json:"data"`
}
func (MongoDatabasesPagedResponse) endpoint(_ ...any) string {
return "databases/mongodb/instances"
}
func (resp *MongoDatabasesPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(MongoDatabasesPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*MongoDatabasesPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}
// ListMongoDatabases lists all Mongo Databases associated with the account
func (c *Client) ListMongoDatabases(ctx context.Context, opts *ListOptions) ([]MongoDatabase, error) {
response := MongoDatabasesPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
// MongoDatabaseBackup is information for interacting with a backup for the existing Mongo Database
type MongoDatabaseBackup struct {
ID int `json:"id"`
Label string `json:"label"`
Type string `json:"type"`
Created *time.Time `json:"-"`
}
func (d *MongoDatabaseBackup) UnmarshalJSON(b []byte) error {
type Mask MongoDatabaseBackup
p := struct {
*Mask
Created *parseabletime.ParseableTime `json:"created"`
}{
Mask: (*Mask)(d),
}
if err := json.Unmarshal(b, &p); err != nil {
return err
}
d.Created = (*time.Time)(p.Created)
return nil
}
// MongoBackupCreateOptions are options used for CreateMongoDatabaseBackup(...)
type MongoBackupCreateOptions struct {
Label string `json:"label"`
Target MongoDatabaseTarget `json:"target"`
}
type MongoDatabaseBackupsPagedResponse struct {
*PageOptions
Data []MongoDatabaseBackup `json:"data"`
}
func (MongoDatabaseBackupsPagedResponse) endpoint(ids ...any) string {
id := ids[0].(int)
return fmt.Sprintf("databases/mongodb/instances/%d/backups", id)
}
func (resp *MongoDatabaseBackupsPagedResponse) castResult(r *resty.Request, e string) (int, int, error) {
res, err := coupleAPIErrors(r.SetResult(MongoDatabaseBackupsPagedResponse{}).Get(e))
if err != nil {
return 0, 0, err
}
castedRes := res.Result().(*MongoDatabaseBackupsPagedResponse)
resp.Data = append(resp.Data, castedRes.Data...)
return castedRes.Pages, castedRes.Results, nil
}
// ListMongoDatabaseBackups lists all Mongo Database Backups associated with the given Mongo Database
func (c *Client) ListMongoDatabaseBackups(ctx context.Context, databaseID int, opts *ListOptions) ([]MongoDatabaseBackup, error) {
response := MongoDatabaseBackupsPagedResponse{}
err := c.listHelper(ctx, &response, opts, databaseID)
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetMongoDatabase returns a single Mongo Database matching the id
func (c *Client) GetMongoDatabase(ctx context.Context, databaseID int) (*MongoDatabase, error) {
e := fmt.Sprintf("databases/mongodb/instances/%d", databaseID)
req := c.R(ctx).SetResult(&MongoDatabase{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*MongoDatabase), nil
}
// CreateMongoDatabase creates a new Mongo Database using the createOpts as configuration, returns the new Mongo Database
func (c *Client) CreateMongoDatabase(ctx context.Context, opts MongoCreateOptions) (*MongoDatabase, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}
e := "databases/mongodb/instances"
req := c.R(ctx).SetResult(&MongoDatabase{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Post(e))
if err != nil {
return nil, err
}
return r.Result().(*MongoDatabase), nil
}
// DeleteMongoDatabase deletes an existing Mongo Database with the given id
func (c *Client) DeleteMongoDatabase(ctx context.Context, databaseID int) error {
e := fmt.Sprintf("databases/mongodb/instances/%d", databaseID)
_, err := coupleAPIErrors(c.R(ctx).Delete(e))
return err
}
// UpdateMongoDatabase updates the given Mongo Database with the provided opts, returns the MongoDatabase with the new settings
func (c *Client) UpdateMongoDatabase(ctx context.Context, databaseID int, opts MongoUpdateOptions) (*MongoDatabase, error) {
body, err := json.Marshal(opts)
if err != nil {
return nil, err
}
e := fmt.Sprintf("databases/mongodb/instances/%d", databaseID)
req := c.R(ctx).SetResult(&MongoDatabase{}).SetBody(string(body))
r, err := coupleAPIErrors(req.Put(e))
if err != nil {
return nil, err
}
return r.Result().(*MongoDatabase), nil
}
// PatchMongoDatabase applies security patches and updates to the underlying operating system of the Managed Mongo Database
func (c *Client) PatchMongoDatabase(ctx context.Context, databaseID int) error {
e := fmt.Sprintf("databases/mongodb/instances/%d/patch", databaseID)
_, err := coupleAPIErrors(c.R(ctx).Post(e))
return err
}
// GetMongoDatabaseCredentials returns the Root Credentials for the given Mongo Database
func (c *Client) GetMongoDatabaseCredentials(ctx context.Context, databaseID int) (*MongoDatabaseCredential, error) {
e := fmt.Sprintf("databases/mongodb/instances/%d/credentials", databaseID)
req := c.R(ctx).SetResult(&MongoDatabaseCredential{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*MongoDatabaseCredential), nil
}
// ResetMongoDatabaseCredentials returns the Root Credentials for the given Mongo Database (may take a few seconds to work)
func (c *Client) ResetMongoDatabaseCredentials(ctx context.Context, databaseID int) error {
e := fmt.Sprintf("databases/mongodb/instances/%d/credentials/reset", databaseID)
_, err := coupleAPIErrors(c.R(ctx).Post(e))
return err
}
// GetMongoDatabaseSSL returns the SSL Certificate for the given Mongo Database
func (c *Client) GetMongoDatabaseSSL(ctx context.Context, databaseID int) (*MongoDatabaseSSL, error) {
e := fmt.Sprintf("databases/mongodb/instances/%d/ssl", databaseID)
req := c.R(ctx).SetResult(&MongoDatabaseSSL{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*MongoDatabaseSSL), nil
}
// GetMongoDatabaseBackup returns a specific Mongo Database Backup with the given ids
func (c *Client) GetMongoDatabaseBackup(ctx context.Context, databaseID int, backupID int) (*MongoDatabaseBackup, error) {
e := fmt.Sprintf("databases/mongodb/instances/%d/backups/%d", databaseID, backupID)
req := c.R(ctx).SetResult(&MongoDatabaseBackup{})
r, err := coupleAPIErrors(req.Get(e))
if err != nil {
return nil, err
}
return r.Result().(*MongoDatabaseBackup), nil
}
// RestoreMongoDatabaseBackup returns the given Mongo Database with the given Backup
func (c *Client) RestoreMongoDatabaseBackup(ctx context.Context, databaseID int, backupID int) error {
e := fmt.Sprintf("databases/mongodb/instances/%d/backups/%d/restore", databaseID, backupID)
_, err := coupleAPIErrors(c.R(ctx).Post(e))
return err
}
// CreateMongoDatabaseBackup creates a snapshot for the given Mongo database
func (c *Client) CreateMongoDatabaseBackup(ctx context.Context, databaseID int, opts MongoBackupCreateOptions) error {
body, err := json.Marshal(opts)
if err != nil {
return err
}
e := fmt.Sprintf("databases/mongodb/instances/%d/backups", databaseID)
_, err = coupleAPIErrors(c.R(ctx).SetBody(string(body)).Post(e))
return err
}