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
This commit is contained in:
parent
592007de7a
commit
464884cabe
27 changed files with 176 additions and 0 deletions
|
|
@ -3,6 +3,7 @@ package loadbalancer
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
|
||||
|
|
@ -16,6 +17,7 @@ import (
|
|||
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer"
|
||||
)
|
||||
|
||||
|
|
@ -319,6 +321,10 @@ func (r *loadBalancerDataSource) Read(ctx context.Context, req datasource.ReadRe
|
|||
|
||||
lbResp, err := r.client.GetLoadBalancer(ctx, projectId, name).Execute()
|
||||
if err != nil {
|
||||
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
|
||||
if ok && oapiErr.StatusCode == http.StatusNotFound {
|
||||
resp.State.RemoveResource(ctx)
|
||||
}
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading load balancer", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package loadbalancer
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
|
@ -26,6 +27,7 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||
"github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/wait"
|
||||
|
|
@ -638,6 +640,11 @@ func (r *loadBalancerResource) Read(ctx context.Context, req resource.ReadReques
|
|||
|
||||
lbResp, err := r.client.GetLoadBalancer(ctx, projectId, name).Execute()
|
||||
if err != nil {
|
||||
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
|
||||
if ok && oapiErr.StatusCode == http.StatusNotFound {
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading load balancer", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue