~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/govmomi/object/virtual_machine.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 object
 
18
 
 
19
import (
 
20
        "github.com/juju/govmomi/property"
 
21
        "github.com/juju/govmomi/vim25"
 
22
        "github.com/juju/govmomi/vim25/methods"
 
23
        "github.com/juju/govmomi/vim25/mo"
 
24
        "github.com/juju/govmomi/vim25/types"
 
25
        "golang.org/x/net/context"
 
26
)
 
27
 
 
28
type VirtualMachine struct {
 
29
        Common
 
30
 
 
31
        InventoryPath string
 
32
}
 
33
 
 
34
func NewVirtualMachine(c *vim25.Client, ref types.ManagedObjectReference) *VirtualMachine {
 
35
        return &VirtualMachine{
 
36
                Common: NewCommon(c, ref),
 
37
        }
 
38
}
 
39
 
 
40
func (v VirtualMachine) PowerOn(ctx context.Context) (*Task, error) {
 
41
        req := types.PowerOnVM_Task{
 
42
                This: v.Reference(),
 
43
        }
 
44
 
 
45
        res, err := methods.PowerOnVM_Task(ctx, v.c, &req)
 
46
        if err != nil {
 
47
                return nil, err
 
48
        }
 
49
 
 
50
        return NewTask(v.c, res.Returnval), nil
 
51
}
 
52
 
 
53
func (v VirtualMachine) PowerOff(ctx context.Context) (*Task, error) {
 
54
        req := types.PowerOffVM_Task{
 
55
                This: v.Reference(),
 
56
        }
 
57
 
 
58
        res, err := methods.PowerOffVM_Task(ctx, v.c, &req)
 
59
        if err != nil {
 
60
                return nil, err
 
61
        }
 
62
 
 
63
        return NewTask(v.c, res.Returnval), nil
 
64
}
 
65
 
 
66
func (v VirtualMachine) Reset(ctx context.Context) (*Task, error) {
 
67
        req := types.ResetVM_Task{
 
68
                This: v.Reference(),
 
69
        }
 
70
 
 
71
        res, err := methods.ResetVM_Task(ctx, v.c, &req)
 
72
        if err != nil {
 
73
                return nil, err
 
74
        }
 
75
 
 
76
        return NewTask(v.c, res.Returnval), nil
 
77
}
 
78
 
 
79
func (v VirtualMachine) Suspend(ctx context.Context) (*Task, error) {
 
80
        req := types.SuspendVM_Task{
 
81
                This: v.Reference(),
 
82
        }
 
83
 
 
84
        res, err := methods.SuspendVM_Task(ctx, v.c, &req)
 
85
        if err != nil {
 
86
                return nil, err
 
87
        }
 
88
 
 
89
        return NewTask(v.c, res.Returnval), nil
 
90
}
 
91
 
 
92
func (v VirtualMachine) ShutdownGuest(ctx context.Context) error {
 
93
        req := types.ShutdownGuest{
 
94
                This: v.Reference(),
 
95
        }
 
96
 
 
97
        _, err := methods.ShutdownGuest(ctx, v.c, &req)
 
98
        return err
 
99
}
 
100
 
 
101
func (v VirtualMachine) RebootGuest(ctx context.Context) error {
 
102
        req := types.RebootGuest{
 
103
                This: v.Reference(),
 
104
        }
 
105
 
 
106
        _, err := methods.RebootGuest(ctx, v.c, &req)
 
107
        return err
 
108
}
 
109
 
 
110
func (v VirtualMachine) Destroy(ctx context.Context) (*Task, error) {
 
111
        req := types.Destroy_Task{
 
112
                This: v.Reference(),
 
113
        }
 
114
 
 
115
        res, err := methods.Destroy_Task(ctx, v.c, &req)
 
116
        if err != nil {
 
117
                return nil, err
 
118
        }
 
119
 
 
120
        return NewTask(v.c, res.Returnval), nil
 
121
}
 
