Skip to content

Commit 5f2acf0

Browse files
authored
Merge pull request #1787 from dougm/finder-examples
Add finder example for MultipleFoundError
2 parents c3fedd3 + c3d102b commit 5f2acf0

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

find/example_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright (c) 2020 VMware, Inc. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package find_test
18+
19+
import (
20+
"context"
21+
"errors"
22+
"fmt"
23+
24+
"github.com/vmware/govmomi/find"
25+
"github.com/vmware/govmomi/simulator"
26+
"github.com/vmware/govmomi/vim25"
27+
)
28+
29+
func ExampleMultipleFoundError() {
30+
model := simulator.VPX()
31+
model.Portgroup = 2
32+
33+
simulator.Run(func(ctx context.Context, c *vim25.Client) error {
34+
finder := find.NewFinder(c)
35+
36+
_, err := finder.Network(ctx, "DC0_DVPG*")
37+
_, ok := err.(*find.MultipleFoundError) // returns DC0_DVPG{0,1}
38+
if !ok {
39+
return errors.New("expected error")
40+
}
41+
42+
net0, err := finder.Network(ctx, "DC0_DVPG0")
43+
if err != nil {
44+
return err
45+
}
46+
fmt.Println(net0.GetInventoryPath())
47+
48+
net1, err := finder.Network(ctx, "DC0_DVPG1")
49+
if err != nil {
50+
return err
51+
}
52+
fmt.Println(net1.GetInventoryPath())
53+
54+
return nil
55+
}, model)
56+
// Output:
57+
// /DC0/network/DC0_DVPG0
58+
// /DC0/network/DC0_DVPG1
59+
}

0 commit comments

Comments
 (0)