diff --git a/CHANGELOG.md b/CHANGELOG.md index 600308d50..c109b0265 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # +## 2.10.0 (Not Released) + +FEATURES: + +- `data/vsphere_network`: Adds ability to add `network_type` 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..dc56f84b7 100644 --- a/vsphere/data_source_vsphere_network.go +++ b/vsphere/data_source_vsphere_network.go @@ -36,6 +36,11 @@ func dataSourceVSphereNetwork() *schema.Resource { Description: "Id of the distributed virtual switch of which the port group is a part of", Optional: true, }, + "network_type": { + Type: schema.TypeString, + Description: "The type of the network (e.g., DistributedVirtualPortgroup, Network)", + Optional: true, + }, }, } } @@ -53,9 +58,28 @@ 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 + + networkType := "" + if v, ok := d.GetOk("network_type"); ok { + networkType = v.(string) + } + + 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, networkType) + 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..240503be3 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,31 @@ func dvsFromUUID(client *govmomi.Client, uuid string) (*object.VmwareDistributed return dvsFromMOID(client, resp.Returnval.Reference().Value) } + +// FromName fetches a network by name and network_type. +func FromName(client *vim25.Client, name string, dc *object.Datacenter, networkType 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) + } + + fmt.Printf("Networks found: %v\n", networks) + + // Filter networks by type + for _, network := range networks { + if network.Reference().Type == networkType { + 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..4f66a3b07 100644 --- a/website/docs/d/network.html.markdown +++ b/website/docs/d/network.html.markdown @@ -29,6 +29,21 @@ 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" + network_type = "Network" + } +``` + ## Argument Reference The following arguments are supported: @@ -43,7 +58,10 @@ 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. - +* `network_type` - (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 ## Attribute Reference