122
 
 
123
func (v VirtualMachine) Clone(ctx context.Context, folder *Folder, name string, config types.VirtualMachineCloneSpec) (*Task, error) {
 
124
        req := types.CloneVM_Task{
 
125
                This:   v.Reference(),
 
126
                Folder: folder.Reference(),
 
127
                Name:   name,
 
128
                Spec:   config,
 
129
        }
 
130
 
 
131
        res, err := methods.CloneVM_Task(ctx, v.c, &req)
 
132
        if err != nil {
 
133
                return nil, err
 
134
        }
 
135
 
 
136
        return NewTask(v.c, res.Returnval), nil
 
137
}
 
138
 
 
139
func (v VirtualMachine) Reconfigure(ctx context.Context, config types.VirtualMachineConfigSpec) (*Task, error) {
 
140
        req := types.ReconfigVM_Task{
 
141
                This: v.Reference(),
 
142
                Spec: config,
 
143
        }
 
144
 
 
145
        res, err := methods.ReconfigVM_Task(ctx, v.c, &req)
 
146
        if err != nil {
 
147
                return nil, err
 
148
        }
 
149
 
 
150
        return NewTask(v.c, res.Returnval), nil
 
151
}
 
152
 
 
153
func (v VirtualMachine) WaitForIP(ctx context.Context) (string, error) {
 
154
        var ip string
 
155
 
 
156
        p := property.DefaultCollector(v.c)
 
157
        err := property.Wait(ctx, p, v.Reference(), []string{"guest.ipAddress"}, func(pc []types.PropertyChange) bool {
 
158
                for _, c := range pc {
 
159
                        if c.Name != "guest.ipAddress" {
 
160
                                continue
 
161
                        }
 
162
                        if c.Op != types.PropertyChangeOpAssign {
 
163
                                continue
 
164
                        }
 
165
                        if c.Val == nil {
 
166
                                continue
 
167
                        }
 
168
 
 
169
                        ip = c.Val.(string)
 
170
                        return true
 
171
                }
 
172
 
 
173
                return false
 
174
        })
 
175
 
 
176
        if err != nil {
 
177
                return "", err
 
178
        }
 
179
 
 
180
        return ip, nil
 
181
}
 
182
 
 
183
// Device returns the VirtualMachine's config.hardware.device property.
 
184
func (v VirtualMachine) Device(ctx context.Context) (VirtualDeviceList, error) {
 
185
        var o mo.VirtualMachine
 
186
 
 
187
        err := v.Properties(ctx, v.Reference(), []string{"config.hardware.device"}, &o)
 
188
        if err != nil {
 
189
                return nil, err
 
190
        }
 
191
 
 
192
        return VirtualDeviceList(o.Config.Hardware.Device), nil
 
193
}
 
194
 
 
195
func (v VirtualMachine) configureDevice(ctx context.Context, op types.VirtualDeviceConfigSpecOperation, fop types.VirtualDeviceConfigSpecFileOperation, devices ...types.BaseVirtualDevice) error {
 
196
        spec := types.VirtualMachineConfigSpec{}
 
197
 
 
198
        for _, device := range devices {
 
199
                config := &types.VirtualDeviceConfigSpec{
 
200
                        Device:    device,
 
201
                        Operation: op,
 
202
                }
 
203
 
 
204
                if disk, ok := device.(*types.VirtualDisk); ok {
 
205
                        config.FileOperation = fop
 
206
 
 
207
                        // Special case to attach an existing disk
 
208
                        if op == types.VirtualDeviceConfigSpecOperationAdd && disk.CapacityInKB == 0 {
 
209
                                childDisk := false
 
210
                                if b, ok := disk.Backing.(*types.VirtualDiskFlatVer2BackingInfo); ok {
 
211
                                        childDisk = b.Parent != nil
 
212
                                }
 
213
 
 
214
                                if !childDisk {
 
215
                                        config.FileOperation = "" // existing disk
 
216
                                }
 
217
                        }
 
218
                }
 
219
 
 
220
                spec.DeviceChange = append(spec.DeviceChange, config)
 
221
        }
 
222
 
 
223
        task, err := v.Reconfigure(ctx, spec)
 
224
        if err != nil {
 
225
                return err
 
226
        }
 
227
 
 
228
        return task.Wait(ctx)
 
229
}
 
