From 09a26396acffd938d095100372dbc8f595dcd623 Mon Sep 17 00:00:00 2001 From: Aashirwad Jain Date: Wed, 8 Jan 2025 16:23:53 +0530 Subject: [PATCH] feat(alert_muting_rule): Add `action_on_muting_rule_window_ended` attribute in `newrelic_alert_muting_rule` Terraform Resource (#2783) Co-authored-by: vinay-newrelic --- go.mod | 2 + go.sum | 4 +- .../resource_newrelic_alert_muting_rule.go | 14 ++++ ...esource_newrelic_alert_muting_rule_test.go | 64 +++++++++++++++++++ .../structures_newrelic_alert_muting_rule.go | 10 +++ .../docs/r/alert_muting_rule.html.markdown | 3 +- 6 files changed, 94 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 95d8a0335..0768ba3fa 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,8 @@ go 1.22 toolchain go1.22.6 +replace github.com/newrelic/newrelic-client-go/v2 => github.com/newrelic/newrelic-client-go/v2 v2.51.4-0.20241217120708-22dfc1bcd16c + require ( github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1 github.com/mitchellh/go-homedir v1.1.0 diff --git a/go.sum b/go.sum index e5471e668..f490c8ace 100644 --- a/go.sum +++ b/go.sum @@ -270,8 +270,8 @@ github.com/newrelic/go-agent/v3 v3.30.0 h1:ZXHCT/Cot4iIPwcegCZURuRQOsfmGA6wilW+S github.com/newrelic/go-agent/v3 v3.30.0/go.mod h1:9utrgxlSryNqRrTvII2XBL+0lpofXbqXApvVWPpbzUg= github.com/newrelic/go-insights v1.0.3 h1:zSNp1CEZnXktzSIEsbHJk8v6ZihdPFP2WsO/fzau3OQ= github.com/newrelic/go-insights v1.0.3/go.mod h1:A20BoT8TNkqPGX2nS/Z2fYmKl3Cqa3iKZd4whzedCY4= -github.com/newrelic/newrelic-client-go/v2 v2.51.3 h1:Bu/cUs6nfMjQMPBcxxHt4Xm30tKDT7ttYy/XRDsWP6Y= -github.com/newrelic/newrelic-client-go/v2 v2.51.3/go.mod h1:+RRjI3nDGWT3kLm9Oi3QxpBm70uu8q1upEHBVWCZFpo= +github.com/newrelic/newrelic-client-go/v2 v2.51.4-0.20241217120708-22dfc1bcd16c h1:U4xKoQkH6OJQqAXxD0eLHO6rzVcIS19fSIsI0DSdzXE= +github.com/newrelic/newrelic-client-go/v2 v2.51.4-0.20241217120708-22dfc1bcd16c/go.mod h1:+RRjI3nDGWT3kLm9Oi3QxpBm70uu8q1upEHBVWCZFpo= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= diff --git a/newrelic/resource_newrelic_alert_muting_rule.go b/newrelic/resource_newrelic_alert_muting_rule.go index 3521e3769..994e2d08b 100644 --- a/newrelic/resource_newrelic_alert_muting_rule.go +++ b/newrelic/resource_newrelic_alert_muting_rule.go @@ -3,6 +3,7 @@ package newrelic import ( "context" "fmt" + "github.com/newrelic/newrelic-client-go/v2/pkg/alerts" "log" "regexp" "time" @@ -183,6 +184,19 @@ func resourceNewRelicAlertMutingRule() *schema.Resource { Elem: scheduleSchema(), Description: "The time window when the MutingRule should actively mute incidents.", }, + "action_on_muting_rule_window_ended": { + Type: schema.TypeString, + Optional: true, + Computed: true, + Description: "The action when the muting rule window is ended or disabled.", + ValidateFunc: validation.StringInSlice( + []string{ + string(alerts.AlertsActionOnMutingRuleWindowEndedTypes.CLOSE_ISSUES_ON_INACTIVE), + string(alerts.AlertsActionOnMutingRuleWindowEndedTypes.DO_NOTHING), + }, + false, + ), + }, }, } } diff --git a/newrelic/resource_newrelic_alert_muting_rule_test.go b/newrelic/resource_newrelic_alert_muting_rule_test.go index f42946c26..95b8ff55b 100644 --- a/newrelic/resource_newrelic_alert_muting_rule_test.go +++ b/newrelic/resource_newrelic_alert_muting_rule_test.go @@ -99,6 +99,38 @@ func TestAccNewRelicAlertMutingRule_Basic(t *testing.T) { }) } +func TestAccNewRelicAlertMutingRule_EndBehaviourInput(t *testing.T) { + resourceName := "newrelic_alert_muting_rule.foo" + rName := acctest.RandString(5) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckNewRelicAlertMutingRuleDestroy, + Steps: []resource.TestStep{ + // Test: Create + { + Config: testAccNewRelicAlertMutingRuleEndBehaviourInput(rName, "new muting rule", "product", "EQUALS", "APM", "CLOSE_ISSUES_ON_INACTIVE"), + Check: resource.ComposeTestCheckFunc( + testAccCheckNewRelicAlertMutingRuleExists(resourceName), + ), + }, + // Test: Update + { + Config: testAccNewRelicAlertMutingRuleEndBehaviourInput(rName, "second muting rule", "conditionType", "NOT_EQUALS", "baseline", "CLOSE_ISSUES_ON_INACTIVE"), + Check: resource.ComposeTestCheckFunc( + testAccCheckNewRelicAlertMutingRuleExists(resourceName), + ), + }, + // // Test: Import + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true}, + }, + }) +} + func TestAccNewRelicAlertMutingRule_BadInput(t *testing.T) { rName := acctest.RandString(5) @@ -239,6 +271,38 @@ resource "newrelic_alert_muting_rule" "foo" { `, name, description, attribute, operator, values) } +func testAccNewRelicAlertMutingRuleEndBehaviourInput( + name string, + description string, + attribute string, + operator string, + values string, + actionOnMutingRuleWindowEnded string, +) string { + return fmt.Sprintf(` + +resource "newrelic_alert_muting_rule" "foo" { + name = "tf-test-%[1]s" + enabled = true + description = "%[2]s" + condition { + conditions { + attribute = "%[3]s" + operator = "EQUALS" + values = ["%[5]s"] + } + conditions { + attribute = "conditionType" + operator = "%[4]s" + values = ["static"] + } + operator = "AND" + } + action_on_muting_rule_window_ended = "%[6]s" +} +`, name, description, attribute, operator, values, actionOnMutingRuleWindowEnded) +} + func testAccNewRelicAlertMutingRuleBadInput( name string, description string, diff --git a/newrelic/structures_newrelic_alert_muting_rule.go b/newrelic/structures_newrelic_alert_muting_rule.go index b4d80f1ed..29681cb72 100644 --- a/newrelic/structures_newrelic_alert_muting_rule.go +++ b/newrelic/structures_newrelic_alert_muting_rule.go @@ -18,6 +18,10 @@ func expandMutingRuleCreateInput(d *schema.ResourceData) (alerts.MutingRuleCreat Description: d.Get("description").(string), } + if actionOnMutingRuleWindowEnded, ok := d.GetOk("action_on_muting_rule_window_ended"); ok { + createInput.ActionOnMutingRuleWindowEnded = alerts.AlertsActionOnMutingRuleWindowEnded(actionOnMutingRuleWindowEnded.(string)) + } + if e, ok := d.GetOk("condition"); ok { createInput.Condition = expandMutingRuleConditionGroup(e.([]interface{})[0].(map[string]interface{})) } @@ -199,6 +203,10 @@ func expandMutingRuleUpdateInput(d *schema.ResourceData) (alerts.MutingRuleUpdat Description: d.Get("description").(string), } + if actionOnMutingRuleWindowEnded, ok := d.GetOk("action_on_muting_rule_window_ended"); ok { + updateInput.ActionOnMutingRuleWindowEnded = alerts.AlertsActionOnMutingRuleWindowEnded(actionOnMutingRuleWindowEnded.(string)) + } + if e, ok := d.GetOk("condition"); ok { x := expandMutingRuleConditionGroup(e.([]interface{})[0].(map[string]interface{})) @@ -270,6 +278,8 @@ func flattenMutingRule(mutingRule *alerts.MutingRule, d *schema.ResourceData) er configuredCondition := x.([]interface{}) _ = d.Set("enabled", mutingRule.Enabled) + _ = d.Set("action_on_muting_rule_window_ended", mutingRule.ActionOnMutingRuleWindowEnded) + err := d.Set("condition", flattenMutingRuleConditionGroup(mutingRule.Condition, configuredCondition)) if err != nil { return nil diff --git a/website/docs/r/alert_muting_rule.html.markdown b/website/docs/r/alert_muting_rule.html.markdown index dc9eabd47..5e931083c 100644 --- a/website/docs/r/alert_muting_rule.html.markdown +++ b/website/docs/r/alert_muting_rule.html.markdown @@ -42,6 +42,7 @@ resource "newrelic_alert_muting_rule" "foo" { weekly_repeat_days = ["MONDAY", "WEDNESDAY", "FRIDAY"] repeat_count = 42 } + action_on_muting_rule_window_ended = "CLOSE_ISSUES_ON_INACTIVE" } ``` @@ -54,7 +55,7 @@ The following arguments are supported: * `name` - The name of the MutingRule. * `description` - The description of the MutingRule. * `schedule` - (Optional) Specify a schedule for enabling the MutingRule. See [Schedule](#schedule) below for details - + * `action_on_muting_rule_window_ended` - (Optional) The action when the muting rule window is ended or disabled. Valid values are `CLOSE_ISSUES_ON_INACTIVE`, `DO_NOTHING` ### Nested `condition` blocks