forked from PagerDuty/terraform-provider-pagerduty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_pagerduty_user_notification_rule_test.go
132 lines (111 loc) · 3.61 KB
/
resource_pagerduty_user_notification_rule_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package pagerduty
import (
"fmt"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/heimweh/go-pagerduty/pagerduty"
)
func TestAccPagerDutyUserNotificationRuleContactMethod_Basic(t *testing.T) {
contactMethodType1 := "email_contact_method"
contactMethodType2 := "phone_contact_method"
contactMethodType3 := "sms_contact_method"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyUserNotificationRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyUserNotificationRuleContactMethodConfig(contactMethodType1),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyUserNotificationRuleExists("pagerduty_user_notification_rule.foo"),
),
},
{
Config: testAccCheckPagerDutyUserNotificationRuleContactMethodConfig(contactMethodType2),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyUserNotificationRuleExists("pagerduty_user_notification_rule.foo"),
),
},
{
Config: testAccCheckPagerDutyUserNotificationRuleContactMethodConfig(contactMethodType3),
Check: resource.ComposeTestCheckFunc(
testAccCheckPagerDutyUserNotificationRuleExists("pagerduty_user_notification_rule.foo"),
),
},
},
})
}
func testAccCheckPagerDutyUserNotificationRuleDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*pagerduty.Client)
for _, r := range s.RootModule().Resources {
if r.Type != "pagerduty_user_notification_rule" {
continue
}
if _, _, err := client.Users.GetNotificationRule(r.Primary.Attributes["user_id"], r.Primary.ID); err == nil {
return fmt.Errorf("User notification rule still exists")
}
}
return nil
}
func testAccCheckPagerDutyUserNotificationRuleExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No user notification rule ID is set")
}
client := testAccProvider.Meta().(*pagerduty.Client)
found, _, err := client.Users.GetNotificationRule(rs.Primary.Attributes["user_id"], rs.Primary.ID)
if err != nil {
return err
}
if found.ID != rs.Primary.ID {
return fmt.Errorf("Notification rule not found: %v - %v", rs.Primary.ID, found)
}
return nil
}
}
func testAccCheckPagerDutyUserNotificationRuleContactMethodConfig(methodType string) string {
return fmt.Sprintf(`
resource "pagerduty_user_notification_rule" "foo" {
user_id = pagerduty_user.foo.id
start_delay_in_minutes = 1
urgency = "high"
contact_method = {
type = "%[1]v"
id = pagerduty_user_contact_method.%[1]v.id
}
}
resource "pagerduty_user" "foo" {
name = "foo bar"
email = "foo@bar.com"
color = "red"
role = "user"
job_title = "bar"
description = "bar"
}
resource "pagerduty_user_contact_method" "email_contact_method" {
user_id = pagerduty_user.foo.id
type = "email_contact_method"
address = "foo-1@bar.com"
label = "Work"
}
resource "pagerduty_user_contact_method" "sms_contact_method" {
user_id = pagerduty_user.foo.id
type = "sms_contact_method"
address = "8005551234"
country_code = "+1"
label = "Work"
}
resource "pagerduty_user_contact_method" "phone_contact_method" {
user_id = pagerduty_user.foo.id
type = "phone_contact_method"
country_code = "+1"
address = "8005551234"
label = "Work"
}
`, methodType)
}