/
cluster_onoff_schedule.go
429 lines (374 loc) · 13.3 KB
/
cluster_onoff_schedule.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package resources
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/couchbasecloud/terraform-provider-couchbase-capella/internal/api"
"github.com/couchbasecloud/terraform-provider-couchbase-capella/internal/errors"
providerschema "github.com/couchbasecloud/terraform-provider-couchbase-capella/internal/schema"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ resource.Resource = &ClusterOnOffSchedule{}
_ resource.ResourceWithConfigure = &ClusterOnOffSchedule{}
_ resource.ResourceWithImportState = &ClusterOnOffSchedule{}
)
const errorMessageAfterOnOffScheduleCreation = "Cluster On/Off Schedule creation is successful, but encountered an error while checking the current" +
" state of the cluster on/off schedule. Please run `terraform plan` after 1-2 minutes to know the" +
" current on/off schedule state. Additionally, run `terraform apply --refresh-only` to update" +
" the state from remote, unexpected error: "
const errorMessageWhileOnOffScheduleCreation = "There is an error during cluster on/off schedule creation. Please check in Capella to see if any hanging resources" +
" have been created, unexpected error: "
// ClusterOnOffSchedule is the cluster OnOffSchedule resource implementation.
type ClusterOnOffSchedule struct {
*providerschema.Data
}
// NewClusterOnOffSchedule is a helper function to simplify the provider implementation.
func NewClusterOnOffSchedule() resource.Resource {
return &ClusterOnOffSchedule{}
}
// Metadata returns the OnOffSchedule resource type name.
func (c *ClusterOnOffSchedule) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_cluster_onoff_schedule"
}
// Schema defines the schema for the OnOffSchedule resource.
func (c *ClusterOnOffSchedule) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = OnOffScheduleSchema()
}
// Create creates a new OnOffSchedule.
func (c *ClusterOnOffSchedule) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan providerschema.ClusterOnOffSchedule
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
err := c.validateCreateClusterOnOffScheduleRequest(plan)
if err != nil {
resp.Diagnostics.AddError(
"Error parsing create cluster on/off schedule request",
"Could not create cluster on/off schedule "+err.Error(),
)
return
}
var organizationId = plan.OrganizationId.ValueString()
var projectId = plan.ProjectId.ValueString()
var clusterId = plan.ClusterId.ValueString()
var days = make([]api.DayItem, 0)
for _, d := range plan.Days {
var from, to *api.OnTimeBoundary
if d.From != nil {
from = &api.OnTimeBoundary{
Hour: d.From.Hour.ValueInt64(),
Minute: d.From.Minute.ValueInt64(),
}
}
if d.To != nil {
to = &api.OnTimeBoundary{
Hour: d.To.Hour.ValueInt64(),
Minute: d.To.Minute.ValueInt64(),
}
}
days = append(days, api.DayItem{
State: d.State.ValueString(),
Day: d.Day.ValueString(),
From: from,
To: to,
})
}
scheduleRequest := api.CreateClusterOnOffScheduleRequest{
Timezone: plan.Timezone.ValueString(),
Days: days,
}
url := fmt.Sprintf("%s/v4/organizations/%s/projects/%s/clusters/%s/onOffSchedule", c.HostURL, organizationId, projectId, clusterId)
cfg := api.EndpointCfg{Url: url, Method: http.MethodPost, SuccessStatus: http.StatusNoContent}
_, err = c.Client.ExecuteWithRetry(
ctx,
cfg,
scheduleRequest,
c.Token,
nil,
)
if err != nil {
resp.Diagnostics.AddError(
"Error executing request",
errorMessageWhileOnOffScheduleCreation+api.ParseError(err),
)
return
}
diags = resp.State.Set(ctx, initializeScheduleWithPlan(plan))
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
refreshedState, err := c.retrieveClusterOnOffSchedule(ctx, organizationId, projectId, clusterId)
if err != nil {
resp.Diagnostics.AddWarning(
"Error Reading Capella Cluster On/Off Schedule",
"Could not read Capella Cluster On/Off Schedule for the cluster: %s "+clusterId+"."+errorMessageAfterOnOffScheduleCreation+api.ParseError(err),
)
return
}
// Set state to fully populated data
diags = resp.State.Set(ctx, refreshedState)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// initializeScheduleWithPlan initializes an instance of providerschema.ClusterOnOffSchedule
// with the specified plan. It marks all computed fields as null.
func initializeScheduleWithPlan(plan providerschema.ClusterOnOffSchedule) providerschema.ClusterOnOffSchedule {
for _, d := range plan.Days {
if d.From != nil {
if d.From.Hour.IsUnknown() || d.From.Hour.IsNull() {
d.From.Hour = types.Int64Null()
}
if d.From.Minute.IsUnknown() || d.From.Minute.IsNull() {
d.From.Minute = types.Int64Null()
}
}
if d.To != nil {
if d.To.Hour.IsUnknown() || d.To.Hour.IsNull() {
d.To.Hour = types.Int64Null()
}
if d.To.Minute.IsUnknown() || d.To.Minute.IsNull() {
d.To.Minute = types.Int64Null()
}
}
}
return plan
}
func (c *ClusterOnOffSchedule) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state providerschema.ClusterOnOffSchedule
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
resourceIDs, err := state.Validate()
if err != nil {
resp.Diagnostics.AddError(
"Error reading cluster on/off schedule",
"Could not read on/off schedule for cluster with id "+state.ClusterId.String()+": "+err.Error(),
)
return
}
var (
organizationId = resourceIDs[providerschema.OrganizationId]
projectId = resourceIDs[providerschema.ProjectId]
clusterId = resourceIDs[providerschema.ClusterId]
)
// Get refreshed on/off schedule from Capella
refreshedState, err := c.retrieveClusterOnOffSchedule(ctx, organizationId, projectId, clusterId)
if err != nil {
resourceNotFound, errString := api.CheckResourceNotFoundError(err)
if resourceNotFound {
tflog.Info(ctx, "resource doesn't exist in remote server removing resource from state file")
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(
"Error reading cluster on/off schedule",
"Could not read on/off schedule for cluster with id "+state.ClusterId.String()+": "+errString,
)
return
}
// Set refreshed state
diags = resp.State.Set(ctx, &refreshedState)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (c *ClusterOnOffSchedule) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan providerschema.ClusterOnOffSchedule
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
resourceIDs, err := plan.Validate()
if err != nil {
resp.Diagnostics.AddError(
"Error updating cluster on/off schedule",
"Could not update on/off schedule for cluster with id"+plan.ClusterId.String()+": "+err.Error(),
)
return
}
var (
organizationId = resourceIDs[providerschema.OrganizationId]
projectId = resourceIDs[providerschema.ProjectId]
clusterId = resourceIDs[providerschema.ClusterId]
)
var days = make([]api.DayItem, 0)
for _, d := range plan.Days {
var from, to *api.OnTimeBoundary
if d.From != nil {
from = &api.OnTimeBoundary{
Hour: d.From.Hour.ValueInt64(),
Minute: d.From.Minute.ValueInt64(),
}
}
if d.To != nil {
to = &api.OnTimeBoundary{
Hour: d.To.Hour.ValueInt64(),
Minute: d.To.Minute.ValueInt64(),
}
}
days = append(days, api.DayItem{
State: d.State.ValueString(),
Day: d.Day.ValueString(),
From: from,
To: to,
})
}
BackupScheduleRequest := api.UpdateClusterOnOffScheduleRequest{
Timezone: plan.Timezone.ValueString(),
Days: days,
}
url := fmt.Sprintf("%s/v4/organizations/%s/projects/%s/clusters/%s/onOffSchedule", c.HostURL, organizationId, projectId, clusterId)
cfg := api.EndpointCfg{Url: url, Method: http.MethodPut, SuccessStatus: http.StatusNoContent}
_, err = c.Client.ExecuteWithRetry(
ctx,
cfg,
BackupScheduleRequest,
c.Token,
nil,
)
if err != nil {
resp.Diagnostics.AddError(
"Error updating cluster on/off schedule",
"Could not update on/off schedule for cluster with id "+plan.ClusterId.String()+": "+api.ParseError(err),
)
return
}
currentState, err := c.retrieveClusterOnOffSchedule(ctx, organizationId, projectId, clusterId)
if err != nil {
resourceNotFound, errString := api.CheckResourceNotFoundError(err)
if resourceNotFound {
tflog.Info(ctx, "resource doesn't exist in remote server removing resource from state file")
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(
"Error reading cluster on/off schedule",
"Could not read on/off schedule for cluster with id "+plan.ClusterId.String()+": "+errString,
)
return
}
// Set state to fully populated data
diags = resp.State.Set(ctx, currentState)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (c *ClusterOnOffSchedule) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Retrieve values from state
var state providerschema.ClusterOnOffSchedule
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
resourceIDs, err := state.Validate()
if err != nil {
resp.Diagnostics.AddError(
"Error deleting cluster on/off schedule",
"Could not delete cluster on/off schedule for cluster with id "+state.ClusterId.String()+" unexpected error: "+err.Error(),
)
return
}
var (
organizationId = resourceIDs[providerschema.OrganizationId]
projectId = resourceIDs[providerschema.ProjectId]
clusterId = resourceIDs[providerschema.ClusterId]
)
url := fmt.Sprintf("%s/v4/organizations/%s/projects/%s/clusters/%s/onOffSchedule", c.HostURL, organizationId, projectId, clusterId)
cfg := api.EndpointCfg{Url: url, Method: http.MethodDelete, SuccessStatus: http.StatusNoContent}
// Delete existing backup schedule
_, err = c.Client.ExecuteWithRetry(
ctx,
cfg,
nil,
c.Token,
nil,
)
if err != nil {
resourceNotFound, errString := api.CheckResourceNotFoundError(err)
if resourceNotFound {
tflog.Info(ctx, "resource doesn't exist in remote server removing resource from state file")
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(
"Error deleting cluster on/off schedule",
"Could not delete cluster on/off schedule for cluster with id "+state.ClusterId.String()+" unexpected error: "+errString,
)
return
}
}
// ImportState imports an already existing cluster on-off schedule that is not created by Terraform.
// Since Capella APIs may require multiple IDs, such as organizationId, projectId, clusterId,
// this function passes the root attribute which is a comma separated string of multiple IDs.
// example: "organization_id=<orgId>,project_id=<projId>,cluster_id=<clusterId>
func (c *ClusterOnOffSchedule) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// Retrieve import ID and save to id attribute
resource.ImportStatePassthroughID(ctx, path.Root("cluster_id"), req, resp)
}
// Configure adds the provider configured client to the cluster on-off schedule resource.
func (c *ClusterOnOffSchedule) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
data, ok := req.ProviderData.(*providerschema.Data)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *ProviderSourceData, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
c.Data = data
}
func (c *ClusterOnOffSchedule) validateCreateClusterOnOffScheduleRequest(plan providerschema.ClusterOnOffSchedule) error {
if plan.OrganizationId.IsNull() {
return errors.ErrOrganizationIdCannotBeEmpty
}
if plan.ProjectId.IsNull() {
return errors.ErrProjectIdCannotBeEmpty
}
if plan.ClusterId.IsNull() {
return errors.ErrClusterIdCannotBeEmpty
}
return nil
}
// retrieveOnOffSchedule retrieves cluster on/off schedule information from the specified organization and project
// using the provided cluster ID by open-api call.
func (c *ClusterOnOffSchedule) retrieveClusterOnOffSchedule(ctx context.Context, organizationId, projectId, clusterId string) (*providerschema.ClusterOnOffSchedule, error) {
url := fmt.Sprintf("%s/v4/organizations/%s/projects/%s/clusters/%s/onOffSchedule", c.HostURL, organizationId, projectId, clusterId)
cfg := api.EndpointCfg{Url: url, Method: http.MethodGet, SuccessStatus: http.StatusOK}
response, err := c.Client.ExecuteWithRetry(
ctx,
cfg,
nil,
c.Token,
nil,
)
if err != nil {
return nil, err
}
onOffScheduleResp := api.GetClusterOnOffScheduleResponse{}
err = json.Unmarshal(response.Body, &onOffScheduleResp)
if err != nil {
return nil, err
}
refreshedState := providerschema.NewClusterOnOffSchedule(&onOffScheduleResp, organizationId, projectId, clusterId)
return refreshedState, nil
}