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 6046f0b
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 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 `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:
Expand Down
35 changes: 32 additions & 3 deletions vsphere/data_source_vsphere_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
},
},
}
}
Expand All @@ -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)
Expand Down
41 changes: 41 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,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")
}
20 changes: 20 additions & 0 deletions website/docs/d/network.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down

0 comments on commit 6046f0b

Please sign in to comment.