Skip to content

Commit 2e36f4c

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

File tree

3 files changed

+40
-31
lines changed

3 files changed

+40
-31
lines changed

docs/resources/apps_rules_alertrule_v0alpha1.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ resource "grafana_apps_rules_alertrule_v0alpha1" "example" {
101101
notification_settings {
102102
contact_point = "grafana-default-email"
103103
}
104+
panel_ref = {
105+
dashboard_uid = "dashboard123"
106+
panel_id = 5
107+
}
104108
}
105109
}
106110
```
@@ -163,7 +167,7 @@ Optional:
163167
- `labels` (Map of String) Key-value pairs to attach to the alert rule that can be used in matching, grouping, and routing.
164168
- `missing_series_evals_to_resolve` (Number) The number of missing series evaluations that must occur before the rule is considered to be resolved.
165169
- `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))
166-
- `panel_ref` (Dynamic) Reference to a panel that this alert rule is associated with. Should be an object with 'dashboard_uid' (string) and 'panel_id' (number) fields.
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))
167171
- `paused` (Boolean) Sets whether the rule should be paused or not.
168172
- `trigger` (Block, Optional) The trigger configuration for the alert rule. (see [below for nested schema](#nestedblock--spec--trigger))
169173

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

186190

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+
187200
<a id="nestedblock--spec--trigger"></a>
188201
### Nested Schema for `spec.trigger`
189202

examples/resources/grafana_apps_rules_alertrule_v0alpha1/resource.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,9 @@ resource "grafana_apps_rules_alertrule_v0alpha1" "example" {
8686
notification_settings {
8787
contact_point = "grafana-default-email"
8888
}
89+
panel_ref = {
90+
dashboard_uid = "dashboard123"
91+
panel_id = 5
92+
}
8993
}
9094
}

internal/resources/appplatform/alertrule_resource.go

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,24 @@ var alertRuleSpecType = types.ObjectType{
2929
"notification_settings": notificationSettingsType,
3030
"annotations": types.MapType{ElemType: types.StringType},
3131
"labels": types.MapType{ElemType: types.StringType},
32-
"panel_ref": types.DynamicType,
32+
"panel_ref": panelRefType,
3333
},
3434
}
3535

3636
type AlertRuleSpecModel struct {
37-
Title types.String `tfsdk:"title"`
38-
Expressions types.Map `tfsdk:"expressions"`
39-
Paused types.Bool `tfsdk:"paused"`
40-
Trigger types.Object `tfsdk:"trigger"`
41-
NoDataState types.String `tfsdk:"no_data_state"`
42-
ExecErrState types.String `tfsdk:"exec_err_state"`
43-
For types.String `tfsdk:"for"`
44-
KeepFiringFor types.String `tfsdk:"keep_firing_for"`
45-
MissingSeriesEvalsToResolve types.Int64 `tfsdk:"missing_series_evals_to_resolve"`
46-
NotificationSettings types.Object `tfsdk:"notification_settings"`
47-
Annotations types.Map `tfsdk:"annotations"`
48-
Labels types.Map `tfsdk:"labels"`
49-
PanelRef types.Dynamic `tfsdk:"panel_ref"`
37+
Title types.String `tfsdk:"title"`
38+
Expressions types.Map `tfsdk:"expressions"`
39+
Paused types.Bool `tfsdk:"paused"`
40+
Trigger types.Object `tfsdk:"trigger"`
41+
NoDataState types.String `tfsdk:"no_data_state"`
42+
ExecErrState types.String `tfsdk:"exec_err_state"`
43+
For types.String `tfsdk:"for"`
44+
KeepFiringFor types.String `tfsdk:"keep_firing_for"`
45+
MissingSeriesEvalsToResolve types.Int64 `tfsdk:"missing_series_evals_to_resolve"`
46+
NotificationSettings types.Object `tfsdk:"notification_settings"`
47+
Annotations types.Map `tfsdk:"annotations"`
48+
Labels types.Map `tfsdk:"labels"`
49+
PanelRef types.Object `tfsdk:"panel_ref"`
5050
}
5151

5252
var notificationSettingsType = types.ObjectType{
@@ -143,9 +143,10 @@ Manages Grafana Alert Rules.
143143
ElementType: types.StringType,
144144
Description: "Key-value pairs to attach to the alert rule that can be used in matching, grouping, and routing.",
145145
},
146-
"panel_ref": schema.DynamicAttribute{
147-
Optional: true,
148-
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.",
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.",
149150
},
150151
},
151152
SpecBlocks: map[string]schema.Block{
@@ -425,11 +426,9 @@ func saveAlertRuleSpec(ctx context.Context, src *v0alpha1.AlertRule, dst *Resour
425426
if d.HasError() {
426427
return d
427428
}
428-
// Convert to dynamic
429-
dynamicValue := types.DynamicValue(panelRef)
430-
values["panel_ref"] = dynamicValue
429+
values["panel_ref"] = panelRef
431430
} else {
432-
values["panel_ref"] = types.DynamicNull()
431+
values["panel_ref"] = types.ObjectNull(panelRefType.AttrTypes)
433432
}
434433
if len(src.Spec.Expressions) > 0 {
435434
// Convert expressions to map[string]string where each string is JSON
@@ -536,15 +535,8 @@ func parseAlertRuleTrigger(ctx context.Context, src types.Object) (v0alpha1.Aler
536535
}, diag.Diagnostics{}
537536
}
538537

539-
func parsePanelRef(ctx context.Context, src types.Dynamic) (v0alpha1.AlertRuleV0alpha1SpecPanelRef, diag.Diagnostics) {
540-
panelRefObj, ok := src.UnderlyingValue().(types.Object)
541-
if !ok {
542-
return v0alpha1.AlertRuleV0alpha1SpecPanelRef{}, diag.Diagnostics{
543-
diag.NewErrorDiagnostic("Invalid panel_ref type", "panel_ref must be an object with dashboard_uid and panel_id fields"),
544-
}
545-
}
546-
547-
attrs := panelRefObj.Attributes()
538+
func parsePanelRef(ctx context.Context, src types.Object) (v0alpha1.AlertRuleV0alpha1SpecPanelRef, diag.Diagnostics) {
539+
attrs := src.Attributes()
548540
dashboardUID, ok := attrs["dashboard_uid"].(types.String)
549541
if !ok {
550542
return v0alpha1.AlertRuleV0alpha1SpecPanelRef{}, diag.Diagnostics{

0 commit comments

Comments
 (0)