Skip to content

Commit 3bd0779

Browse files
committed
Handle 404 on deletes
1 parent 7c173e9 commit 3bd0779

16 files changed

+106
-5
lines changed

internal/provider/destination_filter_resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,12 @@ func (r *destinationFilterResource) Delete(ctx context.Context, req resource.Del
320320
defer body.Body.Close()
321321
}
322322
if err != nil {
323+
if body.StatusCode == http.StatusNotFound {
324+
resp.State.RemoveResource(ctx)
325+
326+
return
327+
}
328+
323329
resp.Diagnostics.AddError(
324330
fmt.Sprintf("Unable to delete Destination Filter (ID: %s)", state.ID.ValueString()),
325331
getError(err, body),

internal/provider/destination_resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,12 @@ func (r *destinationResource) Delete(ctx context.Context, req resource.DeleteReq
701701
defer body.Body.Close()
702702
}
703703
if err != nil {
704+
if body.StatusCode == http.StatusNotFound {
705+
resp.State.RemoveResource(ctx)
706+
707+
return
708+
}
709+
704710
resp.Diagnostics.AddError(
705711
fmt.Sprintf("Unable to delete Destination (ID: %s)", state.ID.ValueString()),
706712
getError(err, body),

internal/provider/destination_subscription_resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,12 @@ func (r *destinationSubscriptionResource) Delete(ctx context.Context, req resour
359359
defer body.Body.Close()
360360
}
361361
if err != nil {
362+
if body.StatusCode == http.StatusNotFound {
363+
resp.State.RemoveResource(ctx)
364+
365+
return
366+
}
367+
362368
resp.Diagnostics.AddError(
363369
fmt.Sprintf("Unable to delete Destination subscription (ID: %s)", config.ID.ValueString()),
364370
getError(err, body),

internal/provider/function_resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ func (r *functionResource) Delete(ctx context.Context, req resource.DeleteReques
290290
defer body.Body.Close()
291291
}
292292
if err != nil {
293+
if body.StatusCode == http.StatusNotFound || body.StatusCode == http.StatusForbidden {
294+
resp.State.RemoveResource(ctx)
295+
296+
return
297+
}
298+
293299
resp.Diagnostics.AddError(
294300
fmt.Sprintf("Unable to delete Function (ID: %s)", config.ID.ValueString()),
295301
getError(err, body),

internal/provider/insert_function_instance_resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,12 @@ func (r *insertFunctionInstanceResource) Delete(ctx context.Context, req resourc
277277
defer body.Body.Close()
278278
}
279279
if err != nil {
280+
if body.StatusCode == http.StatusNotFound {
281+
resp.State.RemoveResource(ctx)
282+
283+
return
284+
}
285+
280286
resp.Diagnostics.AddError(
281287
fmt.Sprintf("Unable to delete Insert Function instance (ID: %s)", config.ID.ValueString()),
282288
getError(err, body),

internal/provider/label_resource.go

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

89
"github.com/hashicorp/terraform-plugin-framework/path"
@@ -200,6 +201,12 @@ func (r *labelResource) Delete(ctx context.Context, req resource.DeleteRequest,
200201
defer body.Body.Close()
201202
}
202203
if err != nil {
204+
if body.StatusCode == http.StatusNotFound {
205+
resp.State.RemoveResource(ctx)
206+
207+
return
208+
}
209+
203210
resp.Diagnostics.AddError(
204211
"Unable to delete Label",
205212
getError(err, body),

internal/provider/profiles_warehouse_resource.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"net/http"
78
"strings"
89

910
"github.com/segmentio/terraform-provider-segment/internal/provider/docs"
@@ -281,6 +282,12 @@ func (r *profilesWarehouseResource) Delete(ctx context.Context, req resource.Del
281282
defer body.Body.Close()
282283
}
283284
if err != nil {
285+
if body.StatusCode == http.StatusNotFound {
286+
resp.State.RemoveResource(ctx)
287+
288+
return
289+
}
290+
284291
resp.Diagnostics.AddError(
285292
fmt.Sprintf("Unable to delete Profiles Warehouse (ID: %s)", config.ID.ValueString()),
286293
getError(err, body),

internal/provider/reverse_etl_model_resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,12 @@ func (r *reverseETLModelResource) Delete(ctx context.Context, req resource.Delet
270270
defer body.Body.Close()
271271
}
272272
if err != nil {
273+
if body.StatusCode == http.StatusNotFound {
274+
resp.State.RemoveResource(ctx)
275+
276+
return
277+
}
278+
273279
resp.Diagnostics.AddError(
274280
fmt.Sprintf("Unable to delete Reverse ETL model (ID: %s)", config.ID.ValueString()),
275281
getError(err, body),

internal/provider/source_resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,12 @@ func (r *sourceResource) Delete(ctx context.Context, req resource.DeleteRequest,
552552
defer body.Body.Close()
553553
}
554554
if err != nil {
555+
if body.StatusCode == http.StatusNotFound {
556+
resp.State.RemoveResource(ctx)
557+
558+
return
559+
}
560+
555561
resp.Diagnostics.AddError(
556562
fmt.Sprintf("Unable to delete Source (ID: %s)", config.ID.ValueString()),
557563
getError(err, body),

internal/provider/source_tracking_plan_connection_resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,12 @@ func (r *sourceTrackingPlanConnectionResource) Delete(ctx context.Context, req r
408408
defer body.Body.Close()
409409
}
410410
if err != nil {
411+
if body.StatusCode == http.StatusNotFound {
412+
resp.State.RemoveResource(ctx)
413+
414+
return
415+
}
416+
411417
resp.Diagnostics.AddError(
412418
"Unable to remove Source-Tracking Plan connection",
413419
getError(err, body),

internal/provider/source_warehouse_connection_resource.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (r *sourceWarehouseConnectionResource) Delete(ctx context.Context, req reso
183183
}
184184

185185
if config.WarehouseID.String() == "" || config.SourceID.String() == "" {
186-
resp.Diagnostics.AddError("Unable to remove Source-Warehouse connection", "At least one ID is empty")
186+
resp.State.RemoveResource(ctx)
187187

188188
return
189189
}
@@ -193,6 +193,12 @@ func (r *sourceWarehouseConnectionResource) Delete(ctx context.Context, req reso
193193
defer body.Body.Close()
194194
}
195195
if err != nil {
196+
if body.StatusCode == http.StatusNotFound {
197+
resp.State.RemoveResource(ctx)
198+
199+
return
200+
}
201+
196202
resp.Diagnostics.AddError(
197203
"Unable to remove Source-Warehouse connection",
198204
getError(err, body),

internal/provider/tracking_plan_resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,12 @@ func (r *trackingPlanResource) Delete(ctx context.Context, req resource.DeleteRe
417417
defer body.Body.Close()
418418
}
419419
if err != nil {
420+
if body.StatusCode == http.StatusNotFound {
421+
resp.State.RemoveResource(ctx)
422+
423+
return
424+
}
425+
420426
resp.Diagnostics.AddError(
421427
fmt.Sprintf("Unable to delete Tracking Plan (ID: %s)", config.ID.ValueString()),
422428
getError(err, body),

internal/provider/transformation_resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,12 @@ func (r *transformationResource) Delete(ctx context.Context, req resource.Delete
310310
defer body.Body.Close()
311311
}
312312
if err != nil {
313+
if body.StatusCode == http.StatusNotFound {
314+
resp.State.RemoveResource(ctx)
315+
316+
return
317+
}
318+
313319
resp.Diagnostics.AddError(
314320
fmt.Sprintf("Unable to delete Transformation (ID: %s)", config.ID.ValueString()),
315321
getError(err, body),

internal/provider/user_group_resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,12 @@ func (r *userGroupResource) Delete(ctx context.Context, req resource.DeleteReque
418418
defer body.Body.Close()
419419
}
420420
if err != nil {
421+
if body.StatusCode == http.StatusNotFound {
422+
resp.State.RemoveResource(ctx)
423+
424+
return
425+
}
426+
421427
resp.Diagnostics.AddError(
422428
fmt.Sprintf("Unable to delete User Group (ID: %s)", config.ID.ValueString()),
423429
getError(err, body),

internal/provider/user_resource.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,7 @@ func (r *userResource) Read(ctx context.Context, req resource.ReadRequest, resp
237237
if state.IsInvite.ValueBool() { // Handle potential invite
238238
foundUser, err := findUser(r.authContext, r.client, state.Email.ValueString())
239239
if err != nil {
240-
resp.Diagnostics.AddError(
241-
"Unable to find user",
242-
err.Error(),
243-
)
240+
resp.State.RemoveResource(ctx)
244241

245242
return
246243
}
@@ -465,6 +462,12 @@ func (r *userResource) Delete(ctx context.Context, req resource.DeleteRequest, r
465462
defer body.Body.Close()
466463
}
467464
if err != nil {
465+
if body.StatusCode == http.StatusNotFound {
466+
resp.State.RemoveResource(ctx)
467+
468+
return
469+
}
470+
468471
resp.Diagnostics.AddError(
469472
fmt.Sprintf("Unable to delete user (ID: %s)", state.ID.ValueString()),
470473
getError(err, body),
@@ -484,6 +487,12 @@ func (r *userResource) Delete(ctx context.Context, req resource.DeleteRequest, r
484487
defer body.Body.Close()
485488
}
486489
if err != nil {
490+
if body.StatusCode == http.StatusNotFound {
491+
resp.State.RemoveResource(ctx)
492+
493+
return
494+
}
495+
487496
resp.Diagnostics.AddError(
488497
fmt.Sprintf("Unable to delete user (ID: %s)", state.ID.ValueString()),
489498
getError(err, body),

internal/provider/warehouse_resource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,12 @@ func (r *warehouseResource) Delete(ctx context.Context, req resource.DeleteReque
423423
defer body.Body.Close()
424424
}
425425
if err != nil {
426+
if body.StatusCode == http.StatusNotFound {
427+
resp.State.RemoveResource(ctx)
428+
429+
return
430+
}
431+
426432
resp.Diagnostics.AddError(
427433
fmt.Sprintf("Unable to delete Warehouse (ID: %s)", config.ID.ValueString()),
428434
getError(err, body),

0 commit comments

Comments
 (0)