Skip to content

Commit 686c77e

Browse files
authored
Merge pull request #143 from StatusCakeDev/fix-heartbeat-monitoring-resource
fix(heartbeat): move check URL to top level of resource
2 parents 1541d16 + 6c12c77 commit 686c77e

File tree

8 files changed

+67
-56
lines changed

8 files changed

+67
-56
lines changed

docs/resources/heartbeat_check.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ description: |-
1717

1818
### Required
1919

20-
- `monitored_resource` (Block List, Min: 1, Max: 1) Monitored resource configuration block. The describes server under test (see [below for nested schema](#nestedblock--monitored_resource))
2120
- `name` (String) Name of the check
2221
- `period` (Number) Number of seconds since the last ping before the check is considered down.
2322

2423
### Optional
2524

2625
- `contact_groups` (Set of String) List of contact group IDs
26+
- `monitored_resource` (Block List, Max: 1) Monitored resource configuration block. This describes the server under test (see [below for nested schema](#nestedblock--monitored_resource))
2727
- `paused` (Boolean) Whether the check should be run
2828
- `tags` (Set of String) List of tags
2929

3030
### Read-Only
3131

32+
- `check_url` (String) URL of the heartbeat check
3233
- `id` (String) The ID of this resource.
3334

3435
<a id="nestedblock--monitored_resource"></a>
@@ -38,10 +39,6 @@ Optional:
3839

3940
- `host` (String) Name of the hosting provider
4041

41-
Read-Only:
42-
43-
- `address` (String) URL of the heartbeat check
44-
4542
## Import
4643

4744
Heartbeat checks can be imported using the check `id`, e.g.

docs/resources/pagespeed_check.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ output "example_com_pagespeed_check_id" {
4343

4444
- `alert_config` (Block List, Min: 1, Max: 1) Alert configuration block. Omitting this block disabled all alerts (see [below for nested schema](#nestedblock--alert_config))
4545
- `check_interval` (Number) Number of seconds between checks
46-
- `monitored_resource` (Block List, Min: 1, Max: 1) Monitored resource configuration block. The describes server under test (see [below for nested schema](#nestedblock--monitored_resource))
46+
- `monitored_resource` (Block List, Min: 1, Max: 1) Monitored resource configuration block. This describes the server under test (see [below for nested schema](#nestedblock--monitored_resource))
4747
- `name` (String) Name of the check
4848
- `region` (String) Region on which to run checks
4949

docs/resources/ssl_check.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ output "example_com_ssl_check_id" {
4747

4848
- `alert_config` (Block List, Min: 1, Max: 1) Alert configuration block (see [below for nested schema](#nestedblock--alert_config))
4949
- `check_interval` (Number) Number of seconds between checks
50-
- `monitored_resource` (Block List, Min: 1, Max: 1) Monitored resource configuration block. The describes server under test (see [below for nested schema](#nestedblock--monitored_resource))
50+
- `monitored_resource` (Block List, Min: 1, Max: 1) Monitored resource configuration block. This describes the server under test (see [below for nested schema](#nestedblock--monitored_resource))
5151

5252
### Optional
5353

docs/resources/uptime_check.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ output "example_com_uptime_check_id" {
7878
### Required
7979

8080
- `check_interval` (Number) Number of seconds between checks
81-
- `monitored_resource` (Block List, Min: 1, Max: 1) Monitored resource configuration block. The describes server under test (see [below for nested schema](#nestedblock--monitored_resource))
81+
- `monitored_resource` (Block List, Min: 1, Max: 1) Monitored resource configuration block. This describes the server under test (see [below for nested schema](#nestedblock--monitored_resource))
8282
- `name` (String) Name of the check
8383

8484
### Optional

internal/provider/resource_heartbeat_check.go

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ func resourceStatusCakeHeartbeatCheck() *schema.Resource {
2828
},
2929

3030
Schema: map[string]*schema.Schema{
31-
"period": &schema.Schema{
32-
Type: schema.TypeInt,
33-
Required: true,
34-
Description: "Number of seconds since the last ping before the check is considered down.",
35-
ValidateFunc: validation.IntBetween(30, 172800),
31+
"check_url": &schema.Schema{
32+
Type: schema.TypeString,
33+
Computed: true,
34+
Description: "URL of the heartbeat check",
3635
},
3736
"contact_groups": &schema.Schema{
3837
Type: schema.TypeSet,
@@ -45,16 +44,11 @@ func resourceStatusCakeHeartbeatCheck() *schema.Resource {
4544
},
4645
"monitored_resource": &schema.Schema{
4746
Type: schema.TypeList,
48-
Required: true,
47+
Optional: true,
4948
MaxItems: 1,
50-
Description: "Monitored resource configuration block. The describes server under test",
49+
Description: "Monitored resource configuration block. This describes the server under test",
5150
Elem: &schema.Resource{
5251
Schema: map[string]*schema.Schema{
53-
"address": &schema.Schema{
54-
Type: schema.TypeString,
55-
Computed: true,
56-
Description: "URL of the heartbeat check",
57-
},
5852
"host": &schema.Schema{
5953
Type: schema.TypeString,
6054
Optional: true,
@@ -76,6 +70,12 @@ func resourceStatusCakeHeartbeatCheck() *schema.Resource {
7670
Default: false,
7771
Description: "Whether the check should be run",
7872
},
73+
"period": &schema.Schema{
74+
Type: schema.TypeInt,
75+
Required: true,
76+
Description: "Number of seconds since the last ping before the check is considered down.",
77+
ValidateFunc: validation.IntBetween(30, 172800),
78+
},
7979
"tags": &schema.Schema{
8080
Type: schema.TypeSet,
8181
Optional: true,
@@ -93,13 +93,6 @@ func resourceStatusCakeHeartbeatCheckCreate(ctx context.Context, d *schema.Resou
9393
client := meta.(*statuscake.Client)
9494
body := make(map[string]interface{})
9595

96-
period, err := expandHeartbeatCheckPeriod(d.Get("period"), d)
97-
if err != nil {
98-
return diag.FromErr(err)
99-
} else if d.HasChange("period") {
100-
body["period"] = period
101-
}
102-
10396
contactGroups, err := expandHeartbeatCheckContactGroups(d.Get("contact_groups"), d)
10497
if err != nil {
10598
return diag.FromErr(err)
@@ -128,6 +121,13 @@ func resourceStatusCakeHeartbeatCheckCreate(ctx context.Context, d *schema.Resou
128121
body["paused"] = paused
129122
}
130123

124+
period, err := expandHeartbeatCheckPeriod(d.Get("period"), d)
125+
if err != nil {
126+
return diag.FromErr(err)
127+
} else if d.HasChange("period") {
128+
body["period"] = period
129+
}
130+
131131
tags, err := expandHeartbeatCheckTags(d.Get("tags"), d)
132132
if err != nil {
133133
return diag.FromErr(err)
@@ -161,10 +161,6 @@ func resourceStatusCakeHeartbeatCheckRead(ctx context.Context, d *schema.Resourc
161161
return diag.Errorf("failed to get heartbeat check with ID: %s", err)
162162
}
163163

164-
if err := d.Set("period", flattenHeartbeatCheckPeriod(res.Data.Period, d)); err != nil {
165-
return diag.Errorf("failed to read period: %s", err)
166-
}
167-
168164
if err := d.Set("contact_groups", flattenHeartbeatCheckContactGroups(res.Data.ContactGroups, d)); err != nil {
169165
return diag.Errorf("failed to read contact groups: %s", err)
170166
}
@@ -181,6 +177,14 @@ func resourceStatusCakeHeartbeatCheckRead(ctx context.Context, d *schema.Resourc
181177
return diag.Errorf("failed to read paused: %s", err)
182178
}
183179

180+
if err := d.Set("period", flattenHeartbeatCheckPeriod(res.Data.Period, d)); err != nil {
181+
return diag.Errorf("failed to read period: %s", err)
182+
}
183+
184+
if err := d.Set("check_url", flattenHeartbeatCheckURL(res.Data.WebsiteURL, d)); err != nil {
185+
return diag.Errorf("failed to read check URL: %s", err)
186+
}
187+
184188
if err := d.Set("tags", flattenHeartbeatCheckTags(res.Data.Tags, d)); err != nil {
185189
return diag.Errorf("failed to read tags: %s", err)
186190
}
@@ -193,13 +197,6 @@ func resourceStatusCakeHeartbeatCheckUpdate(ctx context.Context, d *schema.Resou
193197
body := make(map[string]interface{})
194198
id := d.Id()
195199

196-
period, err := expandHeartbeatCheckPeriod(d.Get("period"), d)
197-
if err != nil {
198-
return diag.FromErr(err)
199-
} else if d.HasChange("period") {
200-
body["period"] = period
201-
}
202-
203200
contactGroups, err := expandHeartbeatCheckContactGroups(d.Get("contact_groups"), d)
204201
if err != nil {
205202
return diag.FromErr(err)
@@ -228,6 +225,13 @@ func resourceStatusCakeHeartbeatCheckUpdate(ctx context.Context, d *schema.Resou
228225
body["paused"] = paused
229226
}
230227

228+
period, err := expandHeartbeatCheckPeriod(d.Get("period"), d)
229+
if err != nil {
230+
return diag.FromErr(err)
231+
} else if d.HasChange("period") {
232+
body["period"] = period
233+
}
234+
231235
tags, err := expandHeartbeatCheckTags(d.Get("tags"), d)
232236
if err != nil {
233237
return diag.FromErr(err)
@@ -258,14 +262,6 @@ func resourceStatusCakeHeartbeatCheckDelete(ctx context.Context, d *schema.Resou
258262
return nil
259263
}
260264

261-
func expandHeartbeatCheckAddress(v interface{}, d *schema.ResourceData) (interface{}, error) {
262-
return v.(string), nil
263-
}
264-
265-
func flattenHeartbeatCheckAddress(v interface{}, d *schema.ResourceData) interface{} {
266-
return v
267-
}
268-
269265
func expandHeartbeatCheckContactGroups(v interface{}, d *schema.ResourceData) (interface{}, error) {
270266
return convertStringSet(v.(*schema.Set)), nil
271267
}
@@ -286,7 +282,16 @@ func expandHeartbeatCheckMonitoredResource(v interface{}, d *schema.ResourceData
286282
l := v.([]interface{})
287283

288284
if len(l) == 0 || l[0] == nil {
289-
return map[string]interface{}{}, nil
285+
// If the monitored resource is not set then return an empty map. This is
286+
// necessary for the Heartbeat API only because the monitored_resource block
287+
// is optional. Therefore when the entire block is removed then the "host"
288+
// field is not set.
289+
//
290+
// At present this causes the API to return an error. This is a bug in the
291+
// API and does have a fix ready to go.
292+
return map[string]interface{}{
293+
"host": "",
294+
}, nil
290295
}
291296

292297
original := l[0].(map[string]interface{})
@@ -304,10 +309,15 @@ func expandHeartbeatCheckMonitoredResource(v interface{}, d *schema.ResourceData
304309

305310
func flattenHeartbeatCheckMonitoredResource(v interface{}, d *schema.ResourceData) interface{} {
306311
data := v.(statuscake.HeartbeatTest)
312+
313+
host := flattenHeartbeatCheckHost(data.Host, d)
314+
if !isValid(host) {
315+
return nil
316+
}
317+
307318
return []map[string]interface{}{
308319
map[string]interface{}{
309-
"address": flattenHeartbeatCheckAddress(data.WebsiteURL, d),
310-
"host": flattenHeartbeatCheckHost(data.Host, d),
320+
"host": host,
311321
},
312322
}
313323
}
@@ -329,13 +339,17 @@ func flattenHeartbeatCheckPaused(v interface{}, d *schema.ResourceData) interfac
329339
}
330340

331341
func expandHeartbeatCheckPeriod(v interface{}, d *schema.ResourceData) (interface{}, error) {
332-
return v.(int), nil
342+
return int32(v.(int)), nil
333343
}
334344

335345
func flattenHeartbeatCheckPeriod(v interface{}, d *schema.ResourceData) interface{} {
336346
return v
337347
}
338348

349+
func flattenHeartbeatCheckURL(v interface{}, d *schema.ResourceData) interface{} {
350+
return v
351+
}
352+
339353
func expandHeartbeatCheckTags(v interface{}, d *schema.ResourceData) (interface{}, error) {
340354
return convertStringSet(v.(*schema.Set)), nil
341355
}

internal/provider/resource_pagespeed_check.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func resourceStatusCakePagespeedCheck() *schema.Resource {
8383
Type: schema.TypeList,
8484
Required: true,
8585
MaxItems: 1,
86-
Description: "Monitored resource configuration block. The describes server under test",
86+
Description: "Monitored resource configuration block. This describes the server under test",
8787
Elem: &schema.Resource{
8888
Schema: map[string]*schema.Schema{
8989
"address": &schema.Schema{

internal/provider/resource_ssl_check.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func resourceStatusCakeSSLCheck() *schema.Resource {
9898
Type: schema.TypeList,
9999
Required: true,
100100
MaxItems: 1,
101-
Description: "Monitored resource configuration block. The describes server under test",
101+
Description: "Monitored resource configuration block. This describes the server under test",
102102
Elem: &schema.Resource{
103103
Schema: map[string]*schema.Schema{
104104
"address": &schema.Schema{
@@ -178,7 +178,7 @@ func resourceStatusCakeSSLCheckCreate(ctx context.Context, d *schema.ResourceDat
178178
body["paused"] = paused
179179
}
180180

181-
userAgent, err := expandSSLCheckHostname(d.Get("user_agent"), d)
181+
userAgent, err := expandSSLCheckUserAgent(d.Get("user_agent"), d)
182182
if err != nil {
183183
return diag.FromErr(err)
184184
} else if d.HasChange("user_agent") {
@@ -290,7 +290,7 @@ func resourceStatusCakeSSLCheckUpdate(ctx context.Context, d *schema.ResourceDat
290290
body["paused"] = paused
291291
}
292292

293-
userAgent, err := expandSSLCheckHostname(d.Get("user_agent"), d)
293+
userAgent, err := expandSSLCheckUserAgent(d.Get("user_agent"), d)
294294
if err != nil {
295295
return diag.FromErr(err)
296296
} else if d.HasChange("user_agent") {

internal/provider/resource_uptime_check.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func resourceStatusCakeUptimeCheck() *schema.Resource {
232232
MaxItems: 1,
233233
Description: "ICMP check configuration block. Only one of `dns_check`, `http_check`, `icmp_check`, and `tcp_check` may be specified",
234234
// There are no special fields for an ICMP check. All that is required
235-
// is the address which is supplied in the `monitoried_resource` block.
235+
// is the address which is supplied in the `monitored_resource` block.
236236
Elem: &schema.Resource{
237237
Schema: map[string]*schema.Schema{
238238
"enabled": &schema.Schema{
@@ -257,7 +257,7 @@ func resourceStatusCakeUptimeCheck() *schema.Resource {
257257
Type: schema.TypeList,
258258
Required: true,
259259
MaxItems: 1,
260-
Description: "Monitored resource configuration block. The describes server under test",
260+
Description: "Monitored resource configuration block. This describes the server under test",
261261
Elem: &schema.Resource{
262262
Schema: map[string]*schema.Schema{
263263
"address": &schema.Schema{

0 commit comments

Comments
 (0)