forked from DataDog/terraform-provider-datadog
/
data_source_datadog_monitor.go
336 lines (315 loc) · 11.1 KB
/
data_source_datadog_monitor.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
package datadog
import (
"context"
"fmt"
"sort"
"strconv"
"strings"
"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 dataSourceDatadogMonitor() *schema.Resource {
return &schema.Resource{
Description: "Use this data source to retrieve information about an existing monitor for use in other resources.",
ReadContext: dataSourceDatadogMonitorRead,
Schema: map[string]*schema.Schema{
"name_filter": {
Description: "A monitor name to limit the search.",
Type: schema.TypeString,
Optional: true,
},
"tags_filter": {
Description: "A list of tags to limit the search. This filters on the monitor scope.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"monitor_tags_filter": {
Description: "A list of monitor tags to limit the search. This filters on the tags set on the monitor itself.",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
// Computed values
"name": {
Description: "Name of the monitor",
Type: schema.TypeString,
Computed: true,
},
"message": {
Description: "Message included with notifications for this monitor",
Type: schema.TypeString,
Computed: true,
},
"escalation_message": {
Description: "Message included with a re-notification for this monitor.",
Type: schema.TypeString,
Computed: true,
},
"query": {
Description: "Query of the monitor.",
Type: schema.TypeString,
Computed: true,
},
"type": {
Description: "Type of the monitor.",
Type: schema.TypeString,
Computed: true,
},
// Options
"monitor_thresholds": {
Description: "Alert thresholds of the monitor.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ok": {
Type: schema.TypeString,
Computed: true,
},
"warning": {
Type: schema.TypeString,
Computed: true,
},
"critical": {
Type: schema.TypeString,
Computed: true,
},
"unknown": {
Type: schema.TypeString,
Computed: true,
},
"warning_recovery": {
Type: schema.TypeString,
Computed: true,
},
"critical_recovery": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"monitor_threshold_windows": {
Description: "Mapping containing `recovery_window` and `trigger_window` values, e.g. `last_15m`. This is only used by anomaly monitors.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"recovery_window": {
Type: schema.TypeString,
Computed: true,
},
"trigger_window": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"notify_no_data": {
Description: "Whether or not this monitor notifies when data stops reporting.",
Type: schema.TypeBool,
Computed: true,
},
"new_group_delay": {
Description: "Time (in seconds) to skip evaluations for new groups.",
Type: schema.TypeInt,
Computed: true,
},
"new_host_delay": {
Description: "Time (in seconds) allowing a host to boot and applications to fully start before starting the evaluation of monitor results.",
Type: schema.TypeInt,
Computed: true,
},
"evaluation_delay": {
Description: "Time (in seconds) for which evaluation is delayed. This is only used by metric monitors.",
Type: schema.TypeInt,
Computed: true,
},
"no_data_timeframe": {
Description: "The number of minutes before the monitor notifies when data stops reporting.",
Type: schema.TypeInt,
Computed: true,
},
"renotify_interval": {
Description: "The number of minutes after the last notification before the monitor re-notifies on the current status.",
Type: schema.TypeInt,
Computed: true,
},
"renotify_occurrences": {
Description: "The number of re-notification messages that should be sent on the current status.",
Type: schema.TypeInt,
Computed: true,
},
"renotify_statuses": {
Description: "The types of statuses for which re-notification messages should be sent.",
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: validators.ValidateEnumValue(datadogV1.NewMonitorRenotifyStatusTypeFromValue),
},
Computed: true,
},
"notify_audit": {
Description: "Whether or not tagged users are notified on changes to the monitor.",
Type: schema.TypeBool,
Computed: true,
},
"timeout_h": {
Description: "Number of hours of the monitor not reporting data before it automatically resolves from a triggered state.",
Type: schema.TypeInt,
Computed: true,
},
"require_full_window": {
Description: "Whether or not the monitor needs a full window of data before it is evaluated.",
Type: schema.TypeBool,
Computed: true,
},
"locked": {
Description: "Whether or not changes to the monitor are restricted to the creator or admins.",
Type: schema.TypeBool,
Computed: true,
},
"restricted_roles": {
// Uncomment when generally available
// Description: "A list of role identifiers to associate with the monitor. Cannot be used with `locked`.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"include_tags": {
Description: "Whether or not notifications from the monitor automatically inserts its triggering tags into the title.",
Type: schema.TypeBool,
Computed: true,
},
"tags": {
Description: "List of tags associated with the monitor.",
// we use TypeSet to represent tags, paradoxically to be able to maintain them ordered;
// we order them explicitly in the read/create/update methods of this resource and using
// TypeSet makes Terraform ignore differences in order when creating a plan
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"enable_logs_sample": {
Description: "Whether or not a list of log values which triggered the alert is included. This is only used by log monitors.",
Type: schema.TypeBool,
Computed: true,
},
"groupby_simple_monitor": {
Description: "Whether or not to trigger one alert if any source breaches a threshold.",
Type: schema.TypeBool,
Computed: true,
},
},
}
}
func dataSourceDatadogMonitorRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerConf := meta.(*ProviderConfiguration)
datadogClientV1 := providerConf.DatadogClientV1
authV1 := providerConf.AuthV1
optionalParams := datadogV1.NewListMonitorsOptionalParameters()
if v, ok := d.GetOk("name_filter"); ok {
optionalParams = optionalParams.WithName(v.(string))
}
if v, ok := d.GetOk("tags_filter"); ok {
optionalParams = optionalParams.WithTags(strings.Join(expandStringList(v.([]interface{})), ","))
}
if v, ok := d.GetOk("monitor_tags_filter"); ok {
optionalParams = optionalParams.WithMonitorTags(strings.Join(expandStringList(v.([]interface{})), ","))
}
monitors, httpresp, err := datadogClientV1.MonitorsApi.ListMonitors(authV1, *optionalParams)
if err != nil {
return utils.TranslateClientErrorDiag(err, httpresp, "error querying monitors")
}
if len(monitors) > 1 {
return diag.Errorf("your query returned more than one result, please try a more specific search criteria")
}
if len(monitors) == 0 {
return diag.Errorf("your query returned no result, please try a less specific search criteria")
}
m := monitors[0]
if err := utils.CheckForUnparsed(m); err != nil {
return diag.FromErr(err)
}
thresholds := make(map[string]string)
if v, ok := m.Options.Thresholds.GetOkOk(); ok {
thresholds["ok"] = fmt.Sprintf("%v", *v)
}
if v, ok := m.Options.Thresholds.GetWarningOk(); ok {
thresholds["warning"] = fmt.Sprintf("%v", *v)
}
if v, ok := m.Options.Thresholds.GetCriticalOk(); ok {
thresholds["critical"] = fmt.Sprintf("%v", *v)
}
if v, ok := m.Options.Thresholds.GetUnknownOk(); ok {
thresholds["unknown"] = fmt.Sprintf("%v", *v)
}
if v, ok := m.Options.Thresholds.GetWarningRecoveryOk(); ok {
thresholds["warning_recovery"] = fmt.Sprintf("%v", *v)
}
if v, ok := m.Options.Thresholds.GetCriticalRecoveryOk(); ok {
thresholds["critical_recovery"] = fmt.Sprintf("%v", *v)
}
thresholdWindows := make(map[string]string)
for k, v := range map[string]string{
"recovery_window": m.Options.ThresholdWindows.GetRecoveryWindow(),
"trigger_window": m.Options.ThresholdWindows.GetTriggerWindow(),
} {
if v != "" {
thresholdWindows[k] = v
}
}
var tags []string
tags = append(tags, m.GetTags()...)
sort.Strings(tags)
var restricted_roles []string
restricted_roles = append(restricted_roles, m.GetRestrictedRoles()...)
sort.Strings(restricted_roles)
d.SetId(strconv.FormatInt(m.GetId(), 10))
d.Set("name", m.GetName())
d.Set("message", m.GetMessage())
d.Set("query", m.GetQuery())
d.Set("type", m.GetType())
if err := d.Set("monitor_thresholds", []interface{}{thresholds}); err != nil {
return diag.FromErr(err)
}
if err := d.Set("monitor_threshold_windows", []interface{}{thresholdWindows}); err != nil {
return diag.FromErr(err)
}
d.Set("new_group_delay", m.Options.GetNewGroupDelay())
d.Set("new_host_delay", m.Options.GetNewHostDelay())
d.Set("evaluation_delay", m.Options.GetEvaluationDelay())
d.Set("notify_no_data", m.Options.GetNotifyNoData())
d.Set("no_data_timeframe", m.Options.GetNoDataTimeframe())
d.Set("renotify_interval", m.Options.GetRenotifyInterval())
d.Set("renotify_occurrences", m.Options.GetRenotifyOccurrences())
d.Set("renotify_statuses", m.Options.GetRenotifyStatuses())
d.Set("notify_audit", m.Options.GetNotifyAudit())
d.Set("timeout_h", m.Options.GetTimeoutH())
d.Set("escalation_message", m.Options.GetEscalationMessage())
d.Set("include_tags", m.Options.GetIncludeTags())
d.Set("tags", tags)
d.Set("require_full_window", m.Options.GetRequireFullWindow()) // TODO Is this one of those options that we neeed to check?
d.Set("locked", m.Options.GetLocked())
d.Set("restricted_roles", restricted_roles)
d.Set("groupby_simple_monitor", m.Options.GetGroupbySimpleMonitor())
if m.GetType() == datadogV1.MONITORTYPE_LOG_ALERT {
d.Set("enable_logs_sample", m.Options.GetEnableLogsSample())
}
return nil
}
func expandStringList(configured []interface{}) []string {
vs := make([]string, 0, len(configured))
for _, v := range configured {
val, ok := v.(string)
if ok && val != "" {
vs = append(vs, v.(string))
}
}
return vs
}