forked from vmware/govmomi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
332 lines (268 loc) · 7.14 KB
/
client.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
Copyright (c) 2014 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 govmomi
import (
"errors"
"net/url"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
)
var serviceInstance = types.ManagedObjectReference{
Type: "ServiceInstance",
Value: "ServiceInstance",
}
type Client struct {
Client *soap.Client
ServiceContent types.ServiceContent
}
func serviceContent(r soap.RoundTripper) (types.ServiceContent, error) {
req := types.RetrieveServiceContent{
This: serviceInstance,
}
res, err := methods.RetrieveServiceContent(r, &req)
if err != nil {
return types.ServiceContent{}, err
}
return res.Returnval, nil
}
func login(r soap.RoundTripper, u url.URL, sc types.ServiceContent) error {
// Return if URL doesn't contain username/password information.
if u.User == nil {
return nil
}
req := types.Login{
This: *sc.SessionManager,
}
req.UserName = u.User.Username()
if pw, ok := u.User.Password(); ok {
req.Password = pw
}
_, err := methods.Login(r, &req)
if err != nil {
return err
}
return nil
}
func NewClient(u url.URL, insecure bool) (*Client, error) {
c := Client{
Client: soap.NewClient(u, insecure),
}
sc, err := serviceContent(c.Client)
if err != nil {
return nil, err
}
err = login(c.Client, u, sc)
if err != nil {
return nil, err
}
c.ServiceContent = sc
return &c, nil
}
// RoundTrip dispatches to the client's SOAP client RoundTrip function.
func (c *Client) RoundTrip(req, res soap.HasFault) error {
return c.Client.RoundTrip(req, res)
}
func (c *Client) UserSession() (*types.UserSession, error) {
var sm mo.SessionManager
err := mo.RetrieveProperties(c, c.ServiceContent.PropertyCollector, *c.ServiceContent.SessionManager, &sm)
if err != nil {
return nil, err
}
return sm.CurrentSession, nil
}
func (c *Client) Properties(obj types.ManagedObjectReference, p []string, dst interface{}) error {
var objs = []types.ManagedObjectReference{obj}
return c.PropertiesN(objs, p, dst)
}
func (c *Client) PropertiesN(objs []types.ManagedObjectReference, p []string, dst interface{}) error {
var propSpec *types.PropertySpec
var objectSet []types.ObjectSpec
for _, obj := range objs {
// Ensure that all object reference types are the same
if propSpec == nil {
propSpec = &types.PropertySpec{
Type: obj.Type,
}
if p == nil {
propSpec.All = true
} else {
propSpec.PathSet = p
}
} else {
if obj.Type != propSpec.Type {
return errors.New("object references must have the same type")
}
}
objectSpec := types.ObjectSpec{
Obj: obj,
Skip: false,
}
objectSet = append(objectSet, objectSpec)
}
req := types.RetrieveProperties{
This: c.ServiceContent.PropertyCollector,
SpecSet: []types.PropertyFilterSpec{
{
ObjectSet: objectSet,
PropSet: []types.PropertySpec{*propSpec},
},
},
}
return mo.RetrievePropertiesForRequest(c, req, dst)
}
func (c *Client) WaitForProperties(obj types.ManagedObjectReference, ps []string, f func([]types.PropertyChange) bool) error {
p, err := c.NewPropertyCollector()
if err != nil {
return err
}
defer p.Destroy()
req := types.CreateFilter{
Spec: types.PropertyFilterSpec{
ObjectSet: []types.ObjectSpec{
{
Obj: obj,
},
},
PropSet: []types.PropertySpec{
{
PathSet: ps,
Type: obj.Type,
},
},
},
}
err = p.CreateFilter(req)
if err != nil {
return err
}
for version := ""; ; {
res, err := p.WaitForUpdates(version)
if err != nil {
return err
}
version = res.Version
for _, fs := range res.FilterSet {
for _, os := range fs.ObjectSet {
if os.Obj == obj {
if f(os.ChangeSet) {
return nil
}
}
}
}
}
}
// Ancestors returns the entire ancestry tree of a specified managed object.
// The return value includes the root node and the specified object itself.
func (c *Client) Ancestors(r Reference) ([]mo.ManagedEntity, error) {
ospec := types.ObjectSpec{
Obj: r.Reference(),
SelectSet: []types.BaseSelectionSpec{
&types.TraversalSpec{
SelectionSpec: types.SelectionSpec{Name: "traverseParent"},
Type: "ManagedEntity",
Path: "parent",
Skip: false,
SelectSet: []types.BaseSelectionSpec{
&types.SelectionSpec{Name: "traverseParent"},
},
},
},
Skip: false,
}
pspec := types.PropertySpec{
Type: "ManagedEntity",
PathSet: []string{"name", "parent"},
}
req := types.RetrieveProperties{
This: c.ServiceContent.PropertyCollector,
SpecSet: []types.PropertyFilterSpec{
{
ObjectSet: []types.ObjectSpec{ospec},
PropSet: []types.PropertySpec{pspec},
},
},
}
var ifaces []interface{}
err := mo.RetrievePropertiesForRequest(c, req, &ifaces)
if err != nil {
return nil, err
}
var out []mo.ManagedEntity
// Build ancestry tree by iteratively finding a new child.
for len(out) < len(ifaces) {
var find types.ManagedObjectReference
if len(out) > 0 {
find = out[len(out)-1].Self
}
// Find entity we're looking for given the last entity in the current tree.
for _, iface := range ifaces {
me := iface.(mo.IsManagedEntity).GetManagedEntity()
if me.Parent == nil {
out = append(out, me)
break
}
if *me.Parent == find {
out = append(out, me)
break
}
}
}
return out, nil
}
func (c *Client) FileManager() FileManager {
return FileManager{c}
}
func (c *Client) GuestOperationsManager() GuestOperationsManager {
return GuestOperationsManager{c}
}
func (c *Client) OvfManager() OvfManager {
return OvfManager{c}
}
func (c *Client) RootFolder() *Folder {
return NewFolder(c, c.ServiceContent.RootFolder)
}
func (c *Client) SearchIndex() SearchIndex {
return SearchIndex{c}
}
func (c *Client) VirtualDiskManager() VirtualDiskManager {
return VirtualDiskManager{c}
}
func (c *Client) LicenseManager() LicenseManager {
return NewLicenseManager(c, *c.ServiceContent.LicenseManager)
}
func (c *Client) StorageResourceManager() StorageResourceManager {
return StorageResourceManager{c}
}
func (c *Client) CustomizationSpecManager() CustomizationSpecManager {
return CustomizationSpecManager{c}
}
// NewPropertyCollector creates a new property collector based on the
// root property collector. It is the responsibility of the caller to
// clean up the property collector when done.
func (c *Client) NewPropertyCollector() (*PropertyCollector, error) {
req := types.CreatePropertyCollector{
This: c.ServiceContent.PropertyCollector,
}
res, err := methods.CreatePropertyCollector(c, &req)
if err != nil {
return nil, err
}
p := PropertyCollector{
c: c,
r: res.Returnval,
}
return &p, nil
}