Compare commits

...

1 Commits

Author SHA1 Message Date
Oscar Hermoso 75a387e19d 🚮 fix: handle when resources are removed externally (#71) 2025-03-15 10:11:52 +08:00
6 changed files with 38 additions and 3 deletions
@@ -239,6 +239,12 @@ func (r *loadBalancerResource) Read(ctx context.Context, req resource.ReadReques
return
}
if lbResp.StatusCode() == http.StatusNotFound {
tflog.Warn(ctx, fmt.Sprintf("Load balancer not found, removing from state: id=%s, name=%s", data.Id.String(), data.Name.ValueString()))
resp.State.RemoveResource(ctx)
return
}
if lbResp.StatusCode() != http.StatusOK {
resp.Diagnostics.AddError(
"Unexpected HTTP status code reading load balancer",
@@ -95,7 +95,7 @@ func (r *serverFirewallRulesResource) Create(ctx context.Context, req resource.C
}
tflog.Debug(ctx, fmt.Sprintf("Creating server firewall rules: server_id=%s", data.ServerId.String()))
serverResp, err := r.bc.client.PostServersServerIdActionsChangeAdvancedFirewallRulesWithResponse(
fwResp, err := r.bc.client.PostServersServerIdActionsChangeAdvancedFirewallRulesWithResponse(
ctx,
data.ServerId.ValueInt64(),
binarylane.PostServersServerIdActionsChangeAdvancedFirewallRulesJSONRequestBody{
@@ -109,10 +109,15 @@ func (r *serverFirewallRulesResource) Create(ctx context.Context, req resource.C
)
return
}
if serverResp.StatusCode() != http.StatusOK {
if fwResp.StatusCode() == http.StatusNotFound {
tflog.Warn(ctx, fmt.Sprintf("Server firewall rules not found, removing from state: server_id=%s", data.ServerId.String()))
resp.State.RemoveResource(ctx)
return
}
if fwResp.StatusCode() != http.StatusOK {
resp.Diagnostics.AddError(
"Unexpected HTTP status code creating server firewall rules",
fmt.Sprintf("Received %s creating server firewall rules: server_id=%s. Details: %s", serverResp.Status(), data.ServerId.String(), serverResp.Body))
fmt.Sprintf("Received %s creating server firewall rules: server_id=%s. Details: %s", fwResp.Status(), data.ServerId.String(), fwResp.Body))
return
}
+7
View File
@@ -576,6 +576,13 @@ func (r *serverResource) Read(ctx context.Context, req resource.ReadRequest, res
)
return
}
if serverResp.StatusCode() == http.StatusNotFound {
tflog.Warn(ctx, fmt.Sprintf("Server not found, removing from state: id=%s, name=%s", data.Id.String(), data.Name.ValueString()))
resp.State.RemoveResource(ctx)
return
}
if serverResp.StatusCode() != http.StatusOK {
resp.Diagnostics.AddError(
fmt.Sprintf("Unexpected HTTP status code %s reading server: name=%s, id=%s", serverResp.Status(), data.Name.ValueString(), data.Id.String()),
+6
View File
@@ -15,6 +15,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
// Ensure the implementation satisfies the expected interfaces.
@@ -123,6 +124,11 @@ func (r *sshKeyResource) Create(ctx context.Context, req resource.CreateRequest,
)
return
}
if sshResp.StatusCode() == http.StatusNotFound {
tflog.Warn(ctx, fmt.Sprintf("SSH Key not found, removing from state: name=%s", data.Name.String()))
resp.State.RemoveResource(ctx)
return
}
if sshResp.StatusCode() != http.StatusOK {
resp.Diagnostics.AddError(
"Unexpected HTTP status code creating SSH key",
+5
View File
@@ -130,6 +130,11 @@ func (r *vpcResource) Read(ctx context.Context, req resource.ReadRequest, resp *
)
return
}
if vpcResp.StatusCode() == http.StatusNotFound {
tflog.Warn(ctx, fmt.Sprintf("VPC not found, removing from state: id=%d", data.Id.ValueInt64()))
resp.State.RemoveResource(ctx)
return
}
if vpcResp.StatusCode() != http.StatusOK {
resp.Diagnostics.AddError(
"Unexpected HTTP status code reading VPC",
@@ -17,6 +17,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
var (
@@ -123,6 +124,11 @@ func (r *vpcRouteEntriesResource) Read(ctx context.Context, req resource.ReadReq
)
return
}
if vpcResp.StatusCode() == http.StatusNotFound {
tflog.Warn(ctx, fmt.Sprintf("VPC route entries not found, removing from state: vpc_id=%d", data.VpcId.ValueInt64()))
resp.State.RemoveResource(ctx)
return
}
if vpcResp.StatusCode() != http.StatusOK {
resp.Diagnostics.AddError(
"Unexpected HTTP status code reading VPC route entries",