aboutsummaryrefslogtreecommitdiffhomepage
path: root/statuscake
diff options
context:
space:
mode:
authorAlexandre Garand <alexandre.garand@fretlink.com>2019-06-27 12:02:56 +0200
committerAlexandre Garand <alexandre.garand@fretlink.com>2019-06-27 15:00:54 +0200
commit82cbfd586d4853b8ef6beafc12c7f2aa015f3c23 (patch)
treea329cf300045af465cc2ae336012ee2425ac3d50 /statuscake
parent2ad41f522127680c37fbaea93d0360fa989e4c40 (diff)
downloadterraform-provider-statuscake-82cbfd586d4853b8ef6beafc12c7f2aa015f3c23.tar.gz
terraform-provider-statuscake-82cbfd586d4853b8ef6beafc12c7f2aa015f3c23.tar.zst
terraform-provider-statuscake-82cbfd586d4853b8ef6beafc12c7f2aa015f3c23.zip
add ssl tests to terraform provider
Diffstat (limited to 'statuscake')
-rw-r--r--statuscake/provider.go1
-rw-r--r--statuscake/provider_test.go3
-rw-r--r--statuscake/resource_statuscakessl.go283
-rw-r--r--statuscake/resource_statuscakessl_test.go222
4 files changed, 509 insertions, 0 deletions
diff --git a/statuscake/provider.go b/statuscake/provider.go
index 93c691f..7f38d7f 100644
--- a/statuscake/provider.go
+++ b/statuscake/provider.go
@@ -25,6 +25,7 @@ func Provider() terraform.ResourceProvider {
25 25
26 ResourcesMap: map[string]*schema.Resource{ 26 ResourcesMap: map[string]*schema.Resource{
27 "statuscake_test": resourceStatusCakeTest(), 27 "statuscake_test": resourceStatusCakeTest(),
28 "statuscake_ssl": resourceStatusCakeSsl(),
28 }, 29 },
29 30
30 ConfigureFunc: providerConfigure, 31 ConfigureFunc: providerConfigure,
diff --git a/statuscake/provider_test.go b/statuscake/provider_test.go
index b1da050..4537960 100644
--- a/statuscake/provider_test.go
+++ b/statuscake/provider_test.go
@@ -38,4 +38,7 @@ func testAccPreCheck(t *testing.T) {
38 if v := os.Getenv("STATUSCAKE_TEST_CONTACT_GROUP_ID"); v == "" { 38 if v := os.Getenv("STATUSCAKE_TEST_CONTACT_GROUP_ID"); v == "" {
39 t.Fatal("STATUSCAKE_TEST_CONTACT_GROUP_ID must be set for acceptance tests") 39 t.Fatal("STATUSCAKE_TEST_CONTACT_GROUP_ID must be set for acceptance tests")
40 } 40 }
41 if v := os.Getenv("STATUSCAKE_SSL_CONTACT_GROUP_ID"); v == "" {
42 t.Fatal("STATUSCAKE_SSL_CONTACT_GROUP_ID must be set for acceptance tests")
43 }
41} 44}
diff --git a/statuscake/resource_statuscakessl.go b/statuscake/resource_statuscakessl.go
new file mode 100644
index 0000000..3dba5bd
--- /dev/null
+++ b/statuscake/resource_statuscakessl.go
@@ -0,0 +1,283 @@
1package statuscake
2
3import (
4 "fmt"
5 "strconv"
6
7 "log"
8
9 "github.com/DreamItGetIT/statuscake"
10 "github.com/hashicorp/terraform/helper/schema"
11)
12
13func resourceStatusCakeSsl() *schema.Resource {
14 return &schema.Resource{
15 Create: CreateSsl,
16 Update: UpdateSsl,
17 Delete: DeleteSsl,
18 Read: ReadSsl,
19 Importer: &schema.ResourceImporter{
20 State: schema.ImportStatePassthrough,
21 },
22
23 Schema: map[string]*schema.Schema{
24 "ssl_id": {
25 Type: schema.TypeString,
26 Computed: true,
27 },
28
29 "domain": {
30 Type: schema.TypeString,
31 Required: true,
32 },
33
34 "contact_groups": {
35 Type: schema.TypeList,
36 Elem: &schema.Schema{Type: schema.TypeString},
37 Computed: true,
38 },
39
40 "contact_groups_c": {
41 Type: schema.TypeString,
42 Required: true,
43 },
44
45 "checkrate": {
46 Type: schema.TypeInt,
47 Required: true,
48 },
49
50 "alert_at": {
51 Type: schema.TypeString,
52 Required: true,
53 },
54
55 "alert_reminder": {
56 Type: schema.TypeBool,
57 Required: true,
58 },
59
60 "alert_expiry": {
61 Type: schema.TypeBool,
62 Required: true,
63 },
64
65 "alert_broken": {
66 Type: schema.TypeBool,
67 Required: true,
68 },
69
70 "alert_mixed": {
71 Type: schema.TypeBool,
72 Required: true,
73 },
74
75 "paused": {
76 Type: schema.TypeBool,
77 Computed: true,
78 },
79
80 "issuer_cn": {
81 Type: schema.TypeString,
82 Computed: true,
83 },
84
85 "cert_score": {
86 Type: schema.TypeString,
87 Computed: true,
88 },
89
90 "cipher_score": {
91 Type: schema.TypeString,
92 Computed: true,
93 },
94
95 "cert_status": {
96 Type: schema.TypeString,
97 Computed: true,
98 },
99
100 "cipher": {
101 Type: schema.TypeString,
102 Computed: true,
103 },
104
105 "valid_from_utc": {
106 Type: schema.TypeString,
107 Computed: true,
108 },
109
110 "valid_until_utc": {
111 Type: schema.TypeString,
112 Computed: true,
113 },
114
115 "mixed_content": {
116 Type: schema.TypeList,
117 Elem: &schema.Schema{
118 Type: schema.TypeMap,
119 Elem: &schema.Schema{Type: schema.TypeString},
120 },
121 Computed: true,
122 },
123
124 "flags": {
125 Type: schema.TypeMap,
126 Elem: &schema.Schema{Type: schema.TypeBool},
127 Computed: true,
128 },
129
130 "last_reminder": {
131 Type: schema.TypeInt,
132 Computed: true,
133 },
134
135 "last_updated_utc": {
136 Type: schema.TypeString,
137 Computed: true,
138 },
139 },
140 }
141}
142
143func CreateSsl(d *schema.ResourceData, meta interface{}) error {
144 client := meta.(*statuscake.Client)
145
146 newSsl := &statuscake.PartialSsl{
147 Domain: d.Get("domain").(string),
148 Checkrate: strconv.Itoa(d.Get("checkrate").(int)),
149 ContactGroupsC: d.Get("contact_groups_c").(string),
150 AlertReminder: d.Get("alert_reminder").(bool),
151 AlertExpiry: d.Get("alert_expiry").(bool),
152 AlertBroken: d.Get("alert_broken").(bool),
153 AlertMixed: d.Get("alert_mixed").(bool),
154 AlertAt: d.Get("alert_at").(string),
155 }
156
157 log.Printf("[DEBUG] Creating new StatusCake Ssl: %s", d.Get("domain").(string))
158
159 response, err := statuscake.NewSsls(client).Create(newSsl)
160 if err != nil {
161 fmt.Println(newSsl)
162 fmt.Println(client)
163 return fmt.Errorf("Error creating StatusCake Ssl: %s", err.Error())
164 }
165
166 d.Set("ssl_id", response.ID)
167 d.Set("contact_groups", response.ContactGroups)
168 d.Set("paused", response.Paused)
169 d.Set("issuer_cn", response.IssuerCn)
170 d.Set("cert_score", response.CertScore)
171 d.Set("cipher_score", response.CipherScore)
172 d.Set("cert_status", response.CertStatus)
173 d.Set("cipher", response.Cipher)
174 d.Set("valid_from_utc", response.ValidFromUtc)
175 d.Set("valid_until_utc", response.ValidUntilUtc)
176 d.Set("mixed_content", response.MixedContent)
177 d.Set("flags", response.Flags)
178 d.Set("last_reminder", response.LastReminder)
179 d.Set("last_updated_utc", response.LastUpdatedUtc)
180 d.SetId(response.ID)
181
182 return ReadSsl(d, meta)
183}
184
185func UpdateSsl(d *schema.ResourceData, meta interface{}) error {
186 client := meta.(*statuscake.Client)
187
188 params := getStatusCakeSslInput(d)
189
190 log.Printf("[DEBUG] StatusCake Ssl Update for %s", d.Id())
191 _, err := statuscake.NewSsls(client).Update(params)
192 if err != nil {
193 return fmt.Errorf("Error Updating StatusCake Ssl: %s", err.Error())
194 }
195 return nil
196}
197
198func DeleteSsl(d *schema.ResourceData, meta interface{}) error {
199 client := meta.(*statuscake.Client)
200
201 log.Printf("[DEBUG] Deleting StatusCake Ssl: %s", d.Id())
202 err := statuscake.NewSsls(client).Delete(d.Id())
203
204 return err
205}
206
207func ReadSsl(d *schema.ResourceData, meta interface{}) error {
208 client := meta.(*statuscake.Client)
209
210 response, err := statuscake.NewSsls(client).Detail(d.Id())
211 if err != nil {
212 return fmt.Errorf("Error Getting StatusCake Ssl Details for %s: Error: %s", d.Id(), err)
213 }
214 d.Set("domain", response.Domain)
215 d.Set("checkrate", response.Checkrate)
216 d.Set("contact_groups_c", response.ContactGroupsC)
217 d.Set("alert_reminder", response.AlertReminder)
218 d.Set("alert_expiry", response.AlertExpiry)
219 d.Set("alert_broken", response.AlertBroken)
220 d.Set("alert_mixed", response.AlertMixed)
221 d.Set("alert_at", response.AlertAt)
222 d.Set("ssl_id", response.ID)
223 d.Set("contact_groups", response.ContactGroups)
224 d.Set("paused", response.Paused)
225 d.Set("issuer_cn", response.IssuerCn)
226 d.Set("cert_score", response.CertScore)
227 d.Set("cipher_score", response.CipherScore)
228 d.Set("cert_status", response.CertStatus)
229 d.Set("cipher", response.Cipher)
230 d.Set("valid_from_utc", response.ValidFromUtc)
231 d.Set("valid_until_utc", response.ValidUntilUtc)
232 d.Set("mixed_content", response.MixedContent)
233 d.Set("flags", response.Flags)
234 d.Set("last_reminder", response.LastReminder)
235 d.Set("last_updated_utc", response.LastUpdatedUtc)
236 d.SetId(response.ID)
237
238 return nil
239}
240
241func getStatusCakeSslInput(d *schema.ResourceData) *statuscake.PartialSsl {
242 sslId, parseErr := strconv.Atoi(d.Id())
243 if parseErr != nil {
244 log.Printf("[DEBUG] Error Parsing StatusCake Id: %s", d.Id())
245 }
246 ssl := &statuscake.PartialSsl{
247 ID: sslId,
248 }
249
250 if v, ok := d.GetOk("domain"); ok {
251 ssl.Domain = v.(string)
252 }
253
254 if v, ok := d.GetOk("checkrate"); ok {
255 ssl.Checkrate = strconv.Itoa(v.(int))
256 }
257
258 if v, ok := d.GetOk("contact_groups_c"); ok {
259 ssl.ContactGroupsC = v.(string)
260 }
261
262 if v, ok := d.GetOk("alert_reminder"); ok {
263 ssl.AlertReminder = v.(bool)
264 }
265
266 if v, ok := d.GetOk("alert_expiry"); ok {
267 ssl.AlertExpiry = v.(bool)
268 }
269
270 if v, ok := d.GetOk("alert_broken"); ok {
271 ssl.AlertBroken = v.(bool)
272 }
273
274 if v, ok := d.GetOk("alert_mixed"); ok {
275 ssl.AlertMixed = v.(bool)
276 }
277
278 if v, ok := d.GetOk("alert_at"); ok {
279 ssl.AlertAt = v.(string)
280 }
281
282 return ssl
283}
diff --git a/statuscake/resource_statuscakessl_test.go b/statuscake/resource_statuscakessl_test.go
new file mode 100644
index 0000000..a9a83da
--- /dev/null
+++ b/statuscake/resource_statuscakessl_test.go
@@ -0,0 +1,222 @@
1package statuscake
2
3import (
4 "fmt"
5 "os"
6 "strconv"
7 "testing"
8
9 "github.com/DreamItGetIT/statuscake"
10 "github.com/hashicorp/terraform/helper/resource"
11 "github.com/hashicorp/terraform/terraform"
12)
13
14func TestAccStatusCakeSsl_basic(t *testing.T) {
15 var ssl statuscake.Ssl
16
17 resource.Test(t, resource.TestCase{
18 PreCheck: func() { testAccPreCheck(t) },
19 Providers: testAccProviders,
20 CheckDestroy: testAccSslCheckDestroy(&ssl),
21 Steps: []resource.TestStep{
22 {
23 Config: interpolateTerraformTemplateSsl(testAccSslConfig_basic),
24 Check: resource.ComposeTestCheckFunc(
25 testAccSslCheckExists("statuscake_ssl.exemple", &ssl),
26 testAccSslCheckAttributes("statuscake_ssl.exemple", &ssl),
27 ),
28 },
29 },
30 })
31}
32
33func TestAccStatusCakeSsl_withUpdate(t *testing.T) {
34 var ssl statuscake.Ssl
35
36 resource.Test(t, resource.TestCase{
37 PreCheck: func() { testAccPreCheck(t) },
38 Providers: testAccProviders,
39 CheckDestroy: testAccSslCheckDestroy(&ssl),
40 Steps: []resource.TestStep{
41 {
42 Config: interpolateTerraformTemplateSsl(testAccSslConfig_basic),
43 Check: resource.ComposeTestCheckFunc(
44 testAccSslCheckExists("statuscake_ssl.exemple", &ssl),
45 testAccSslCheckAttributes("statuscake_ssl.exemple", &ssl),
46 ),
47 },
48
49 {
50 Config: testAccSslConfig_update,
51 Check: resource.ComposeTestCheckFunc(
52 testAccSslCheckExists("statuscake_ssl.exemple", &ssl),
53 testAccSslCheckAttributes("statuscake_ssl.exemple", &ssl),
54 resource.TestCheckResourceAttr("statuscake_ssl.exemple", "check_rate", "86400"),
55 resource.TestCheckResourceAttr("statuscake_ssl.exemple", "domain", "https://www.exemple.com"),
56 resource.TestCheckResourceAttr("statuscake_ssl.exemple", "contact_group_c", ""),
57 resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_at", "18,8,2019"),
58 resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_reminder", "false"),
59 resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_expiry", "false"),
60 resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_broken", "true"),
61 resource.TestCheckResourceAttr("statuscake_ssl.exemple", "alert_mixed", "false"),
62 ),
63 },
64 },
65 })
66}
67
68func testAccSslCheckExists(rn string, ssl *statuscake.Ssl) resource.TestCheckFunc {
69 return func(s *terraform.State) error {
70 rs, ok := s.RootModule().Resources[rn]
71 if !ok {
72 return fmt.Errorf("resource not found: %s", rn)
73 }
74
75 if rs.Primary.ID == "" {
76 return fmt.Errorf("SslID not set")
77 }
78
79 client := testAccProvider.Meta().(*statuscake.Client)
80 sslId := rs.Primary.ID
81
82 gotSsl, err := statuscake.NewSsls(client).Detail(sslId)
83 if err != nil {
84 return fmt.Errorf("error getting ssl: %s", err)
85 }
86
87 *ssl = *gotSsl
88
89 return nil
90 }
91}
92
93func testAccSslCheckAttributes(rn string, ssl *statuscake.Ssl) resource.TestCheckFunc {
94 return func(s *terraform.State) error {
95 attrs := s.RootModule().Resources[rn].Primary.Attributes
96
97 check := func(key, stateValue, sslValue string) error {
98 if sslValue != stateValue {
99 return fmt.Errorf("different values for %s in state (%s) and in statuscake (%s)",
100 key, stateValue, sslValue)
101 }
102 return nil
103 }
104
105 for key, value := range attrs {
106 var err error
107
108 switch key {
109 case "domain":
110 err = check(key, value, ssl.Domain)
111 case "contact_groups_c":
112 err = check(key, value, ssl.ContactGroupsC)
113 case "checkrate":
114 err = check(key, value, strconv.Itoa(ssl.Checkrate))
115 case "alert_at":
116 err = check(key, value, ssl.AlertAt)
117 case "alert_reminder":
118 err = check(key, value, strconv.FormatBool(ssl.AlertReminder))
119 case "alert_expiry":
120 err = check(key, value, strconv.FormatBool(ssl.AlertExpiry))
121 case "alert_broken":
122 err = check(key, value, strconv.FormatBool(ssl.AlertBroken))
123 case "alert_mixed":
124 err = check(key, value, strconv.FormatBool(ssl.AlertMixed))
125 case "paused":
126 err = check(key, value, strconv.FormatBool(ssl.Paused))
127 case "issuer_cn":
128 err = check(key, value, ssl.IssuerCn)
129 case "contact_groups":
130 for _, tv := range ssl.ContactGroups {
131 err = check(key, value, tv)
132 if err != nil {
133 return err
134 }
135 }
136 case "cert_score":
137 err = check(key, value, ssl.CertScore)
138 case "cert_status":
139 err = check(key, value, ssl.CertStatus)
140 case "cipher":
141 err = check(key, value, ssl.Cipher)
142 case "valid_from_utc":
143 err = check(key, value, ssl.ValidFromUtc)
144 case "valid_until_utc":
145 err = check(key, value, ssl.ValidUntilUtc)
146 case "last_reminder":
147 err = check(key, value, strconv.Itoa(ssl.LastReminder))
148 case "last_updated_utc":
149 err = check(key, value, ssl.LastUpdatedUtc)
150 case "flags":
151 for _, tv := range ssl.Flags {
152 err = check(key, value, strconv.FormatBool(tv))
153 if err != nil {
154 return err
155 }
156 }
157
158 case "mixed_content":
159 for _, tv := range ssl.MixedContent {
160 for _, tv2 := range tv {
161 err = check(key, value, tv2)
162 if err != nil {
163 return err
164 }
165 }
166 }
167 }
168 if err != nil {
169 return err
170 }
171 }
172 return nil
173 }
174}
175
176func testAccSslCheckDestroy(ssl *statuscake.Ssl) resource.TestCheckFunc {
177 return func(s *terraform.State) error {
178 client := testAccProvider.Meta().(*statuscake.Client)
179 _, err := statuscake.NewSsls(client).Detail(ssl.ID)
180 if err == nil {
181 return fmt.Errorf("ssl still exists")
182 }
183
184 return nil
185 }
186}
187
188func interpolateTerraformTemplateSsl(template string) string {
189 sslContactGroupId := "43402"
190
191 if v := os.Getenv("STATUSCAKE_SSL_CONTACT_GROUP_ID"); v != "" {
192 sslContactGroupId = v
193 }
194
195 return fmt.Sprintf(template, sslContactGroupId)
196}
197
198const testAccSslConfig_basic = `
199resource "statuscake_ssl" "exemple" {
200 domain = "https://www.exemple.com"
201 contact_groups_c = "%s"
202 checkrate = 3600
203 alert_at = "18,7,2019"
204 alert_reminder = true
205 alert_expiry = true
206 alert_broken = false
207 alert_mixed = true
208}
209`
210
211const testAccSslConfig_update = `
212resource "statuscake_ssl" "exemple" {
213 domain = "https://www.exemple.com"
214 contact_groups_c = ""
215 checkrate = 86400
216 alert_at = "18,8,2019"
217 alert_reminder = false
218 alert_expiry = false
219 alert_broken = true
220 alert_mixed = false
221}
222`