Skip to content

feat: add DiffSuppressFunc for network_adapters on r/host_virtual_switch #2388

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

Merged
merged 1 commit into from
Apr 23, 2025
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
45 changes: 41 additions & 4 deletions vsphere/host_virtual_switch_structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ func schemaHostVirtualSwitchBondBridge() map[string]*schema.Schema {

// HostVirtualSwitchBondBridge
"network_adapters": {
Type: schema.TypeList,
Required: true,
Description: "The list of network adapters to bind to this virtual switch.",
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeList,
Required: true,
Description: "The list of network adapters to bind to this virtual switch.",
Elem: &schema.Schema{Type: schema.TypeString},
DiffSuppressFunc: networkAdaptersSupressDiff,
},
}
}
Expand Down Expand Up @@ -199,3 +200,39 @@ func splitHostVirtualSwitchID(raw string) (string, string, error) {
func virtualSwitchIDsFromResourceID(d *schema.ResourceData) (string, string, error) {
return splitHostVirtualSwitchID(d.Id())
}

// networkAdaptersSupressDiff checks if the 'network_adapters' set has changed, ignoring and difference
// in the order of elements.
func networkAdaptersSupressDiff(_, _, _ string, d *schema.ResourceData) bool {
o, n := d.GetChange("network_adapters")
oldAdapters := o.([]interface{})
newAdapters := n.([]interface{})

if len(oldAdapters) != len(newAdapters) {
return false
}

for _, oldAdapter := range oldAdapters {
if found := isAdapterFound(oldAdapter, newAdapters); !found {
return false
}
}

for _, newAdapter := range newAdapters {
if found := isAdapterFound(newAdapter, oldAdapters); !found {
return false
}
}

return true
}

func isAdapterFound(adapter interface{}, set []interface{}) bool {
for _, v := range set {
if adapter == v {
return true
}
}

return false
}
Loading