Skip to content

Commit 1a43c75

Browse files
authored
feat: add method to get DVS for DVP (#373)
## Issue N/A ## Description Add helper to retrieve the parent Distributed Switch object for a Distributed Port Group. --------- Signed-off-by: Tyler Gillson <tyler.gillson@gmail.com>
1 parent eca18fd commit 1a43c75

11 files changed

Lines changed: 138 additions & 30 deletions

File tree

api/vcenter/types.go

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,61 @@ const (
1212
// ClusterDefaultResourcePoolName is the default resource pool name for a cluster.
1313
ClusterDefaultResourcePoolName = "Resources"
1414

15-
// ClusterInventoryPath is the path for cluster inventory.
16-
ClusterInventoryPath = "/%s/host/%s"
17-
1815
// DefaultDomain is the default vCenter domain.
1916
DefaultDomain = "VSPHERE.LOCAL"
17+
)
18+
19+
const (
20+
// DatastoreInventoryPrefix is the prefix for datastore inventory.
21+
// Replacements: datacenter name.
22+
DatastoreInventoryPrefix = "/%s/datastore/"
23+
24+
// HostInventoryPath is the path for cluster or host system inventory.
25+
// Replacements: datacenter name, cluster or host system name.
26+
HostInventoryPath = "/%s/host/%s"
27+
28+
// HostInventoryPrefix is the prefix for host inventory.
29+
// Replacements: datacenter name.
30+
HostInventoryPrefix = "/%s/host/"
31+
32+
// HostInventoryGlob is the path for listing all host inventory in a
33+
// datacenter; including host systems and clusters.
34+
// Replacements: datacenter name.
35+
HostInventoryGlob = "/%s/host/*"
2036

21-
// HostSystemInventoryPath is the path for host system inventory.
22-
HostSystemInventoryPath = "/%s/host/%s/%s"
37+
// HostChildInventoryPath is the path for host system or resource pool inventory.
38+
// Replacements: datacenter name, cluster, host system, or resource pool name.
39+
HostChildInventoryPath = "/%s/host/%s/%s"
40+
41+
// NetworkInventoryPath is the path for network inventory.
42+
// Replacements: datacenter name, network name.
43+
NetworkInventoryPath = "/%s/network/%s"
44+
45+
// NetworkInventoryPrefix is the prefix for network inventory.
46+
// Replacements: datacenter name.
47+
NetworkInventoryPrefix = "/%s/network/"
2348

2449
// ResourcePoolInventoryPath is the path for resource pool inventory.
50+
// Replacements: datacenter name, cluster name, resource pool name.
2551
ResourcePoolInventoryPath = "/%s/host/%s/Resources/%s"
52+
53+
// ResourcePoolInventoryGlob is the path for listing all resource pools
54+
// in a cluster.
55+
// Replacements: datacenter name, cluster name.
56+
ResourcePoolInventoryGlob = "/%s/host/%s/Resources/*"
57+
58+
// ResourcePoolChildInventoryGlob is the path for listing all child inventory
59+
// in a resource pool.
60+
// Replacements: datacenter name, cluster name, resource pool name.
61+
ResourcePoolChildInventoryGlob = "/%s/host/%s/Resources/%s/*"
62+
63+
// VMFolderInventoryPath is the path for VM folder inventory.
64+
// Replacements: datacenter name, vm folder name.
65+
VMFolderInventoryPath = "/%s/vm/%s"
66+
67+
// VMFolderInventoryPrefix is the prefix for VM folder inventory.
68+
// Replacements: datacenter name.
69+
VMFolderInventoryPrefix = "/%s/vm/"
2670
)
2771

2872
// Account contains vCenter account details.

pkg/validators/computeresources/computeresources.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func resourcePoolUsage(ctx context.Context, rule v1alpha1.ComputeResourceRule, f
222222
// cpu & memory
223223
inventoryPath := fmt.Sprintf(vcenter.ResourcePoolInventoryPath, driver.Datacenter, rule.ClusterName, rule.EntityName)
224224
if rule.EntityName == vcenter.ClusterDefaultResourcePoolName {
225-
inventoryPath = fmt.Sprintf("/%s/host/%s/%s", driver.Datacenter, rule.ClusterName, rule.EntityName)
225+
inventoryPath = fmt.Sprintf(vcenter.HostChildInventoryPath, driver.Datacenter, rule.ClusterName, rule.EntityName)
226226
}
227227
resourcePool, virtualMachines, err := GetResourcePoolAndVMs(ctx, inventoryPath, finder)
228228
if err != nil {

pkg/validators/tags/tags.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,17 @@ func tagIsValid(tagsManager *tags.Manager, finder *find.Finder, datacenter strin
101101

102102
switch e := entity.Map[rule.EntityType]; e {
103103
case entity.Cluster:
104-
inventoryPath = fmt.Sprintf(vcenter.ClusterInventoryPath, datacenter, rule.EntityName)
104+
inventoryPath = fmt.Sprintf(vcenter.HostInventoryPath, datacenter, rule.EntityName)
105105
case entity.Datacenter:
106106
inventoryPath = rule.EntityName
107107
case entity.Folder:
108108
inventoryPath = rule.EntityName
109109
case entity.Host:
110-
inventoryPath = fmt.Sprintf(vcenter.HostSystemInventoryPath, datacenter, rule.ClusterName, rule.EntityName)
110+
inventoryPath = fmt.Sprintf(vcenter.HostChildInventoryPath, datacenter, rule.ClusterName, rule.EntityName)
111111
case entity.ResourcePool:
112112
inventoryPath = fmt.Sprintf(vcenter.ResourcePoolInventoryPath, datacenter, rule.ClusterName, rule.EntityName)
113113
if rule.EntityName == vcenter.ClusterDefaultResourcePoolName {
114-
inventoryPath = fmt.Sprintf("/%s/host/%s/%s", datacenter, rule.ClusterName, rule.EntityName)
114+
inventoryPath = fmt.Sprintf(vcenter.HostChildInventoryPath, datacenter, rule.ClusterName, rule.EntityName)
115115
}
116116
case entity.VirtualMachine:
117117
inventoryPath = rule.EntityName

pkg/vcsim/vcsim.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (v *VCSimulator) Start() {
9393
v.Options = govcOptions{
9494
InsecureConnection: strconv.FormatBool(true),
9595
Datacenter: "DC0",
96-
DistributedVirtualPortgroup: "DVPG0",
96+
DistributedVirtualPortgroup: "DC0_DVPG0",
9797
DistributedVirtualSwitch: "DVS0",
9898
Cluster: "DC0_C0",
9999
ResourcePool: "DC0_C0_RP0",

pkg/vsphere/cluster.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import (
99
"github.com/pkg/errors"
1010
"github.com/vmware/govmomi/find"
1111
"github.com/vmware/govmomi/object"
12+
13+
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
1214
)
1315

1416
// GetCluster returns the cluster if it exists
1517
func (v *VCenterDriver) GetCluster(ctx context.Context, finder *find.Finder, datacenter, clusterName string) (*object.ClusterComputeResource, error) {
16-
path := fmt.Sprintf("/%s/host/%s", datacenter, clusterName)
18+
path := fmt.Sprintf(vcenter.HostInventoryPath, datacenter, clusterName)
1719
cluster, err := finder.ClusterComputeResource(ctx, path)
1820
if err != nil {
1921
return nil, err
@@ -72,7 +74,7 @@ func (v *VCenterDriver) getClusterComputeResources(ctx context.Context, datacent
7274
if err != nil {
7375
return "", nil, err
7476
}
75-
prefix := fmt.Sprintf("/%s/host/", dc)
77+
prefix := fmt.Sprintf(vcenter.HostInventoryPrefix, dc)
7678

7779
ccrs, err := finder.ClusterComputeResourceList(ctx, "*")
7880
if err != nil {

pkg/vsphere/datastore.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/pkg/errors"
1010
"github.com/vmware/govmomi/find"
1111
"github.com/vmware/govmomi/object"
12+
13+
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
1214
)
1315

1416
// GetDatastore returns a datastore object if it exists
@@ -42,7 +44,7 @@ func (v *VCenterDriver) getDatastores(ctx context.Context, datacenter string) (s
4244
if err != nil {
4345
return "", nil, err
4446
}
45-
prefix := fmt.Sprintf("/%s/datastore/", dc)
47+
prefix := fmt.Sprintf(vcenter.DatastoreInventoryPrefix, dc)
4648

4749
ds, err := finder.DatastoreList(ctx, "*")
4850
if err != nil {

pkg/vsphere/folder.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/pkg/errors"
1010
"github.com/vmware/govmomi/find"
1111
"github.com/vmware/govmomi/object"
12+
13+
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
1214
)
1315

1416
// FolderExists checks if a folder exists in the vCenter inventory
@@ -77,5 +79,5 @@ func (v *VCenterDriver) GetVMFolders(ctx context.Context, datacenter string) ([]
7779
}
7880

7981
func folderPrefix(datacenter string) string {
80-
return fmt.Sprintf("/%s/vm/", datacenter)
82+
return fmt.Sprintf(vcenter.VMFolderInventoryPrefix, datacenter)
8183
}

pkg/vsphere/host.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import (
1919

2020
// GetHost returns the host system if it exists
2121
func (v *VCenterDriver) GetHost(ctx context.Context, finder *find.Finder, datacenter, clusterName, hostName string) (*object.HostSystem, error) {
22-
path := fmt.Sprintf("/%s/host/%s/%s", datacenter, clusterName, hostName)
22+
path := fmt.Sprintf(vcenter.HostChildInventoryPath, datacenter, clusterName, hostName)
2323

2424
// Handle datacenter level hosts
2525
if clusterName == "" {
26-
path = fmt.Sprintf("/%s/host/%s", datacenter, hostName)
26+
path = fmt.Sprintf(vcenter.HostInventoryPath, datacenter, hostName)
2727
}
2828

2929
host, err := finder.HostSystem(ctx, path)
@@ -40,9 +40,9 @@ func (v *VCenterDriver) GetHostSystems(ctx context.Context, datacenter, cluster
4040
return nil, err
4141
}
4242

43-
path := fmt.Sprintf("/%s/host/%s", datacenter, cluster)
43+
path := fmt.Sprintf(vcenter.HostInventoryPath, datacenter, cluster)
4444
if cluster == "" {
45-
path = fmt.Sprintf("/%s/host/*", datacenter)
45+
path = fmt.Sprintf(vcenter.HostInventoryGlob, datacenter)
4646
}
4747

4848
hss, err := finder.HostSystemList(ctx, path)

pkg/vsphere/network.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import (
99
"github.com/pkg/errors"
1010
"github.com/vmware/govmomi/find"
1111
"github.com/vmware/govmomi/object"
12+
"github.com/vmware/govmomi/vim25/mo"
13+
14+
"github.com/validator-labs/validator-plugin-vsphere/api/vcenter"
1215
)
1316

1417
// GetNetwork returns a network object if it exists
@@ -33,7 +36,7 @@ func (v *VCenterDriver) GetNetworkTypeByName(ctx context.Context, datacenter, na
3336
return "", err
3437
}
3538

36-
inventoryPath := fmt.Sprintf("/%s/network/%s", dc, name)
39+
inventoryPath := fmt.Sprintf(vcenter.NetworkInventoryPath, dc, name)
3740

3841
nr, err := finder.Network(ctx, inventoryPath)
3942
if err != nil {
@@ -80,7 +83,7 @@ func (v *VCenterDriver) getNetworkReferences(ctx context.Context, datacenter str
8083
if err != nil {
8184
return "", nil, err
8285
}
83-
prefix := fmt.Sprintf("/%s/network/", dc)
86+
prefix := fmt.Sprintf(vcenter.NetworkInventoryPrefix, dc)
8487

8588
ns, err := finder.NetworkList(ctx, "*")
8689
if err != nil {
@@ -129,6 +132,24 @@ func (v *VCenterDriver) GetDistributedVirtualPortgroups(ctx context.Context, dat
129132
return networks, nil
130133
}
131134

135+
// GetDistributedVirtualSwitchNameFromPortGroup returns the name of a distributed port group's distributed switch
136+
func (v *VCenterDriver) GetDistributedVirtualSwitchNameFromPortGroup(ctx context.Context, dvp *object.DistributedVirtualPortgroup) (string, error) {
137+
138+
var dpgMo mo.DistributedVirtualPortgroup
139+
if err := dvp.Properties(ctx, dvp.Reference(), []string{"config"}, &dpgMo); err != nil {
140+
return "", err
141+
}
142+
dvsRef := dpgMo.Config.DistributedVirtualSwitch.Reference()
143+
144+
// Retrieve the Distributed Virtual Switch object using its reference
145+
var dvsMo mo.DistributedVirtualSwitch
146+
if err := v.Client.RetrieveOne(ctx, dvsRef, []string{"config"}, &dvsMo); err != nil {
147+
return "", err
148+
}
149+
150+
return dvsMo.Config.GetDVSConfigInfo().Name, nil
151+
}
152+
132153
// GetDistributedVirtualSwitch returns a distributed virtual switch object if it exists
133154
func (v *VCenterDriver) GetDistributedVirtualSwitch(ctx context.Context, finder *find.Finder, path string) (*object.DistributedVirtualSwitch, error) {
134155
nr, err := finder.Network(ctx, path)

pkg/vsphere/network_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,37 @@ func TestGetNetworkTypeByName(t *testing.T) {
144144
})
145145
}
146146
}
147+
148+
func TestGetDistributedVirtualSwitchNameFromPortGroup(t *testing.T) {
149+
vcSim := vcsim.NewVCSim("admin@vsphere.local", 8457, logr.Logger{})
150+
vcSim.Start()
151+
defer vcSim.Shutdown()
152+
153+
ctx := context.Background()
154+
155+
driver, err := NewVCenterDriver(vcSim.Account, vcSim.Options.Datacenter, logr.Logger{})
156+
if err != nil {
157+
t.Fatal(err)
158+
}
159+
160+
finder, _, err := driver.GetFinderWithDatacenter(ctx, vcSim.Options.Datacenter)
161+
if err != nil {
162+
t.Errorf("GetFinderWithDatacenter() got %v", err)
163+
}
164+
165+
dvp, err := driver.GetDistributedVirtualPortgroup(ctx, finder, vcSim.Options.DistributedVirtualPortgroup)
166+
if err != nil {
167+
t.Errorf("GetDistributedVirtualPortgroup() got %v", err)
168+
}
169+
170+
dvsName, err := driver.GetDistributedVirtualSwitchNameFromPortGroup(ctx, dvp)
171+
if err != nil {
172+
t.Errorf("GetDistributedVirtualSwitchNameFromPortGroup() got %v", err)
173+
}
174+
175+
expected := vcSim.Options.DistributedVirtualSwitch
176+
177+
if !reflect.DeepEqual(dvsName, expected) {
178+
t.Errorf("GetDistributedVirtualSwitchNameFromPortGroup() got %s != expected %s", dvsName, expected)
179+
}
180+
}

0 commit comments

Comments
 (0)