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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
|
package statuscake
import (
"fmt"
"github.com/DreamItGetIT/statuscake"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"os"
"strconv"
"testing"
)
func TestAccStatusCakeSsl_basic(t *testing.T) {
var ssl statuscake.Ssl
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccSslCheckDestroy(&ssl),
Steps: []resource.TestStep{
{
Config: interpolateTerraformTemplateSsl(testAccSslConfig_basic),
Check: resource.ComposeTestCheckFunc(
testAccSslCheckExists("statuscake_ssl.exemple", &ssl),
testAccSslCheckAttributes("statuscake_ssl.exemple", &ssl),
),
},
},
})
}
func TestAccStatusCakeSsl_withUpdate(t *testing.T) {
var ssl statuscake.Ssl
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccSslCheckDestroy(&ssl),
Steps: []resource.TestStep{
{
Config: interpolateTerraformTemplateSsl(testAccSslConfig_basic),
Check: resource.ComposeTestCheckFunc(
testAccSslCheckExists("statuscake_ssl.exemple", &ssl),
testAccSslCheckAttributes("statuscake_ssl.exemple", &ssl),
),
},
{
Config: testAccSslConfig_update,
Check: resource.ComposeTestCheckFunc(
testAccSslCheckExists("statuscake_ssl.exemple", &ssl),
testAccSslCheckAttributes("statuscake_ssl.exemple", &ssl),
resource.TestCheckResourceAttr("statuscake_ssl.exemple", "checkrate", "86400"),
resource.TestCheckResourceAttr("statuscake_ssl.exemple", "domain", "https://www.exemple.com"),
resource.TestCheckResourceAttr("statuscake_ssl.exemple", "contact_groups_c", ""),
resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_at", "18,81,2019"),
resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_reminder", "false"),
resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_expiry", "false"),
resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_broken", "true"),
resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_mixed", "false"),
),
},
},
})
}
func testAccSslCheckExists(rn string, ssl *statuscake.Ssl) 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("SslID not set")
}
client := testAccProvider.Meta().(*statuscake.Client)
sslId := rs.Primary.ID
gotSsl, err := statuscake.NewSsls(client).Detail(sslId)
if err != nil {
return fmt.Errorf("error getting ssl: %s", err)
}
gotSsl.LastUpdatedUtc = "0000-00-00 00:00:00" // quick fix to avoid issue with it because the state is updated before the value change but it is changed when gotSsl is created
*ssl = *gotSsl
return nil
}
}
func testAccSslCheckAttributes(rn string, ssl *statuscake.Ssl) resource.TestCheckFunc {
return func(s *terraform.State) error {
attrs := s.RootModule().Resources[rn].Primary.Attributes
check := func(key, stateValue, sslValue string) error {
if sslValue != stateValue {
return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)",
key, stateValue, sslValue)
}
return nil
}
for key, value := range attrs {
var err error
switch key {
case "domain":
err = check(key, value, ssl.Domain)
case "contact_groups_c":
err = check(key, value, ssl.ContactGroupsC)
case "checkrate":
err = check(key, value, strconv.Itoa(ssl.Checkrate))
case "alert_at":
err = check(key, value, ssl.AlertAt)
case "alert_reminder":
err = check(key, value, strconv.FormatBool(ssl.AlertReminder))
case "alert_expiry":
err = check(key, value, strconv.FormatBool(ssl.AlertExpiry))
case "alert_broken":
err = check(key, value, strconv.FormatBool(ssl.AlertBroken))
case "alert_mixed":
err = check(key, value, strconv.FormatBool(ssl.AlertMixed))
case "last_updated_utc":
err = check(key, value, ssl.LastUpdatedUtc)
case "paused":
err = check(key, value, strconv.FormatBool(ssl.Paused))
case "issuer_cn":
err = check(key, value, ssl.IssuerCn)
case "contact_groups":
for _, tv := range ssl.ContactGroups {
err = check(key, value, tv)
if err != nil {
return err
}
}
case "cert_score":
err = check(key, value, ssl.CertScore)
case "cert_status":
err = check(key, value, ssl.CertStatus)
case "cipher":
err = check(key, value, ssl.Cipher)
case "valid_from_utc":
err = check(key, value, ssl.ValidFromUtc)
case "valid_until_utc":
err = check(key, value, ssl.ValidUntilUtc)
case "last_reminder":
err = check(key, value, strconv.Itoa(ssl.LastReminder))
case "flags":
for _, tv := range ssl.Flags {
err = check(key, value, strconv.FormatBool(tv))
if err != nil {
return err
}
}
case "mixed_content":
for _, tv := range ssl.MixedContent {
for _, tv2 := range tv {
err = check(key, value, tv2)
if err != nil {
return err
}
}
}
}
if err != nil {
return err
}
}
return nil
}
}
func testAccSslCheckDestroy(ssl *statuscake.Ssl) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*statuscake.Client)
_, err := statuscake.NewSsls(client).Detail(ssl.ID)
if err == nil {
return fmt.Errorf("ssl still exists")
}
return nil
}
}
func interpolateTerraformTemplateSsl(template string) string {
sslContactGroupId := "43402"
if v := os.Getenv("STATUSCAKE_SSL_CONTACT_GROUP_ID"); v != "" {
sslContactGroupId = v
}
if sslContactGroupId == "-1" {
sslContactGroupId = ""
}
return fmt.Sprintf(template, sslContactGroupId)
}
const testAccSslConfig_basic = `
resource "statuscake_ssl" "exemple" {
domain = "https://www.exemple.com"
contact_groups_c = "%s"
checkrate = 3600
alert_at = "18,71,2019"
alert_reminder = true
alert_expiry = true
alert_broken = false
alert_mixed = true
}
`
const testAccSslConfig_update = `
resource "statuscake_ssl" "exemple" {
domain = "https://www.exemple.com"
contact_groups_c = ""
checkrate = 86400
alert_at = "18,81,2019"
alert_reminder = false
alert_expiry = false
alert_broken = true
alert_mixed = false
}
`
|