Skip to content

Commit 90bd975

Browse files
committed
fix: support multiple property.Collector.Retrieve base type fields
Prior to 3b7ff25 (PropertyCollector index support) mo.LoadObjectFromContent would panic when trying to load specific fields of a base type, e.g. a mo.Datastore "info.url". While that change fixed the panic, it only allowed 1 such field to be loaded - that is fixed with this change. Signed-off-by: Doug MacEachern <[email protected]>
1 parent e0ed729 commit 90bd975

File tree

3 files changed

+53
-47
lines changed

3 files changed

+53
-47
lines changed

object/datastore_test.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
/*
2-
Copyright (c) 2015 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-
*/
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
164

175
package object_test
186

@@ -21,9 +9,12 @@ import (
219
"strings"
2210
"testing"
2311

12+
"github.com/vmware/govmomi/find"
2413
"github.com/vmware/govmomi/object"
14+
"github.com/vmware/govmomi/property"
2515
"github.com/vmware/govmomi/simulator"
2616
"github.com/vmware/govmomi/vim25"
17+
"github.com/vmware/govmomi/vim25/mo"
2718
"github.com/vmware/govmomi/vim25/soap"
2819
)
2920

@@ -96,3 +87,32 @@ func TestDatastoreFindInventoryPath(t *testing.T) {
9687
}
9788
}, model)
9889
}
90+
91+
func TestDatastoreInfo(t *testing.T) {
92+
simulator.Test(func(ctx context.Context, c *vim25.Client) {
93+
obj, err := find.NewFinder(c).DefaultDatastore(ctx)
94+
if err != nil {
95+
t.Fatal(err)
96+
}
97+
pc := property.DefaultCollector(c)
98+
99+
props := []string{
100+
"info.url",
101+
"info.name",
102+
}
103+
104+
var ds mo.Datastore
105+
err = pc.RetrieveOne(ctx, obj.Reference(), props, &ds)
106+
if err != nil {
107+
t.Fatal(err)
108+
}
109+
110+
info := ds.Info.GetDatastoreInfo()
111+
if info.Url == "" {
112+
t.Error("no info.url")
113+
}
114+
if info.Name == "" {
115+
t.Error("no info.name")
116+
}
117+
})
118+
}

vim25/mo/retrieve_test.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
/*
2-
Copyright (c) 2014-2024 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-
*/
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
164

175
package mo
186

@@ -225,6 +213,10 @@ func TestDatastoreInfoURL(t *testing.T) {
225213
Name: "info.url",
226214
Val: "ds:///vmfs/volumes/666d7a79-cb0d28b2-57c8-0645602e1b58/",
227215
},
216+
{
217+
Name: "info.name",
218+
Val: "foo",
219+
},
228220
},
229221
MissingSet: nil,
230222
},
@@ -241,4 +233,8 @@ func TestDatastoreInfoURL(t *testing.T) {
241233
if info.Url != content[0].PropSet[0].Val.(string) {
242234
t.Errorf("info.url=%s", info.Url)
243235
}
236+
237+
if info.Name != content[0].PropSet[1].Val.(string) {
238+
t.Errorf("info.name=%s", info.Name)
239+
}
244240
}

vim25/mo/type_info.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
/*
2-
Copyright (c) 2014-2024 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-
*/
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
164

175
package mo
186

@@ -192,8 +180,10 @@ var nilValue reflect.Value
192180
func assignValue(val reflect.Value, fi []int, pv reflect.Value, field ...string) {
193181
// Indexed property path can only use base types
194182
if val.Kind() == reflect.Interface {
195-
base := baseType(val.Type())
196-
val.Set(reflect.New(base))
183+
if val.IsNil() {
184+
base := baseType(val.Type())
185+
val.Set(reflect.New(base))
186+
}
197187
val = val.Elem()
198188
}
199189

0 commit comments

Comments
 (0)