Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle resource not found errors #478

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/Fixed-20240209-134722.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: Fixed
body: Properly handle resource not found errors for `subscription`, `state`,
`state_transitions`, `product_selection`, `attribute_group` and
`associate_role`.
time: 2024-02-09T13:47:22.878305+01:00
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/elliotchance/orderedmap/v2 v2.2.0
github.com/elliotchance/pie/v2 v2.8.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-docs v0.16.0
github.com/hashicorp/terraform-plugin-framework v1.4.2
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
Expand All @@ -14,6 +15,7 @@ require (
github.com/labd/commercetools-go-sdk v1.4.0
github.com/stretchr/testify v1.8.4
golang.org/x/oauth2 v0.14.0
golang.org/x/text v0.14.0
)

require (
Expand All @@ -34,7 +36,6 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.6.0 // indirect
Expand Down Expand Up @@ -74,7 +75,6 @@ require (
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/grpc v1.59.0 // indirect
Expand Down
5 changes: 2 additions & 3 deletions internal/resources/associate_role/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import (
"context"
"errors"
"time"

"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
Expand All @@ -14,8 +13,8 @@
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"

"github.com/labd/commercetools-go-sdk/platform"

"github.com/labd/terraform-provider-commercetools/internal/utils"
)

Expand Down Expand Up @@ -160,7 +159,7 @@
// Read remote associate role and check for errors.
associateRole, err := r.client.AssociateRoles().WithId(state.ID.ValueString()).Get().Execute(ctx)
if err != nil {
if errors.Is(err, platform.ErrNotFound) {
if utils.IsResourceNotFoundError(err) {

Check warning on line 162 in internal/resources/associate_role/resource.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/associate_role/resource.go#L162

Added line #L162 was not covered by tests
resp.State.RemoveResource(ctx)
return
}
Expand Down
9 changes: 4 additions & 5 deletions internal/resources/attribute_group/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

import (
"context"
"errors"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/labd/terraform-provider-commercetools/internal/customtypes"
"regexp"
"time"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
sdk_resource "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/labd/commercetools-go-sdk/platform"

"github.com/labd/terraform-provider-commercetools/internal/customtypes"
"github.com/labd/terraform-provider-commercetools/internal/utils"
)

Expand Down Expand Up @@ -158,7 +157,7 @@

res, err := r.client.AttributeGroups().WithId(current.ID.ValueString()).Get().Execute(ctx)
if err != nil {
if errors.Is(err, platform.ErrNotFound) {
if utils.IsResourceNotFoundError(err) {

Check warning on line 160 in internal/resources/attribute_group/resource.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/attribute_group/resource.go#L160

Added line #L160 was not covered by tests
resp.State.RemoveResource(ctx)
return
}
Expand Down
3 changes: 1 addition & 2 deletions internal/resources/product_selection/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import (
"context"
"errors"
"time"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
Expand Down Expand Up @@ -163,7 +162,7 @@
// Read remote product selection and check for errors.
productSelection, err := r.client.ProductSelections().WithId(state.ID.ValueString()).Get().Execute(ctx)
if err != nil {
if errors.Is(err, platform.ErrNotFound) {
if utils.IsResourceNotFoundError(err) {

Check warning on line 165 in internal/resources/product_selection/resource.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/product_selection/resource.go#L165

Added line #L165 was not covered by tests
resp.State.RemoveResource(ctx)
return
}
Expand Down
3 changes: 1 addition & 2 deletions internal/resources/state/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import (
"context"
"errors"
"time"

"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
Expand Down Expand Up @@ -178,7 +177,7 @@

res, err := r.client.States().WithId(state.ID.ValueString()).Get().Execute(ctx)
if err != nil {
if errors.Is(err, platform.ErrNotFound) {
if utils.IsResourceNotFoundError(err) {

Check warning on line 180 in internal/resources/state/resource.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/state/resource.go#L180

Added line #L180 was not covered by tests
resp.State.RemoveResource(ctx)
return
}
Expand Down
3 changes: 1 addition & 2 deletions internal/resources/state_transition/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import (
"context"
"errors"
"sync"
"time"

Expand All @@ -21,8 +20,8 @@
_ resource.Resource = &stateTransitionResource{}
_ resource.ResourceWithConfigure = &stateTransitionResource{}
_ resource.ResourceWithImportState = &stateTransitionResource{}
globalUniqueStore map[string]bool

Check failure on line 23 in internal/resources/state_transition/resource.go

View workflow job for this annotation

GitHub Actions / test

var `globalUniqueStore` is unused (unused)
globalUniqueMutex sync.Mutex

Check failure on line 24 in internal/resources/state_transition/resource.go

View workflow job for this annotation

GitHub Actions / test

var `globalUniqueMutex` is unused (unused)
)

func init() {
Expand Down Expand Up @@ -154,7 +153,7 @@

res, err := r.client.States().WithId(resourceID).Get().Execute(ctx)
if err != nil {
if errors.Is(err, platform.ErrNotFound) {
if utils.IsResourceNotFoundError(err) {

Check warning on line 156 in internal/resources/state_transition/resource.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/state_transition/resource.go#L156

Added line #L156 was not covered by tests
resp.State.RemoveResource(ctx)
return
}
Expand Down
7 changes: 3 additions & 4 deletions internal/resources/subscription/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import (
"context"
"errors"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"regexp"
"time"

Expand All @@ -16,6 +14,7 @@
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/labd/commercetools-go-sdk/platform"

"github.com/labd/terraform-provider-commercetools/internal/customvalidator"
Expand Down Expand Up @@ -376,7 +375,7 @@

subscription, err := r.client.Subscriptions().WithId(state.ID.ValueString()).Get().Execute(ctx)
if err != nil {
if errors.Is(err, platform.ErrNotFound) {
if utils.IsResourceNotFoundError(err) {

Check warning on line 378 in internal/resources/subscription/resource.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/subscription/resource.go#L378

Added line #L378 was not covered by tests
resp.State.RemoveResource(ctx)
return
}
Expand Down Expand Up @@ -473,7 +472,7 @@
func (r *subscriptionResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
subscription, err := r.client.Subscriptions().WithId(req.ID).Get().Execute(ctx)
if err != nil {
if errors.Is(err, platform.ErrNotFound) {
if utils.IsResourceNotFoundError(err) {

Check warning on line 475 in internal/resources/subscription/resource.go

View check run for this annotation

Codecov / codecov/patch

internal/resources/subscription/resource.go#L475

Added line #L475 was not covered by tests
resp.State.RemoveResource(ctx)
return
}
Expand Down
Loading