Skip to content

Commit 480b2fe

Browse files
moustafabrwwiv
andauthored
applatform: remove dynamic types from alertrule resource using map (#2465)
This dynamic type was preventing generation of crossplane provider tooling as dynamic types are not supported. Follow up to #2426 Object type is also not supported unfortunately. Follow up to #2463 Co-authored-by: William Wernert <[email protected]>
1 parent 2e36f4c commit 480b2fe

File tree

3 files changed

+18
-35
lines changed

3 files changed

+18
-35
lines changed

docs/resources/apps_rules_alertrule_v0alpha1.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ resource "grafana_apps_rules_alertrule_v0alpha1" "example" {
103103
}
104104
panel_ref = {
105105
dashboard_uid = "dashboard123"
106-
panel_id = 5
106+
panel_id = "5"
107107
}
108108
}
109109
}
@@ -167,7 +167,7 @@ Optional:
167167
- `labels` (Map of String) Key-value pairs to attach to the alert rule that can be used in matching, grouping, and routing.
168168
- `missing_series_evals_to_resolve` (Number) The number of missing series evaluations that must occur before the rule is considered to be resolved.
169169
- `notification_settings` (Block, Optional) Notification settings for the rule. If specified, it overrides the notification policies. (see [below for nested schema](#nestedblock--spec--notification_settings))
170-
- `panel_ref` (Object) Reference to a panel that this alert rule is associated with. Should be an object with 'dashboard_uid' (string) and 'panel_id' (number) fields. (see [below for nested schema](#nestedatt--spec--panel_ref))
170+
- `panel_ref` (Map of String) Reference to a panel that this alert rule is associated with. Should be an object with 'dashboard_uid' (string) and 'panel_id' (number) fields.
171171
- `paused` (Boolean) Sets whether the rule should be paused or not.
172172
- `trigger` (Block, Optional) The trigger configuration for the alert rule. (see [below for nested schema](#nestedblock--spec--trigger))
173173

@@ -188,15 +188,6 @@ Optional:
188188
- `repeat_interval` (String) Minimum time interval for re-sending a notification if an alert is still firing.
189189

190190

191-
<a id="nestedatt--spec--panel_ref"></a>
192-
### Nested Schema for `spec.panel_ref`
193-
194-
Optional:
195-
196-
- `dashboard_uid` (String)
197-
- `panel_id` (Number)
198-
199-
200191
<a id="nestedblock--spec--trigger"></a>
201192
### Nested Schema for `spec.trigger`
202193

examples/resources/grafana_apps_rules_alertrule_v0alpha1/resource.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ resource "grafana_apps_rules_alertrule_v0alpha1" "example" {
8888
}
8989
panel_ref = {
9090
dashboard_uid = "dashboard123"
91-
panel_id = 5
91+
panel_id = "5"
9292
}
9393
}
9494
}

internal/resources/appplatform/alertrule_resource.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package appplatform
33
import (
44
"context"
55
"encoding/json"
6+
"strconv"
67

78
"github.com/grafana/grafana/apps/alerting/rules/pkg/apis/alerting/v0alpha1"
89
"github.com/grafana/terraform-provider-grafana/v4/internal/common"
@@ -29,7 +30,7 @@ var alertRuleSpecType = types.ObjectType{
2930
"notification_settings": notificationSettingsType,
3031
"annotations": types.MapType{ElemType: types.StringType},
3132
"labels": types.MapType{ElemType: types.StringType},
32-
"panel_ref": panelRefType,
33+
"panel_ref": types.MapType{ElemType: types.StringType},
3334
},
3435
}
3536

@@ -46,7 +47,7 @@ type AlertRuleSpecModel struct {
4647
NotificationSettings types.Object `tfsdk:"notification_settings"`
4748
Annotations types.Map `tfsdk:"annotations"`
4849
Labels types.Map `tfsdk:"labels"`
49-
PanelRef types.Object `tfsdk:"panel_ref"`
50+
PanelRef types.Map `tfsdk:"panel_ref"`
5051
}
5152

5253
var notificationSettingsType = types.ObjectType{
@@ -71,13 +72,6 @@ type NotificationSettingsModel struct {
7172
RepeatInterval types.String `tfsdk:"repeat_interval"`
7273
}
7374

74-
var panelRefType = types.ObjectType{
75-
AttrTypes: map[string]attr.Type{
76-
"dashboard_uid": types.StringType,
77-
"panel_id": types.Int64Type,
78-
},
79-
}
80-
8175
type PanelRefModel struct {
8276
DashboardUID types.String `tfsdk:"dashboard_uid"`
8377
PanelID types.Int64 `tfsdk:"panel_id"`
@@ -143,10 +137,10 @@ Manages Grafana Alert Rules.
143137
ElementType: types.StringType,
144138
Description: "Key-value pairs to attach to the alert rule that can be used in matching, grouping, and routing.",
145139
},
146-
"panel_ref": schema.ObjectAttribute{
147-
AttributeTypes: panelRefType.AttributeTypes(),
148-
Optional: true,
149-
Description: "Reference to a panel that this alert rule is associated with. Should be an object with 'dashboard_uid' (string) and 'panel_id' (number) fields.",
140+
"panel_ref": schema.MapAttribute{
141+
ElementType: types.StringType,
142+
Optional: true,
143+
Description: "Reference to a panel that this alert rule is associated with. Should be an object with 'dashboard_uid' (string) and 'panel_id' (number) fields.",
150144
},
151145
},
152146
SpecBlocks: map[string]schema.Block{
@@ -422,13 +416,13 @@ func saveAlertRuleSpec(ctx context.Context, src *v0alpha1.AlertRule, dst *Resour
422416
values["labels"] = types.MapNull(types.StringType)
423417
}
424418
if src.Spec.PanelRef != nil {
425-
panelRef, d := types.ObjectValueFrom(ctx, panelRefType.AttrTypes, src.Spec.PanelRef)
419+
panelRef, d := types.MapValueFrom(ctx, types.StringType, src.Spec.PanelRef)
426420
if d.HasError() {
427421
return d
428422
}
429423
values["panel_ref"] = panelRef
430424
} else {
431-
values["panel_ref"] = types.ObjectNull(panelRefType.AttrTypes)
425+
values["panel_ref"] = types.MapNull(types.StringType)
432426
}
433427
if len(src.Spec.Expressions) > 0 {
434428
// Convert expressions to map[string]string where each string is JSON
@@ -535,28 +529,26 @@ func parseAlertRuleTrigger(ctx context.Context, src types.Object) (v0alpha1.Aler
535529
}, diag.Diagnostics{}
536530
}
537531

538-
func parsePanelRef(ctx context.Context, src types.Object) (v0alpha1.AlertRuleV0alpha1SpecPanelRef, diag.Diagnostics) {
539-
attrs := src.Attributes()
532+
func parsePanelRef(ctx context.Context, src types.Map) (v0alpha1.AlertRuleV0alpha1SpecPanelRef, diag.Diagnostics) {
533+
attrs := src.Elements()
540534
dashboardUID, ok := attrs["dashboard_uid"].(types.String)
541535
if !ok {
542536
return v0alpha1.AlertRuleV0alpha1SpecPanelRef{}, diag.Diagnostics{
543537
diag.NewErrorDiagnostic("Invalid panel_ref.dashboard_uid", "dashboard_uid must be a string"),
544538
}
545539
}
546540

547-
panelID, ok := attrs["panel_id"].(types.Number)
548-
if !ok {
541+
panelIDStr, ok := attrs["panel_id"].(types.String)
542+
panelID, err := strconv.ParseInt(panelIDStr.String(), 10, 64)
543+
if !ok || err != nil {
549544
return v0alpha1.AlertRuleV0alpha1SpecPanelRef{}, diag.Diagnostics{
550545
diag.NewErrorDiagnostic("Invalid panel_ref.panel_id", "panel_id must be a number"),
551546
}
552547
}
553548

554-
panelIDBigFloat := panelID.ValueBigFloat()
555-
panelIDInt64, _ := panelIDBigFloat.Int64()
556-
557549
return v0alpha1.AlertRuleV0alpha1SpecPanelRef{
558550
DashboardUID: dashboardUID.ValueString(),
559-
PanelID: panelIDInt64,
551+
PanelID: panelID,
560552
}, diag.Diagnostics{}
561553
}
562554

0 commit comments

Comments
 (0)