From 09a4ecf9d9e9d1d41a6a1e906013ebc50efd1efa Mon Sep 17 00:00:00 2001 From: Lucas Bremgartner Date: Wed, 20 Nov 2024 15:43:37 +0100 Subject: [PATCH] bugfix: fix interface sort order Signed-off-by: Lucas Bremgartner --- internal/instance/resource_instance.go | 23 +++++++++++++++++------ internal/network/resource_network_test.go | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/internal/instance/resource_instance.go b/internal/instance/resource_instance.go index 33e9ce3..f2bfcd5 100644 --- a/internal/instance/resource_instance.go +++ b/internal/instance/resource_instance.go @@ -27,7 +27,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types/basetypes" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" - "github.com/lxc/incus/v6/client" + incus "github.com/lxc/incus/v6/client" "github.com/lxc/incus/v6/shared/api" "github.com/lxc/terraform-provider-incus/internal/common" @@ -739,7 +739,7 @@ func (r InstanceResource) SyncState(ctx context.Context, tfState *tfsdk.State, s m.IPv6 = types.StringNull() m.MAC = types.StringNull() - // First there is an access_interface set, extract IPv4, IPv4, and + // First there is an access_interface set, extract IPv4, IPv6, and // MAC addresses from it. accIface, ok := instance.Config["user.access_interface"] if ok { @@ -1336,13 +1336,24 @@ func (s sortedInterfaces) Swap(i, j int) { } func (s sortedInterfaces) Less(i, j int) bool { + favorWithValue := func(a, b string) bool { + if a == "" && b == "" { + return false + } + + if len(a) > 0 && len(b) > 0 { + return false + } + return true + } + // Favor those with a host interface name. - if s[i].HostName != s[j].HostName { + if favorWithValue(s[i].HostName, s[j].HostName) { return s[i].HostName == "" } // Favor those with a MAC address. - if s[i].Hwaddr != s[j].Hwaddr { + if favorWithValue(s[i].Hwaddr, s[j].Hwaddr) { return s[i].Hwaddr == "" } @@ -1370,8 +1381,8 @@ func (s sortedInterfaces) Less(i, j int) bool { return false } -// findAddresses returns looks for the most optimal interface on the -// instance to return the IPv4, IPv6 and MAC address and interface name from. +// findAddresses looks for the most optimal interface on the instance to return +// the IPv4, IPv6 and MAC address and interface name from. func findAddresses(state *api.InstanceState) (string, string, string, string, bool) { if len(state.Network) == 0 { return "", "", "", "", false diff --git a/internal/network/resource_network_test.go b/internal/network/resource_network_test.go index 3403338..538e9fa 100644 --- a/internal/network/resource_network_test.go +++ b/internal/network/resource_network_test.go @@ -4,7 +4,7 @@ import ( "fmt" "testing" - "github.com/dustinkirkland/golang-petname" + petname "github.com/dustinkirkland/golang-petname" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/lxc/terraform-provider-incus/internal/acctest"