Skip to content

Commit

Permalink
update for correctly parsing alert_grouping_parameters.config in `p…
Browse files Browse the repository at this point in the history
…agerduty_service`
  • Loading branch information
imjaroiswebdev committed Sep 15, 2022
1 parent 0e93cba commit 48c6407
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 10 deletions.
25 changes: 15 additions & 10 deletions pagerduty/resource_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,13 +564,21 @@ func expandAlertGroupingParameters(v interface{}) *pagerduty.AlertGroupingParame
return nil
}
riur := pre.(map[string]interface{})
if len(riur["type"].(string)) > 0 {
gt := riur["type"].(string)
alertGroupingParameters.Type = &gt
groupingType := ""
if riur["type"].(string) != "" {
groupingType = riur["type"].(string)
alertGroupingParameters.Type = &groupingType
}

if val, ok := riur["config"]; ok {
alertGroupingParameters.Config = expandAlertGroupingConfig(val)
// For Intelligent grouping type, config is null
alertGroupingParameters.Config = nil
if groupingType == "time" {
timeConfig := riur["config"].(map[string]interface{})
to := timeConfig["timeout"].(int)
alertGroupingParameters.Config.Timeout = &to
}
if groupingType == "content_based" {
alertGroupingParameters.Config = expandAlertGroupingContentBasedConfig(riur["config"])
}
return alertGroupingParameters
}
Expand All @@ -587,13 +595,14 @@ func expandAutoPauseNotificationsParameters(v interface{}) *pagerduty.AutoPauseN
return autoPauseNotificationsParameters
}

func expandAlertGroupingConfig(v interface{}) *pagerduty.AlertGroupingConfig {
func expandAlertGroupingContentBasedConfig(v interface{}) *pagerduty.AlertGroupingConfig {
alertGroupingConfig := &pagerduty.AlertGroupingConfig{}
if len(v.([]interface{})) == 0 || v.([]interface{})[0] == nil {
return nil
}
riur := v.([]interface{})[0].(map[string]interface{})

alertGroupingConfig.Fields = []string{}
if val, ok := riur["fields"]; ok {
for _, field := range val.([]interface{}) {
alertGroupingConfig.Fields = append(alertGroupingConfig.Fields, field.(string))
Expand All @@ -603,10 +612,6 @@ func expandAlertGroupingConfig(v interface{}) *pagerduty.AlertGroupingConfig {
agg := val.(string)
alertGroupingConfig.Aggregate = &agg
}
if val, ok := riur["timeout"]; ok {
to := val.(int)
alertGroupingConfig.Timeout = &to
}
return alertGroupingConfig
}
func flattenAlertGroupingParameters(v *pagerduty.AlertGroupingParameters) interface{} {
Expand Down
133 changes: 133 additions & 0 deletions pagerduty/resource_pagerduty_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,58 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"),
),
},
{
Config: testAccCheckPagerDutyServiceConfigWithAlertIntelligentGroupingUpdated(username, email, escalationPolicy, service),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "name", service),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "description", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "auto_resolve_timeout", "1800"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "acknowledgement_timeout", "1800"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "alert_creation", "create_alerts_and_incidents"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.type", "intelligent"),
resource.TestCheckNoResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.urgency", "high"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"),
),
},
{
Config: testAccCheckPagerDutyServiceConfigWithAlertIntelligentGroupingDescriptionUpdated(username, email, escalationPolicy, service),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "name", service),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "description", "bar"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "auto_resolve_timeout", "1800"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "acknowledgement_timeout", "1800"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "alert_creation", "create_alerts_and_incidents"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.type", "intelligent"),
resource.TestCheckNoResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.config.0"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.urgency", "high"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "incident_urgency_rule.0.type", "constant"),
),
},
},
})
}
Expand Down Expand Up @@ -1002,6 +1054,87 @@ resource "pagerduty_service" "foo" {
`, username, email, escalationPolicy, service)
}

func testAccCheckPagerDutyServiceConfigWithAlertIntelligentGroupingUpdated(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 = "foo"
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"
config {
fields = null
timeout = 0
}
}
}
`, username, email, escalationPolicy, service)
}

func testAccCheckPagerDutyServiceConfigWithAlertIntelligentGroupingDescriptionUpdated(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"
config {}
}
}
`, username, email, escalationPolicy, service)
}

func testAccCheckPagerDutyServiceConfigWithAutoPauseNotificationsParameters(username, email, escalationPolicy, service string) string {
return fmt.Sprintf(`
resource "pagerduty_user" "foo" {
Expand Down

0 comments on commit 48c6407

Please sign in to comment.