Skip to content

Fix inability to update managed ML alerts #1684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions internal/resources/machinelearning/resource_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ func (r *alertResource) Schema(ctx context.Context, req resource.SchemaRequest,
"id": schema.StringAttribute{
Description: "The ID of the alert.",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"title": schema.StringAttribute{
Description: "The title of the alert.",
Expand Down Expand Up @@ -341,9 +344,9 @@ func (r *alertResource) read(ctx context.Context, model resourceAlertModel) (*re
err error
)
if model.JobID.ValueString() != "" {
alert, err = r.mlapi.JobAlert(ctx, model.JobID.ValueString(), model.ID.String())
alert, err = r.mlapi.JobAlert(ctx, model.JobID.ValueString(), model.ID.ValueString())
} else {
alert, err = r.mlapi.OutlierAlert(ctx, model.OutlierID.ValueString(), model.ID.String())
alert, err = r.mlapi.OutlierAlert(ctx, model.OutlierID.ValueString(), model.ID.ValueString())
}
if err != nil {
return nil, diag.Diagnostics{diag.NewErrorDiagnostic("Unable to read resource", err.Error())}
Expand Down
44 changes: 41 additions & 3 deletions internal/resources/machinelearning/resource_alert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestAccResourceJobAlert(t *testing.T) {
testutils.CheckCloudInstanceTestsEnabled(t)

randomJobName := acctest.RandomWithPrefix("Test Job")
randomAlertName := acctest.RandomWithPrefix("Test Job Alert")
randomAlertName := acctest.RandomWithPrefix("Test Alert")

var job mlapi.Job
var alert mlapi.Alert
Expand All @@ -37,6 +37,28 @@ func TestAccResourceJobAlert(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccMLJobCheckExists("grafana_machine_learning_job.test_alert_job", &job),
testAccMLJobAlertCheckExists("grafana_machine_learning_alert.test_job_alert", &job, &alert),
resource.TestCheckResourceAttrSet("grafana_machine_learning_alert.test_job_alert", "id"),
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "title", randomAlertName),
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "anomaly_condition", "any"),
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "threshold", ">0.8"),
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "window", "15m"),
),
},
// Update the alert with a new anomaly condition.
{
Config: testutils.TestAccExampleWithReplace(t, "resources/grafana_machine_learning_alert/resource.tf", map[string]string{
"Test Job": randomJobName,
"Test Alert": randomAlertName,
"\"any\"": "\"low\"",
}),
Check: resource.ComposeTestCheckFunc(
testAccMLJobCheckExists("grafana_machine_learning_job.test_alert_job", &job),
testAccMLJobAlertCheckExists("grafana_machine_learning_alert.test_job_alert", &job, &alert),
resource.TestCheckResourceAttrSet("grafana_machine_learning_alert.test_job_alert", "id"),
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "title", randomAlertName),
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "anomaly_condition", "low"),
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "threshold", ">0.8"),
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_job_alert", "window", "15m"),
),
},
{
Expand All @@ -54,8 +76,8 @@ func TestAccResourceJobAlert(t *testing.T) {
func TestAccResourceOutlierAlert(t *testing.T) {
testutils.CheckCloudInstanceTestsEnabled(t)

randomOutlierName := acctest.RandomWithPrefix("Test Job")
randomAlertName := acctest.RandomWithPrefix("Test Outlier Alert")
randomOutlierName := acctest.RandomWithPrefix("Test Outlier")
randomAlertName := acctest.RandomWithPrefix("Test Alert")

var outlier mlapi.OutlierDetector
var alert mlapi.Alert
Expand All @@ -74,6 +96,22 @@ func TestAccResourceOutlierAlert(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccMLOutlierCheckExists("grafana_machine_learning_outlier_detector.test_alert_outlier_detector", &outlier),
testAccMLOutlierAlertCheckExists("grafana_machine_learning_alert.test_outlier_alert", &outlier, &alert),
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_outlier_alert", "title", randomAlertName),
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_outlier_alert", "window", "1h"),
),
},
// Test updating the window.
{
Config: testutils.TestAccExampleWithReplace(t, "resources/grafana_machine_learning_alert/outlier_alert.tf", map[string]string{
"Test Outlier": randomOutlierName,
"Test Alert": randomAlertName,
"\"1h\"": "\"30m\"",
}),
Check: resource.ComposeTestCheckFunc(
testAccMLOutlierCheckExists("grafana_machine_learning_outlier_detector.test_alert_outlier_detector", &outlier),
testAccMLOutlierAlertCheckExists("grafana_machine_learning_alert.test_outlier_alert", &outlier, &alert),
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_outlier_alert", "title", randomAlertName),
resource.TestCheckResourceAttr("grafana_machine_learning_alert.test_outlier_alert", "window", "30m"),
),
},
{
Expand Down
Loading