Skip to content

Commit 1570df4

Browse files
committed
Properly handle resource not found errors
This updates the check if an errors is due to a resource not being found for all new resources (`subscription`, `state`, `state_transitions`, `product_selection`, `attribute_group` and `associate_role`).
1 parent adc2eaf commit 1570df4

File tree

8 files changed

+19
-20
lines changed

8 files changed

+19
-20
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: Fixed
2+
body: Properly handle resource not found errors for `subscription`, `state`,
3+
`state_transitions`, `product_selection`, `attribute_group` and
4+
`associate_role`.
5+
time: 2024-02-09T13:47:22.878305+01:00

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.21
55
require (
66
github.com/elliotchance/orderedmap/v2 v2.2.0
77
github.com/elliotchance/pie/v2 v2.8.0
8+
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
89
github.com/hashicorp/terraform-plugin-docs v0.16.0
910
github.com/hashicorp/terraform-plugin-framework v1.4.2
1011
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
@@ -14,6 +15,7 @@ require (
1415
github.com/labd/commercetools-go-sdk v1.4.0
1516
github.com/stretchr/testify v1.8.4
1617
golang.org/x/oauth2 v0.14.0
18+
golang.org/x/text v0.14.0
1719
)
1820

1921
require (
@@ -34,7 +36,6 @@ require (
3436
github.com/hashicorp/errwrap v1.1.0 // indirect
3537
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
3638
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
37-
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
3839
github.com/hashicorp/go-hclog v1.5.0 // indirect
3940
github.com/hashicorp/go-multierror v1.1.1 // indirect
4041
github.com/hashicorp/go-plugin v1.6.0 // indirect
@@ -74,7 +75,6 @@ require (
7475
golang.org/x/mod v0.14.0 // indirect
7576
golang.org/x/net v0.18.0 // indirect
7677
golang.org/x/sys v0.14.0 // indirect
77-
golang.org/x/text v0.14.0 // indirect
7878
google.golang.org/appengine v1.6.8 // indirect
7979
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
8080
google.golang.org/grpc v1.59.0 // indirect

internal/resources/associate_role/resource.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package associate_role
22

33
import (
44
"context"
5-
"errors"
65
"time"
76

87
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
@@ -14,8 +13,8 @@ import (
1413
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1514
"github.com/hashicorp/terraform-plugin-framework/types"
1615
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
17-
1816
"github.com/labd/commercetools-go-sdk/platform"
17+
1918
"github.com/labd/terraform-provider-commercetools/internal/utils"
2019
)
2120

@@ -160,7 +159,7 @@ func (r *associateRoleResource) Read(ctx context.Context, req resource.ReadReque
160159
// Read remote associate role and check for errors.
161160
associateRole, err := r.client.AssociateRoles().WithId(state.ID.ValueString()).Get().Execute(ctx)
162161
if err != nil {
163-
if errors.Is(err, platform.ErrNotFound) {
162+
if utils.IsResourceNotFoundError(err) {
164163
resp.State.RemoveResource(ctx)
165164
return
166165
}

internal/resources/attribute_group/resource.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@ package attribute_group
22

33
import (
44
"context"
5-
"errors"
6-
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
7-
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
8-
"github.com/labd/terraform-provider-commercetools/internal/customtypes"
95
"regexp"
106
"time"
117

8+
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
129
"github.com/hashicorp/terraform-plugin-framework/path"
1310
"github.com/hashicorp/terraform-plugin-framework/resource"
1411
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
12+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1513
"github.com/hashicorp/terraform-plugin-framework/types"
1614
sdk_resource "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1715
"github.com/labd/commercetools-go-sdk/platform"
1816

17+
"github.com/labd/terraform-provider-commercetools/internal/customtypes"
1918
"github.com/labd/terraform-provider-commercetools/internal/utils"
2019
)
2120

@@ -158,7 +157,7 @@ func (r *Resource) Read(ctx context.Context, req resource.ReadRequest, resp *res
158157

159158
res, err := r.client.AttributeGroups().WithId(current.ID.ValueString()).Get().Execute(ctx)
160159
if err != nil {
161-
if errors.Is(err, platform.ErrNotFound) {
160+
if utils.IsResourceNotFoundError(err) {
162161
resp.State.RemoveResource(ctx)
163162
return
164163
}

internal/resources/product_selection/resource.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package product_selection
22

33
import (
44
"context"
5-
"errors"
65
"time"
76

87
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
@@ -163,7 +162,7 @@ func (r *productSelectionResource) Read(ctx context.Context, req resource.ReadRe
163162
// Read remote product selection and check for errors.
164163
productSelection, err := r.client.ProductSelections().WithId(state.ID.ValueString()).Get().Execute(ctx)
165164
if err != nil {
166-
if errors.Is(err, platform.ErrNotFound) {
165+
if utils.IsResourceNotFoundError(err) {
167166
resp.State.RemoveResource(ctx)
168167
return
169168
}

internal/resources/state/resource.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package state
22

33
import (
44
"context"
5-
"errors"
65
"time"
76

87
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
@@ -178,7 +177,7 @@ func (r *stateResource) Read(ctx context.Context, req resource.ReadRequest, resp
178177

179178
res, err := r.client.States().WithId(state.ID.ValueString()).Get().Execute(ctx)
180179
if err != nil {
181-
if errors.Is(err, platform.ErrNotFound) {
180+
if utils.IsResourceNotFoundError(err) {
182181
resp.State.RemoveResource(ctx)
183182
return
184183
}

internal/resources/state_transition/resource.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package state_transition
22

33
import (
44
"context"
5-
"errors"
65
"sync"
76
"time"
87

@@ -154,7 +153,7 @@ func (r *stateTransitionResource) Read(ctx context.Context, req resource.ReadReq
154153

155154
res, err := r.client.States().WithId(resourceID).Get().Execute(ctx)
156155
if err != nil {
157-
if errors.Is(err, platform.ErrNotFound) {
156+
if utils.IsResourceNotFoundError(err) {
158157
resp.State.RemoveResource(ctx)
159158
return
160159
}

internal/resources/subscription/resource.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package subscription
22

33
import (
44
"context"
5-
"errors"
6-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
75
"regexp"
86
"time"
97

@@ -16,6 +14,7 @@ import (
1614
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1715
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1816
"github.com/hashicorp/terraform-plugin-framework/types"
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
1918
"github.com/labd/commercetools-go-sdk/platform"
2019

2120
"github.com/labd/terraform-provider-commercetools/internal/customvalidator"
@@ -376,7 +375,7 @@ func (r *subscriptionResource) Read(ctx context.Context, req resource.ReadReques
376375

377376
subscription, err := r.client.Subscriptions().WithId(state.ID.ValueString()).Get().Execute(ctx)
378377
if err != nil {
379-
if errors.Is(err, platform.ErrNotFound) {
378+
if utils.IsResourceNotFoundError(err) {
380379
resp.State.RemoveResource(ctx)
381380
return
382381
}
@@ -473,7 +472,7 @@ func (r *subscriptionResource) Delete(ctx context.Context, req resource.DeleteRe
473472
func (r *subscriptionResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
474473
subscription, err := r.client.Subscriptions().WithId(req.ID).Get().Execute(ctx)
475474
if err != nil {
476-
if errors.Is(err, platform.ErrNotFound) {
475+
if utils.IsResourceNotFoundError(err) {
477476
resp.State.RemoveResource(ctx)
478477
return
479478
}

0 commit comments

Comments
 (0)