Skip to content

Commit 3d031bd

Browse files
committed
Fix inability to update managed ML alerts
The ID field was not being populated by the plan so we were attempting to create a new alert instead of updating the existing alert. The ID is now pulled from the existing state and test coverage for the update path was added.
1 parent a8bb914 commit 3d031bd

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

internal/resources/machinelearning/resource_alert.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ func (r *alertResource) Update(ctx context.Context, req resource.UpdateRequest,
277277
if resp.Diagnostics.HasError() {
278278
return
279279
}
280+
// It is required to fetch the ID from the state data. It is not present in the plan data.
281+
var stateData resourceAlertModel
282+
resp.Diagnostics.Append(req.State.Get(ctx, &stateData)...)
283+
if resp.Diagnostics.HasError() {
284+
return
285+
}
286+
data.ID = stateData.ID
280287

281288
alert, err := alertFromModel(data)
282289
if err != nil {
@@ -341,9 +348,9 @@ func (r *alertResource) read(ctx context.Context, model resourceAlertModel) (*re
341348
err error
342349
)
343350
if model.JobID.ValueString() != "" {
344-
alert, err = r.mlapi.JobAlert(ctx, model.JobID.ValueString(), model.ID.String())
351+
alert, err = r.mlapi.JobAlert(ctx, model.JobID.ValueString(), model.ID.ValueString())
345352
} else {
346-
alert, err = r.mlapi.OutlierAlert(ctx, model.OutlierID.ValueString(), model.ID.String())
353+
alert, err = r.mlapi.OutlierAlert(ctx, model.OutlierID.ValueString(), model.ID.ValueString())
347354
}
348355
if err != nil {
349356
return nil, diag.Diagnostics{diag.NewErrorDiagnostic("Unable to read resource", err.Error())}

internal/resources/machinelearning/resource_alert_test.go

+41-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestAccResourceJobAlert(t *testing.T) {
1818
testutils.CheckCloudInstanceTestsEnabled(t)
1919

2020
randomJobName := acctest.RandomWithPrefix("Test Job")
21-
randomAlertName := acctest.RandomWithPrefix("Test Job Alert")
21+
randomAlertName := acctest.RandomWithPrefix("Test Alert")
2222

2323
var job mlapi.Job
2424
var alert mlapi.Alert
@@ -37,6 +37,28 @@ func TestAccResourceJobAlert(t *testing.T) {
3737
Check: resource.ComposeTestCheckFunc(
3838
testAccMLJobCheckExists("grafana_machine_learning_job.test_alert_job", &job),
3939
testAccMLJobAlertCheckExists("grafana_machine_learning_alert.test_job_alert", &job, &alert),
40+
resource.TestCheckResourceAttrSet("grafana_machine_learning_alert.test_job_alert", "id"),
41+
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "title", randomAlertName),
42+
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "anomaly_condition", "any"),
43+
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "threshold", ">0.8"),
44+
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "window", "15m"),
45+
),
46+
},
47+
// Update the alert with a new anomaly conditon.
48+
{
49+
Config: testutils.TestAccExampleWithReplace(t, "resources/grafana_machine_learning_alert/resource.tf", map[string]string{
50+
"Test Job": randomJobName,
51+
"Test Alert": randomAlertName,
52+
"\"any\"": "\"low\"",
53+
}),
54+
Check: resource.ComposeTestCheckFunc(
55+
testAccMLJobCheckExists("grafana_machine_learning_job.test_alert_job", &job),
56+
testAccMLJobAlertCheckExists("grafana_machine_learning_alert.test_job_alert", &job, &alert),
57+
resource.TestCheckResourceAttrSet("grafana_machine_learning_alert.test_job_alert", "id"),
58+
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "title", randomAlertName),
59+
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "anomaly_condition", "low"),
60+
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "threshold", ">0.8"),
61+
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "window", "15m"),
4062
),
4163
},
4264
{
@@ -54,8 +76,8 @@ func TestAccResourceJobAlert(t *testing.T) {
5476
func TestAccResourceOutlierAlert(t *testing.T) {
5577
testutils.CheckCloudInstanceTestsEnabled(t)
5678

57-
randomOutlierName := acctest.RandomWithPrefix("Test Job")
58-
randomAlertName := acctest.RandomWithPrefix("Test Outlier Alert")
79+
randomOutlierName := acctest.RandomWithPrefix("Test Outlier")
80+
randomAlertName := acctest.RandomWithPrefix("Test Alert")
5981

6082
var outlier mlapi.OutlierDetector
6183
var alert mlapi.Alert
@@ -74,6 +96,22 @@ func TestAccResourceOutlierAlert(t *testing.T) {
7496
Check: resource.ComposeTestCheckFunc(
7597
testAccMLOutlierCheckExists("grafana_machine_learning_outlier_detector.test_alert_outlier_detector", &outlier),
7698
testAccMLOutlierAlertCheckExists("grafana_machine_learning_alert.test_outlier_alert", &outlier, &alert),
99+
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_outlier_alert", "title", randomAlertName),
100+
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_outlier_alert", "window", "1h"),
101+
),
102+
},
103+
// Test updating the window.
104+
{
105+
Config: testutils.TestAccExampleWithReplace(t, "resources/grafana_machine_learning_alert/outlier_alert.tf", map[string]string{
106+
"Test Outlier": randomOutlierName,
107+
"Test Alert": randomAlertName,
108+
"\"1h\"": "\"30m\"",
109+
}),
110+
Check: resource.ComposeTestCheckFunc(
111+
testAccMLOutlierCheckExists("grafana_machine_learning_outlier_detector.test_alert_outlier_detector", &outlier),
112+
testAccMLOutlierAlertCheckExists("grafana_machine_learning_alert.test_outlier_alert", &outlier, &alert),
113+
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_outlier_alert", "title", randomAlertName),
114+
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_outlier_alert", "window", "30m"),
77115
),
78116
},
79117
{

0 commit comments

Comments
 (0)