terraform/builtin/providers/pagerduty/resource_pagerduty_escalati...

148 lines
3.6 KiB
Go
Raw Normal View History

2016-09-23 21:43:06 +02:00
package pagerduty
import (
"fmt"
"testing"
"github.com/PagerDuty/go-pagerduty"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccPagerDutyEscalationPolicy_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyEscalationPolicyDestroy,
Steps: []resource.TestStep{
resource.TestStep{
2016-10-16 16:48:20 +02:00
Config: testAccCheckPagerDutyEscalationPolicyConfig,
2016-09-23 21:43:06 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "name", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "description", "foo"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "num_loops", "1"),
),
},
resource.TestStep{
2016-10-16 16:48:20 +02:00
Config: testAccCheckPagerDutyEscalationPolicyConfigUpdated,
2016-09-23 21:43:06 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyEscalationPolicyExists("pagerduty_escalation_policy.foo"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "name", "bar"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "description", "bar"),
resource.TestCheckResourceAttr(
"pagerduty_escalation_policy.foo", "num_loops", "2"),
),
},
},
})
}
func testAccCheckPagerDutyEscalationPolicyDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*pagerduty.Client)
for _, r := range s.RootModule().Resources {
if r.Type != "pagerduty_escalation_policy" {
continue
}
_, err := client.GetEscalationPolicy(r.Primary.ID, &pagerduty.GetEscalationPolicyOptions{})
if err == nil {
return fmt.Errorf("Escalation Policy still exists")
}
}
return nil
}
func testAccCheckPagerDutyEscalationPolicyExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
2016-10-16 16:48:20 +02:00
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No Escalation Policy ID is set")
}
2016-09-23 21:43:06 +02:00
client := testAccProvider.Meta().(*pagerduty.Client)
2016-10-16 16:48:20 +02:00
found, err := client.GetEscalationPolicy(rs.Primary.ID, &pagerduty.GetEscalationPolicyOptions{})
if err != nil {
return err
}
if found.ID != rs.Primary.ID {
return fmt.Errorf("Escalation policy not found: %v - %v", rs.Primary.ID, found)
2016-09-23 21:43:06 +02:00
}
2016-10-16 16:48:20 +02:00
2016-09-23 21:43:06 +02:00
return nil
}
}
2016-10-16 16:48:20 +02:00
const testAccCheckPagerDutyEscalationPolicyConfig = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
2016-09-23 21:43:06 +02:00
resource "pagerduty_escalation_policy" "foo" {
name = "foo"
description = "foo"
num_loops = 1
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
2016-09-23 21:43:06 +02:00
}
2016-10-16 16:48:20 +02:00
`
const testAccCheckPagerDutyEscalationPolicyConfigUpdated = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
2016-09-23 21:43:06 +02:00
}
resource "pagerduty_escalation_policy" "foo" {
name = "bar"
description = "bar"
num_loops = 2
escalation_rule {
escalation_delay_in_minutes = 10
2016-09-23 21:43:06 +02:00
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
escalation_rule {
escalation_delay_in_minutes = 20
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
2016-09-23 21:43:06 +02:00
}
2016-10-16 16:48:20 +02:00
`