/
metric_export_datadog_config_resource.go
443 lines (400 loc) · 13.8 KB
/
metric_export_datadog_config_resource.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
Copyright 2023 The Cockroach Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package provider
import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/cockroachdb/cockroach-cloud-sdk-go/pkg/client"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
)
var metricExportDatadogConfigAttributes = map[string]schema.Attribute{
"id": schema.StringAttribute{
Required: true,
MarkdownDescription: "Cluster ID.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
},
},
"site": schema.StringAttribute{
Required: true,
MarkdownDescription: "The Datadog region to export to.",
},
"api_key": schema.StringAttribute{
Required: true,
Sensitive: true,
MarkdownDescription: "A Datadog API key.",
},
"status": schema.StringAttribute{
Computed: true,
Description: "Encodes the possible states that a metric export configuration can be in as it is created, deployed, and disabled.",
},
"user_message": schema.StringAttribute{
Computed: true,
Description: "Elaborates on the metric export status and hints at how to fix issues that may have occurred during asynchronous operations.",
},
}
type metricExportDatadogConfigResource struct {
provider *provider
}
func (r *metricExportDatadogConfigResource) Schema(
_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse,
) {
resp.Schema = schema.Schema{
Description: "DataDog metric export configuration for a cluster.",
Attributes: metricExportDatadogConfigAttributes,
}
}
func (r *metricExportDatadogConfigResource) Metadata(
_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_metric_export_datadog_config"
}
func (r *metricExportDatadogConfigResource) Configure(
_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse,
) {
if req.ProviderData == nil {
return
}
var ok bool
if r.provider, ok = req.ProviderData.(*provider); !ok {
resp.Diagnostics.AddError("Internal provider error",
fmt.Sprintf("Error in Configure: expected %T but got %T", provider{}, req.ProviderData))
}
}
// Create
func (r *metricExportDatadogConfigResource) Create(
ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse,
) {
if r.provider == nil || !r.provider.configured {
addConfigureProviderErr(&resp.Diagnostics)
return
}
var plan ClusterDatadogMetricExportConfig
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
clusterID := plan.ID.ValueString()
// Check cluster
cluster, _, err := r.provider.service.GetCluster(ctx, clusterID)
if err != nil {
resp.Diagnostics.AddError(
"Error getting cluster",
fmt.Sprintf("Could not retrieve cluster info: %s", formatAPIErrorMessage(err)),
)
return
}
if cluster.Config.Serverless != nil {
resp.Diagnostics.AddError(
"Incompatible cluster type",
"Datadog metric export services are only available for dedicated clusters",
)
return
}
site, err := client.NewDatadogSiteTypeFromValue(plan.Site.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error preparing Datadog metric export config",
fmt.Sprintf("Invalid Datadog site: %s", err),
)
return
}
apiObj := &client.DatadogMetricExportInfo{}
err = retry.RetryContext(ctx, clusterUpdateTimeout, retryEnableDatadogMetricExport(
ctx, clusterUpdateTimeout, r.provider.service, clusterID, cluster,
client.NewEnableDatadogMetricExportRequest(plan.ApiKey.ValueString(), *site),
apiObj,
))
if err != nil {
resp.Diagnostics.AddError(
"Error enabling Datadog metric export", err.Error(),
)
return
}
err = retry.RetryContext(ctx, clusterUpdateTimeout,
waitForDatdogMetricExportReadyFunc(ctx, clusterID, r.provider.service, apiObj))
if err != nil {
resp.Diagnostics.AddError(
"Error enabling Datadog metric export",
fmt.Sprintf("Could not enable Datadog metric export: %s", formatAPIErrorMessage(err)),
)
return
}
var state ClusterDatadogMetricExportConfig
// The API returns a truncated API key, so use the plan value.
state.ApiKey = plan.ApiKey
loadDatadogMetricExportIntoTerraformState(apiObj, &state)
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
}
func retryEnableDatadogMetricExport(
ctx context.Context,
timeout time.Duration,
cl client.Service,
clusterID string,
cluster *client.Cluster,
apiRequest *client.EnableDatadogMetricExportRequest,
apiObj *client.DatadogMetricExportInfo,
) retry.RetryFunc {
return func() *retry.RetryError {
apiResp, httpResp, err := cl.EnableDatadogMetricExport(ctx, clusterID, apiRequest)
if err != nil {
apiErrMsg := formatAPIErrorMessage(err)
if (httpResp != nil && httpResp.StatusCode == http.StatusServiceUnavailable) ||
strings.Contains(apiErrMsg, "lock") {
// Wait for cluster to be ready.
clusterErr := retry.RetryContext(ctx, clusterUpdateTimeout,
waitForClusterReadyFunc(ctx, clusterID, cl, cluster))
if clusterErr != nil {
return retry.NonRetryableError(
fmt.Errorf("error checking cluster availability: %s", clusterErr.Error()))
}
return retry.RetryableError(fmt.Errorf("cluster was not ready - trying again"))
}
return retry.NonRetryableError(
fmt.Errorf("could not enable Datadog metric export: %v", apiErrMsg),
)
}
*apiObj = *apiResp
return nil
}
}
func loadDatadogMetricExportIntoTerraformState(
apiObj *client.DatadogMetricExportInfo, state *ClusterDatadogMetricExportConfig,
) {
state.ID = types.StringValue(apiObj.GetClusterId())
state.Site = types.StringValue(string(apiObj.GetSite()))
state.Status = types.StringValue(string(apiObj.GetStatus()))
state.UserMessage = types.StringValue(apiObj.GetUserMessage())
}
func waitForDatdogMetricExportReadyFunc(
ctx context.Context,
clusterID string,
cl client.Service,
datadogMetricExportInfo *client.DatadogMetricExportInfo,
) retry.RetryFunc {
return func() *retry.RetryError {
apiObj, httpResp, err := cl.GetDatadogMetricExportInfo(ctx, clusterID)
if err != nil {
if httpResp != nil && httpResp.StatusCode < http.StatusInternalServerError {
return retry.NonRetryableError(fmt.Errorf(
"error getting Datadog metric export info: %s", formatAPIErrorMessage(err)))
} else {
return retry.RetryableError(fmt.Errorf(
"encountered a server error while reading Datadog metric export status - trying again"))
}
}
*datadogMetricExportInfo = *apiObj
switch datadogMetricExportInfo.GetStatus() {
case client.METRICEXPORTSTATUSTYPE_ERROR:
errMsg := "an error occurred during Datadog metric export config update"
if datadogMetricExportInfo.GetUserMessage() != "" {
errMsg = fmt.Sprintf("%s: %s", errMsg, datadogMetricExportInfo.GetUserMessage())
}
return retry.NonRetryableError(fmt.Errorf(errMsg))
case client.METRICEXPORTSTATUSTYPE_ENABLING, client.METRICEXPORTSTATUSTYPE_DISABLING:
return retry.RetryableError(fmt.Errorf("the Datadog metric export is not ready yet"))
default:
return nil
}
}
}
// Read
func (r *metricExportDatadogConfigResource) Read(
ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse,
) {
if r.provider == nil || !r.provider.configured {
addConfigureProviderErr(&resp.Diagnostics)
return
}
var state ClusterDatadogMetricExportConfig
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() || state.ID.IsNull() {
return
}
clusterID := state.ID.ValueString()
apiObj, httpResp, err := r.provider.service.GetDatadogMetricExportInfo(ctx, clusterID)
if err != nil {
if httpResp != nil && httpResp.StatusCode == http.StatusNotFound {
resp.Diagnostics.AddWarning(
"Datadog metric export config not found",
fmt.Sprintf(
"The Datadog metric export config with cluster ID %s is not found. Removing from state.",
clusterID,
))
resp.State.RemoveResource(ctx)
return
} else {
// Check cluster existence.
cluster, clusterHttpResp, clusterErr := r.provider.service.GetCluster(ctx, clusterID)
if clusterErr != nil {
if clusterHttpResp != nil && clusterHttpResp.StatusCode == http.StatusNotFound {
resp.Diagnostics.AddWarning(
"Cluster not found",
fmt.Sprintf("The Datadog metric export config's cluster with ID %s is not found. Removing from state.",
clusterID,
))
resp.State.RemoveResource(ctx)
return
} else {
resp.Diagnostics.AddError(
"Error getting cluster",
fmt.Sprintf("Unexpected error retrieving the Datadog metric export config's cluster: %s",
formatAPIErrorMessage(clusterErr),
))
}
}
if cluster.State == client.CLUSTERSTATETYPE_DELETED {
resp.Diagnostics.AddWarning(
"Cluster deleted",
fmt.Sprintf("The Datadog metric export config's cluster with ID %s was deleted. Removing from state.",
clusterID,
))
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(
"Error getting Datadog metric export info",
fmt.Sprintf("Unexpected error retrieving Datadog metric export info: %s", formatAPIErrorMessage(err)))
}
return
}
loadDatadogMetricExportIntoTerraformState(apiObj, &state)
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
}
// Update
func (r *metricExportDatadogConfigResource) Update(
ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse,
) {
// Get plan values
var plan ClusterDatadogMetricExportConfig
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get current state
var state ClusterDatadogMetricExportConfig
diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
site, err := client.NewDatadogSiteTypeFromValue(plan.Site.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error preparing Datadog metric export config",
fmt.Sprintf("Invalid Datadog site: %s", err),
)
return
}
clusterID := plan.ID.ValueString()
cluster := &client.Cluster{}
apiObj := &client.DatadogMetricExportInfo{}
err = retry.RetryContext(ctx, clusterUpdateTimeout, retryEnableDatadogMetricExport(
ctx, clusterUpdateTimeout, r.provider.service, clusterID, cluster,
client.NewEnableDatadogMetricExportRequest(plan.ApiKey.ValueString(), *site),
apiObj,
))
if err != nil {
resp.Diagnostics.AddError(
"Error enabling Datadog metric export", err.Error(),
)
return
}
err = retry.RetryContext(ctx, clusterUpdateTimeout,
waitForDatdogMetricExportReadyFunc(ctx, clusterID, r.provider.service, apiObj))
if err != nil {
resp.Diagnostics.AddError(
"Error enabling Datadog metric export",
fmt.Sprintf("Could not enable Datadog metric export: %s", formatAPIErrorMessage(err)),
)
return
}
// The API returns a truncated API key, so use the plan value.
state.ApiKey = plan.ApiKey
loadDatadogMetricExportIntoTerraformState(apiObj, &state)
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
}
// Delete
func (r *metricExportDatadogConfigResource) Delete(
ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse,
) {
var state ClusterDatadogMetricExportConfig
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
clusterID := state.ID.ValueString()
cluster := &client.Cluster{}
retryFunc := func() retry.RetryFunc {
return func() *retry.RetryError {
_, httpResp, err := r.provider.service.DeleteDatadogMetricExport(ctx, clusterID)
if err != nil {
apiErrMsg := formatAPIErrorMessage(err)
if httpResp != nil {
if httpResp.StatusCode == http.StatusNotFound {
// Datadog metric export config or cluster is already gone. Swallow the error.
return nil
}
if httpResp.StatusCode == http.StatusServiceUnavailable ||
strings.Contains(apiErrMsg, "lock") {
// Wait for cluster to be ready.
clusterErr := retry.RetryContext(ctx, clusterUpdateTimeout,
waitForClusterReadyFunc(ctx, clusterID, r.provider.service, cluster))
if clusterErr != nil {
return retry.NonRetryableError(
fmt.Errorf("Error checking cluster availability: %s", clusterErr.Error()))
}
return retry.RetryableError(fmt.Errorf("cluster was not ready - trying again"))
}
}
return retry.NonRetryableError(
fmt.Errorf("could not delete Datadog metric export config: %v", apiErrMsg))
}
return nil
}
}
err := retry.RetryContext(ctx, clusterUpdateTimeout, retryFunc())
if err != nil {
resp.Diagnostics.AddError(
"Error deleting Datadog metric export config", err.Error(),
)
return
}
// Remove resource from state
resp.State.RemoveResource(ctx)
}
// Import state
func (r *metricExportDatadogConfigResource) ImportState(
ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse,
) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
func NewMetricExportDatadogConfigResource() resource.Resource {
return &metricExportDatadogConfigResource{}
}