forked from DataDog/terraform-provider-datadog
/
resource_datadog_slo_correction.go
258 lines (231 loc) · 8.66 KB
/
resource_datadog_slo_correction.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
package datadog
import (
"context"
"github.com/Telemetron/terraform-provider-datadog/v3/datadog/internal/utils"
"github.com/Telemetron/terraform-provider-datadog/v3/datadog/internal/validators"
datadogV1 "github.com/DataDog/datadog-api-client-go/api/v1/datadog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceDatadogSloCorrection() *schema.Resource {
return &schema.Resource{
Description: "Resource for interacting with the slo_correction API.",
CreateContext: resourceDatadogSloCorrectionCreate,
ReadContext: resourceDatadogSloCorrectionRead,
UpdateContext: resourceDatadogSloCorrectionUpdate,
DeleteContext: resourceDatadogSloCorrectionDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"category": {
Type: schema.TypeString,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewSLOCorrectionCategoryFromValue),
Required: true,
Description: "Category the SLO correction belongs to.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Description of the correction being made.",
},
"end": {
Type: schema.TypeInt,
Optional: true,
ConflictsWith: []string{"rrule", "duration"},
Description: "Ending time of the correction in epoch seconds. Required for one time corrections, but optional if `rrule` is specified",
},
"slo_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of the SLO that this correction will be applied to.",
},
"start": {
Type: schema.TypeInt,
Required: true,
Description: "Starting time of the correction in epoch seconds.",
},
"timezone": {
Type: schema.TypeString,
Optional: true,
Description: "The timezone to display in the UI for the correction times (defaults to \"UTC\")",
},
"duration": {
Type: schema.TypeInt,
Optional: true,
Description: "Length of time in seconds for a specified `rrule` recurring SLO correction (required if specifying `rrule`)",
},
"rrule": {
Type: schema.TypeString,
Optional: true,
Description: "Recurrence rules as defined in the iCalendar RFC 5545.",
},
},
}
}
func buildDatadogSloCorrection(d *schema.ResourceData) *datadogV1.SLOCorrectionCreateRequest {
result := datadogV1.NewSLOCorrectionCreateRequestWithDefaults()
// `type` is hardcoded to 'correction' in Data
// only need to set `attributes` here
createData := datadogV1.NewSLOCorrectionCreateData(datadogV1.SLOCORRECTIONTYPE_CORRECTION)
attributes := datadogV1.NewSLOCorrectionCreateRequestAttributesWithDefaults()
correctionCategory := datadogV1.SLOCorrectionCategory(d.Get("category").(string))
attributes.SetCategory(correctionCategory)
attributes.SetStart(int64(d.Get("start").(int)))
if end, ok := d.GetOk("end"); ok {
attributes.SetEnd(int64(end.(int)))
}
attributes.SetSloId(d.Get("slo_id").(string))
if timezone, ok := d.GetOk("timezone"); ok {
attributes.SetTimezone(timezone.(string))
}
if description, ok := d.GetOk("description"); ok {
attributes.SetDescription(description.(string))
}
if rrule, ok := d.GetOk("rrule"); ok {
attributes.SetRrule(rrule.(string))
}
if duration, ok := d.GetOk("duration"); ok {
attributes.SetDuration(int64(duration.(int)))
}
createData.SetAttributes(*attributes)
result.SetData(*createData)
return result
}
func buildDatadogSloCorrectionUpdate(d *schema.ResourceData) *datadogV1.SLOCorrectionUpdateRequest {
result := datadogV1.NewSLOCorrectionUpdateRequestWithDefaults()
updateData := datadogV1.NewSLOCorrectionUpdateData()
attributes := datadogV1.NewSLOCorrectionUpdateRequestAttributesWithDefaults()
if description, ok := d.GetOk("description"); ok {
attributes.SetDescription(description.(string))
}
if timezone, ok := d.GetOk("timezone"); ok {
attributes.SetTimezone(timezone.(string))
}
if start, ok := d.GetOk("start"); ok {
attributes.SetStart(int64(start.(int)))
}
if end, ok := d.GetOk("end"); ok {
attributes.SetEnd(int64(end.(int)))
}
if category, ok := d.GetOk("category"); ok {
attributes.SetCategory(datadogV1.SLOCorrectionCategory(category.(string)))
}
if rrule, ok := d.GetOk("rrule"); ok {
attributes.SetRrule(rrule.(string))
}
if duration, ok := d.GetOk("duration"); ok {
attributes.SetDuration(int64(duration.(int)))
}
updateData.SetAttributes(*attributes)
result.SetData(*updateData)
return result
}
func resourceDatadogSloCorrectionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
datadogClient := providerConf.DatadogClientV1
auth := providerConf.AuthV1
ddObject := buildDatadogSloCorrection(d)
response, httpResponse, err := datadogClient.ServiceLevelObjectiveCorrectionsApi.CreateSLOCorrection(auth, *ddObject)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpResponse, "error creating SloCorrection")
}
if err := utils.CheckForUnparsed(response); err != nil {
return diag.FromErr(err)
}
sloCorrection := response.GetData()
d.SetId(sloCorrection.GetId())
return updateSLOCorrectionState(d, response.Data)
}
func updateSLOCorrectionState(d *schema.ResourceData, sloCorrectionData *datadogV1.SLOCorrection) diag.Diagnostics {
if sloCorrectionAttributes, ok := sloCorrectionData.GetAttributesOk(); ok {
if category, ok := sloCorrectionAttributes.GetCategoryOk(); ok {
if err := d.Set("category", string(*category)); err != nil {
return diag.FromErr(err)
}
}
if description, ok := sloCorrectionAttributes.GetDescriptionOk(); ok {
if err := d.Set("description", *description); err != nil {
return diag.FromErr(err)
}
}
if sloID, ok := sloCorrectionAttributes.GetSloIdOk(); ok {
if err := d.Set("slo_id", *sloID); err != nil {
return diag.FromErr(err)
}
}
if timezone, ok := sloCorrectionAttributes.GetTimezoneOk(); ok {
if err := d.Set("timezone", *timezone); err != nil {
return diag.FromErr(err)
}
}
if start, ok := sloCorrectionAttributes.GetStartOk(); ok {
if err := d.Set("start", *start); err != nil {
return diag.FromErr(err)
}
}
if end, ok := sloCorrectionAttributes.GetEndOk(); ok {
if err := d.Set("end", *end); err != nil {
return diag.FromErr(err)
}
}
if rrule, ok := sloCorrectionAttributes.GetRruleOk(); ok {
if err := d.Set("rrule", *rrule); err != nil {
return diag.FromErr(err)
}
}
if duration, ok := sloCorrectionAttributes.GetDurationOk(); ok {
if err := d.Set("duration", *duration); err != nil {
return diag.FromErr(err)
}
}
}
return nil
}
func resourceDatadogSloCorrectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
datadogClient := providerConf.DatadogClientV1
auth := providerConf.AuthV1
var err error
id := d.Id()
sloCorrectionGetResp, httpResponse, err := datadogClient.ServiceLevelObjectiveCorrectionsApi.GetSLOCorrection(auth, id)
if err != nil {
if httpResponse != nil && httpResponse.StatusCode == 404 {
// this condition takes on the job of the deprecated Exists handlers
d.SetId("")
return nil
}
return utils.TranslateClientErrorDiag(err, httpResponse, "error reading SloCorrection")
}
if err := utils.CheckForUnparsed(sloCorrectionGetResp); err != nil {
return diag.FromErr(err)
}
return updateSLOCorrectionState(d, sloCorrectionGetResp.Data)
}
func resourceDatadogSloCorrectionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
datadogClient := providerConf.DatadogClientV1
auth := providerConf.AuthV1
ddObject := buildDatadogSloCorrectionUpdate(d)
id := d.Id()
response, httpResponse, err := datadogClient.ServiceLevelObjectiveCorrectionsApi.UpdateSLOCorrection(auth, id, *ddObject)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpResponse, "error creating SloCorrection")
}
if err := utils.CheckForUnparsed(response); err != nil {
return diag.FromErr(err)
}
return updateSLOCorrectionState(d, response.Data)
}
func resourceDatadogSloCorrectionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
datadogClient := providerConf.DatadogClientV1
auth := providerConf.AuthV1
var err error
id := d.Id()
httpResponse, err := datadogClient.ServiceLevelObjectiveCorrectionsApi.DeleteSLOCorrection(auth, id)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpResponse, "error deleting SloCorrection")
}
return nil
}