Skip to content

Commit 464884c

Browse files
authored
Remove deleted resources from Terraform state on Read (LB and DSA) (#340)
* Fix for load balancer resource * Fix for lb credential resource * Fix for all DSA resources * Fix for lb datasource * Fix for all DSA datasources * Adjustments after review * Remove early return from all datasources
1 parent 592007d commit 464884c

27 files changed

Lines changed: 176 additions & 0 deletions

File tree

stackit/internal/services/loadbalancer/credential/resource.go

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

89
"github.com/google/uuid"
@@ -15,6 +16,7 @@ import (
1516
"github.com/hashicorp/terraform-plugin-framework/types"
1617
"github.com/hashicorp/terraform-plugin-log/tflog"
1718
"github.com/stackitcloud/stackit-sdk-go/core/config"
19+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1820
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer"
1921
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/wait"
2022
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
@@ -236,6 +238,11 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
236238
// Get credentials
237239
credResp, err := r.client.GetCredentials(ctx, projectId, credentialsRef).Execute()
238240
if err != nil {
241+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
242+
if ok && oapiErr.StatusCode == http.StatusNotFound {
243+
resp.State.RemoveResource(ctx)
244+
return
245+
}
239246
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
240247
return
241248
}

stackit/internal/services/loadbalancer/loadbalancer/datasource.go

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

78
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
89
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
@@ -16,6 +17,7 @@ import (
1617

1718
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1819
"github.com/stackitcloud/stackit-sdk-go/core/config"
20+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1921
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer"
2022
)
2123

@@ -319,6 +321,10 @@ func (r *loadBalancerDataSource) Read(ctx context.Context, req datasource.ReadRe
319321

320322
lbResp, err := r.client.GetLoadBalancer(ctx, projectId, name).Execute()
321323
if err != nil {
324+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
325+
if ok && oapiErr.StatusCode == http.StatusNotFound {
326+
resp.State.RemoveResource(ctx)
327+
}
322328
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading load balancer", fmt.Sprintf("Calling API: %v", err))
323329
return
324330
}

stackit/internal/services/loadbalancer/loadbalancer/resource.go

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

@@ -26,6 +27,7 @@ import (
2627
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
2728
"github.com/hashicorp/terraform-plugin-log/tflog"
2829
"github.com/stackitcloud/stackit-sdk-go/core/config"
30+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
2931
"github.com/stackitcloud/stackit-sdk-go/core/utils"
3032
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer"
3133
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/wait"
@@ -638,6 +640,11 @@ func (r *loadBalancerResource) Read(ctx context.Context, req resource.ReadReques
638640

639641
lbResp, err := r.client.GetLoadBalancer(ctx, projectId, name).Execute()
640642
if err != nil {
643+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
644+
if ok && oapiErr.StatusCode == http.StatusNotFound {
645+
resp.State.RemoveResource(ctx)
646+
return
647+
}
641648
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading load balancer", fmt.Sprintf("Calling API: %v", err))
642649
return
643650
}

stackit/internal/services/logme/credential/datasource.go

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

78
"github.com/hashicorp/terraform-plugin-framework/datasource"
89
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
@@ -12,6 +13,7 @@ import (
1213

1314
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1415
"github.com/stackitcloud/stackit-sdk-go/core/config"
16+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1517
"github.com/stackitcloud/stackit-sdk-go/services/logme"
1618
)
1719

@@ -149,6 +151,10 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
149151

150152
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
151153
if err != nil {
154+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
155+
if ok && oapiErr.StatusCode == http.StatusNotFound {
156+
resp.State.RemoveResource(ctx)
157+
}
152158
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
153159
return
154160
}

stackit/internal/services/logme/credential/resource.go

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

89
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
@@ -17,6 +18,7 @@ import (
1718
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1819
"github.com/hashicorp/terraform-plugin-framework/types"
1920
"github.com/stackitcloud/stackit-sdk-go/core/config"
21+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
2022
"github.com/stackitcloud/stackit-sdk-go/services/logme"
2123
"github.com/stackitcloud/stackit-sdk-go/services/logme/wait"
2224
)
@@ -229,6 +231,11 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
229231

230232
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
231233
if err != nil {
234+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
235+
if ok && oapiErr.StatusCode == http.StatusNotFound {
236+
resp.State.RemoveResource(ctx)
237+
return
238+
}
232239
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
233240
return
234241
}

stackit/internal/services/logme/instance/datasource.go

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

78
"github.com/hashicorp/terraform-plugin-framework/datasource"
89
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
@@ -12,6 +13,7 @@ import (
1213

1314
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1415
"github.com/stackitcloud/stackit-sdk-go/core/config"
16+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1517
"github.com/stackitcloud/stackit-sdk-go/services/logme"
1618
)
1719

@@ -165,6 +167,10 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
165167

166168
instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).Execute()
167169
if err != nil {
170+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
171+
if ok && (oapiErr.StatusCode == http.StatusNotFound || oapiErr.StatusCode == http.StatusGone) {
172+
resp.State.RemoveResource(ctx)
173+
}
168174
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err))
169175
return
170176
}

stackit/internal/services/logme/instance/resource.go

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

89
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
@@ -21,6 +22,7 @@ import (
2122
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
2223
"github.com/hashicorp/terraform-plugin-framework/types"
2324
"github.com/stackitcloud/stackit-sdk-go/core/config"
25+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
2426
"github.com/stackitcloud/stackit-sdk-go/services/logme"
2527
"github.com/stackitcloud/stackit-sdk-go/services/logme/wait"
2628
)
@@ -299,6 +301,11 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
299301

300302
instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).Execute()
301303
if err != nil {
304+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
305+
if ok && (oapiErr.StatusCode == http.StatusNotFound || oapiErr.StatusCode == http.StatusGone) {
306+
resp.State.RemoveResource(ctx)
307+
return
308+
}
302309
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err))
303310
return
304311
}

stackit/internal/services/mariadb/credential/datasource.go

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

78
"github.com/hashicorp/terraform-plugin-framework/datasource"
89
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
@@ -13,6 +14,7 @@ import (
1314
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1415
"github.com/hashicorp/terraform-plugin-framework/types"
1516
"github.com/stackitcloud/stackit-sdk-go/core/config"
17+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1618
"github.com/stackitcloud/stackit-sdk-go/services/mariadb"
1719
)
1820

@@ -157,6 +159,10 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ
157159

158160
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
159161
if err != nil {
162+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
163+
if ok && oapiErr.StatusCode == http.StatusNotFound {
164+
resp.State.RemoveResource(ctx)
165+
}
160166
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
161167
return
162168
}

stackit/internal/services/mariadb/credential/resource.go

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

89
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
@@ -18,6 +19,7 @@ import (
1819
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1920
"github.com/hashicorp/terraform-plugin-framework/types"
2021
"github.com/stackitcloud/stackit-sdk-go/core/config"
22+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
2123
"github.com/stackitcloud/stackit-sdk-go/services/mariadb"
2224
"github.com/stackitcloud/stackit-sdk-go/services/mariadb/wait"
2325
)
@@ -239,6 +241,11 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest,
239241

240242
recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).Execute()
241243
if err != nil {
244+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
245+
if ok && oapiErr.StatusCode == http.StatusNotFound {
246+
resp.State.RemoveResource(ctx)
247+
return
248+
}
242249
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credential", fmt.Sprintf("Calling API: %v", err))
243250
return
244251
}

stackit/internal/services/mariadb/instance/datasource.go

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

78
"github.com/hashicorp/terraform-plugin-framework/datasource"
89
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
@@ -12,6 +13,7 @@ import (
1213

1314
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1415
"github.com/stackitcloud/stackit-sdk-go/core/config"
16+
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1517
"github.com/stackitcloud/stackit-sdk-go/services/mariadb"
1618
)
1719

@@ -165,6 +167,10 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
165167

166168
instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).Execute()
167169
if err != nil {
170+
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
171+
if ok && (oapiErr.StatusCode == http.StatusNotFound || oapiErr.StatusCode == http.StatusGone) {
172+
resp.State.RemoveResource(ctx)
173+
}
168174
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err))
169175
return
170176
}

0 commit comments

Comments
 (0)