Skip to content

Commit

Permalink
fix(v6provider/refresh): refreshing missing resources now clears them…
Browse files Browse the repository at this point in the history
… from state (#133)

The new resources implemented with the Plugin Framework had a bug where
they would not clear our state of resources which no longer exist in the
API.

This has now been fixed, so resources deleted outside of Terraform are
correctly detected as missing, yielding a plan to recreate them.

Explicitly testing this behavior seems to be fraught with annoyance and
flickering results, so for now I've left that out.
  • Loading branch information
jimehk authored Jun 20, 2024
1 parent b4cb8bf commit 0ce024b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 55 deletions.
28 changes: 16 additions & 12 deletions internal/v6provider/resource_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ 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/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/krystal/go-katapult"
"github.com/krystal/go-katapult/core"
Expand Down Expand Up @@ -215,7 +214,7 @@ func (r *IPResource) Create(
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)

if err := r.IPRead(ctx, &plan, &resp.State); err != nil {
if err := r.IPRead(ctx, ip.ID, &plan); err != nil {
resp.Diagnostics.AddError("IP Address Read Error", err.Error())
return
}
Expand All @@ -236,7 +235,16 @@ func (r *IPResource) Read(
return
}

if err := r.IPRead(ctx, state, &resp.State); err != nil {
if err := r.IPRead(ctx, state.ID.ValueString(), state); err != nil {
if errors.Is(err, katapult.ErrNotFound) {
r.M.Logger.Info(
"IP Address not found, removing from state",
"id", state.ID.ValueString(),
)
resp.State.RemoveResource(ctx)
return
}

resp.Diagnostics.AddError("IP Address Read Error", err.Error())
return
}
Expand Down Expand Up @@ -266,7 +274,8 @@ func (r *IPResource) Update(
return
}

ipRef := core.IPAddressRef{ID: state.ID.ValueString()}
id := state.ID.ValueString()
ipRef := core.IPAddressRef{ID: id}
args := &core.IPAddressUpdateArguments{}

if !plan.VIP.Equal(state.VIP) {
Expand All @@ -284,7 +293,7 @@ func (r *IPResource) Update(
return
}

if err := r.IPRead(ctx, &plan, &resp.State); err != nil {
if err := r.IPRead(ctx, id, &plan); err != nil {
resp.Diagnostics.AddError("IP Address Read Error", err.Error())
return
}
Expand Down Expand Up @@ -314,16 +323,11 @@ func (r *IPResource) Delete(

func (r *IPResource) IPRead(
ctx context.Context,
id string,
model *IPResourceModel,
state *tfsdk.State,
) error {
ip, _, err := r.M.Core.IPAddresses.GetByID(ctx, model.ID.ValueString())
ip, _, err := r.M.Core.IPAddresses.GetByID(ctx, id)
if err != nil {
if errors.Is(err, katapult.ErrNotFound) {
state.RemoveResource(ctx)
return nil
}

return err
}

Expand Down
30 changes: 14 additions & 16 deletions internal/v6provider/resource_load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/krystal/go-katapult"
"github.com/krystal/go-katapult/core"
Expand Down Expand Up @@ -199,7 +198,7 @@ func (r *LoadBalancerResource) Create(
return
}

if err := r.LoadBalancerRead(ctx, lb.ID, &plan, &resp.State); err != nil {
if err := r.LoadBalancerRead(ctx, lb.ID, &plan); err != nil {
resp.Diagnostics.AddError("Load Balancer Read Error", err.Error())
return
}
Expand All @@ -219,12 +218,18 @@ func (r *LoadBalancerResource) Read(
return
}

if err := r.LoadBalancerRead(
ctx,
state.ID.ValueString(),
state,
&resp.State,
); err != nil {
err := r.LoadBalancerRead(ctx, state.ID.ValueString(), state)
if err != nil {
if errors.Is(err, katapult.ErrNotFound) {
r.M.Logger.Info(
"Load Balancer not found, removing from state",
"id", state.ID.ValueString(),
)
resp.State.RemoveResource(ctx)

return
}

resp.Diagnostics.AddError("Load Balancer Read Error", err.Error())
return
}
Expand Down Expand Up @@ -281,7 +286,7 @@ func (r *LoadBalancerResource) Update(
return
}

if err := r.LoadBalancerRead(ctx, id, &plan, &resp.State); err != nil {
if err := r.LoadBalancerRead(ctx, id, &plan); err != nil {
resp.Diagnostics.AddError("Load Balancer Read Error", err.Error())
return
}
Expand Down Expand Up @@ -323,16 +328,9 @@ func (r *LoadBalancerResource) LoadBalancerRead(
ctx context.Context,
id string,
model *LoadBalancerResourceModel,
state *tfsdk.State,
) error {
lb, _, err := r.M.Core.LoadBalancers.GetByID(ctx, id)
if err != nil {
if errors.Is(err, katapult.ErrNotFound) {
state.RemoveResource(ctx)

return nil
}

return err
}

Expand Down
41 changes: 14 additions & 27 deletions internal/v6provider/resource_load_balancer_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/krystal/go-katapult"
Expand Down Expand Up @@ -391,11 +390,7 @@ func (r *LoadBalancerRuleResource) Create(
return
}

if err := r.LoadBalancerRuleRead(ctx,
lbr.ID,
&plan,
&resp.State,
); err != nil {
if err := r.LoadBalancerRuleRead(ctx, lbr.ID, &plan); err != nil {
resp.Diagnostics.AddError(
"LoadBalancerRule Read Error",
"Error reading LoadBalancerRule: "+err.Error(),
Expand All @@ -417,11 +412,17 @@ func (r *LoadBalancerRuleResource) Read(
return
}

if err := r.LoadBalancerRuleRead(ctx,
state.ID.ValueString(),
state,
&resp.State,
); err != nil {
err := r.LoadBalancerRuleRead(ctx, state.ID.ValueString(), state)
if err != nil {
if errors.Is(err, katapult.ErrNotFound) {
r.M.Logger.Info(
"LoadBalancerRule not found, removing from state",
"id", state.ID.ValueString(),
)
resp.State.RemoveResource(ctx)
return
}

resp.Diagnostics.AddError(
"LoadBalancerRule Read Error",
"Error reading LoadBalancerRule: "+err.Error(),
Expand Down Expand Up @@ -454,7 +455,6 @@ func (r *LoadBalancerRuleResource) Update(
}

id := state.ID.ValueString()

lbrRef := core.LoadBalancerRuleRef{ID: id}
args, diags := buildLoadBalancerRuleUpdateArgs(ctx, &plan, &state)
resp.Diagnostics.Append(diags...)
Expand All @@ -475,12 +475,7 @@ func (r *LoadBalancerRuleResource) Update(
plan.LoadBalancerID = state.LoadBalancerID
}

if err := r.LoadBalancerRuleRead(
ctx,
id,
&plan,
&resp.State,
); err != nil {
if err = r.LoadBalancerRuleRead(ctx, id, &plan); err != nil {
resp.Diagnostics.AddError(
"LoadBalancerRule Read Error",
"Error reading LoadBalancerRule: "+err.Error(),
Expand Down Expand Up @@ -526,17 +521,9 @@ func (r *LoadBalancerRuleResource) LoadBalancerRuleRead(
ctx context.Context,
id string,
model *LoadBalancerRuleResourceModel,
state *tfsdk.State,
) error {
lbr, _, err := r.M.Core.LoadBalancerRules.GetByID(ctx,
id)
lbr, _, err := r.M.Core.LoadBalancerRules.GetByID(ctx, id)
if err != nil {
if errors.Is(err, katapult.ErrNotFound) {
state.RemoveResource(ctx)

return nil
}

return err
}

Expand Down

0 comments on commit 0ce024b

Please sign in to comment.