terraform/builtin/providers/pagerduty/resource_pagerduty_service_...

159 lines
4.0 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 TestAccPagerDutyService_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyServiceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
2016-10-16 16:48:20 +02:00
Config: testAccCheckPagerDutyServiceConfig,
2016-09-23 21:43:06 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "name", "foo"),
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.TestStep{
2016-10-16 16:48:20 +02:00
Config: testAccCheckPagerDutyServiceConfigUpdated,
2016-09-23 21:43:06 +02:00
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyServiceExists("pagerduty_service.foo"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "name", "bar"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "description", "bar"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "auto_resolve_timeout", "3600"),
resource.TestCheckResourceAttr(
"pagerduty_service.foo", "acknowledgement_timeout", "3600"),
),
},
},
})
}
func testAccCheckPagerDutyServiceDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*pagerduty.Client)
for _, r := range s.RootModule().Resources {
if r.Type != "pagerduty_service" {
continue
}
2016-10-18 20:47:21 +02:00
_, err := client.GetService(r.Primary.ID, &pagerduty.GetServiceOptions{})
2016-09-23 21:43:06 +02:00
if err == nil {
return fmt.Errorf("Service still exists")
}
}
return nil
}
func testAccCheckPagerDutyServiceExists(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 Service ID is set")
}
2016-09-23 21:43:06 +02:00
client := testAccProvider.Meta().(*pagerduty.Client)
2016-10-16 16:48:20 +02:00
2016-10-18 20:47:21 +02:00
found, err := client.GetService(rs.Primary.ID, &pagerduty.GetServiceOptions{})
2016-10-16 16:48:20 +02:00
if err != nil {
return err
2016-09-23 21:43:06 +02:00
}
2016-10-16 16:48:20 +02:00
if found.ID != rs.Primary.ID {
return fmt.Errorf("Service not found: %v - %v", rs.Primary.ID, found)
}
2016-09-23 21:43:06 +02:00
return nil
}
}
2016-10-16 16:48:20 +02:00
const testAccCheckPagerDutyServiceConfig = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
resource "pagerduty_escalation_policy" "foo" {
name = "bar"
description = "bar"
num_loops = 2
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
2016-10-16 16:48:20 +02:00
}
resource "pagerduty_service" "foo" {
name = "foo"
description = "foo"
auto_resolve_timeout = 1800
acknowledgement_timeout = 1800
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
2016-10-16 16:48:20 +02:00
}
`
const testAccCheckPagerDutyServiceConfigUpdated = `
resource "pagerduty_user" "foo" {
name = "foo"
email = "foo@bar.com"
color = "green"
role = "user"
job_title = "foo"
description = "foo"
}
resource "pagerduty_escalation_policy" "foo" {
name = "bar"
description = "bar"
num_loops = 2
escalation_rule {
escalation_delay_in_minutes = 10
target {
type = "user_reference"
id = "${pagerduty_user.foo.id}"
}
}
2016-09-23 21:43:06 +02:00
}
resource "pagerduty_service" "foo" {
name = "bar"
description = "bar"
auto_resolve_timeout = 3600
acknowledgement_timeout = 3600
escalation_policy = "${pagerduty_escalation_policy.foo.id}"
2016-09-23 21:43:06 +02:00
}
2016-10-16 16:48:20 +02:00
`