Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(primary-ip): conflict when deleting IP #994

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading