diff --git a/go.mod b/go.mod index ce3aefc75..11be425bc 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/hashicorp/terraform-exec v0.16.0 github.com/hashicorp/terraform-json v0.13.0 github.com/hashicorp/terraform-plugin-sdk/v2 v2.11.0 - github.com/heimweh/go-pagerduty v0.0.0-20231201203054-09be11e40bea + github.com/heimweh/go-pagerduty v0.0.0-20231207205722-b4c4cc9f249e ) require ( diff --git a/go.sum b/go.sum index 8acd45800..ac258abcd 100644 --- a/go.sum +++ b/go.sum @@ -250,8 +250,8 @@ github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ= github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM= -github.com/heimweh/go-pagerduty v0.0.0-20231201203054-09be11e40bea h1:JJnJ9l1XBIFepPShm8XNvbILSMVQW8sjeAKctexHots= -github.com/heimweh/go-pagerduty v0.0.0-20231201203054-09be11e40bea/go.mod h1:r59w5iyN01Qvi734yA5hZldbSeJJmsJzee/1kQ/MK7s= +github.com/heimweh/go-pagerduty v0.0.0-20231207205722-b4c4cc9f249e h1:u8v+2ZZOb9QFGiE676aDj08N9kFTNHcMtY6+9g57+ko= +github.com/heimweh/go-pagerduty v0.0.0-20231207205722-b4c4cc9f249e/go.mod h1:r59w5iyN01Qvi734yA5hZldbSeJJmsJzee/1kQ/MK7s= github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= diff --git a/pagerduty/resource_pagerduty_escalation_policy.go b/pagerduty/resource_pagerduty_escalation_policy.go index 485ca31bc..372d39578 100644 --- a/pagerduty/resource_pagerduty_escalation_policy.go +++ b/pagerduty/resource_pagerduty_escalation_policy.go @@ -59,6 +59,25 @@ func resourcePagerDutyEscalationPolicy() *schema.Resource { Required: true, ValidateFunc: validation.IntAtLeast(1), }, + "escalation_rule_assignment_strategy": { + Type: schema.TypeList, + Optional: true, + Computed: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "type": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ValidateDiagFunc: validateValueDiagFunc([]string{ + "assign_to_everyone", + "round_robin", + }), + }, + }, + }, + }, "target": { Type: schema.TypeList, Required: true, @@ -151,7 +170,7 @@ func fetchEscalationPolicy(d *schema.ResourceData, meta interface{}, errCallback return err } - o := &pagerduty.GetEscalationPolicyOptions{} + o := &pagerduty.GetEscalationPolicyOptions{Includes: []string{"escalation_rule_assignment_strategies"}} return resource.Retry(5*time.Minute, func() *resource.RetryError { escalationPolicy, _, err := client.EscalationPolicies.Get(d.Id(), o) @@ -247,7 +266,8 @@ func expandEscalationRules(v interface{}) []*pagerduty.EscalationRule { for _, er := range v.([]interface{}) { rer := er.(map[string]interface{}) escalationRule := &pagerduty.EscalationRule{ - EscalationDelayInMinutes: rer["escalation_delay_in_minutes"].(int), + EscalationDelayInMinutes: rer["escalation_delay_in_minutes"].(int), + EscalationRuleAssignmentStrategy: expandEscalationRuleAssignmentStrategy(rer["escalation_rule_assignment_strategy"]), } for _, ert := range rer["target"].([]interface{}) { @@ -266,6 +286,21 @@ func expandEscalationRules(v interface{}) []*pagerduty.EscalationRule { return escalationRules } +func expandEscalationRuleAssignmentStrategy(v interface{}) *pagerduty.EscalationRuleAssignmentStrategy { + log.Printf("expandEscalationRuleAssignmentStrategy_v is %+v", v) + escalationRuleAssignmentStrategy := &pagerduty.EscalationRuleAssignmentStrategy{} + pre := v.([]interface{}) + if len(pre) == 0 || isNilFunc(pre[0]) { + return nil + } + + eras := pre[0].(map[string]interface{}) + teras := eras["type"].(string) + log.Printf("expandEscalationRuleAssignmentStrategy_teras is %#v", teras) + escalationRuleAssignmentStrategy.Type = teras + return escalationRuleAssignmentStrategy +} + func flattenEscalationRules(v []*pagerduty.EscalationRule) []map[string]interface{} { var escalationRules []map[string]interface{} @@ -275,6 +310,13 @@ func flattenEscalationRules(v []*pagerduty.EscalationRule) []map[string]interfac "escalation_delay_in_minutes": er.EscalationDelayInMinutes, } + var escalationRuleAssignmentStrategy []map[string]interface{} + if er.EscalationRuleAssignmentStrategy != nil { + eras := map[string]interface{}{"type": er.EscalationRuleAssignmentStrategy.Type} + escalationRuleAssignmentStrategy = append(escalationRuleAssignmentStrategy, eras) + } + escalationRule["escalation_rule_assignment_strategy"] = escalationRuleAssignmentStrategy + var targets []map[string]interface{} for _, ert := range er.Targets { diff --git a/pagerduty/resource_pagerduty_escalation_policy_test.go b/pagerduty/resource_pagerduty_escalation_policy_test.go index c2c24bbc2..13d4c9e3e 100644 --- a/pagerduty/resource_pagerduty_escalation_policy_test.go +++ b/pagerduty/resource_pagerduty_escalation_policy_test.go @@ -110,6 +110,57 @@ func TestAccPagerDutyEscalationPolicy_Basic(t *testing.T) { }) } +func TestAccPagerDutyEscalationPolicyWithRoundRobinAssignmentStrategy(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)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPagerDutyEscalationPolicyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckPagerDutyEscalationPolicyConfig(username, email, escalationPolicy), + Check: resource.ComposeTestCheckFunc( + testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"), + resource.TestCheckResourceAttr( + "pagerduty_escalation_policy.foo", "rule.0.escalation_rule_assignment_strategy.0.type", "assign_to_everyone"), + ), + }, + { + Config: testAccCheckPagerDutyEscalationPolicyWithRoundRoundAssignmentStrategyConfig(username, email, escalationPolicy, "not_valid_strategy"), + PlanOnly: true, + ExpectError: regexp.MustCompile(`Must be one of \[\]string{"assign_to_everyone", "round_robin"}`), + }, + { + Config: testAccCheckPagerDutyEscalationPolicyWithRoundRoundAssignmentStrategyConfig(username, email, escalationPolicy, "round_robin"), + Check: resource.ComposeTestCheckFunc( + testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"), + resource.TestCheckResourceAttr( + "pagerduty_escalation_policy.foo", "name", escalationPolicy), + resource.TestCheckResourceAttr( + "pagerduty_escalation_policy.foo", "description", "foo"), + resource.TestCheckResourceAttr( + "pagerduty_escalation_policy.foo", "num_loops", "1"), + resource.TestCheckResourceAttr( + "pagerduty_escalation_policy.foo", "rule.#", "1"), + resource.TestCheckResourceAttr( + "pagerduty_escalation_policy.foo", "rule.0.escalation_rule_assignment_strategy.0.type", "round_robin"), + ), + }, + { + Config: testAccCheckPagerDutyEscalationPolicyWithRoundRoundAssignmentStrategyConfig(username, email, escalationPolicy, "assign_to_everyone"), + Check: resource.ComposeTestCheckFunc( + testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"), + resource.TestCheckResourceAttr( + "pagerduty_escalation_policy.foo", "rule.0.escalation_rule_assignment_strategy.0.type", "assign_to_everyone"), + ), + }, + }, + }) +} + func TestAccPagerDutyEscalationPolicy_FormatValidation(t *testing.T) { username := fmt.Sprintf("tf-%s", acctest.RandString(5)) email := fmt.Sprintf("%s@foo.test", username) @@ -214,6 +265,37 @@ func TestAccPagerDutyEscalationPolicyWithTeams_Basic(t *testing.T) { }) } +func testAccCheckPagerDutyEscalationPolicyWithRoundRoundAssignmentStrategyConfig(name, email, escalationPolicy, strategy 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 = "foo" + num_loops = 1 + + rule { + escalation_delay_in_minutes = 10 + escalation_rule_assignment_strategy { + type = "%s" + } + + target { + type = "user_reference" + id = pagerduty_user.foo.id + } + } +} +`, name, email, escalationPolicy, strategy) +} + func testAccCheckPagerDutyEscalationPolicyDestroy(s *terraform.State) error { client, _ := testAccProvider.Meta().(*Config).Client() for _, r := range s.RootModule().Resources { diff --git a/vendor/github.com/heimweh/go-pagerduty/pagerduty/escalation_policy.go b/vendor/github.com/heimweh/go-pagerduty/pagerduty/escalation_policy.go index b41557781..ff5cea537 100644 --- a/vendor/github.com/heimweh/go-pagerduty/pagerduty/escalation_policy.go +++ b/vendor/github.com/heimweh/go-pagerduty/pagerduty/escalation_policy.go @@ -6,11 +6,18 @@ import "fmt" // related methods of the PagerDuty API. type EscalationPolicyService service +// EscalationRuleAssignmentStrategy represents an Escalation rule assignment +// strategy +type EscalationRuleAssignmentStrategy struct { + Type string `json:"type,omitempty"` +} + // EscalationRule represents an escalation rule. type EscalationRule struct { - EscalationDelayInMinutes int `json:"escalation_delay_in_minutes,omitempty"` - ID string `json:"id,omitempty"` - Targets []*EscalationTargetReference `json:"targets,omitempty"` + EscalationDelayInMinutes int `json:"escalation_delay_in_minutes,omitempty"` + EscalationRuleAssignmentStrategy *EscalationRuleAssignmentStrategy `json:"escalation_rule_assignment_strategy,omitempty"` + ID string `json:"id,omitempty"` + Targets []*EscalationTargetReference `json:"targets,omitempty"` } // EscalationPolicy represents an escalation policy. diff --git a/vendor/modules.txt b/vendor/modules.txt index bda6064cb..c90d1773a 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -182,7 +182,7 @@ github.com/hashicorp/terraform-svchost # github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d ## explicit github.com/hashicorp/yamux -# github.com/heimweh/go-pagerduty v0.0.0-20231201203054-09be11e40bea +# github.com/heimweh/go-pagerduty v0.0.0-20231207205722-b4c4cc9f249e ## explicit; go 1.17 github.com/heimweh/go-pagerduty/pagerduty github.com/heimweh/go-pagerduty/persistentconfig diff --git a/website/docs/r/escalation_policy.html.markdown b/website/docs/r/escalation_policy.html.markdown index 2441ee1e6..4e32824ee 100644 --- a/website/docs/r/escalation_policy.html.markdown +++ b/website/docs/r/escalation_policy.html.markdown @@ -58,8 +58,13 @@ The following arguments are supported: Escalation rules (`rule`) supports the following: * `escalation_delay_in_minutes` - (Required) The number of minutes before an unacknowledged incident escalates away from this rule. + * `escalation_rule_assignment_strategy` - (Optional) The strategy used to assign the escalation rule to an incident. Documented below. * `targets` - (Required) A target block. Target blocks documented below. +Incident assignment strategy for Escalation Rule (`escalation_rule_assignment_strategy`) supports the following: + +* `type` - (Optional) Can be `round_robin` or `assign_to_everyone`. + Targets (`target`) supports the following: * `type` - (Optional) Can be `user_reference` or `schedule_reference`. Defaults to `user_reference`. For multiple users as example, repeat the target.