-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_metric_alert_rule.go
More file actions
369 lines (341 loc) · 11.7 KB
/
data_source_metric_alert_rule.go
File metadata and controls
369 lines (341 loc) · 11.7 KB
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
package main
import (
"context"
"fmt"
axonopsClient "terraform-provider-axonops/client"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
var _ datasource.DataSource = (*metricAlertRuleDataSource)(nil)
var _ datasource.DataSourceWithConfigure = (*metricAlertRuleDataSource)(nil)
type metricAlertRuleDataSource struct {
client *axonopsClient.AxonopsHttpClient
}
func NewMetricAlertRuleDataSource() datasource.DataSource {
return &metricAlertRuleDataSource{}
}
func (d *metricAlertRuleDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*axonopsClient.AxonopsHttpClient)
if !ok {
resp.Diagnostics.AddError(
"Unexpected DataSource Configure Type",
fmt.Sprintf("Expected *axonopsClient.AxonopsHttpClient, got: %T.", req.ProviderData),
)
return
}
d.client = client
}
func (d *metricAlertRuleDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_metric_alert_rule"
}
func (d *metricAlertRuleDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Reads a metric alert rule.",
Attributes: map[string]schema.Attribute{
"cluster_name": schema.StringAttribute{
Required: true,
Description: "The name of the cluster.",
},
"cluster_type": schema.StringAttribute{
Required: true,
Description: "The cluster type (cassandra, kafka, or dse).",
},
"id": schema.StringAttribute{
Required: true,
Description: "The unique identifier for the alert rule.",
},
"name": schema.StringAttribute{
Computed: true,
Description: "The name of the alert rule.",
},
"metric": schema.StringAttribute{
Computed: true,
Description: "The PromQL-style metric expression.",
},
"operator": schema.StringAttribute{
Computed: true,
Description: "Comparison operator.",
},
"warning_value": schema.Float64Attribute{
Computed: true,
Description: "Warning threshold value.",
},
"critical_value": schema.Float64Attribute{
Computed: true,
Description: "Critical threshold value.",
},
"duration": schema.StringAttribute{
Computed: true,
Description: "Duration before triggering.",
},
"dashboard": schema.StringAttribute{
Computed: true,
Description: "The name of the dashboard containing the chart for this alert.",
},
"chart": schema.StringAttribute{
Computed: true,
Description: "The title of the chart (panel) within the dashboard for this alert.",
},
"correlation_id": schema.StringAttribute{
Computed: true,
Description: "Correlation ID linking this alert to a dashboard widget.",
},
"annotations": schema.SingleNestedAttribute{
Computed: true,
Description: "Alert rule annotations.",
Attributes: map[string]schema.Attribute{
"summary": schema.StringAttribute{
Computed: true,
Description: "Summary template for the alert.",
},
"description": schema.StringAttribute{
Computed: true,
Description: "Description of the alert rule.",
},
"widget_url": schema.StringAttribute{
Computed: true,
Description: "URL to the associated dashboard widget.",
},
},
},
"integrations": schema.SingleNestedAttribute{
Computed: true,
Description: "Integration routing configuration.",
Attributes: map[string]schema.Attribute{
"type": schema.StringAttribute{
Computed: true,
Description: "Integration type.",
},
"routing": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "Routing keys or team identifiers.",
},
"override_info": schema.BoolAttribute{
Computed: true,
Description: "Override info-level alerts.",
},
"override_warning": schema.BoolAttribute{
Computed: true,
Description: "Override warning-level alerts.",
},
"override_error": schema.BoolAttribute{
Computed: true,
Description: "Override error-level alerts.",
},
},
},
"dc": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "Datacenter filters.",
},
"rack": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "Rack filters.",
},
"host_id": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "Host ID filters.",
},
"scope": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "Scope filters.",
},
"keyspace": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "Keyspace filters.",
},
"percentile": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "Percentile filters.",
},
"consistency": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "Consistency level filters.",
},
"topic": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "Kafka topic filters.",
},
"group_id": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "Kafka consumer group ID filters.",
},
"group_by": schema.ListAttribute{
ElementType: types.StringType,
Computed: true,
Description: "Group by fields.",
},
},
}
}
type metricAlertRuleDataSourceData struct {
ClusterName types.String `tfsdk:"cluster_name"`
ClusterType types.String `tfsdk:"cluster_type"`
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Metric types.String `tfsdk:"metric"`
Operator types.String `tfsdk:"operator"`
WarningValue types.Float64 `tfsdk:"warning_value"`
CriticalValue types.Float64 `tfsdk:"critical_value"`
Duration types.String `tfsdk:"duration"`
Dashboard types.String `tfsdk:"dashboard"`
Chart types.String `tfsdk:"chart"`
CorrelationId types.String `tfsdk:"correlation_id"`
Annotations types.Object `tfsdk:"annotations"`
Integrations types.Object `tfsdk:"integrations"`
Dc types.List `tfsdk:"dc"`
Rack types.List `tfsdk:"rack"`
HostId types.List `tfsdk:"host_id"`
Scope types.List `tfsdk:"scope"`
Keyspace types.List `tfsdk:"keyspace"`
Percentile types.List `tfsdk:"percentile"`
Consistency types.List `tfsdk:"consistency"`
Topic types.List `tfsdk:"topic"`
GroupId types.List `tfsdk:"group_id"`
GroupBy types.List `tfsdk:"group_by"`
}
// duplicated from resource file per codebase convention (each file is self-contained)
var dsAnnotationsAttrTypes = map[string]attr.Type{
"summary": types.StringType,
"description": types.StringType,
"widget_url": types.StringType,
}
var dsIntegrationsAttrTypes = map[string]attr.Type{
"type": types.StringType,
"routing": types.ListType{ElemType: types.StringType},
"override_info": types.BoolType,
"override_warning": types.BoolType,
"override_error": types.BoolType,
}
func dsBuildAnnotationsObject(ctx context.Context, ann axonopsClient.MetricAlertAnnotations) (types.Object, diag.Diagnostics) {
return types.ObjectValueFrom(ctx, dsAnnotationsAttrTypes, annotationsModel{
Summary: types.StringValue(ann.Summary),
Description: types.StringValue(ann.Description),
WidgetUrl: types.StringValue(ann.WidgetUrl),
})
}
func dsBuildIntegrationsObject(ctx context.Context, integ axonopsClient.MetricAlertIntegrations) (types.Object, diag.Diagnostics) {
routing := integ.Routing
if routing == nil {
routing = []string{}
}
routingList, diags := types.ListValueFrom(ctx, types.StringType, routing)
if diags.HasError() {
return types.ObjectNull(dsIntegrationsAttrTypes), diags
}
obj, d := types.ObjectValueFrom(ctx, dsIntegrationsAttrTypes, integrationsModel{
Type: types.StringValue(integ.Type),
Routing: routingList,
OverrideInfo: types.BoolValue(integ.OverrideInfo),
OverrideWarning: types.BoolValue(integ.OverrideWarning),
OverrideError: types.BoolValue(integ.OverrideError),
})
diags.Append(d...)
return obj, diags
}
func (d *metricAlertRuleDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data metricAlertRuleDataSourceData
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
rules, err := d.client.GetAlertRules(data.ClusterType.ValueString(), data.ClusterName.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read alert rules: %s", err))
return
}
var found *axonopsClient.MetricAlertRule
for _, rule := range rules {
if rule.ID == data.ID.ValueString() {
found = &rule
break
}
}
if found == nil {
resp.Diagnostics.AddError("Not Found", fmt.Sprintf("Alert rule %s not found", data.ID.ValueString()))
return
}
data.Name = types.StringValue(found.Alert)
// Strip trailing " operator value" from expression to get base metric
data.Metric = types.StringValue(stripExprSuffix(found.Expr))
data.Operator = types.StringValue(found.Operator)
data.WarningValue = types.Float64Value(found.WarningValue)
data.CriticalValue = types.Float64Value(found.CriticalValue)
data.Duration = types.StringValue(found.For)
data.CorrelationId = types.StringValue(found.CorrelationId)
// Reverse-resolve correlation ID to dashboard/chart names
if found.CorrelationId != "" {
templates, err := d.client.GetDashboardTemplates(data.ClusterType.ValueString(), data.ClusterName.ValueString())
if err != nil {
tflog.Warn(ctx, fmt.Sprintf("Could not fetch dashboard templates: %s", err))
} else {
for _, dash := range templates.Dashboards {
for _, panel := range dash.Panels {
if panel.UUID == found.CorrelationId {
data.Dashboard = types.StringValue(dash.Name)
data.Chart = types.StringValue(panel.Title)
break
}
}
if !data.Dashboard.IsNull() {
break
}
}
}
}
if data.Dashboard.IsNull() {
data.Dashboard = types.StringValue("")
}
if data.Chart.IsNull() {
data.Chart = types.StringValue("")
}
data.Annotations, diags = dsBuildAnnotationsObject(ctx, found.Annotations)
resp.Diagnostics.Append(diags...)
data.Integrations, diags = dsBuildIntegrationsObject(ctx, found.Integrations)
resp.Diagnostics.Append(diags...)
// Parse filters
filterMap := map[string]*types.List{
"dc": &data.Dc,
"rack": &data.Rack,
"host_id": &data.HostId,
"scope": &data.Scope,
"keyspace": &data.Keyspace,
"percentile": &data.Percentile,
"consistency": &data.Consistency,
"topic": &data.Topic,
"GroupID": &data.GroupId,
"groupBy": &data.GroupBy,
}
// Set all filters to empty
emptyList, _ := types.ListValueFrom(ctx, types.StringType, []string{})
for _, v := range filterMap {
*v = emptyList
}
// Set filters from API response
for _, filter := range found.Filters {
if target, ok := filterMap[filter.Name]; ok {
*target, diags = types.ListValueFrom(ctx, types.StringType, filter.Value)
resp.Diagnostics.Append(diags...)
}
}
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}