diff --git a/CHANGELOG.md b/CHANGELOG.md index 600308d50..5a75d46ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # +## 2.10.0 (Not Released) + +FEATURES: + +- `data/vsphere_network`: Adds ability to add `additional_filters` to find port groups based on network type of Standard virtual port + group, distributed virtual port group, or network port group. + [#2281](https://github.com/hashicorp/terraform-provider-vsphere/pull/2281) + ## 2.9.3 (October 8, 2024) BUG FIX: diff --git a/vsphere/data_source_vsphere_network.go b/vsphere/data_source_vsphere_network.go index 33bf8211d..538e7f7eb 100644 --- a/vsphere/data_source_vsphere_network.go +++ b/vsphere/data_source_vsphere_network.go @@ -36,6 +36,12 @@ func dataSourceVSphereNetwork() *schema.Resource { Description: "Id of the distributed virtual switch of which the port group is a part of", Optional: true, }, + "additional_filters": { + Type: schema.TypeMap, + Description: "Additional filters to uniquely identify the network", + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, }, } } @@ -53,9 +59,32 @@ func dataSourceVSphereNetworkRead(d *schema.ResourceData, meta interface{}) erro return fmt.Errorf("cannot locate datacenter: %s", err) } } - net, err := network.FromNameAndDVSUuid(client, name, dc, dvSwitchUUID) - if err != nil { - return fmt.Errorf("error fetching network: %s", err) + var net object.NetworkReference + var err error + + vimClient := client.Client + + additionalFilters := make(map[string]string) + if v, ok := d.GetOk("additional_filters"); ok { + for key, value := range v.(map[string]interface{}) { + additionalFilters[key] = value.(string) + } + } + + fmt.Printf("Searching for network with name: %s, additional filters: %v\n", name, additionalFilters) + + if dvSwitchUUID != "" { + // Handle distributed virtual switch port group + net, err = network.FromNameAndDVSUuid(client, name, dc, dvSwitchUUID) + if err != nil { + return fmt.Errorf("error fetching DVS network: %s", err) + } + } else { + // Handle standard switch port group, Distributed port Group or OpaqueNetwork + net, err = network.FromName(vimClient, name, dc, additionalFilters) + if err != nil { + return fmt.Errorf("error fetching standard switch network: %s", err) + } } d.SetId(net.Reference().Value) diff --git a/vsphere/internal/helper/network/network_helper.go b/vsphere/internal/helper/network/network_helper.go index 3f1a72afc..2900db75b 100644 --- a/vsphere/internal/helper/network/network_helper.go +++ b/vsphere/internal/helper/network/network_helper.go @@ -12,6 +12,7 @@ import ( "github.com/vmware/govmomi/find" "github.com/vmware/govmomi/object" "github.com/vmware/govmomi/view" + "github.com/vmware/govmomi/vim25" "github.com/vmware/govmomi/vim25/methods" "github.com/vmware/govmomi/vim25/mo" "github.com/vmware/govmomi/vim25/types" @@ -197,3 +198,43 @@ func dvsFromUUID(client *govmomi.Client, uuid string) (*object.VmwareDistributed return dvsFromMOID(client, resp.Returnval.Reference().Value) } + +func FromName(client *vim25.Client, name string, dc *object.Datacenter, additionalFilters map[string]string) (object.NetworkReference, error) { + ctx := context.TODO() + finder := find.NewFinder(client, true) + + // Set the datacenter + if dc != nil { + finder.SetDatacenter(dc) + } + + // Find the network by name + networks, err := finder.NetworkList(ctx, name) + if err != nil { + return nil, fmt.Errorf("error finding network %s: %v", name, err) + } + + // Debugging: Log the networks found + fmt.Printf("Networks found: %v\n", networks) + + // Filter networks by name and type + for _, network := range networks { + match := true + for key, value := range additionalFilters { + switch key { + case "network_type": + if network.Reference().Type != value { + match = false + } + } + if !match { + break + } + } + if match { + return network, nil + } + } + + return nil, fmt.Errorf("no network found matching the specified criteria") +} diff --git a/website/docs/d/network.html.markdown b/website/docs/d/network.html.markdown index 1b51a93c7..b85b22553 100644 --- a/website/docs/d/network.html.markdown +++ b/website/docs/d/network.html.markdown @@ -29,6 +29,23 @@ data "vsphere_network" "network" { } ``` +## Example Usage + +```hcl + +data "vsphere_datacenter" "datacenter" { + name = "dc-01" +} + +data "vsphere_network" "my_port_group" { + datacenter_id = data.vsphere_datacenter.datacenter.id + name = "VM Network" + additional_filters = { + network_type = "Network" + } +} +``` + ## Argument Reference The following arguments are supported: @@ -43,6 +60,9 @@ The following arguments are supported: network objects, the ID of the distributed virtual switch for which the port group belongs. It is useful to differentiate port groups with same name using the distributed virtual switch ID. +* `additional_filters` - (Optional) For standard virtual switch port groups, distributed virtual switch port groups and NSX port groups. + Networks are found by setting `network_type` to Network, DistributedVirtualPortgroup or OpaqueNetwork. This is required if you have + multiple port groups with the same name [docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider