Remove deleted resources and datasources from Terraform state on Read (all remaining services) (#346)
* Remove deleted resources and datasources from state on Read * Simplify code * Fix function description Co-authored-by: Diogo Ferrão <diogo.ferrao@freiheit.com> * Fix function description Co-authored-by: Diogo Ferrão <diogo.ferrao@freiheit.com> * Fix whitespace --------- Co-authored-by: Diogo Ferrão <diogo.ferrao@freiheit.com>
This commit is contained in:
parent
464884cabe
commit
66d6ec2bd0
33 changed files with 262 additions and 40 deletions
|
|
@ -3,6 +3,7 @@ package argus
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||
|
|
@ -11,7 +12,9 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"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/services/argus"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/argus/wait"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate"
|
||||
)
|
||||
|
|
@ -215,11 +218,20 @@ func (d *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
|
|||
}
|
||||
projectId := model.ProjectId.ValueString()
|
||||
instanceId := model.InstanceId.ValueString()
|
||||
instanceResponse, err := d.client.GetInstance(ctx, instanceId, projectId).Execute()
|
||||
instanceResp, err := d.client.GetInstance(ctx, instanceId, projectId).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 instance", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
if instanceResp != nil && instanceResp.Status != nil && *instanceResp.Status == wait.DeleteSuccess {
|
||||
resp.State.RemoveResource(ctx)
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", "Instance was deleted successfully")
|
||||
return
|
||||
}
|
||||
|
||||
aclList, err := d.client.ListACL(ctx, instanceId, projectId).Execute()
|
||||
if err != nil {
|
||||
|
|
@ -228,7 +240,7 @@ func (d *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques
|
|||
}
|
||||
|
||||
// Map response body to schema
|
||||
err = mapFields(ctx, instanceResponse, &model)
|
||||
err = mapFields(ctx, instanceResp, &model)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Processing API payload: %v", err))
|
||||
return
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package argus
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator"
|
||||
|
|
@ -18,6 +19,7 @@ import (
|
|||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
"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/argus"
|
||||
"github.com/stackitcloud/stackit-sdk-go/services/argus/wait"
|
||||
|
|
@ -370,9 +372,18 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r
|
|||
|
||||
instanceResp, err := r.client.GetInstance(ctx, instanceId, projectId).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 instance", fmt.Sprintf("Calling API: %v", err))
|
||||
return
|
||||
}
|
||||
if instanceResp != nil && instanceResp.Status != nil && *instanceResp.Status == wait.DeleteSuccess {
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
aclList, err := r.client.ListACL(ctx, instanceId, projectId).Execute()
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue