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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
package statuscake
import (
"fmt"
"strconv"
"testing"
"github.com/DreamItGetIT/statuscake"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
func TestAccStatusCake_basic(t *testing.T) {
var test statuscake.Test
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccTestCheckDestroy(&test),
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccTestConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccTestCheckExists("statuscake_test.google", &test),
testAccTestCheckAttributes("statuscake_test.google", &test),
),
},
},
})
}
func TestAccStatusCake_withUpdate(t *testing.T) {
var test statuscake.Test
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccTestCheckDestroy(&test),
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccTestConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccTestCheckExists("statuscake_test.google", &test),
),
},
resource.TestStep{
Config: testAccTestConfig_update,
Check: resource.ComposeTestCheckFunc(
testAccTestCheckExists("statuscake_test.google", &test),
testAccTestCheckAttributes("statuscake_test.google", &test),
resource.TestCheckResourceAttr("statuscake_test.google", "check_rate", "500"),
resource.TestCheckResourceAttr("statuscake_test.google", "paused", "true"),
resource.TestCheckResourceAttr("statuscake_test.google", "contact_id", "0"),
),
},
},
})
}
func testAccTestCheckExists(rn string, test *statuscake.Test) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[rn]
if !ok {
return fmt.Errorf("resource not found: %s", rn)
}
if rs.Primary.ID == "" {
return fmt.Errorf("TestID not set")
}
client := testAccProvider.Meta().(*statuscake.Client)
testId, parseErr := strconv.Atoi(rs.Primary.ID)
if parseErr != nil {
return fmt.Errorf("error in statuscake test CheckExists: %s", parseErr)
}
gotTest, err := client.Tests().Detail(testId)
if err != nil {
return fmt.Errorf("error getting test: %s", err)
}
*test = *gotTest
return nil
}
}
func testAccTestCheckAttributes(rn string, test *statuscake.Test) resource.TestCheckFunc {
return func(s *terraform.State) error {
attrs := s.RootModule().Resources[rn].Primary.Attributes
check := func(key, stateValue, testValue string) error {
if testValue != stateValue {
return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)",
key, stateValue, testValue)
}
return nil
}
for key, value := range attrs {
var err error
switch key {
case "website_name":
err = check(key, value, test.WebsiteName)
case "website_url":
err = check(key, value, test.WebsiteURL)
case "check_rate":
err = check(key, value, strconv.Itoa(test.CheckRate))
case "test_type":
err = check(key, value, test.TestType)
case "paused":
err = check(key, value, strconv.FormatBool(test.Paused))
case "timeout":
err = check(key, value, strconv.Itoa(test.Timeout))
case "contact_id":
err = check(key, value, strconv.Itoa(test.ContactID))
}
if err != nil {
return err
}
}
return nil
}
}
func testAccTestCheckDestroy(test *statuscake.Test) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*statuscake.Client)
err := client.Tests().Delete(test.TestID)
if err == nil {
return fmt.Errorf("test still exists")
}
return nil
}
}
const testAccTestConfig_basic = `
resource "statuscake_test" "google" {
website_name = "google.com"
website_url = "www.google.com"
test_type = "HTTP"
check_rate = 300
contact_id = 12345
}
`
const testAccTestConfig_update = `
resource "statuscake_test" "google" {
website_name = "google.com"
website_url = "www.google.com"
test_type = "HTTP"
check_rate = 500
paused = true
}
`
|