Skip to content

Commit

Permalink
Merge pull request #781 from imjaroiswebdev/issue-518-add-round-robin…
Browse files Browse the repository at this point in the history
…-support

[CSGI-2314] Add Round Robin support to `pagerduty_escalation_policy`
  • Loading branch information
imjaroiswebdev authored Dec 7, 2023
2 parents b2d7f72 + a17948b commit 6f39184
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
46 changes: 44 additions & 2 deletions pagerduty/resource_pagerduty_escalation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{}) {
Expand All @@ -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{}

Expand All @@ -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 {
Expand Down
82 changes: 82 additions & 0 deletions pagerduty/resource_pagerduty_escalation_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("%[email protected]", 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("%[email protected]", username)
Expand Down Expand Up @@ -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 {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/escalation_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6f39184

Please sign in to comment.