Skip to content

Commit

Permalink
handle 404s and remove from state if it happens (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
greatestusername authored Feb 26, 2024
1 parent c326ee8 commit 7671546
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
7 changes: 6 additions & 1 deletion synthetics/resource_api_check_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,12 @@ func resourceApiCheckV2Read(ctx context.Context, d *schema.ResourceData, meta in
return diag.FromErr(err)
}

o, _, err := c.GetApiCheckV2(checkID)
o, r, err := c.GetApiCheckV2(checkID)
if err != nil && (err.Error() == "Status Code: 404 Not Found" || r.StatusCode == 0) {
d.SetId("")
log.Println("[WARN] Resource exists in state but not in API. Removing resource from state.")
return diags
}
if err != nil {
return diag.FromErr(err)
}
Expand Down
7 changes: 6 additions & 1 deletion synthetics/resource_browser_check_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,12 @@ func resourceBrowserCheckV2Read(ctx context.Context, d *schema.ResourceData, met
return diag.FromErr(err)
}

o, _, err := c.GetBrowserCheckV2(checkID)
o, r, err := c.GetBrowserCheckV2(checkID)
if err != nil && (err.Error() == "Status Code: 404 Not Found" || r.StatusCode == 0) {
d.SetId("")
log.Println("[WARN] Resource exists in state but not in API. Removing resource from state.")
return diags
}
if err != nil {
return diag.FromErr(err)
}
Expand Down
7 changes: 6 additions & 1 deletion synthetics/resource_http_check_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,12 @@ func resourceHttpCheckV2Read(ctx context.Context, d *schema.ResourceData, meta i
return diag.FromErr(err)
}

o, _, err := c.GetHttpCheckV2(checkID)
o, r, err := c.GetHttpCheckV2(checkID)
if err != nil && (err.Error() == "Status Code: 404 Not Found" || r.StatusCode == 0) {
d.SetId("")
log.Println("[WARN] Resource exists in state but not in API. Removing resource from state.")
return diags
}
if err != nil {
return diag.FromErr(err)
}
Expand Down
7 changes: 6 additions & 1 deletion synthetics/resource_location_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ func resourceLocationV2Read(ctx context.Context, d *schema.ResourceData, meta in

var locationID = d.Id()

location, _, err := c.GetLocationV2(locationID)
location, r, err := c.GetLocationV2(locationID)
if err != nil && (err.Error() == "Status Code: 404 Not Found" || r.StatusCode == 0) {
d.SetId("")
log.Println("[WARN] Resource exists in state but not in API. Removing resource from state.")
return diags
}
if err != nil {
return diag.FromErr(err)
}
Expand Down
7 changes: 6 additions & 1 deletion synthetics/resource_port_check_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@ func resourcePortCheckV2Read(ctx context.Context, d *schema.ResourceData, meta i
return diag.FromErr(err)
}

o, _, err := c.GetPortCheckV2(checkID)
o, r, err := c.GetPortCheckV2(checkID)
if err != nil && (err.Error() == "Status Code: 404 Not Found" || r.StatusCode == 0) {
d.SetId("")
log.Println("[WARN] Resource exists in state but not in API. Removing resource from state.")
return diags
}
if err != nil {
return diag.FromErr(err)
}
Expand Down
7 changes: 6 additions & 1 deletion synthetics/resource_variable_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ func resourceVariableV2Read(ctx context.Context, d *schema.ResourceData, meta in
return diag.FromErr(err)
}

variable, _, err := c.GetVariableV2(variableID)
variable, r, err := c.GetVariableV2(variableID)
if err != nil && (err.Error() == "Status Code: 404 Not Found" || r.StatusCode == 0) {
d.SetId("")
log.Println("[WARN] Resource exists in state but not in API. Removing resource from state.")
return diags
}
if err != nil {
return diag.FromErr(err)
}
Expand Down

0 comments on commit 7671546

Please sign in to comment.