Skip to content
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

Add support for service level auto_pause_notifications_parameters #525

Merged
merged 3 commits into from
Aug 9, 2022
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
54 changes: 54 additions & 0 deletions pagerduty/resource_pagerduty_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ func resourcePagerDutyService() *schema.Resource {
},
},
},
"auto_pause_notifications_parameters": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.IntInSlice([]int{120, 180, 300, 600, 900})),
},
},
},
},
"auto_resolve_timeout": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -340,6 +361,9 @@ func buildServiceStruct(d *schema.ResourceData) (*pagerduty.Service, error) {
}
}
}
if attr, ok := d.GetOk("auto_pause_notifications_parameters"); ok {
service.AutoPauseNotificationsParameters = expandAutoPauseNotificationsParameters(attr)
}

if attr, ok := d.GetOk("escalation_policy"); ok {
service.EscalationPolicy = &pagerduty.EscalationPolicyReference{
Expand Down Expand Up @@ -500,6 +524,12 @@ func flattenService(d *schema.ResourceData, service *pagerduty.Service) error {
return err
}
}
if service.AutoPauseNotificationsParameters != nil {
if err := d.Set("auto_pause_notifications_parameters", flattenAutoPauseNotificationsParameters(service.AutoPauseNotificationsParameters)); err != nil {
return err
}
}

if service.IncidentUrgencyRule != nil {
if err := d.Set("incident_urgency_rule", flattenIncidentUrgencyRule(service.IncidentUrgencyRule)); err != nil {
return err
Expand Down Expand Up @@ -545,6 +575,18 @@ func expandAlertGroupingParameters(v interface{}) *pagerduty.AlertGroupingParame
return alertGroupingParameters
}

func expandAutoPauseNotificationsParameters(v interface{}) *pagerduty.AutoPauseNotificationsParameters {
riur := v.([]interface{})[0].(map[string]interface{})
autoPauseNotificationsParameters := &pagerduty.AutoPauseNotificationsParameters{
Enabled: riur["enabled"].(bool),
}
if autoPauseNotificationsParameters.Enabled {
timeout := riur["timeout"].(int)
autoPauseNotificationsParameters.Timeout = &timeout
}
return autoPauseNotificationsParameters
}

func expandAlertGroupingConfig(v interface{}) *pagerduty.AlertGroupingConfig {
alertGroupingConfig := &pagerduty.AlertGroupingConfig{}
if len(v.([]interface{})) == 0 || v.([]interface{})[0] == nil {
Expand Down Expand Up @@ -596,6 +638,18 @@ func flattenAlertGroupingConfig(v *pagerduty.AlertGroupingConfig) interface{} {

return []interface{}{alertGroupingConfig}
}

func flattenAutoPauseNotificationsParameters(v *pagerduty.AutoPauseNotificationsParameters) []interface{} {
autoPauseNotificationsParameters := map[string]interface{}{
"enabled": v.Enabled,
}
if v.Enabled {
autoPauseNotificationsParameters["timeout"] = v.Timeout
}

return []interface{}{autoPauseNotificationsParameters}
}

func expandIncidentUrgencyRule(v interface{}) *pagerduty.IncidentUrgencyRule {
riur := v.([]interface{})[0].(map[string]interface{})
incidentUrgencyRule := &pagerduty.IncidentUrgencyRule{
Expand Down
195 changes: 195 additions & 0 deletions pagerduty/resource_pagerduty_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,87 @@ func TestAccPagerDutyService_AlertContentGrouping(t *testing.T) {
})
}

func TestAccPagerDutyService_AutoPauseNotificationsParameters(t *testing.T) {
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
email := fmt.Sprintf("%s@foo.test", username)
escalationPolicy := fmt.Sprintf("tf-%s", acctest.RandString(5))
service := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyServiceDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyServiceConfigWithAutoPauseNotificationsParameters(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", "auto_pause_notifications_parameters.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "auto_pause_notifications_parameters.0.enabled", "true"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "auto_pause_notifications_parameters.0.timeout", "300"),
),
},
{
Config: testAccCheckPagerDutyServiceConfigWithAutoPauseNotificationsParametersUpdated(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", "auto_pause_notifications_parameters.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "auto_pause_notifications_parameters.0.enabled", "false"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "auto_pause_notifications_parameters.0.timeout", "0"),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We get 0 instead of null because that's the default for TypeInt, see hashicorp/terraform-plugin-sdk#90.

Switching to https://github.com/hashicorp/terraform-plugin-framework in the future may help here.

),
},
{
Config: testAccCheckPagerDutyServiceConfigWithAutoPauseNotificationsParametersRemoved(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", "auto_pause_notifications_parameters.#", "1"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "auto_pause_notifications_parameters.0.enabled", "false"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "auto_pause_notifications_parameters.0.timeout", "0"),
),
},
},
})
}

func TestAccPagerDutyService_BasicWithIncidentUrgencyRules(t *testing.T) {
username := fmt.Sprintf("tf-%s", acctest.RandString(5))
email := fmt.Sprintf("%s@foo.test", username)
Expand Down Expand Up @@ -883,6 +964,7 @@ resource "pagerduty_service" "foo" {
}
`, username, email, escalationPolicy, service)
}

func testAccCheckPagerDutyServiceConfigWithAlertGroupingUpdated(username, email, escalationPolicy, service string) string {
return fmt.Sprintf(`
resource "pagerduty_user" "foo" {
Expand Down Expand Up @@ -920,6 +1002,119 @@ resource "pagerduty_service" "foo" {
`, username, email, escalationPolicy, service)
}

func testAccCheckPagerDutyServiceConfigWithAutoPauseNotificationsParameters(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"
auto_pause_notifications_parameters {
enabled = true
timeout = 300
}
}
`, username, email, escalationPolicy, service)
}

func testAccCheckPagerDutyServiceConfigWithAutoPauseNotificationsParametersUpdated(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"
auto_pause_notifications_parameters {
enabled = false
timeout = null
}
}
`, username, email, escalationPolicy, service)
}

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

func testAccCheckPagerDutyServiceConfigUpdated(username, email, escalationPolicy, service string) string {
return fmt.Sprintf(`
resource "pagerduty_user" "foo" {
Expand Down
10 changes: 5 additions & 5 deletions pagerduty/resource_pagerduty_slack_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,11 @@ func expandConfigList(v interface{}) []string {
return items
}

//Expands the use of star wildcard ("*") configuration for an attribute to its
//matching expected value by PagerDuty's API, which is nil. This is necessary
//when the API accepts and interprets nil and empty configurations as valid
//settings. The state produced by this kind of config can be reverted to the API
//expected values with sibbling function `flattenStarWildcardConfig`.
// Expands the use of star wildcard ("*") configuration for an attribute to its
// matching expected value by PagerDuty's API, which is nil. This is necessary
// when the API accepts and interprets nil and empty configurations as valid
// settings. The state produced by this kind of config can be reverted to the API
// expected values with sibbling function `flattenStarWildcardConfig`.
func expandStarWildcardConfig(c []string) []string {
if isUsingStarWildcardConfig := len(c) == 1 && c[0] == StarWildcardConfig; isUsingStarWildcardConfig {
c = nil
Expand Down
11 changes: 11 additions & 0 deletions website/docs/r/service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ resource "pagerduty_service" "example" {
acknowledgement_timeout = 600
escalation_policy = pagerduty_escalation_policy.foo.id
alert_creation = "create_alerts_and_incidents"

auto_pause_notifications_parameters {
enabled = true
timeout = 300
}
}
```

Expand All @@ -57,6 +62,7 @@ The following arguments are supported:
* `alert_grouping` - (Optional) (Deprecated) Defines how alerts on this service will be automatically grouped into incidents. Note that the alert grouping features are available only on certain plans. If not set, each alert will create a separate incident; If value is set to `time`: All alerts within a specified duration will be grouped into the same incident. This duration is set in the `alert_grouping_timeout` setting (described below). Available on Standard, Enterprise, and Event Intelligence plans; If value is set to `intelligent` - Alerts will be intelligently grouped based on a machine learning model that looks at the alert summary, timing, and the history of grouped alerts. Available on Enterprise and Event Intelligence plan. This field is deprecated, use `alert_grouping_parameters.type` instead,
* `alert_grouping_timeout` - (Optional) (Deprecated) The duration in minutes within which to automatically group incoming alerts. This setting applies only when `alert_grouping` is set to `time`. To continue grouping alerts until the incident is resolved, set this value to `0`. This field is deprecated, use `alert_grouping_parameters.config.timeout` instead,
* `alert_grouping_parameters` - (Optional) Defines how alerts on this service will be automatically grouped into incidents. Note that the alert grouping features are available only on certain plans. If not set, each alert will create a separate incident.
* `auto_pause_notifications_parameters` - (Optional) Defines how alerts on this service are automatically suspended for a period of time before triggering, when identified as likely being transient. Note that automatically pausing notifications is only available on certain plans as mentioned [here](https://support.pagerduty.com/docs/auto-pause-incident-notifications).

The `alert_grouping_parameters` block contains the following arguments:

Expand All @@ -66,6 +72,11 @@ The `alert_grouping_parameters` block contains the following arguments:
* `aggregate` - (Optional) One of `any` or `all`. This setting applies only when `type` is set to `content_based`. Group alerts based on one or all of `fields` value(s).
* `fields` - (Optional) Alerts will be grouped together if the content of these fields match. This setting applies only when `type` is set to `content_based`.

The `auto_pause_notifications_parameters` block contains the following arguments:

* `enabled` (Optional) - Indicates whether alerts should be automatically suspended when identified as transient. If not passed in, will default to 'false'.
* `timeout` (Optional) - Indicates in seconds how long alerts should be suspended before triggering. Allowed values: `120`, `180`, `300`, `600`, `900` if `enabled` is `true`. Must be omitted or set to `null` if `enabled` is `false`.


You may specify one optional `incident_urgency_rule` block configuring what urgencies to use.
Your PagerDuty account must have the `urgencies` ability to assign an incident urgency rule.
Expand Down