Skip to content

Commit

Permalink
Merge pull request #779 from imjaroiswebdev/hotfix/alert-grouping-dif…
Browse files Browse the repository at this point in the history
…f-val-broken

[CSGI-2303] Hotfix - Alert grouping parameters input validation broken
  • Loading branch information
imjaroiswebdev authored Dec 4, 2023
2 parents a236322 + 58f01ab commit 3462f33
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 10 deletions.
17 changes: 9 additions & 8 deletions pagerduty/resource_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,18 +341,19 @@ func customizePagerDutyServiceDiff(context context.Context, diff *schema.Resourc
aggregateVal := diff.Get(agppath + "aggregate").(string)
fieldsVal := diff.Get(agppath + "fields").([]interface{})
timeWindowVal := diff.Get(agppath + "time_window").(int)
hasChangeAgpType := diff.HasChange("alert_grouping_parameters")

if agpType == "time" && (aggregateVal != "" || len(fieldsVal) > 0 || timeWindowVal > 0) {
return fmt.Errorf("Alert grouping parameters configuration of type \"time\" only supports setting \"timeout\" attribute")
}
if agpType == "content_based" && (timeoutVal > 0 || timeWindowVal > 0) {
return fmt.Errorf("Alert grouping parameters configuration of type \"content_based\" only supports setting \"aggregate\" and \"fields\" attributes")
}
if agpType == "content_based" && (aggregateVal == "" || len(fieldsVal) == 0) {
return fmt.Errorf("When using Alert grouping parameters configuration of type \"content_based\" is in use, attributes \"aggregate\" and \"fields\" are required")
}
if agpType == "intelligent" && (aggregateVal != "" || len(fieldsVal) > 0 || timeoutVal > 0) {
return fmt.Errorf("Alert grouping parameters configuration of type \"intelligent\" only supports setting the optional attribute \"time_window\"")
if (aggregateVal != "" || len(fieldsVal) > 0) && (agpType != "" && hasChangeAgpType && agpType != "content_based") {
return fmt.Errorf("Alert grouping parameters configuration attributes \"aggregate\" and \"fields\" are only supported by \"content_based\" type Alert Grouping")
}
if timeoutVal > 0 && (agpType != "" && hasChangeAgpType && agpType != "time") {
return fmt.Errorf("Alert grouping parameters configuration attribute \"timeout\" is only supported by \"time\" type Alert Grouping")
}
if (timeWindowVal > 300) && (agpType != "" && hasChangeAgpType && agpType != "intelligent") {
return fmt.Errorf("Alert grouping parameters configuration attribute \"time_window\" is only supported by \"intelligent\" type Alert Grouping")
}
}

Expand Down
189 changes: 187 additions & 2 deletions pagerduty/resource_pagerduty_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ func TestAccPagerDutyService_Basic(t *testing.T) {
}

func TestAccPagerDutyService_FormatValidation(t *testing.T) {
service := fmt.Sprintf("ts-%s", acctest.RandString(5))
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
email := fmt.Sprintf("%[email protected]", username)
escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5))
Expand Down Expand Up @@ -171,6 +172,126 @@ func TestAccPagerDutyService_FormatValidation(t *testing.T) {
PlanOnly: true,
ExpectError: regexp.MustCompile(errMessageMatcher),
},
// Alert grouping parameters "Content Based" type input validation
{
Config: testAccCheckPagerDutyServiceAlertGroupingInputValidationConfig(username, email, escalationPolicy, service,
`
alert_grouping_parameters {
type = "content_based"
config {}
}
`,
),
PlanOnly: true,
ExpectError: regexp.MustCompile("When using Alert grouping parameters configuration of type \"content_based\" is in use, attributes \"aggregate\" and \"fields\" are required"),
},
{
Config: testAccCheckPagerDutyServiceAlertGroupingInputValidationConfig(username, email, escalationPolicy, service,
`
alert_grouping_parameters {
type = "content_based"
config {
aggregate = "all"
fields = ["custom_details.source_id"]
}
}
`,
),
},
{
Config: testAccCheckPagerDutyServiceAlertGroupingInputValidationConfig(username, email, escalationPolicy, service,
`
alert_grouping_parameters {
type = "time"
config {
aggregate = "all"
fields = ["custom_details.source_id"]
}
}
`,
),
PlanOnly: true,
ExpectError: regexp.MustCompile("Alert grouping parameters configuration attributes \"aggregate\" and \"fields\" are only supported by \"content_based\" type Alert Grouping"),
},
// Alert grouping parameters "time" type input validation
{
Config: testAccCheckPagerDutyServiceAlertGroupingInputValidationConfig(username, email, escalationPolicy, service,
`
alert_grouping_parameters {
type = "time"
config {
timeout = 5
}
}
`,
),
},
{
Config: testAccCheckPagerDutyServiceAlertGroupingInputValidationConfig(username, email, escalationPolicy, service,
`
alert_grouping_parameters {
type = "intelligent"
config {
timeout = 5
}
}
`,
),
PlanOnly: true,
ExpectError: regexp.MustCompile("Alert grouping parameters configuration attribute \"timeout\" is only supported by \"time\" type Alert Grouping"),
},
// Alert grouping parameters "intelligent" type input validation
{
Config: testAccCheckPagerDutyServiceAlertGroupingInputValidationConfig(username, email, escalationPolicy, service,
`
alert_grouping_parameters {
type = "time"
config {
time_window = 600
}
}
`,
),
PlanOnly: true,
ExpectError: regexp.MustCompile("Alert grouping parameters configuration attribute \"time_window\" is only supported by \"intelligent\" type Alert Grouping"),
},
{
Config: testAccCheckPagerDutyServiceAlertGroupingInputValidationConfig(username, email, escalationPolicy, service,
`
alert_grouping_parameters {
type = "intelligent"
config {}
}
`,
),
},
{
Config: testAccCheckPagerDutyServiceAlertGroupingInputValidationConfig(username, email, escalationPolicy, service,
`
alert_grouping_parameters {
type = "intelligent"
config {
time_window = 5
}
}
`,
),
PlanOnly: true,
ExpectError: regexp.MustCompile("Intelligent alert grouping time window value must be between 300 and 3600"),
},
{
Config: testAccCheckPagerDutyServiceAlertGroupingInputValidationConfig(username, email, escalationPolicy, service,
`
alert_grouping_parameters {
type = "intelligent"
config {
time_window = 300
}
}
`,
),
PlanOnly: true,
},
},
})
}
Expand Down Expand Up @@ -284,7 +405,11 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
),
},
{
Config: testAccCheckPagerDutyServiceConfigWithAlertContentGroupingUpdated(username, email, escalationPolicy, service),
Config: testAccCheckPagerDutyServiceConfigWithAlertContentGrouping(username, email, escalationPolicy, service),
PlanOnly: true,
},
{
Config: testAccCheckPagerDutyServiceConfigWithAlertIntelligentGroupingUpdated(username, email, escalationPolicy, service),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
resource.TestCheckResourceAttr(
Expand All @@ -298,7 +423,31 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "alert_creation", "create_alerts_and_incidents"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "alert_grouping", "rules"),
"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: testAccCheckPagerDutyServiceConfigWithAlertContentGroupingUpdated(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.TestCheckNoResourceAttr(
"pagerduty_service.foo", "alert_grouping_parameters.0.config"),
resource.TestCheckNoResourceAttr(
Expand Down Expand Up @@ -1136,6 +1285,42 @@ resource "pagerduty_service" "foo" {
`, username, email, escalationPolicy, service)
}

func testAccCheckPagerDutyServiceAlertGroupingInputValidationConfig(username, email, escalationPolicy, service, alertGroupingParams 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"
%s
}
`, username, email, escalationPolicy, service, alertGroupingParams)
}

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

0 comments on commit 3462f33

Please sign in to comment.