230
 
 
231
// AddDevice adds the given devices to the VirtualMachine
 
232
func (v VirtualMachine) AddDevice(ctx context.Context, device ...types.BaseVirtualDevice) error {
 
233
        return v.configureDevice(ctx, types.VirtualDeviceConfigSpecOperationAdd, types.VirtualDeviceConfigSpecFileOperationCreate, device...)
 
234
}
 
235
 
 
236
// EditDevice edits the given (existing) devices on the VirtualMachine
 
237
func (v VirtualMachine) EditDevice(ctx context.Context, device ...types.BaseVirtualDevice) error {
 
238
        return v.configureDevice(ctx, types.VirtualDeviceConfigSpecOperationEdit, types.VirtualDeviceConfigSpecFileOperationReplace, device...)
 
239
}
 
240
 
 
241
// RemoveDevice removes the given devices on the VirtualMachine
 
242
func (v VirtualMachine) RemoveDevice(ctx context.Context, device ...types.BaseVirtualDevice) error {
 
243
        return v.configureDevice(ctx, types.VirtualDeviceConfigSpecOperationRemove, types.VirtualDeviceConfigSpecFileOperationDestroy, device...)
 
244
}
 
245
 
 
246
// BootOptions returns the VirtualMachine's config.bootOptions property.
 
247
func (v VirtualMachine) BootOptions(ctx context.Context) (*types.VirtualMachineBootOptions, error) {
 
248
        var o mo.VirtualMachine
 
249
 
 
250
        err := v.Properties(ctx, v.Reference(), []string{"config.bootOptions"}, &o)
 
251
        if err != nil {
 
252
                return nil, err
 
253
        }
 
254
 
 
255
        return o.Config.BootOptions, nil
 
256
}
 
257
 
 
258
// SetBootOptions reconfigures the VirtualMachine with the given options.
 
259
func (v VirtualMachine) SetBootOptions(ctx context.Context, options *types.VirtualMachineBootOptions) error {
 
260
        spec := types.VirtualMachineConfigSpec{}
 
261
 
 
262
        spec.BootOptions = options
 
263
 
 
264
        task, err := v.Reconfigure(ctx, spec)
 
265
        if err != nil {
 
266
                return err
 
267
        }
 
268
 
 
269
        return task.Wait(ctx)
 
270
}
 
271
 
 
272
// Answer answers a pending question.
 
273
func (v VirtualMachine) Answer(ctx context.Context, id, answer string) error {
 
274
        req := types.AnswerVM{
 
275
                This:         v.Reference(),
 
276
                QuestionId:   id,
 
277
                AnswerChoice: answer,
 
278
        }
 
279
 
 
280
        _, err := methods.AnswerVM(ctx, v.c, &req)
 
281
        if err != nil {
 
282
                return err
 
283
        }
 
284
 
 
285
        return nil
 
286
}
 
287
 
 
288
func (v VirtualMachine) MarkAsTemplate(ctx context.Context) error {
 
289
        req := types.MarkAsTemplate{
 
290
                This: v.Reference(),
 
291
        }
 
292
 
 
293
        _, err := methods.MarkAsTemplate(ctx, v.c, &req)
 
294
        if err != nil {
 
295
                return err
 
296
        }
 
297
 
 
298
        return nil
 
299
}
 
300
 
 
301
func (v VirtualMachine) MarkAsVirtualMachine(ctx context.Context, pool ResourcePool, host *HostSystem) error {
 
302
        req := types.MarkAsVirtualMachine{
 
303
                This: v.Reference(),
 
304
                Pool: pool.Reference(),
 
305
        }
 
306
 
 
307
        if host != nil {
 
308
                ref := host.Reference()
 
309
                req.Host = &ref
 
310
        }
 
311
 
 
312
        _, err := methods.MarkAsVirtualMachine(ctx, v.c, &req)
 
313
        if err != nil {
 
314
                return err
 
315
        }
 
316
 
 
317
        return nil
 
318
}