-
Notifications
You must be signed in to change notification settings - Fork 2
/
managed_object.go
66 lines (54 loc) · 1.5 KB
/
managed_object.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package vim
import (
"errors"
)
type ManagedObject struct {
*ManagedObjectReference
}
func (mo *ManagedObject) currentProperty(name string) (interface{}, error) {
if session == nil {
return nil, errors.New("A vSphere session must be created first")
}
if name == "" {
return nil, errors.New("A property name is required")
}
this := &ManagedObjectReference{
Type: mo.Type,
Value: mo.Value,
}
// Create object spec
objSpec := &ObjectSpec{
Obj: this,
Skip: false,
}
// Create property spec
propSpec := &PropertySpec{
Type: mo.Type,
All: false,
PathSet: []string{name},
}
// Create property filter spec using object and property specs
filterSpec := &PropertyFilterSpec{
PropSet: []*PropertySpec{propSpec},
ObjectSet: []*ObjectSpec{objSpec},
}
// Retrieves ServiceContent using method instead of Content() accessor
// to avoid a stack overflow
pc := session.ServiceContent.PropertyCollector
// It does not use pc.RetrievePropertiesEx because we want to support older
// versions of vSphere too
objs, err := pc.RetrieveProperties([]*PropertyFilterSpec{filterSpec})
if err != nil {
return nil, err
}
if len(objs) == 0 ||
len(objs[0].MissingSet) > 0 ||
len(objs[0].PropSet) == 0 {
return nil, nil
}
val, err := objs[0].PropSet[0].Val.Value()
return val, err
}