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

bugfix: fix interface sort order #171

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
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
23 changes: 17 additions & 6 deletions internal/instance/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: line break

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a line break here.

Do you have some rules I can align to, about when and where you would like to have line breaks?

My feeling says: either I omit the line breaks, since this is a very short function, where all the pieces belong together (no real paragraphs in the sense that they touch different things) or I would add an other new line also after the second if block (before the return).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essentially line break after every block is the coding style we've been going with.


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 == ""
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/network/resource_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading