aboutsummaryrefslogtreecommitdiffhomepage
path: root/statuscake/resource_statuscakecontactgroup_test.go
diff options
context:
space:
mode:
authorAlexandre Garand <alexandre.garand@fretlink.com>2019-08-09 11:10:50 +0200
committerAlexandre Garand <alexandre.garand@fretlink.com>2019-08-09 11:10:50 +0200
commit00adefd9b3c8168d52522da0c1370c45171c03e3 (patch)
treec2ecc2f0aa567fcadf1ebfa96764f2057215a03e /statuscake/resource_statuscakecontactgroup_test.go
parent4bbf9881d0a859d7eb54fcac2c8cdfa429503f9b (diff)
parent0b957cd46a2a3a209c45778da2a7e1e55a94f3c7 (diff)
downloadterraform-provider-statuscake-00adefd9b3c8168d52522da0c1370c45171c03e3.tar.gz
terraform-provider-statuscake-00adefd9b3c8168d52522da0c1370c45171c03e3.tar.zst
terraform-provider-statuscake-00adefd9b3c8168d52522da0c1370c45171c03e3.zip
Merge branch 'add_contact_groups' of github.com:alexandreFre/terraform-provider-statuscake
Diffstat (limited to 'statuscake/resource_statuscakecontactgroup_test.go')
-rw-r--r--statuscake/resource_statuscakecontactgroup_test.go157
1 files changed, 157 insertions, 0 deletions
diff --git a/statuscake/resource_statuscakecontactgroup_test.go b/statuscake/resource_statuscakecontactgroup_test.go
new file mode 100644
index 0000000..a409808
--- /dev/null
+++ b/statuscake/resource_statuscakecontactgroup_test.go
@@ -0,0 +1,157 @@
1package statuscake
2
3import (
4 "fmt"
5 "github.com/DreamItGetIT/statuscake"
6 "github.com/hashicorp/terraform/helper/resource"
7 "github.com/hashicorp/terraform/terraform"
8 "strconv"
9 "testing"
10)
11
12func TestAccStatusCakeContactGroup_basic(t *testing.T) {
13 var contactGroup statuscake.ContactGroup
14
15 resource.Test(t, resource.TestCase{
16 PreCheck: func() { testAccPreCheck(t) },
17 Providers: testAccProviders,
18 CheckDestroy: testAccContactGroupCheckDestroy(&contactGroup),
19 Steps: []resource.TestStep{
20 {
21 Config: testAccContactGroupConfig_basic,
22 Check: resource.ComposeTestCheckFunc(
23 testAccContactGroupCheckExists("statuscake_contact_group.exemple", &contactGroup),
24 testAccContactGroupCheckAttributes("statuscake_contact_group.exemple", &contactGroup),
25 ),
26 },
27 },
28 })
29}
30
31func TestAccStatusCakeContactGroup_withUpdate(t *testing.T) {
32 var contactGroup statuscake.ContactGroup
33
34 resource.Test(t, resource.TestCase{
35 PreCheck: func() { testAccPreCheck(t) },
36 Providers: testAccProviders,
37 CheckDestroy: testAccContactGroupCheckDestroy(&contactGroup),
38 Steps: []resource.TestStep{
39 {
40 Config: testAccContactGroupConfig_basic,
41 Check: resource.ComposeTestCheckFunc(
42 testAccContactGroupCheckExists("statuscake_contact_group.exemple", &contactGroup),
43 testAccContactGroupCheckAttributes("statuscake_contact_group.exemple", &contactGroup),
44 ),
45 },
46
47 {
48 Config: testAccContactGroupConfig_update,
49 Check: resource.ComposeTestCheckFunc(
50 testAccContactGroupCheckExists("statuscake_contact_group.exemple", &contactGroup),
51 testAccContactGroupCheckAttributes("statuscake_contact_group.exemple", &contactGroup),
52 resource.TestCheckResourceAttr("statuscake_contact_group.exemple", "group_name", "group"),
53 resource.TestCheckResourceAttr("statuscake_contact_group.exemple", "ping_url", "https"),
54 ),
55 },
56 },
57 })
58}
59
60func testAccContactGroupCheckExists(rn string, contactGroup *statuscake.ContactGroup) resource.TestCheckFunc {
61 return func(s *terraform.State) error {
62 rs, ok := s.RootModule().Resources[rn]
63 if !ok {
64 return fmt.Errorf("resource not found: %s", rn)
65 }
66
67 if rs.Primary.ID == "" {
68 return fmt.Errorf("ContactGroupID not set")
69 }
70
71 client := testAccProvider.Meta().(*statuscake.Client)
72 contactGroupId, _ := strconv.Atoi(rs.Primary.ID)
73
74 gotContactGroup, err := statuscake.NewContactGroups(client).Detail(contactGroupId)
75 if err != nil {
76 return fmt.Errorf("error getting ContactGroup: %s", err)
77 }
78
79 *contactGroup = *gotContactGroup
80
81 return nil
82 }
83}
84
85func testAccContactGroupCheckAttributes(rn string, contactGroup *statuscake.ContactGroup) resource.TestCheckFunc {
86 return func(s *terraform.State) error {
87 attrs := s.RootModule().Resources[rn].Primary.Attributes
88
89 check := func(key, stateValue, contactGroupValue string) error {
90 if contactGroupValue != stateValue {
91 return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)",
92 key, stateValue, contactGroupValue)
93 }
94 return nil
95 }
96
97 for key, value := range attrs {
98 var err error
99
100 switch key {
101 case "contact_id":
102 err = check(key, value, strconv.Itoa(contactGroup.ContactID))
103 case "desktop_alert":
104 err = check(key, value, contactGroup.DesktopAlert)
105 case "ping_url":
106 err = check(key, value, contactGroup.PingURL)
107 case "group_name":
108 err = check(key, value, contactGroup.GroupName)
109 case "pushover":
110 err = check(key, value, contactGroup.Pushover)
111 case "boxcar":
112 err = check(key, value, contactGroup.Boxcar)
113 case "mobiles":
114 err = check(key, value, contactGroup.Mobiles)
115 case "emails":
116 for _, tv := range contactGroup.Emails {
117 err = check(key, value, tv)
118 if err != nil {
119 return err
120 }
121 }
122 }
123 if err != nil {
124 return err
125 }
126 }
127 return nil
128 }
129}
130
131func testAccContactGroupCheckDestroy(contactGroup *statuscake.ContactGroup) resource.TestCheckFunc {
132 return func(s *terraform.State) error {
133 client := testAccProvider.Meta().(*statuscake.Client)
134 _, err := statuscake.NewContactGroups(client).Detail(contactGroup.ContactID)
135 if err == nil {
136 return fmt.Errorf("contact_group still exists")
137 }
138
139 return nil
140 }
141}
142
143const testAccContactGroupConfig_basic = `
144resource "statuscake_contact_group" "exemple" {
145 emails= ["aaa","bbb"]
146 group_name= "groupname"
147 ping_url= "http"
148}
149`
150
151const testAccContactGroupConfig_update = `
152resource "statuscake_contact_group" "exemple" {
153 emails= ["aaa","bbb","ccc"]
154 group_name= "group"
155 ping_url= "https"
156}
157`