2
Copyright (c) 2014 VMware, Inc. All Rights Reserved.
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
8
http://www.apache.org/licenses/LICENSE-2.0
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.
22
"github.com/juju/govmomi/vim25/methods"
23
"github.com/juju/govmomi/vim25/soap"
24
"github.com/juju/govmomi/vim25/types"
25
"golang.org/x/net/context"
28
// objectContentToType loads an ObjectContent value into the value it
29
// represents. If the ObjectContent value has a non-empty 'MissingSet' field,
30
// it returns the first fault it finds there as error. If the 'MissingSet'
31
// field is empty, it returns a pointer to a reflect.Value. It handles contain
32
// nested properties, such as 'guest.ipAddress' or 'config.hardware'.
33
func objectContentToType(o types.ObjectContent) (*reflect.Value, error) {
34
// Expect no properties in the missing set
35
for _, p := range o.MissingSet {
36
return nil, soap.WrapVimFault(p.Fault.Fault)
39
ti := typeInfoForType(o.Obj.Type)
40
v, err := ti.LoadFromObjectContent(o)
48
// LoadRetrievePropertiesResponse converts the response of a call to
49
// RetrieveProperties to one or more managed objects.
50
func LoadRetrievePropertiesResponse(res *types.RetrievePropertiesResponse, dst interface{}) error {
51
rt := reflect.TypeOf(dst)
52
if rt == nil || rt.Kind() != reflect.Ptr {
56
rv := reflect.ValueOf(dst).Elem()
58
panic("cannot set dst")
62
switch rt.Elem().Kind() {
67
panic("unexpected type")
71
for _, p := range res.Returnval {
72
v, err := objectContentToType(p)
76
rv.Set(reflect.Append(rv, v.Elem()))
79
switch len(res.Returnval) {
82
v, err := objectContentToType(res.Returnval[0])
88
// If dst is not a slice, expect to receive 0 or 1 results
89
panic("more than 1 result")
96
// RetrievePropertiesForRequest calls the RetrieveProperties method with the
97
// specified request and decodes the response struct into the value pointed to
99
func RetrievePropertiesForRequest(ctx context.Context, r soap.RoundTripper, req types.RetrieveProperties, dst interface{}) error {
100
res, err := methods.RetrieveProperties(ctx, r, &req)
105
return LoadRetrievePropertiesResponse(res, dst)
108
// RetrieveProperties retrieves the properties of the managed object specified
109
// as obj and decodes the response struct into the value pointed to by dst.
110
func RetrieveProperties(ctx context.Context, r soap.RoundTripper, pc, obj types.ManagedObjectReference, dst interface{}) error {
111
req := types.RetrieveProperties{
113
SpecSet: []types.PropertyFilterSpec{
115
ObjectSet: []types.ObjectSpec{
118
Skip: types.NewBool(false),
121
PropSet: []types.PropertySpec{
123
All: types.NewBool(true),
131
return RetrievePropertiesForRequest(ctx, r, req, dst)