diff --git a/find/finder.go b/find/finder.go index 67ad8b737..4830fc26e 100644 --- a/find/finder.go +++ b/find/finder.go @@ -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. @@ -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} } @@ -805,12 +810,6 @@ func (f *Finder) NetworkList(ctx context.Context, path string) ([]object.Network 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 } diff --git a/find/finder_test.go b/find/finder_test.go new file mode 100644 index 000000000..050ca6bd1 --- /dev/null +++ b/find/finder_test.go @@ -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) +} diff --git a/list/lister.go b/list/lister.go index 8121c9dd9..92a40e8ba 100644 --- a/list/lister.go +++ b/list/lister.go @@ -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. diff --git a/simulator/finder_test.go b/simulator/finder_test.go index 91ae431dc..3a45121d3 100644 --- a/simulator/finder_test.go +++ b/simulator/finder_test.go @@ -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.