-
Notifications
You must be signed in to change notification settings - Fork 81
/
databases.go
193 lines (164 loc) · 6.18 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
package linodego
import (
"context"
"encoding/json"
"time"
"github.com/linode/linodego/internal/parseabletime"
)
type (
DatabaseEngineType string
DatabaseDayOfWeek int
DatabaseMaintenanceFrequency string
DatabaseStatus string
)
const (
DatabaseMaintenanceDayMonday DatabaseDayOfWeek = iota + 1
DatabaseMaintenanceDayTuesday
DatabaseMaintenanceDayWednesday
DatabaseMaintenanceDayThursday
DatabaseMaintenanceDayFriday
DatabaseMaintenanceDaySaturday
DatabaseMaintenanceDaySunday
)
const (
DatabaseMaintenanceFrequencyWeekly DatabaseMaintenanceFrequency = "weekly"
DatabaseMaintenanceFrequencyMonthly DatabaseMaintenanceFrequency = "monthly"
)
const (
DatabaseEngineTypeMySQL DatabaseEngineType = "mysql"
DatabaseEngineTypePostgres DatabaseEngineType = "postgresql"
)
const (
DatabaseStatusProvisioning DatabaseStatus = "provisioning"
DatabaseStatusActive DatabaseStatus = "active"
DatabaseStatusDeleting DatabaseStatus = "deleting"
DatabaseStatusDeleted DatabaseStatus = "deleted"
DatabaseStatusSuspending DatabaseStatus = "suspending"
DatabaseStatusSuspended DatabaseStatus = "suspended"
DatabaseStatusResuming DatabaseStatus = "resuming"
DatabaseStatusRestoring DatabaseStatus = "restoring"
DatabaseStatusFailed DatabaseStatus = "failed"
DatabaseStatusDegraded DatabaseStatus = "degraded"
DatabaseStatusUpdating DatabaseStatus = "updating"
DatabaseStatusBackingUp DatabaseStatus = "backing_up"
)
// A Database is a instance of Linode Managed Databases
type Database struct {
ID int `json:"id"`
Status DatabaseStatus `json:"status"`
Label string `json:"label"`
Hosts DatabaseHost `json:"hosts"`
Region string `json:"region"`
Type string `json:"type"`
Engine string `json:"engine"`
Version string `json:"version"`
ClusterSize int `json:"cluster_size"`
ReplicationType string `json:"replication_type"`
SSLConnection bool `json:"ssl_connection"`
Encrypted bool `json:"encrypted"`
AllowList []string `json:"allow_list"`
InstanceURI string `json:"instance_uri"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
}
// DatabaseHost for Primary/Secondary of Database
type DatabaseHost struct {
Primary string `json:"primary"`
Secondary string `json:"secondary,omitempty"`
}
// DatabaseEngine is information about Engines supported by Linode Managed Databases
type DatabaseEngine struct {
ID string `json:"id"`
Engine string `json:"engine"`
Version string `json:"version"`
}
// DatabaseMaintenanceWindow stores information about a MySQL cluster's maintenance window
type DatabaseMaintenanceWindow struct {
DayOfWeek DatabaseDayOfWeek `json:"day_of_week"`
Duration int `json:"duration"`
Frequency DatabaseMaintenanceFrequency `json:"frequency"`
HourOfDay int `json:"hour_of_day"`
WeekOfMonth *int `json:"week_of_month"`
}
// DatabaseType is information about the supported Database Types by Linode Managed Databases
type DatabaseType struct {
ID string `json:"id"`
Label string `json:"label"`
Class string `json:"class"`
VirtualCPUs int `json:"vcpus"`
Disk int `json:"disk"`
Memory int `json:"memory"`
Engines DatabaseTypeEngineMap `json:"engines"`
}
// DatabaseTypeEngineMap stores a list of Database Engine types by engine
type DatabaseTypeEngineMap struct {
MySQL []DatabaseTypeEngine `json:"mysql"`
}
// DatabaseTypeEngine Sizes and Prices
type DatabaseTypeEngine struct {
Quantity int `json:"quantity"`
Price ClusterPrice `json:"price"`
}
// ClusterPrice for Hourly and Monthly price models
type ClusterPrice struct {
Hourly float32 `json:"hourly"`
Monthly float32 `json:"monthly"`
}
func (d *Database) UnmarshalJSON(b []byte) error {
type Mask Database
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
}
// ListDatabases lists all Database instances in Linode Managed Databases for the account
func (c *Client) ListDatabases(ctx context.Context, opts *ListOptions) ([]Database, error) {
response, err := getPaginatedResults[Database](ctx, c, "databases/instances", opts)
if err != nil {
return nil, err
}
return response, nil
}
// ListDatabaseEngines lists all Database Engines. This endpoint is cached by default.
func (c *Client) ListDatabaseEngines(ctx context.Context, opts *ListOptions) ([]DatabaseEngine, error) {
response, err := getPaginatedResults[DatabaseEngine](ctx, c, "databases/engines", opts)
if err != nil {
return nil, err
}
return response, nil
}
// GetDatabaseEngine returns a specific Database Engine. This endpoint is cached by default.
func (c *Client) GetDatabaseEngine(ctx context.Context, _ *ListOptions, engineID string) (*DatabaseEngine, error) {
e := formatAPIPath("databases/engines/%s", engineID)
response, err := doGETRequest[DatabaseEngine](ctx, c, e)
if err != nil {
return nil, err
}
return response, nil
}
// ListDatabaseTypes lists all Types of Database provided in Linode Managed Databases. This endpoint is cached by default.
func (c *Client) ListDatabaseTypes(ctx context.Context, opts *ListOptions) ([]DatabaseType, error) {
response, err := getPaginatedResults[DatabaseType](ctx, c, "databases/types", opts)
if err != nil {
return nil, err
}
return response, nil
}
// GetDatabaseType returns a specific Database Type. This endpoint is cached by default.
func (c *Client) GetDatabaseType(ctx context.Context, _ *ListOptions, typeID string) (*DatabaseType, error) {
e := formatAPIPath("databases/types/%s", typeID)
response, err := doGETRequest[DatabaseType](ctx, c, e)
if err != nil {
return nil, err
}
return response, nil
}