Skip to content

Commit

Permalink
Finder: support DistributedVirtualSwitch traversal
Browse files Browse the repository at this point in the history
While the native vSphere SearchIndex and ContainerView APIs don't support
inventory paths with this structure, it is useful for use with NSXt3 where
portgroup may not be unique within the same network inventory folder.
  • Loading branch information
dougm committed May 29, 2020
1 parent 10c22fd commit 7cdad99
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
5 changes: 5 additions & 0 deletions govc/test/import.bats
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ load test_helper
run govc import.ovf -options - "$ovf" <<<"$options"
assert_failure # 2 networks have the same name

options=$(jq ".NetworkMapping[].Network = \"DVS0/NSX-dvpg\"" <<<"$spec")

run govc import.ovf -name ttylinux2 -options - "$ovf" <<<"$options"
assert_success # switch_name/portgroup_name is unique

switch=$(govc find -i network -name DVS0)
id=$(govc find -i network -config.distributedVirtualSwitch "$switch" -name NSX-dvpg)
options=$(jq ".NetworkMapping[].Network = \"$id\"" <<<"$spec")
Expand Down
16 changes: 16 additions & 0 deletions govc/test/network.bats
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ load test_helper

run govc device.remove -vm $vm $eth0
assert_failure "govc: device '$eth0' not found"

# Test PG's with the same name
run govc dvs.create DVS1 # DVS0 already exists
assert_success

run govc dvs.portgroup.add -dvs DVS0 -type ephemeral NSX-dvpg
assert_success

run govc dvs.portgroup.add -dvs DVS1 -type ephemeral NSX-dvpg
assert_success

run govc vm.network.add -vm $vm -net NSX-dvpg
assert_failure # resolves to multiple networks

run govc vm.network.add -vm $vm -net DVS0/NSX-dvpg
assert_success # switch_name/portgroup_name is unique
}

@test "network change backing" {
Expand Down
1 change: 1 addition & 0 deletions govc/vm/network/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func (cmd *add) Description() string {
Examples:
govc vm.network.add -vm $vm -net "VM Network" -net.adapter e1000e
govc vm.network.add -vm $vm -net SwitchName/PortgroupName
govc device.info -vm $vm ethernet-*`
}

Expand Down
65 changes: 65 additions & 0 deletions list/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ func (l Lister) List(ctx context.Context) ([]Element, error) {
return l.ListHostSystem(ctx)
case "VirtualApp":
return l.ListVirtualApp(ctx)
case "VmwareDistributedVirtualSwitch", "DistributedVirtualSwitch":
return l.ListDistributedVirtualSwitch(ctx)
default:
return nil, fmt.Errorf("cannot traverse type " + l.Reference.Type)
}
Expand Down Expand Up @@ -497,6 +499,69 @@ func (l Lister) ListHostSystem(ctx context.Context) ([]Element, error) {
return es, nil
}

func (l Lister) ListDistributedVirtualSwitch(ctx context.Context) ([]Element, error) {
ospec := types.ObjectSpec{
Obj: l.Reference,
Skip: types.NewBool(true),
}

fields := []string{
"portgroup",
}

for _, f := range fields {
tspec := types.TraversalSpec{
Path: f,
Skip: types.NewBool(false),
Type: "DistributedVirtualSwitch",
}

ospec.SelectSet = append(ospec.SelectSet, &tspec)
}

childTypes := []string{
"DistributedVirtualPortgroup",
}

var pspecs []types.PropertySpec
for _, t := range childTypes {
pspec := types.PropertySpec{
Type: t,
}

if l.All {
pspec.All = types.NewBool(true)
} else {
pspec.PathSet = []string{"name"}
}

pspecs = append(pspecs, pspec)
}

req := types.RetrieveProperties{
SpecSet: []types.PropertyFilterSpec{
{
ObjectSet: []types.ObjectSpec{ospec},
PropSet: pspecs,
},
},
}

var dst []interface{}

err := l.retrieveProperties(ctx, req, &dst)
if err != nil {
return nil, err
}

es := []Element{}
for _, v := range dst {
es = append(es, ToElement(v.(mo.Reference), l.Prefix))
}

return es, nil
}

func (l Lister) ListVirtualApp(ctx context.Context) ([]Element, error) {
ospec := types.ObjectSpec{
Obj: l.Reference,
Expand Down

0 comments on commit 7cdad99

Please sign in to comment.