Skip to content

Commit 7c173e9

Browse files
authored
Handle 404s during reads (#180)
1 parent b412071 commit 7c173e9

17 files changed

+103
-9
lines changed

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ linters:
2626
- predeclared
2727
- revive
2828
- staticcheck
29-
- tenv
29+
- usetesting
3030
- unconvert
3131
- unparam
3232
- unused

internal/provider/destination_filter_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
"regexp"
78
"strings"
89

@@ -201,6 +202,12 @@ func (r *destinationFilterResource) Read(ctx context.Context, req resource.ReadR
201202
defer body.Body.Close()
202203
}
203204
if err != nil {
205+
if body.StatusCode == http.StatusNotFound {
206+
resp.State.RemoveResource(ctx)
207+
208+
return
209+
}
210+
204211
resp.Diagnostics.AddError(
205212
fmt.Sprintf("Unable to read Destination Filter (ID: %s)", previousState.ID.ValueString()),
206213
getError(err, body),

internal/provider/destination_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

78
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
89
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
@@ -587,6 +588,12 @@ func (r *destinationResource) Read(ctx context.Context, req resource.ReadRequest
587588
defer body.Body.Close()
588589
}
589590
if err != nil {
591+
if body.StatusCode == http.StatusNotFound {
592+
resp.State.RemoveResource(ctx)
593+
594+
return
595+
}
596+
590597
resp.Diagnostics.AddError(
591598
fmt.Sprintf("Unable to read Destination (ID: %s)", previousState.ID.ValueString()),
592599
getError(err, body),

internal/provider/destination_subscription_resource.go

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

910
"github.com/segmentio/terraform-provider-segment/internal/provider/docs"
@@ -228,6 +229,12 @@ func (r *destinationSubscriptionResource) Read(ctx context.Context, req resource
228229
defer body.Body.Close()
229230
}
230231
if err != nil {
232+
if body.StatusCode == http.StatusNotFound {
233+
resp.State.RemoveResource(ctx)
234+
235+
return
236+
}
237+
231238
resp.Diagnostics.AddError(
232239
fmt.Sprintf("Unable to read Destination subscription (ID: %s)", previousState.ID.ValueString()),
233240
getError(err, body),

internal/provider/function_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
"regexp"
78

89
"github.com/segmentio/terraform-provider-segment/internal/provider/docs"
@@ -189,6 +190,12 @@ func (r *functionResource) Read(ctx context.Context, req resource.ReadRequest, r
189190
defer body.Body.Close()
190191
}
191192
if err != nil {
193+
if body.StatusCode == http.StatusNotFound {
194+
resp.State.RemoveResource(ctx)
195+
196+
return
197+
}
198+
192199
resp.Diagnostics.AddError(
193200
fmt.Sprintf("Unable to read Function (ID: %s)", previousState.ID.ValueString()),
194201
getError(err, body),

internal/provider/insert_function_instance_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/segmentio/terraform-provider-segment/internal/provider/docs"
@@ -159,6 +160,12 @@ func (r *insertFunctionInstanceResource) Read(ctx context.Context, req resource.
159160
defer body.Body.Close()
160161
}
161162
if err != nil {
163+
if body.StatusCode == http.StatusNotFound {
164+
resp.State.RemoveResource(ctx)
165+
166+
return
167+
}
168+
162169
resp.Diagnostics.AddError(
163170
fmt.Sprintf("Unable to read Insert Function instance (ID: %s)", previousState.ID.ValueString()),
164171
getError(err, body),

internal/provider/label_resource.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,7 @@ func (r *labelResource) Read(ctx context.Context, req resource.ReadRequest, resp
163163
}
164164

165165
if label == nil {
166-
resp.Diagnostics.AddError(
167-
"Unable to find Label",
168-
fmt.Sprintf("Unable to find Label with key: %q and value: %q", state.Key, state.Value),
169-
)
166+
resp.State.RemoveResource(ctx)
170167

171168
return
172169
}

internal/provider/profiles_warehouse_resource.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ func (r *profilesWarehouseResource) Read(ctx context.Context, req resource.ReadR
167167

168168
warehouse, err := findProfileWarehouse(r.authContext, r.client, previousState.ID.ValueString(), previousState.SpaceID.ValueString())
169169
if err != nil {
170+
resp.State.RemoveResource(ctx)
171+
170172
resp.Diagnostics.AddError(
171173
fmt.Sprintf("Unable to read Profiles Warehouse (ID: %s)", previousState.ID.ValueString()),
172174
err.Error(),
@@ -176,10 +178,7 @@ func (r *profilesWarehouseResource) Read(ctx context.Context, req resource.ReadR
176178
}
177179

178180
if warehouse == nil {
179-
resp.Diagnostics.AddError(
180-
"Unable to find Profile Warehouse",
181-
fmt.Sprintf("Profile Warehouse with id '%s' and space id '%s' not found", previousState.ID.ValueString(), previousState.SpaceID.ValueString()),
182-
)
181+
resp.State.RemoveResource(ctx)
183182

184183
return
185184
}

internal/provider/reverse_etl_model_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

78
"github.com/segmentio/terraform-provider-segment/internal/provider/docs"
89
"github.com/segmentio/terraform-provider-segment/internal/provider/models"
@@ -160,6 +161,12 @@ func (r *reverseETLModelResource) Read(ctx context.Context, req resource.ReadReq
160161
defer body.Body.Close()
161162
}
162163
if err != nil {
164+
if body.StatusCode == http.StatusNotFound {
165+
resp.State.RemoveResource(ctx)
166+
167+
return
168+
}
169+
163170
resp.Diagnostics.AddError(
164171
fmt.Sprintf("Unable to read Reverse ETL model (ID: %s)", previousState.ID.ValueString()),
165172
getError(err, body),

internal/provider/source_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

78
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
89
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
@@ -387,6 +388,12 @@ func (r *sourceResource) Read(ctx context.Context, req resource.ReadRequest, res
387388
defer body.Body.Close()
388389
}
389390
if err != nil {
391+
if body.StatusCode == http.StatusNotFound {
392+
resp.State.RemoveResource(ctx)
393+
394+
return
395+
}
396+
390397
resp.Diagnostics.AddError(
391398
fmt.Sprintf("Unable to read Source (ID: %s)", previousState.ID.ValueString()),
392399
getError(err, body),

0 commit comments

Comments
 (0)