Skip to content

Commit

Permalink
fix(primary-ip): conflict when deleting IP
Browse files Browse the repository at this point in the history
There sometimes happens a conflict in terraform 1.9+ as it handles the
state update before a delete differently. Both the server resource as
well as the Primary IP resource try to shut down the server, even though
the Primary IP should not as it is no longer assigned to the server.

This causes our e2e tests to fail.

This fixes the regression by explicitly checking that the server still
has the IP assigned before trying to detach it.
  • Loading branch information
apricote committed Sep 11, 2024
1 parent 89cfcd8 commit 7f931ff
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions internal/primaryip/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hetznercloud/hcloud-go/hcloud"

"github.com/hetznercloud/terraform-provider-hcloud/internal/util/hcloudutil"
)

Expand Down Expand Up @@ -256,22 +257,26 @@ func resourcePrimaryIPDelete(ctx context.Context, d *schema.ResourceData, m inte

if assigneeID, ok := d.GetOk("assignee_id"); ok && assigneeID != 0 {
if server, _, err := client.Server.Get(ctx, strconv.Itoa(assigneeID.(int))); err == nil && server != nil {
offAction, _, _ := client.Server.Poweroff(ctx, server)
// if offErr != nil {
// return hcloudutil.ErrorToDiag(offErr)
// }
if offActionErr := hcloudutil.WaitForAction(ctx, &client.Action, offAction); offActionErr != nil {
return hcloudutil.ErrorToDiag(offActionErr)
}
// dont catch error, because its possible that the primary IP got already unassigned on server destroy
UnassignPrimaryIP(ctx, client, primaryIPID)

onAction, _, _ := client.Server.Poweron(ctx, server)
// if onErr != nil {
// return hcloudutil.ErrorToDiag(onErr)
// }
if onActionErr := hcloudutil.WaitForAction(ctx, &client.Action, onAction); onActionErr != nil {
return hcloudutil.ErrorToDiag(onActionErr)
// The server does not have this primary ip assigned anymore, no need to try to detach it before deleting
// Workaround for https://github.com/hashicorp/terraform/issues/35568
if server.PublicNet.IPv4.ID == assigneeID || server.PublicNet.IPv6.ID == assigneeID {
offAction, _, _ := client.Server.Poweroff(ctx, server)
// if offErr != nil {
// return hcloudutil.ErrorToDiag(offErr)
// }
if offActionErr := hcloudutil.WaitForAction(ctx, &client.Action, offAction); offActionErr != nil {
return hcloudutil.ErrorToDiag(offActionErr)
}
// dont catch error, because its possible that the primary IP got already unassigned on server destroy
UnassignPrimaryIP(ctx, client, primaryIPID)

onAction, _, _ := client.Server.Poweron(ctx, server)
// if onErr != nil {
// return hcloudutil.ErrorToDiag(onErr)
// }
if onActionErr := hcloudutil.WaitForAction(ctx, &client.Action, onAction); onActionErr != nil {
return hcloudutil.ErrorToDiag(onActionErr)
}
}
}
}
Expand Down

0 comments on commit 7f931ff

Please sign in to comment.