-
Notifications
You must be signed in to change notification settings - Fork 896
/
retrieve.go
292 lines (245 loc) · 7.4 KB
/
retrieve.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
Copyright (c) 2014-2015 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 mo
import (
"context"
"reflect"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
)
func ignoreMissingProperty(ref types.ManagedObjectReference, p types.MissingProperty) bool {
switch ref.Type {
case "VirtualMachine":
switch p.Path {
case "environmentBrowser":
// See https://github.com/vmware/govmomi/pull/242
return true
case "alarmActionsEnabled":
// Seen with vApp child VM
return true
}
}
return false
}
// ObjectContentToType loads an ObjectContent value into the value it
// represents. If the ObjectContent value has a non-empty 'MissingSet' field,
// it returns the first fault it finds there as error. If the 'MissingSet'
// field is empty, it returns a pointer to a reflect.Value. It handles contain
// nested properties, such as 'guest.ipAddress' or 'config.hardware'.
func ObjectContentToType(o types.ObjectContent, ptr ...bool) (interface{}, error) {
// Expect no properties in the missing set
for _, p := range o.MissingSet {
if ignoreMissingProperty(o.Obj, p) {
continue
}
return nil, soap.WrapVimFault(p.Fault.Fault)
}
ti := typeInfoForType(o.Obj.Type)
v, err := ti.LoadFromObjectContent(o)
if err != nil {
return nil, err
}
if len(ptr) == 1 && ptr[0] {
return v.Interface(), nil
}
return v.Elem().Interface(), nil
}
// ApplyPropertyChange converts the response of a call to WaitForUpdates
// and applies it to the given managed object.
func ApplyPropertyChange(obj Reference, changes []types.PropertyChange) {
t := typeInfoForType(obj.Reference().Type)
v := reflect.ValueOf(obj)
for _, p := range changes {
rv, ok := t.props[p.Name]
if !ok {
// For now, skip unknown properties allowing PC updates to be triggered
// for partial updates (e.g. extensionList["my.extension"]).
// Ultimately we should support partial updates by assigning the value
// reflectively in assignValue.
continue
}
assignValue(v, rv, reflect.ValueOf(p.Val))
}
}
// LoadObjectContent converts the response of a call to
// RetrieveProperties{Ex} to one or more managed objects.
func LoadObjectContent(content []types.ObjectContent, dst interface{}) error {
rt := reflect.TypeOf(dst)
if rt == nil || rt.Kind() != reflect.Ptr {
panic("need pointer")
}
rv := reflect.ValueOf(dst).Elem()
if !rv.CanSet() {
panic("cannot set dst")
}
isSlice := false
switch rt.Elem().Kind() {
case reflect.Struct:
case reflect.Slice:
isSlice = true
default:
panic("unexpected type")
}
if isSlice {
for _, p := range content {
v, err := ObjectContentToType(p)
if err != nil {
return err
}
vt := reflect.TypeOf(v)
if !rv.Type().AssignableTo(vt) {
// For example: dst is []ManagedEntity, res is []HostSystem
if field, ok := vt.FieldByName(rt.Elem().Elem().Name()); ok && field.Anonymous {
rv.Set(reflect.Append(rv, reflect.ValueOf(v).FieldByIndex(field.Index)))
continue
}
}
rv.Set(reflect.Append(rv, reflect.ValueOf(v)))
}
} else {
switch len(content) {
case 0:
case 1:
v, err := ObjectContentToType(content[0])
if err != nil {
return err
}
vt := reflect.TypeOf(v)
if !rv.Type().AssignableTo(vt) {
// For example: dst is ComputeResource, res is ClusterComputeResource
if field, ok := vt.FieldByName(rt.Elem().Name()); ok && field.Anonymous {
rv.Set(reflect.ValueOf(v).FieldByIndex(field.Index))
return nil
}
}
rv.Set(reflect.ValueOf(v))
default:
// If dst is not a slice, expect to receive 0 or 1 results
panic("more than 1 result")
}
}
return nil
}
// RetrievePropertiesEx wraps RetrievePropertiesEx and ContinueRetrievePropertiesEx to collect properties in batches.
func RetrievePropertiesEx(ctx context.Context, r soap.RoundTripper, req types.RetrievePropertiesEx) ([]types.ObjectContent, error) {
rx, err := methods.RetrievePropertiesEx(ctx, r, &req)
if err != nil {
return nil, err
}
if rx.Returnval == nil {
return nil, nil
}
objects := rx.Returnval.Objects
token := rx.Returnval.Token
for token != "" {
cx, err := methods.ContinueRetrievePropertiesEx(ctx, r, &types.ContinueRetrievePropertiesEx{
This: req.This,
Token: token,
})
if err != nil {
return nil, err
}
token = cx.Returnval.Token
objects = append(objects, cx.Returnval.Objects...)
}
return objects, nil
}
// RetrievePropertiesForRequest calls the RetrieveProperties method with the
// specified request and decodes the response struct into the value pointed to
// by dst.
func RetrievePropertiesForRequest(ctx context.Context, r soap.RoundTripper, req types.RetrieveProperties, dst interface{}) error {
objects, err := RetrievePropertiesEx(ctx, r, types.RetrievePropertiesEx{
This: req.This,
SpecSet: req.SpecSet,
})
if err != nil {
return err
}
return LoadObjectContent(objects, dst)
}
// RetrieveProperties retrieves the properties of the managed object specified
// as obj and decodes the response struct into the value pointed to by dst.
func RetrieveProperties(ctx context.Context, r soap.RoundTripper, pc, obj types.ManagedObjectReference, dst interface{}) error {
req := types.RetrieveProperties{
This: pc,
SpecSet: []types.PropertyFilterSpec{
{
ObjectSet: []types.ObjectSpec{
{
Obj: obj,
Skip: types.NewBool(false),
},
},
PropSet: []types.PropertySpec{
{
All: types.NewBool(true),
Type: obj.Type,
},
},
},
},
}
return RetrievePropertiesForRequest(ctx, r, req, dst)
}
var morType = reflect.TypeOf((*types.ManagedObjectReference)(nil)).Elem()
// References returns all non-nil moref field values in the given struct.
// Only Anonymous struct fields are followed by default. The optional follow
// param will follow any struct fields when true.
func References(s interface{}, follow ...bool) []types.ManagedObjectReference {
var refs []types.ManagedObjectReference
rval := reflect.ValueOf(s)
rtype := rval.Type()
if rval.Kind() == reflect.Ptr {
rval = rval.Elem()
rtype = rval.Type()
}
for i := 0; i < rval.NumField(); i++ {
val := rval.Field(i)
finfo := rtype.Field(i)
if finfo.Anonymous {
refs = append(refs, References(val.Interface(), follow...)...)
continue
}
if finfo.Name == "Self" {
continue
}
ftype := val.Type()
if ftype.Kind() == reflect.Slice {
if ftype.Elem() == morType {
s := val.Interface().([]types.ManagedObjectReference)
for i := range s {
refs = append(refs, s[i])
}
}
continue
}
if ftype.Kind() == reflect.Ptr {
if val.IsNil() {
continue
}
val = val.Elem()
ftype = val.Type()
}
if ftype == morType {
refs = append(refs, val.Interface().(types.ManagedObjectReference))
continue
}
if len(follow) != 0 && follow[0] {
if ftype.Kind() == reflect.Struct && val.CanSet() {
refs = append(refs, References(val.Interface(), follow...)...)
}
}
}
return refs
}