Skip to content

Commit 477eaff

Browse files
committed
fix: check 404 in all resource Read handlers and remove resource if needed
1 parent 555fb98 commit 477eaff

12 files changed

+72
-11
lines changed

internal/provider/alert_channel_resource.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,13 @@ func (r *AlertChannelResource) Read(
341341
return
342342
}
343343

344-
// TODO: Check if we really have to do the weird 404 handling
345344
realizedModel, err := r.client.GetAlertChannel(ctx, id)
346345
if err != nil {
346+
if SDKIsHTTPNotFoundError(err) {
347+
resp.State.RemoveResource(ctx)
348+
return
349+
}
350+
347351
resp.Diagnostics.AddError(
348352
"Error Reading Checkly Alert Channel",
349353
fmt.Sprintf("Could not retrieve alert channel, unexpected error: %s", err),

internal/provider/check_group_resource.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,13 @@ func (r *CheckGroupResource) Read(
234234
return
235235
}
236236

237-
// TODO: Check if we really have to do the weird 404 handling
238237
realizedModel, err := r.client.GetGroup(ctx, id)
239238
if err != nil {
239+
if SDKIsHTTPNotFoundError(err) {
240+
resp.State.RemoveResource(ctx)
241+
return
242+
}
243+
240244
resp.Diagnostics.AddError(
241245
"Error Reading Checkly Check Group",
242246
fmt.Sprintf("Could not retrieve check group, unexpected error: %s", err),

internal/provider/check_resource.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,13 @@ func (r *CheckResource) Read(
300300
return
301301
}
302302

303-
// TODO: Check if we really have to do the weird 404 handling
304303
realizedModel, err := r.client.GetCheck(ctx, state.ID.ValueString())
305304
if err != nil {
305+
if SDKIsHTTPNotFoundError(err) {
306+
resp.State.RemoveResource(ctx)
307+
return
308+
}
309+
306310
resp.Diagnostics.AddError(
307311
"Error Reading Checkly Check",
308312
fmt.Sprintf("Could not retrieve check, unexpected error: %s", err),

internal/provider/dashboard_resource.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,13 @@ func (r *DashboardResource) Read(
241241
return
242242
}
243243

244-
// TODO: Check if we really have to do the weird 404 handling
245244
realizedModel, err := r.client.GetDashboard(ctx, state.ID.ValueString())
246245
if err != nil {
246+
if SDKIsHTTPNotFoundError(err) {
247+
resp.State.RemoveResource(ctx)
248+
return
249+
}
250+
247251
resp.Diagnostics.AddError(
248252
"Error Reading Checkly Dashboard",
249253
fmt.Sprintf("Could not retrieve dashboard, unexpected error: %s", err),

internal/provider/environment_variable_resource.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,16 @@ func (r *EnvironmentVariableResource) Read(
170170
return
171171
}
172172

173-
// TODO: Check if we really have to do the weird 404 handling
174173
realizedModel, err := r.client.GetEnvironmentVariable(
175174
ctx,
176175
state.ID.ValueString(),
177176
)
178177
if err != nil {
178+
if SDKIsHTTPNotFoundError(err) {
179+
resp.State.RemoveResource(ctx)
180+
return
181+
}
182+
179183
resp.Diagnostics.AddError(
180184
"Error Reading Checkly Environment Variable",
181185
fmt.Sprintf("Could not retrieve environment variable, unexpected error: %s", err),

internal/provider/heartbeat_resource.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,13 @@ func (r *HeartbeatResource) Read(
216216
return
217217
}
218218

219-
// TODO: Check if we really have to do the weird 404 handling
220219
realizedModel, err := r.client.GetHeartbeatCheck(ctx, state.ID.ValueString())
221220
if err != nil {
221+
if SDKIsHTTPNotFoundError(err) {
222+
resp.State.RemoveResource(ctx)
223+
return
224+
}
225+
222226
resp.Diagnostics.AddError(
223227
"Error Reading Checkly Heartbeat",
224228
fmt.Sprintf("Could not retrieve heartbeat, unexpected error: %s", err),

internal/provider/maintenance_windows_resource.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,13 @@ func (r *MaintenanceWindowsResource) Read(
194194
return
195195
}
196196

197-
// TODO: Check if we really have to do the weird 404 handling
198197
realizedModel, err := r.client.GetMaintenanceWindow(ctx, id)
199198
if err != nil {
199+
if SDKIsHTTPNotFoundError(err) {
200+
resp.State.RemoveResource(ctx)
201+
return
202+
}
203+
200204
resp.Diagnostics.AddError(
201205
"Error Reading Checkly Maintenance Window",
202206
fmt.Sprintf("Could not retrieve maintenance window, unexpected error: %s", err),

internal/provider/private_location_resource.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,13 @@ func (r *PrivateLocationResource) Read(
168168
return
169169
}
170170

171-
// TODO: Check if we really have to do the weird 404 handling
172171
realizedModel, err := r.client.GetPrivateLocation(ctx, state.ID.ValueString())
173172
if err != nil {
173+
if SDKIsHTTPNotFoundError(err) {
174+
resp.State.RemoveResource(ctx)
175+
return
176+
}
177+
174178
resp.Diagnostics.AddError(
175179
"Error Reading Checkly Private Location",
176180
fmt.Sprintf("Could not retrieve private location, unexpected error: %s", err),

internal/provider/sdk.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provider
33
import (
44
"fmt"
55
"strconv"
6+
"strings"
67

78
"github.com/hashicorp/terraform-plugin-framework/attr"
89
"github.com/hashicorp/terraform-plugin-framework/diag"
@@ -84,3 +85,19 @@ func SDKKeyValuesIntoMap(values *[]checkly.KeyValue) types.Map {
8485

8586
return types.MapValueMust(types.StringType, mapValues)
8687
}
88+
89+
func SDKIsHTTPNotFoundError(err error) bool {
90+
// Unfortunately the SDK presents HTTP errors in a completely unusable way,
91+
// forcing us to match against string values.
92+
msg := err.Error()
93+
94+
switch {
95+
case strings.Contains(msg, "unexpected response status: 404"):
96+
return true
97+
// Unfortunate inconsistency.
98+
case strings.Contains(msg, "unexpected response status 404"):
99+
return true
100+
}
101+
102+
return false
103+
}

internal/provider/snippet_resource.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,13 @@ func (r *SnippetResource) Read(
169169
return
170170
}
171171

172-
// TODO: Check if we really have to do the weird 404 handling
173172
realizedModel, err := r.client.GetSnippet(ctx, id)
174173
if err != nil {
174+
if SDKIsHTTPNotFoundError(err) {
175+
resp.State.RemoveResource(ctx)
176+
return
177+
}
178+
175179
resp.Diagnostics.AddError(
176180
"Error Reading Checkly Snippet",
177181
fmt.Sprintf("Could not retrieve snippet, unexpected error: %s", err),

0 commit comments

Comments
 (0)