Skip to content

Commit

Permalink
fix: primary ip assigned test
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Jan 3, 2025
1 parent a1699b9 commit 3b462ec
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 31 deletions.
51 changes: 22 additions & 29 deletions internal/server/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,19 +369,19 @@ func resourceServerCreate(ctx context.Context, d *schema.ResourceData, m interfa
createPublicNet := hcloud.ServerCreatePublicNet{}
for _, publicNetBlock := range publicNet.(*schema.Set).List() {
publicNetEntry := publicNetBlock.(map[string]interface{})
if enableIPv4, err := toServerPublicNet[bool](publicNetEntry, "ipv4_enabled"); err == nil {
if enableIPv4, err := ToPublicNetField[bool](publicNetEntry, "ipv4_enabled"); err == nil {
createPublicNet.EnableIPv4 = enableIPv4
}
if enableIPv6, err := toServerPublicNet[bool](publicNetEntry, "ipv6_enabled"); err == nil {
if enableIPv6, err := ToPublicNetField[bool](publicNetEntry, "ipv6_enabled"); err == nil {
createPublicNet.EnableIPv6 = enableIPv6
}
if ipv4, err := toServerPublicNet[int64](publicNetEntry, "ipv4"); err == nil && ipv4 != 0 {
if ipv4, err := ToPublicNetField[int](publicNetEntry, "ipv4"); err == nil && ipv4 != 0 {
createPublicNet.EnableIPv4 = true
createPublicNet.IPv4 = &hcloud.PrimaryIP{ID: ipv4}
createPublicNet.IPv4 = &hcloud.PrimaryIP{ID: util.CastInt64(ipv4)}
}
if ipv6, err := toServerPublicNet[int64](publicNetEntry, "ipv6"); err == nil && ipv6 != 0 {
if ipv6, err := ToPublicNetField[int](publicNetEntry, "ipv6"); err == nil && ipv6 != 0 {
createPublicNet.EnableIPv6 = true
createPublicNet.IPv6 = &hcloud.PrimaryIP{ID: ipv6}
createPublicNet.IPv6 = &hcloud.PrimaryIP{ID: util.CastInt64(ipv6)}
}
}
opts.PublicNet = &createPublicNet
Expand Down Expand Up @@ -678,10 +678,10 @@ func updatePublicNet(ctx context.Context, o interface{}, n interface{}, c *hclou
for _, d := range diffToRemove.List() {
fields := d.(map[string]interface{})
ipv4IDToRemove, ipv6IDToRemove = collectPrimaryIPIDs(fields)
if ipv4Enabled, err := toPublicNetPrimaryIPField[bool](fields, "ipv4_enabled"); err == nil {
if ipv4Enabled, err := ToPublicNetField[bool](fields, "ipv4_enabled"); err == nil {
ipv4EnabledInRemoveDiff = ipv4Enabled
}
if ipv6Enabled, err := toPublicNetPrimaryIPField[bool](fields, "ipv6_enabled"); err == nil {
if ipv6Enabled, err := ToPublicNetField[bool](fields, "ipv6_enabled"); err == nil {
ipv6EnabledInRemoveDiff = ipv6Enabled
}
}
Expand Down Expand Up @@ -720,7 +720,7 @@ func updatePublicNet(ctx context.Context, o interface{}, n interface{}, c *hclou
fields := d.(map[string]interface{})
ipv4IDToAdd, ipv6IDToAdd := collectPrimaryIPIDs(fields)

if ipv4Enabled, err := toPublicNetPrimaryIPField[bool](fields, "ipv4_enabled"); err == nil {
if ipv4Enabled, err := ToPublicNetField[bool](fields, "ipv4_enabled"); err == nil {
if err := publicNetUpdateDecision(ctx,
c,
ipv4Enabled,
Expand All @@ -733,7 +733,7 @@ func updatePublicNet(ctx context.Context, o interface{}, n interface{}, c *hclou
return err
}
}
if ipv6Enabled, err := toPublicNetPrimaryIPField[bool](fields, "ipv6_enabled"); err == nil {
if ipv6Enabled, err := ToPublicNetField[bool](fields, "ipv6_enabled"); err == nil {
if err := publicNetUpdateDecision(ctx,
c, ipv6Enabled,
ipv6EnabledInRemoveDiff,
Expand Down Expand Up @@ -1278,34 +1278,27 @@ func setProtection(ctx context.Context, c *hcloud.Client, server *hcloud.Server,
return hcloudutil.WaitForAction(ctx, &c.Action, action)
}

func toServerPublicNet[V int64 | bool](field map[string]interface{}, key string) (V, error) {
var op = "toServerPublicNet"
var valType V
if valType, ok := field[key].(V); ok {
return valType, nil
}
return valType, fmt.Errorf("%s: unable to apply value to public_net values", op)
}

func collectPrimaryIPIDs(primaryIPList map[string]interface{}) (int64, int64) {
var IPv4ID int64
var IPv6ID int64
if id, err := toPublicNetPrimaryIPField[int64](primaryIPList, "ipv4"); id != 0 && err == nil {
IPv4ID = id
if id, err := ToPublicNetField[int](primaryIPList, "ipv4"); id != 0 && err == nil {
IPv4ID = util.CastInt64(id)
}
if id, err := toPublicNetPrimaryIPField[int64](primaryIPList, "ipv6"); id != 0 && err == nil {
IPv6ID = id
if id, err := ToPublicNetField[int](primaryIPList, "ipv6"); id != 0 && err == nil {
IPv6ID = util.CastInt64(id)
}
return IPv4ID, IPv6ID
}

func toPublicNetPrimaryIPField[V int64 | bool](field map[string]interface{}, key string) (V, error) {
var op = "toPublicNetPrimaryIPField"
var fieldValue V
if fieldValue, ok := field[key].(V); ok {
return fieldValue, nil
func ToPublicNetField[V int | bool](field map[string]interface{}, key string) (V, error) {
var op = "ToPublicNetField"

if value, ok := field[key]; ok {
return value.(V), nil
}
return fieldValue, fmt.Errorf("%s: field does not contain ID", op)

var zero V
return zero, fmt.Errorf("%s: field does not contain key: %s", op, key)
}

func onServerCreateWithoutPublicNet(opts *hcloud.ServerCreateOpts, d *schema.ResourceData, fn func(opts *hcloud.ServerCreateOpts) error) diag.Diagnostics {
Expand Down
27 changes: 27 additions & 0 deletions internal/server/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/hetznercloud/hcloud-go/v2/hcloud"

Expand Down Expand Up @@ -1200,3 +1201,29 @@ func userDataHashSum(userData string) string {
sum := sha1.Sum([]byte(userData))
return base64.StdEncoding.EncodeToString(sum[:])
}

func TestToPublicNetField(t *testing.T) {
t.Run("int", func(t *testing.T) {
got, err := server.ToPublicNetField[int](map[string]any{"key": int(1)}, "key")
require.NoError(t, err)
require.Equal(t, int(1), got)
})

t.Run("bool", func(t *testing.T) {
got, err := server.ToPublicNetField[bool](map[string]any{"key": true}, "key")
require.NoError(t, err)
require.Equal(t, true, got)
})

t.Run("int not found", func(t *testing.T) {
got, err := server.ToPublicNetField[int](map[string]any{}, "key")
require.EqualError(t, err, "ToPublicNetField: field does not contain key: key")
require.Equal(t, int(0), got)
})

t.Run("bool not found", func(t *testing.T) {
got, err := server.ToPublicNetField[bool](map[string]any{}, "key")
require.EqualError(t, err, "ToPublicNetField: field does not contain key: key")
require.Equal(t, false, got)
})
}
4 changes: 2 additions & 2 deletions internal/teste2e/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ var (
TestLoadBalancerType = "lb11"

// TestDataCenter is the default datacenter where we execute our tests.
TestDataCenter = getEnv("TEST_DATACENTER", "nbg1-dc3")
TestDataCenter = getEnv("TEST_DATACENTER", "hel1-dc2")

// TestLocationName is the default location where we execute our tests.
TestLocationName = getEnv("TEST_LOCATION", "nbg1")
TestLocationName = getEnv("TEST_LOCATION", "hel1")
)

func ProtoV6ProviderFactories() map[string]func() (tfprotov6.ProviderServer, error) {
Expand Down

0 comments on commit 3b462ec

Please sign in to comment.