From 0000a892bdf037232d4bd2f11c097e7b3ba8f7f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Fri, 17 Nov 2023 19:39:31 -0300 Subject: [PATCH 1/2] suppress diff when serv alert grouping config is omitted --- pagerduty/resource_pagerduty_service.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pagerduty/resource_pagerduty_service.go b/pagerduty/resource_pagerduty_service.go index 96ca3f680..6003977fa 100644 --- a/pagerduty/resource_pagerduty_service.go +++ b/pagerduty/resource_pagerduty_service.go @@ -28,6 +28,21 @@ func resourcePagerDutyService() *schema.Resource { return fmt.Errorf("general urgency cannot be set for a use_support_hours incident urgency rule type") } } + + // Due to alert_grouping_parameters.type = null is a valid configuration + // for disabling Service's Alert Grouping configuration and having an + // empty alert_grouping_parameters.config block is also valid, API ignore + // this input fields, and turns out that API response for Service + // configuration doesn't bring a representation of this HCL, which leads + // to a permadiff, described in + // https://github.com/PagerDuty/terraform-provider-pagerduty/issues/700 + // + // So, bellow is the formated representation alert_grouping_parameters + // value when this permadiff appears and must be ignored. + ignoreThisAlertGroupingParamsConfigDiff := `[]interface {}{map[string]interface {}{"config":[]interface {}{interface {}(nil)}, "type":""}}` + if agpdiff, ok := diff.Get("alert_grouping_parameters").([]interface{}); ok && diff.NewValueKnown("alert_grouping_parameters") && fmt.Sprintf("%#v", agpdiff) == ignoreThisAlertGroupingParamsConfigDiff { + diff.Clear("alert_grouping_parameters") + } return nil }, Importer: &schema.ResourceImporter{ @@ -96,6 +111,7 @@ func resourcePagerDutyService() *schema.Resource { "config": { Type: schema.TypeList, Optional: true, + Computed: true, MaxItems: 1, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ @@ -584,6 +600,7 @@ func expandAlertGroupingParameters(v interface{}) *pagerduty.AlertGroupingParame // For Intelligent grouping type, config is null alertGroupingParameters.Config = nil + log.Printf("[MYDEBUG] config: %#v", alertGroupingParameters.Config) if groupingType == "content_based" || groupingType == "time" { alertGroupingParameters.Config = expandAlertGroupingConfig(riur["config"], groupingType) } From 63b66cd8a837d4dac7b35c5b1628061aff8d6574 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Reyes?= Date: Fri, 17 Nov 2023 19:41:21 -0300 Subject: [PATCH 2/2] add test cases for covering regressions --- pagerduty/resource_pagerduty_service_test.go | 101 +++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/pagerduty/resource_pagerduty_service_test.go b/pagerduty/resource_pagerduty_service_test.go index 897c7ac77..a0998b759 100644 --- a/pagerduty/resource_pagerduty_service_test.go +++ b/pagerduty/resource_pagerduty_service_test.go @@ -419,6 +419,30 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) { "pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"), ), }, + { + Config: testAccCheckPagerDutyServiceConfigWithAlertIntelligentGroupingOmittingConfig(username, email, escalationPolicy, service), + Check: resource.ComposeTestCheckFunc( + testAccCheckPagerDutyServiceExists("pagerduty_service.foo"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "name", service), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "alert_grouping_parameters.0.type", "intelligent"), + resource.TestCheckNoResourceAttr( + "pagerduty_service.foo", "alert_grouping_parameters.0.config.0"), + ), + }, + { + Config: testAccCheckPagerDutyServiceConfigWithAlertIntelligentGroupingTypeNullEmptyConfigConfig(username, email, escalationPolicy, service), + Check: resource.ComposeTestCheckFunc( + testAccCheckPagerDutyServiceExists("pagerduty_service.foo"), + resource.TestCheckResourceAttr( + "pagerduty_service.foo", "name", service), + resource.TestCheckNoResourceAttr( + "pagerduty_service.foo", "alert_grouping_parameters.0.type"), + resource.TestCheckNoResourceAttr( + "pagerduty_service.foo", "alert_grouping_parameters.0.config.0"), + ), + }, }, }) } @@ -1384,6 +1408,83 @@ resource "pagerduty_service" "foo" { `, username, email, escalationPolicy, service) } +func testAccCheckPagerDutyServiceConfigWithAlertIntelligentGroupingOmittingConfig(username, email, escalationPolicy, service string) string { + return fmt.Sprintf(` +resource "pagerduty_user" "foo" { + name = "%s" + email = "%s" + color = "green" + role = "user" + job_title = "foo" + description = "foo" +} + +resource "pagerduty_escalation_policy" "foo" { + name = "%s" + description = "bar" + num_loops = 2 + rule { + escalation_delay_in_minutes = 10 + target { + type = "user_reference" + id = pagerduty_user.foo.id + } + } +} + +resource "pagerduty_service" "foo" { + name = "%s" + description = "bar" + auto_resolve_timeout = 1800 + acknowledgement_timeout = 1800 + escalation_policy = pagerduty_escalation_policy.foo.id + alert_creation = "create_alerts_and_incidents" + alert_grouping_parameters { + type = "intelligent" + } +} +`, username, email, escalationPolicy, service) +} + +func testAccCheckPagerDutyServiceConfigWithAlertIntelligentGroupingTypeNullEmptyConfigConfig(username, email, escalationPolicy, service string) string { + return fmt.Sprintf(` +resource "pagerduty_user" "foo" { + name = "%s" + email = "%s" + color = "green" + role = "user" + job_title = "foo" + description = "foo" +} + +resource "pagerduty_escalation_policy" "foo" { + name = "%s" + description = "bar" + num_loops = 2 + rule { + escalation_delay_in_minutes = 10 + target { + type = "user_reference" + id = pagerduty_user.foo.id + } + } +} + +resource "pagerduty_service" "foo" { + name = "%s" + description = "bar" + auto_resolve_timeout = 1800 + acknowledgement_timeout = 1800 + escalation_policy = pagerduty_escalation_policy.foo.id + alert_creation = "create_alerts_and_incidents" + alert_grouping_parameters { + type = null + config {} + } +} +`, username, email, escalationPolicy, service) +} + func testAccCheckPagerDutyServiceConfigWithAutoPauseNotificationsParameters(username, email, escalationPolicy, service string) string { return fmt.Sprintf(` resource "pagerduty_user" "foo" {