~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/govmomi/vim25/mo/retrieve.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2014 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
*/
 
16
 
 
17
package mo
 
18
 
 
19
import (
 
20
        "reflect"
 
21
 
 
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"
 
26
)
 
27
 
 
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)
 
37
        }
 
38
 
 
39
        ti := typeInfoForType(o.Obj.Type)
 
40
        v, err := ti.LoadFromObjectContent(o)
 
41
        if err != nil {
 
42
                return nil, err
 
43
        }
 
44
 
 
45
        return &v, nil
 
46
}
 
47
 
 
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 {
 
53
                panic("need pointer")
 
54
        }
 
55
 
 
56
        rv := reflect.ValueOf(dst).Elem()
 
57
        if !rv.CanSet() {
 
58
                panic("cannot set dst")
 
59
        }
 
60
 
 
61
        isSlice := false
 
62
        switch rt.Elem().Kind() {
 
63
        case reflect.Struct:
 
64
        case reflect.Slice:
 
65
                isSlice = true
 
66
        default:
 
67
                panic("unexpected type")
 
68
        }
 
69
 
 
70
        if isSlice {
 
71
                for _, p := range res.Returnval {
 
72
                        v, err := objectContentToType(p)
 
73
                        if err != nil {
 
74
                                return err
 
75
                        }
 
76
                        rv.Set(reflect.Append(rv, v.Elem()))
 
77
                }
 
78
        } else {
 
79
                switch len(res.Returnval) {
 
80
                case 0:
 
81
                case 1:
 
82
                        v, err := objectContentToType(res.Returnval[0])
 
83
                        if err != nil {
 
84
                                return err
 
85
                        }
 
86
                        rv.Set(v.Elem())
 
87
                default:
 
88
                        // If dst is not a slice, expect to receive 0 or 1 results
 
89
                        panic("more than 1 result")
 
90
                }
 
91
        }
 
92
 
 
93
        return nil
 
94
}
 
95
 
 
96
// RetrievePropertiesForRequest calls the RetrieveProperties method with the
 
97
// specified request and decodes the response struct into the value pointed to
 
98
// by dst.
 
99
func RetrievePropertiesForRequest(ctx context.Context, r soap.RoundTripper, req types.RetrieveProperties, dst interface{}) error {
 
100
        res, err := methods.RetrieveProperties(ctx, r, &req)
 
101
        if err != nil {
 
102
                return err
 
103
        }
 
104
 
 
105
        return LoadRetrievePropertiesResponse(res, dst)
 
106
}
 
107
 
 
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{
 
112
                This: pc,
 
113
                SpecSet: []types.PropertyFilterSpec{
 
114
                        {
 
115
                                ObjectSet: []types.ObjectSpec{
 
116
                                        {
 
117
                                                Obj:  obj,
 
118
                                                Skip: types.NewBool(false),
 
119
                                        },
 
120
                                },
 
121
                                PropSet: []types.PropertySpec{
 
122
                                        {
 
123
                                                All:  types.NewBool(true),
 
124
                                                Type: obj.Type,
 
125
                                        },
 
126
                                },
 
127
                        },
 
128
                },
 
129
        }
 
130
 
 
131
        return RetrievePropertiesForRequest(ctx, r, req, dst)
 
132
}