Skip to content

Commit c737066

Browse files
authored
Merge pull request #3524 from dougm/issue-3523
fix: use complete InventoryPath in Datacenter.Folders
2 parents b3b8216 + ad2d357 commit c737066

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

object/datacenter.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
2+
Copyright (c) 2015-2024 VMware, Inc. All Rights Reserved.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -18,7 +18,7 @@ package object
1818

1919
import (
2020
"context"
21-
"fmt"
21+
"path"
2222

2323
"github.com/vmware/govmomi/vim25"
2424
"github.com/vmware/govmomi/vim25/methods"
@@ -69,8 +69,12 @@ func (d *Datacenter) Folders(ctx context.Context) (*DatacenterFolders, error) {
6969
{"network", &df.NetworkFolder.InventoryPath},
7070
}
7171

72+
dcPath := d.InventoryPath
73+
if dcPath == "" {
74+
dcPath = "/" + md.Name
75+
}
7276
for _, p := range paths {
73-
*p.path = fmt.Sprintf("/%s/%s", md.Name, p.name)
77+
*p.path = path.Join(dcPath, p.name)
7478
}
7579

7680
return df, nil

object/datacenter_test.go

+57-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
2+
Copyright (c) 2015-2024 VMware, Inc. All Rights Reserved.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
66
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
1010
Unless required by applicable law or agreed to in writing, software
1111
distributed under the License is distributed on an "AS IS" BASIS,
@@ -14,7 +14,60 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package object
17+
package object_test
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/vmware/govmomi/find"
24+
"github.com/vmware/govmomi/object"
25+
"github.com/vmware/govmomi/simulator"
26+
"github.com/vmware/govmomi/vim25"
27+
)
1828

1929
// Datacenter should implement the Reference interface.
20-
var _ Reference = Datacenter{}
30+
var _ object.Reference = object.Datacenter{}
31+
32+
func TestDatacenterFolders(t *testing.T) {
33+
model := simulator.VPX()
34+
model.Datacenter = 2
35+
model.Folder = 1
36+
37+
simulator.Test(func(ctx context.Context, c *vim25.Client) {
38+
search := object.NewSearchIndex(c)
39+
finder := find.NewFinder(c)
40+
41+
dc, err := finder.Datacenter(ctx, "DC1")
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
46+
f, err := dc.Folders(ctx)
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
51+
folders := []*object.Folder{
52+
f.DatastoreFolder,
53+
f.HostFolder,
54+
f.NetworkFolder,
55+
f.VmFolder,
56+
}
57+
58+
for _, folder := range folders {
59+
p := "/F0/DC1/" + folder.Name()
60+
if p != folder.InventoryPath {
61+
t.Errorf("InventoryPath=%s", folder.InventoryPath)
62+
}
63+
64+
ref, err := search.FindByInventoryPath(ctx, folder.InventoryPath)
65+
if err != nil {
66+
t.Fatal(err)
67+
}
68+
if ref == nil {
69+
t.Errorf("invalid InventoryPath: %s", folder.InventoryPath)
70+
}
71+
}
72+
}, model)
73+
}

0 commit comments

Comments
 (0)