Skip to content

Commit

Permalink
feat: d/vsphere_network add support for network_type
Browse files Browse the repository at this point in the history
Added the feature to allow for port group to be found if there are two port groups with the same name but one is standard virtual port group vs distributed virtual port group.

Signed-off-by: Jared Burns <jared.burns@broadcom.com>
  • Loading branch information
burnsjared0415 committed Oct 10, 2024
1 parent adf6714 commit cfd7226
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# <!-- markdownlint-disable first-line-h1 no-inline-html -->

## 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:
Expand Down
30 changes: 27 additions & 3 deletions vsphere/data_source_vsphere_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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. One of `DistributedVirtualPortgroup` for distributed port groups, `Network` for standard (host-based) port groups, or `OpaqueNetwork` for networks managed externally, such as those managed by NSX.",
Optional: true,
},
},
}
}
Expand All @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions vsphere/internal/helper/network/network_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
17 changes: 16 additions & 1 deletion website/docs/d/network.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -43,7 +58,7 @@ 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) The type for the discovered network. This is required if you have multiple port groups with the same name. This will be one of `DistributedVirtualPortgroup` for distributed port groups, `Network` for standard (host-based) port groups, or `OpaqueNetwork` for networks managed externally, such as those managed by NSX.
[docs-about-morefs]: /docs/providers/vsphere/index.html#use-of-managed-object-references-by-the-vsphere-provider

## Attribute Reference
Expand Down

0 comments on commit cfd7226

Please sign in to comment.