Skip to content

Commit

Permalink
fix(server): migration to explicit primary IPs fails
Browse files Browse the repository at this point in the history
When migrating a server from the previously used implicit primary IPs to
using the `hcloud_primary_ip` resources and specifying those in the
`hcloud_server.public_net` argument failed, because the state for
the `public_net` was not correctly populated. This caused the servers to
require an unnecessary re-attach of the primary ips.

Fixes #577
  • Loading branch information
apricote committed Nov 23, 2022
1 parent 06139fb commit 8601a63
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/server/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func Resource() *schema.Resource {
"public_net": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
DiffSuppressFunc: func(_, _, _ string, d *schema.ResourceData) bool {
// Diff is only valid if "public_net" resource is set in
// terraform configuration.
Expand Down Expand Up @@ -1123,6 +1124,10 @@ func getServerAttributes(d *schema.ResourceData, s *hcloud.Server) map[string]in
res["network"] = networkToTerraformNetworks(s.PrivateNet)
}

// Wrap in slice, as type of public_net is Set, even though only one element
// can be set.
res["public_net"] = []map[string]interface{}{publicNetToTerraformPublicNet(s.PublicNet)}

if s.PlacementGroup != nil {
res["placement_group_id"] = s.PlacementGroup.ID
}
Expand All @@ -1147,6 +1152,22 @@ func networkToTerraformNetworks(privateNetworks []hcloud.ServerPrivateNet) []map
return tfPrivateNetworks
}

func publicNetToTerraformPublicNet(publicNet hcloud.ServerPublicNet) map[string]interface{} {
tfPublicNet := make(map[string]interface{})

if !publicNet.IPv4.IsUnspecified() {
tfPublicNet["ipv4_enabled"] = true
tfPublicNet["ipv4"] = publicNet.IPv4.ID
}

if !publicNet.IPv6.IsUnspecified() {
tfPublicNet["ipv6_enabled"] = true
tfPublicNet["ipv6"] = publicNet.IPv6.ID
}

return tfPublicNet
}

func getPlacementGroup(ctx context.Context, c *hcloud.Client, id int) (*hcloud.PlacementGroup, error) {
placementGroup, _, err := c.PlacementGroup.GetByID(ctx, id)
if err != nil {
Expand Down

0 comments on commit 8601a63

Please sign in to comment.