From 464884cabe554cf30e40f94da197cd79b657a48e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Palet?= Date: Mon, 22 Apr 2024 15:19:10 +0100 Subject: [PATCH] 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 --- .../internal/services/loadbalancer/credential/resource.go | 7 +++++++ .../services/loadbalancer/loadbalancer/datasource.go | 6 ++++++ .../services/loadbalancer/loadbalancer/resource.go | 7 +++++++ stackit/internal/services/logme/credential/datasource.go | 6 ++++++ stackit/internal/services/logme/credential/resource.go | 7 +++++++ stackit/internal/services/logme/instance/datasource.go | 6 ++++++ stackit/internal/services/logme/instance/resource.go | 7 +++++++ stackit/internal/services/mariadb/credential/datasource.go | 6 ++++++ stackit/internal/services/mariadb/credential/resource.go | 7 +++++++ stackit/internal/services/mariadb/instance/datasource.go | 6 ++++++ stackit/internal/services/mariadb/instance/resource.go | 7 +++++++ .../internal/services/opensearch/credential/datasource.go | 6 ++++++ .../internal/services/opensearch/credential/resource.go | 7 +++++++ .../internal/services/opensearch/instance/datasource.go | 6 ++++++ stackit/internal/services/opensearch/instance/resource.go | 7 +++++++ .../internal/services/postgresql/credential/datasource.go | 6 ++++++ .../internal/services/postgresql/credential/resource.go | 7 +++++++ .../internal/services/postgresql/instance/datasource.go | 6 ++++++ stackit/internal/services/postgresql/instance/resource.go | 7 +++++++ .../internal/services/rabbitmq/credential/datasource.go | 6 ++++++ stackit/internal/services/rabbitmq/credential/resource.go | 7 +++++++ stackit/internal/services/rabbitmq/instance/datasource.go | 6 ++++++ stackit/internal/services/rabbitmq/instance/resource.go | 7 +++++++ stackit/internal/services/redis/credential/datasource.go | 6 ++++++ stackit/internal/services/redis/credential/resource.go | 7 +++++++ stackit/internal/services/redis/instance/datasource.go | 6 ++++++ stackit/internal/services/redis/instance/resource.go | 7 +++++++ 27 files changed, 176 insertions(+) diff --git a/stackit/internal/services/loadbalancer/credential/resource.go b/stackit/internal/services/loadbalancer/credential/resource.go index 8fe508dc..070de0ed 100644 --- a/stackit/internal/services/loadbalancer/credential/resource.go +++ b/stackit/internal/services/loadbalancer/credential/resource.go @@ -3,6 +3,7 @@ package loadbalancer import ( "context" "fmt" + "net/http" "strings" "github.com/google/uuid" @@ -15,6 +16,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/services/loadbalancer" "github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/wait" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" @@ -236,6 +238,11 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest, // Get credentials credResp, err := r.client.GetCredentials(ctx, projectId, credentialsRef).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 credential", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/loadbalancer/loadbalancer/datasource.go b/stackit/internal/services/loadbalancer/loadbalancer/datasource.go index 9a89b046..89e52e47 100644 --- a/stackit/internal/services/loadbalancer/loadbalancer/datasource.go +++ b/stackit/internal/services/loadbalancer/loadbalancer/datasource.go @@ -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 } diff --git a/stackit/internal/services/loadbalancer/loadbalancer/resource.go b/stackit/internal/services/loadbalancer/loadbalancer/resource.go index d1c856ad..7246c8d6 100644 --- a/stackit/internal/services/loadbalancer/loadbalancer/resource.go +++ b/stackit/internal/services/loadbalancer/loadbalancer/resource.go @@ -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 } diff --git a/stackit/internal/services/logme/credential/datasource.go b/stackit/internal/services/logme/credential/datasource.go index eea98bea..afa4e3c7 100644 --- a/stackit/internal/services/logme/credential/datasource.go +++ b/stackit/internal/services/logme/credential/datasource.go @@ -3,6 +3,7 @@ package logme import ( "context" "fmt" + "net/http" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -12,6 +13,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/logme" ) @@ -149,6 +151,10 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).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 credential", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/logme/credential/resource.go b/stackit/internal/services/logme/credential/resource.go index 9d558080..05932d04 100644 --- a/stackit/internal/services/logme/credential/resource.go +++ b/stackit/internal/services/logme/credential/resource.go @@ -3,6 +3,7 @@ package logme import ( "context" "fmt" + "net/http" "strings" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -17,6 +18,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/logme" "github.com/stackitcloud/stackit-sdk-go/services/logme/wait" ) @@ -229,6 +231,11 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest, recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).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 credential", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/logme/instance/datasource.go b/stackit/internal/services/logme/instance/datasource.go index e2a17676..4a307906 100644 --- a/stackit/internal/services/logme/instance/datasource.go +++ b/stackit/internal/services/logme/instance/datasource.go @@ -3,6 +3,7 @@ package logme import ( "context" "fmt" + "net/http" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -12,6 +13,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/logme" ) @@ -165,6 +167,10 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).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 || oapiErr.StatusCode == http.StatusGone) { + resp.State.RemoveResource(ctx) + } core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/logme/instance/resource.go b/stackit/internal/services/logme/instance/resource.go index f2c318e0..dccf84a7 100644 --- a/stackit/internal/services/logme/instance/resource.go +++ b/stackit/internal/services/logme/instance/resource.go @@ -3,6 +3,7 @@ package logme import ( "context" "fmt" + "net/http" "strings" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" @@ -21,6 +22,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/logme" "github.com/stackitcloud/stackit-sdk-go/services/logme/wait" ) @@ -299,6 +301,11 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).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 || oapiErr.StatusCode == http.StatusGone) { + resp.State.RemoveResource(ctx) + return + } core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/mariadb/credential/datasource.go b/stackit/internal/services/mariadb/credential/datasource.go index 32e09f7b..5198c782 100644 --- a/stackit/internal/services/mariadb/credential/datasource.go +++ b/stackit/internal/services/mariadb/credential/datasource.go @@ -3,6 +3,7 @@ package mariadb import ( "context" "fmt" + "net/http" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -13,6 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/mariadb" ) @@ -157,6 +159,10 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).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 credential", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/mariadb/credential/resource.go b/stackit/internal/services/mariadb/credential/resource.go index 1cf7d338..983a689a 100644 --- a/stackit/internal/services/mariadb/credential/resource.go +++ b/stackit/internal/services/mariadb/credential/resource.go @@ -3,6 +3,7 @@ package mariadb import ( "context" "fmt" + "net/http" "strings" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -18,6 +19,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/mariadb" "github.com/stackitcloud/stackit-sdk-go/services/mariadb/wait" ) @@ -239,6 +241,11 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest, recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).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 credential", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/mariadb/instance/datasource.go b/stackit/internal/services/mariadb/instance/datasource.go index 71279253..b4849584 100644 --- a/stackit/internal/services/mariadb/instance/datasource.go +++ b/stackit/internal/services/mariadb/instance/datasource.go @@ -3,6 +3,7 @@ package mariadb import ( "context" "fmt" + "net/http" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -12,6 +13,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/mariadb" ) @@ -165,6 +167,10 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).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 || oapiErr.StatusCode == http.StatusGone) { + resp.State.RemoveResource(ctx) + } core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/mariadb/instance/resource.go b/stackit/internal/services/mariadb/instance/resource.go index 1f3db6fc..4cb2d0c8 100644 --- a/stackit/internal/services/mariadb/instance/resource.go +++ b/stackit/internal/services/mariadb/instance/resource.go @@ -3,6 +3,7 @@ package mariadb import ( "context" "fmt" + "net/http" "strings" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" @@ -21,6 +22,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/mariadb" "github.com/stackitcloud/stackit-sdk-go/services/mariadb/wait" ) @@ -299,6 +301,11 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).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 || oapiErr.StatusCode == http.StatusGone) { + resp.State.RemoveResource(ctx) + return + } core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/opensearch/credential/datasource.go b/stackit/internal/services/opensearch/credential/datasource.go index f05dccd8..d2f28a6b 100644 --- a/stackit/internal/services/opensearch/credential/datasource.go +++ b/stackit/internal/services/opensearch/credential/datasource.go @@ -3,6 +3,7 @@ package opensearch import ( "context" "fmt" + "net/http" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -13,6 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/opensearch" ) @@ -157,6 +159,10 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).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 credential", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/opensearch/credential/resource.go b/stackit/internal/services/opensearch/credential/resource.go index fc55a7ac..39f53b7a 100644 --- a/stackit/internal/services/opensearch/credential/resource.go +++ b/stackit/internal/services/opensearch/credential/resource.go @@ -3,6 +3,7 @@ package opensearch import ( "context" "fmt" + "net/http" "strings" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -18,6 +19,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/opensearch" "github.com/stackitcloud/stackit-sdk-go/services/opensearch/wait" ) @@ -239,6 +241,11 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest, recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).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 credential", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/opensearch/instance/datasource.go b/stackit/internal/services/opensearch/instance/datasource.go index c1d600a2..a6522743 100644 --- a/stackit/internal/services/opensearch/instance/datasource.go +++ b/stackit/internal/services/opensearch/instance/datasource.go @@ -3,6 +3,7 @@ package opensearch import ( "context" "fmt" + "net/http" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -12,6 +13,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/opensearch" ) @@ -165,6 +167,10 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).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 || oapiErr.StatusCode == http.StatusGone) { + resp.State.RemoveResource(ctx) + } core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/opensearch/instance/resource.go b/stackit/internal/services/opensearch/instance/resource.go index 3623137d..fc1da5e1 100644 --- a/stackit/internal/services/opensearch/instance/resource.go +++ b/stackit/internal/services/opensearch/instance/resource.go @@ -3,6 +3,7 @@ package opensearch import ( "context" "fmt" + "net/http" "strings" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" @@ -21,6 +22,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/opensearch" "github.com/stackitcloud/stackit-sdk-go/services/opensearch/wait" ) @@ -299,6 +301,11 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).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 || oapiErr.StatusCode == http.StatusGone) { + resp.State.RemoveResource(ctx) + return + } core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/postgresql/credential/datasource.go b/stackit/internal/services/postgresql/credential/datasource.go index 99b63c47..af9c0bfd 100644 --- a/stackit/internal/services/postgresql/credential/datasource.go +++ b/stackit/internal/services/postgresql/credential/datasource.go @@ -3,6 +3,7 @@ package postgresql import ( "context" "fmt" + "net/http" "strings" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -14,6 +15,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/postgresql" ) @@ -173,6 +175,10 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).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 credential", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/postgresql/credential/resource.go b/stackit/internal/services/postgresql/credential/resource.go index 4b941412..8a115959 100644 --- a/stackit/internal/services/postgresql/credential/resource.go +++ b/stackit/internal/services/postgresql/credential/resource.go @@ -3,6 +3,7 @@ package postgresql import ( "context" "fmt" + "net/http" "strings" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -18,6 +19,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/postgresql" "github.com/stackitcloud/stackit-sdk-go/services/postgresql/wait" ) @@ -255,6 +257,11 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest, recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).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 credential", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/postgresql/instance/datasource.go b/stackit/internal/services/postgresql/instance/datasource.go index b5291e6a..cfab5dea 100644 --- a/stackit/internal/services/postgresql/instance/datasource.go +++ b/stackit/internal/services/postgresql/instance/datasource.go @@ -3,6 +3,7 @@ package postgresql import ( "context" "fmt" + "net/http" "strings" "github.com/hashicorp/terraform-plugin-framework/datasource" @@ -14,6 +15,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/postgresql" ) @@ -195,6 +197,10 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).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 || oapiErr.StatusCode == http.StatusGone) { + resp.State.RemoveResource(ctx) + } core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/postgresql/instance/resource.go b/stackit/internal/services/postgresql/instance/resource.go index 9004c926..f41fb7d7 100644 --- a/stackit/internal/services/postgresql/instance/resource.go +++ b/stackit/internal/services/postgresql/instance/resource.go @@ -3,6 +3,7 @@ package postgresql import ( "context" "fmt" + "net/http" "strings" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" @@ -21,6 +22,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/postgresql" "github.com/stackitcloud/stackit-sdk-go/services/postgresql/wait" ) @@ -376,6 +378,11 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).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 || oapiErr.StatusCode == http.StatusGone) { + resp.State.RemoveResource(ctx) + return + } core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/rabbitmq/credential/datasource.go b/stackit/internal/services/rabbitmq/credential/datasource.go index 8d32180f..1bab12b3 100644 --- a/stackit/internal/services/rabbitmq/credential/datasource.go +++ b/stackit/internal/services/rabbitmq/credential/datasource.go @@ -3,6 +3,7 @@ package rabbitmq import ( "context" "fmt" + "net/http" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -13,6 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/rabbitmq" ) @@ -168,6 +170,10 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).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 credential", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/rabbitmq/credential/resource.go b/stackit/internal/services/rabbitmq/credential/resource.go index b66b82d3..7edbc05f 100644 --- a/stackit/internal/services/rabbitmq/credential/resource.go +++ b/stackit/internal/services/rabbitmq/credential/resource.go @@ -3,6 +3,7 @@ package rabbitmq import ( "context" "fmt" + "net/http" "strings" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -18,6 +19,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/rabbitmq" "github.com/stackitcloud/stackit-sdk-go/services/rabbitmq/wait" ) @@ -253,6 +255,11 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest, recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).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 credential", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/rabbitmq/instance/datasource.go b/stackit/internal/services/rabbitmq/instance/datasource.go index a77a0c55..125854b4 100644 --- a/stackit/internal/services/rabbitmq/instance/datasource.go +++ b/stackit/internal/services/rabbitmq/instance/datasource.go @@ -3,6 +3,7 @@ package rabbitmq import ( "context" "fmt" + "net/http" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -12,6 +13,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/rabbitmq" ) @@ -165,6 +167,10 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).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 || oapiErr.StatusCode == http.StatusGone) { + resp.State.RemoveResource(ctx) + } core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/rabbitmq/instance/resource.go b/stackit/internal/services/rabbitmq/instance/resource.go index 7e55a399..e3793492 100644 --- a/stackit/internal/services/rabbitmq/instance/resource.go +++ b/stackit/internal/services/rabbitmq/instance/resource.go @@ -3,6 +3,7 @@ package rabbitmq import ( "context" "fmt" + "net/http" "strings" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" @@ -21,6 +22,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/rabbitmq" "github.com/stackitcloud/stackit-sdk-go/services/rabbitmq/wait" ) @@ -299,6 +301,11 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).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 || oapiErr.StatusCode == http.StatusGone) { + resp.State.RemoveResource(ctx) + return + } core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/redis/credential/datasource.go b/stackit/internal/services/redis/credential/datasource.go index 4d1d5616..a9a88aa7 100644 --- a/stackit/internal/services/redis/credential/datasource.go +++ b/stackit/internal/services/redis/credential/datasource.go @@ -3,6 +3,7 @@ package redis import ( "context" "fmt" + "net/http" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -13,6 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/redis" ) @@ -159,6 +161,10 @@ func (r *credentialDataSource) Read(ctx context.Context, req datasource.ReadRequ recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).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 credential", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/redis/credential/resource.go b/stackit/internal/services/redis/credential/resource.go index 135a748f..7bce44ba 100644 --- a/stackit/internal/services/redis/credential/resource.go +++ b/stackit/internal/services/redis/credential/resource.go @@ -3,6 +3,7 @@ package redis import ( "context" "fmt" + "net/http" "strings" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -18,6 +19,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/redis" "github.com/stackitcloud/stackit-sdk-go/services/redis/wait" ) @@ -241,6 +243,11 @@ func (r *credentialResource) Read(ctx context.Context, req resource.ReadRequest, recordSetResp, err := r.client.GetCredentials(ctx, projectId, instanceId, credentialId).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 credential", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/redis/instance/datasource.go b/stackit/internal/services/redis/instance/datasource.go index fe9b1168..dfaeacc7 100644 --- a/stackit/internal/services/redis/instance/datasource.go +++ b/stackit/internal/services/redis/instance/datasource.go @@ -3,6 +3,7 @@ package redis import ( "context" "fmt" + "net/http" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -12,6 +13,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/redis" ) @@ -165,6 +167,10 @@ func (r *instanceDataSource) Read(ctx context.Context, req datasource.ReadReques instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).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 || oapiErr.StatusCode == http.StatusGone) { + resp.State.RemoveResource(ctx) + } core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err)) return } diff --git a/stackit/internal/services/redis/instance/resource.go b/stackit/internal/services/redis/instance/resource.go index 5420ca46..90f5e018 100644 --- a/stackit/internal/services/redis/instance/resource.go +++ b/stackit/internal/services/redis/instance/resource.go @@ -3,6 +3,7 @@ package redis import ( "context" "fmt" + "net/http" "strings" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" @@ -21,6 +22,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/redis" "github.com/stackitcloud/stackit-sdk-go/services/redis/wait" ) @@ -299,6 +301,11 @@ func (r *instanceResource) Read(ctx context.Context, req resource.ReadRequest, r instanceResp, err := r.client.GetInstance(ctx, projectId, instanceId).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 || oapiErr.StatusCode == http.StatusGone) { + resp.State.RemoveResource(ctx) + return + } core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading instance", fmt.Sprintf("Calling API: %v", err)) return }