Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: support find.Network by cluster path #3045

Merged
merged 2 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions find/finder.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2014-2020 VMware, Inc. All Rights Reserved.
Copyright (c) 2014-2023 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -784,6 +784,11 @@ func (f *Finder) NetworkList(ctx context.Context, path string) ([]object.Network
}

if len(ns) == 0 {
net, nerr := f.networkByID(ctx, path)
if nerr == nil {
return []object.NetworkReference{net}, nil
}

return nil, &NotFoundError{"network", path}
}

Expand All @@ -798,18 +803,13 @@ func (f *Finder) NetworkList(ctx context.Context, path string) ([]object.Network
// Examples:
// - Name: "dvpg-1"
// - Inventory Path: "vds-1/dvpg-1"
// - Cluster Path: "/dc-1/host/cluster-1/dvpg-1"
// - ManagedObject ID: "DistributedVirtualPortgroup:dvportgroup-53"
// - Logical Switch UUID: "da2a59b8-2450-4cb2-b5cc-79c4c1d2144c"
// - Segment ID: "/infra/segments/vnet_ce50e69b-1784-4a14-9206-ffd7f1f146f7"
func (f *Finder) Network(ctx context.Context, path string) (object.NetworkReference, error) {
networks, err := f.NetworkList(ctx, path)
if err != nil {
if _, ok := err.(*NotFoundError); ok {
net, nerr := f.networkByID(ctx, path)
if nerr == nil {
return net, nil
}
}
return nil, err
}

Expand Down
107 changes: 107 additions & 0 deletions find/finder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
Copyright (c) 2023-2023 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package find_test // TODO: move ../simulator/finder_test.go tests here

import (
"context"
"testing"

"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
)

func TestFindNetwork(t *testing.T) {
model := simulator.VPX()
model.PortgroupNSX = 3

simulator.Test(func(ctx context.Context, c *vim25.Client) {
finder := find.NewFinder(c)
pc := property.DefaultCollector(c)

pgs, err := finder.NetworkList(ctx, "DC0_NSXPG*")
if err != nil {
t.Fatal(err)
}

// Rename DC0_NSXPG1 to DC0_NSXPG0 so we have a duplicate name
task, err := pgs[1].(*object.DistributedVirtualPortgroup).Rename(ctx, pgs[0].(*object.DistributedVirtualPortgroup).Name())
if err != nil {
t.Fatal(err)
}
err = task.Wait(ctx)
if err != nil {
t.Fatal(err)
}

// 2 PGs, same switch, same name
pgs, err = finder.NetworkList(ctx, "DC0_NSXPG0")
if err != nil {
t.Fatal(err)
}

if len(pgs) != 2 {
t.Fatalf("expected 2 NSX PGs, got %d", len(pgs))
}

for _, pg := range pgs {
// Using InventoryPath fails as > 1 are found
_, err = finder.Network(ctx, pg.GetInventoryPath())
if _, ok := err.(*find.MultipleFoundError); !ok {
t.Fatalf("expected MultipleFoundError, got %s", err)
}

// Find by MOID
_, err = finder.Network(ctx, pg.Reference().String())
if err != nil {
t.Errorf("find by moid: %s", err)
}

// Find by Switch UUID
var props mo.DistributedVirtualPortgroup
err = pc.RetrieveOne(ctx, pg.Reference(), []string{"config.logicalSwitchUuid", "config.segmentId"}, &props)
if err != nil {
t.Fatal(err)
}

net, err := finder.Network(ctx, props.Config.LogicalSwitchUuid)
if err != nil {
t.Fatal(err)
}

if net.Reference() != pg.Reference() {
t.Errorf("%s vs %s", net.Reference(), pg.Reference())
}

net, err = finder.Network(ctx, props.Config.SegmentId)
if err != nil {
t.Fatal(err)
}

networks, err := finder.NetworkList(ctx, props.Config.SegmentId)
if err != nil {
t.Fatal(err)
}
if len(networks) != 1 {
t.Errorf("expected 1 network, found %d", len(networks))
}
}
}, model)
}
5 changes: 5 additions & 0 deletions govc/test/import.bats
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ load test_helper
assert_success # switch_name/portgroup_name is unique
grep -v "invalid NetworkMapping.Name" <<<"$output"

options=$(jq ".NetworkMapping[].Network = \"/DC0/host/DC0_C0/DC0_DVPG0\"" <<<"$spec")

run govc import.ovf -name ttylinux3 -options - "$ovf" <<<"$options"
assert_success # cluster path 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
4 changes: 3 additions & 1 deletion list/lister.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2014-2016 VMware, Inc. All Rights Reserved.
Copyright (c) 2014-2023 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -312,6 +312,7 @@ func (l Lister) ListComputeResource(ctx context.Context) ([]Element, error) {

fields := []string{
"host",
"network",
"resourcePool",
}

Expand All @@ -327,6 +328,7 @@ func (l Lister) ListComputeResource(ctx context.Context) ([]Element, error) {

childTypes := []string{
"HostSystem",
"Network",
"ResourcePool",
}

Expand Down
4 changes: 2 additions & 2 deletions simulator/finder_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Copyright (c) 2017-2023 VMware, Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestFinderVPX(t *testing.T) {
{"ManagedObjectList", "/F[01]", 2},
{"ManagedObjectListChildren", "/*", m.Datacenter + 3},
{"ManagedObjectListChildren", "/*/*", 19},
{"ManagedObjectListChildren", "/*/*/*", 25},
{"ManagedObjectListChildren", "/*/*/*", 31},
{"FolderList", "/*", m.Folder},
{"DatacenterList", "/F0/*", 1},
{"DatacenterList", "/DC0", 1},
Expand Down