/
resource_mysql.go
371 lines (325 loc) · 12.6 KB
/
resource_mysql.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package database
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/exoscale/egoscale/v2/oapi"
"github.com/exoscale/terraform-provider-exoscale/pkg/validators"
)
type ResourceMysqlModel struct {
AdminPassword types.String `tfsdk:"admin_password"`
AdminUsername types.String `tfsdk:"admin_username"`
BackupSchedule types.String `tfsdk:"backup_schedule"`
IpFilter types.Set `tfsdk:"ip_filter"`
Settings types.String `tfsdk:"mysql_settings"`
Version types.String `tfsdk:"version"`
}
var ResourceMysqlSchema = schema.SingleNestedBlock{
MarkdownDescription: "*mysql* database service type specific arguments. Structure is documented below.",
Attributes: map[string]schema.Attribute{
"admin_password": schema.StringAttribute{
MarkdownDescription: "A custom administrator account password (may only be set at creation time).",
Optional: true,
Sensitive: true,
},
"admin_username": schema.StringAttribute{
MarkdownDescription: "A custom administrator account username (may only be set at creation time).",
Optional: true,
},
"backup_schedule": schema.StringAttribute{
MarkdownDescription: "The automated backup schedule (`HH:MM`).",
Optional: true,
Computed: true,
},
"ip_filter": schema.SetAttribute{
ElementType: types.StringType,
MarkdownDescription: "A list of CIDR blocks to allow incoming connections from.",
Optional: true,
Computed: true,
Validators: []validator.Set{
setvalidator.ValueStringsAre(validators.IsCIDRNetworkValidator{Min: 0, Max: 128}),
},
},
"mysql_settings": schema.StringAttribute{
MarkdownDescription: "MySQL configuration settings in JSON format (`exo dbaas type show mysql --settings=mysql` for reference).",
Optional: true,
Computed: true,
},
"version": schema.StringAttribute{
MarkdownDescription: "MySQL major version (`exo dbaas type show mysql` for reference; may only be set at creation time).",
Optional: true,
Computed: true,
},
},
}
// createMysql function handles MySQL specific part of database resource creation logic.
func (r *Resource) createMysql(ctx context.Context, data *ResourceModel, diagnostics *diag.Diagnostics) {
mysqlData := &ResourceMysqlModel{}
if data.Mysql != nil {
mysqlData = data.Mysql
}
service := oapi.CreateDbaasServiceMysqlJSONRequestBody{
Plan: data.Plan.ValueString(),
TerminationProtection: data.TerminationProtection.ValueBoolPointer(),
}
if !mysqlData.Version.IsUnknown() {
service.Version = mysqlData.Version.ValueStringPointer()
}
if !mysqlData.AdminPassword.IsNull() {
service.AdminPassword = mysqlData.AdminPassword.ValueStringPointer()
}
if !mysqlData.AdminUsername.IsNull() {
service.AdminUsername = mysqlData.AdminUsername.ValueStringPointer()
}
if !mysqlData.IpFilter.IsUnknown() {
obj := []string{}
if len(mysqlData.IpFilter.Elements()) > 0 {
dg := mysqlData.IpFilter.ElementsAs(ctx, &obj, false)
if dg.HasError() {
diagnostics.Append(dg...)
return
}
}
service.IpFilter = &obj
}
if !data.MaintenanceDOW.IsUnknown() && !data.MaintenanceTime.IsUnknown() {
service.Maintenance = &struct {
Dow oapi.CreateDbaasServiceMysqlJSONBodyMaintenanceDow `json:"dow"`
Time string `json:"time"`
}{
Dow: oapi.CreateDbaasServiceMysqlJSONBodyMaintenanceDow(data.MaintenanceDOW.ValueString()),
Time: data.MaintenanceTime.ValueString(),
}
}
if !mysqlData.BackupSchedule.IsUnknown() {
bh, bm, err := parseBackupSchedule(mysqlData.BackupSchedule.ValueString())
if err != nil {
diagnostics.AddError("Validation error", fmt.Sprintf("Unable to parse backup schedule, got error: %s", err))
return
}
service.BackupSchedule = &struct {
BackupHour *int64 `json:"backup-hour,omitempty"`
BackupMinute *int64 `json:"backup-minute,omitempty"`
}{
BackupHour: &bh,
BackupMinute: &bm,
}
}
settingsSchema, err := r.client.GetDbaasSettingsMysqlWithResponse(ctx)
if err != nil {
diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read database settings schema, got error: %s", err))
return
}
if settingsSchema.StatusCode() != http.StatusOK {
diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read database settings schema, unexpected status: %s", settingsSchema.Status()))
return
}
if !mysqlData.Settings.IsUnknown() {
obj, err := validateSettings(mysqlData.Settings.ValueString(), settingsSchema.JSON200.Settings.Mysql)
if err != nil {
diagnostics.AddError("Validation error", fmt.Sprintf("invalid settings: %s", err))
return
}
service.MysqlSettings = &obj
}
res, err := r.client.CreateDbaasServiceMysqlWithResponse(
ctx,
oapi.DbaasServiceName(data.Name.ValueString()),
service,
)
if err != nil {
diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create database service mysql, got error: %s", err))
return
}
if res.StatusCode() != http.StatusOK {
diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create database settings schema, unexpected status: %s", res.Status()))
return
}
r.readMysql(ctx, data, diagnostics)
}
// readMysql function handles MySQL specific part of database resource Read logic.
// It is used in the dedicated Read action but also as a finishing step of Create, Update and Import.
func (r *Resource) readMysql(ctx context.Context, data *ResourceModel, diagnostics *diag.Diagnostics) {
caCert, err := r.client.GetDatabaseCACertificate(ctx, data.Zone.ValueString())
if err != nil {
diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get CA Certificate: %s", err))
return
}
data.CA = types.StringValue(caCert)
res, err := r.client.GetDbaasServiceMysqlWithResponse(ctx, oapi.DbaasServiceName(data.Id.ValueString()))
if err != nil {
diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read database service mysql, got error: %s", err))
return
}
if res.StatusCode() != http.StatusOK {
diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read database service mysql, unexpected status: %s", res.Status()))
return
}
apiService := res.JSON200
data.CreatedAt = types.StringValue(apiService.CreatedAt.String())
data.DiskSize = types.Int64PointerValue(apiService.DiskSize)
data.NodeCPUs = types.Int64PointerValue(apiService.NodeCpuCount)
data.NodeMemory = types.Int64PointerValue(apiService.NodeMemory)
data.Nodes = types.Int64PointerValue(apiService.NodeCount)
data.State = types.StringPointerValue((*string)(apiService.State))
data.TerminationProtection = types.BoolPointerValue(apiService.TerminationProtection)
data.UpdatedAt = types.StringValue(apiService.UpdatedAt.String())
data.MaintenanceDOW = types.StringNull()
data.MaintenanceTime = types.StringNull()
if apiService.Maintenance != nil {
data.MaintenanceDOW = types.StringValue(string(apiService.Maintenance.Dow))
data.MaintenanceTime = types.StringValue(apiService.Maintenance.Time)
}
if data.Mysql == nil {
data.Mysql = &ResourceMysqlModel{}
}
data.Mysql.BackupSchedule = types.StringNull()
if apiService.BackupSchedule != nil {
backupHour := types.Int64PointerValue(apiService.BackupSchedule.BackupHour)
backupMinute := types.Int64PointerValue(apiService.BackupSchedule.BackupMinute)
data.Mysql.BackupSchedule = types.StringValue(fmt.Sprintf(
"%02d:%02d",
backupHour.ValueInt64(),
backupMinute.ValueInt64(),
))
}
data.Mysql.IpFilter = types.SetNull(types.StringType)
if apiService.IpFilter != nil {
v, dg := types.SetValueFrom(ctx, types.StringType, *apiService.IpFilter)
if dg.HasError() {
diagnostics.Append(dg...)
return
}
data.Mysql.IpFilter = v
}
data.Mysql.Version = types.StringNull()
if apiService.Version != nil {
data.Mysql.Version = types.StringValue(strings.SplitN(*apiService.Version, ".", 2)[0])
}
data.Mysql.Settings = types.StringNull()
if apiService.MysqlSettings != nil {
settings, err := json.Marshal(*apiService.MysqlSettings)
if err != nil {
diagnostics.AddError("Validation error", fmt.Sprintf("invalid settings: %s", err))
return
}
data.Mysql.Settings = types.StringValue(string(settings))
}
}
// updateMysql function handles MySQL specific part of database resource Update logic.
func (r *Resource) updateMysql(ctx context.Context, stateData *ResourceModel, planData *ResourceModel, diagnostics *diag.Diagnostics) {
var updated bool
service := oapi.UpdateDbaasServiceMysqlJSONRequestBody{}
if (!planData.MaintenanceDOW.Equal(stateData.MaintenanceDOW) && !planData.MaintenanceDOW.IsUnknown()) ||
(!planData.MaintenanceTime.Equal(stateData.MaintenanceTime) && !planData.MaintenanceTime.IsUnknown()) {
service.Maintenance = &struct {
Dow oapi.UpdateDbaasServiceMysqlJSONBodyMaintenanceDow `json:"dow"`
Time string `json:"time"`
}{
Dow: oapi.UpdateDbaasServiceMysqlJSONBodyMaintenanceDow(planData.MaintenanceDOW.ValueString()),
Time: planData.MaintenanceTime.ValueString(),
}
updated = true
}
if !planData.Plan.Equal(stateData.Plan) {
service.Plan = planData.Plan.ValueStringPointer()
updated = true
}
if !planData.TerminationProtection.Equal(stateData.TerminationProtection) {
service.TerminationProtection = planData.TerminationProtection.ValueBoolPointer()
updated = true
}
if planData.Mysql != nil {
if stateData.Mysql == nil {
stateData.Mysql = &ResourceMysqlModel{}
}
if !planData.Mysql.BackupSchedule.Equal(stateData.Mysql.BackupSchedule) {
bh, bm, err := parseBackupSchedule(planData.Mysql.BackupSchedule.ValueString())
if err != nil {
diagnostics.AddError("Validation error", fmt.Sprintf("Unable to parse backup schedule, got error: %s", err))
return
}
service.BackupSchedule = &struct {
BackupHour *int64 `json:"backup-hour,omitempty"`
BackupMinute *int64 `json:"backup-minute,omitempty"`
}{
BackupHour: &bh,
BackupMinute: &bm,
}
updated = true
}
if !planData.Mysql.IpFilter.Equal(stateData.Mysql.IpFilter) {
obj := []string{}
if len(planData.Mysql.IpFilter.Elements()) > 0 {
dg := planData.Mysql.IpFilter.ElementsAs(ctx, &obj, false)
if dg.HasError() {
diagnostics.Append(dg...)
return
}
}
service.IpFilter = &obj
updated = true
}
if !planData.Mysql.Settings.Equal(stateData.Mysql.Settings) {
settingsSchema, err := r.client.GetDbaasSettingsMysqlWithResponse(ctx)
if err != nil {
diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read database settings schema, got error: %s", err))
return
}
if settingsSchema.StatusCode() != http.StatusOK {
diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read database settings schema, unexpected status: %s", settingsSchema.Status()))
return
}
if planData.Mysql.Settings.ValueString() != "" {
obj, err := validateSettings(planData.Mysql.Settings.ValueString(), settingsSchema.JSON200.Settings.Mysql)
if err != nil {
diagnostics.AddError("Validation error", fmt.Sprintf("invalid Mysql settings: %s", err))
return
}
service.MysqlSettings = &obj
}
updated = true
}
// Aiven would overwrite the backup schedule with random value if we don't specify it explicitly every time.
if service.BackupSchedule == nil {
bh, bm, err := parseBackupSchedule(planData.Mysql.BackupSchedule.ValueString())
if err != nil {
diagnostics.AddError("Validation error", fmt.Sprintf("Unable to parse backup schedule, got error: %s", err))
return
}
service.BackupSchedule = &struct {
BackupHour *int64 `json:"backup-hour,omitempty"`
BackupMinute *int64 `json:"backup-minute,omitempty"`
}{
BackupHour: &bh,
BackupMinute: &bm,
}
}
}
if !updated {
tflog.Info(ctx, "no updates detected", map[string]interface{}{})
} else {
res, err := r.client.UpdateDbaasServiceMysqlWithResponse(
ctx,
oapi.DbaasServiceName(planData.Id.ValueString()),
service,
)
if err != nil {
diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create database service mysql, got error: %s", err))
return
}
if res.StatusCode() != http.StatusOK {
diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create database service mysql, unexpected status: %s", res.Status()))
return
}
}
r.readMysql(ctx, planData, diagnostics)
}