-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
331 lines (278 loc) · 10.9 KB
/
client.go
File metadata and controls
331 lines (278 loc) · 10.9 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license
package virtualmachine
import (
"context"
"log"
"github.com/microsoft/moc/pkg/auth"
"github.com/microsoft/moc/pkg/errors"
"github.com/microsoft/moc/pkg/marshal"
"github.com/microsoft/wssd-sdk-for-go/services/compute"
"github.com/microsoft/wssd-sdk-for-go/services/compute/virtualmachine/internal"
)
type Service interface {
Get(context.Context, string, string) (*[]compute.VirtualMachine, error)
CreateOrUpdate(context.Context, string, string, *compute.VirtualMachine) (*compute.VirtualMachine, error)
Delete(context.Context, string, string) error
Hydrate(context.Context, string, string, *compute.VirtualMachine) (*compute.VirtualMachine, error)
Start(context.Context, string, string) error
Stop(context.Context, string, string) error
Poweroff(context.Context, string, string, bool) error
Pause(context.Context, string, string) error
Save(context.Context, string, string) error
RemoveIsoDisk(context.Context, string, string) error
RepairGuestAgent(context.Context, string, string) error
RunCommand(context.Context, string, string, *compute.VirtualMachineRunCommandRequest) (*compute.VirtualMachineRunCommandResponse, error)
Validate(context.Context, string, string) error
GetHyperVVmId(context.Context, string, string) (*compute.VirtualMachineHyperVVmId, error)
HasHyperVVm(context.Context, string) (bool, error)
}
type VirtualMachineClient struct {
compute.BaseClient
internal Service
}
func NewVirtualMachineClient(cloudFQDN string, authorizer auth.Authorizer) (*VirtualMachineClient, error) {
c, err := internal.NewVirtualMachineClient(cloudFQDN, authorizer)
if err != nil {
return nil, err
}
return &VirtualMachineClient{internal: c}, nil
}
// Get methods invokes the client Get method
func (c *VirtualMachineClient) Get(ctx context.Context, group, name string) (*[]compute.VirtualMachine, error) {
return c.internal.Get(ctx, group, name)
}
// CreateOrUpdate methods invokes create or update on the client
func (c *VirtualMachineClient) CreateOrUpdate(ctx context.Context, group, name string, compute *compute.VirtualMachine) (*compute.VirtualMachine, error) {
return c.internal.CreateOrUpdate(ctx, group, name, compute)
}
// Delete methods invokes delete of the compute resource
func (c *VirtualMachineClient) Delete(ctx context.Context, group string, name string) error {
return c.internal.Delete(ctx, group, name)
}
// Hydrate methods creates MOC representation of the VM resource
func (c *VirtualMachineClient) Hydrate(ctx context.Context, group string, name string, compute *compute.VirtualMachine) (*compute.VirtualMachine, error) {
return c.internal.Hydrate(ctx, group, name, compute)
}
func (c *VirtualMachineClient) Start(ctx context.Context, group string, name string) (err error) {
err = c.internal.Start(ctx, group, name)
return
}
func (c *VirtualMachineClient) Stop(ctx context.Context, group string, name string) (err error) {
err = c.internal.Stop(ctx, group, name)
return
}
// Poweroff initiates a VM shutdown operation
// skipShutdown: false (default/recommended) = graceful shutdown with guest OS notification
//
// true = force immediate shutdown (not recommended, may cause data loss)
func (c *VirtualMachineClient) Poweroff(ctx context.Context, group string, name string, skipShutdown bool) (err error) {
err = c.internal.Poweroff(ctx, group, name, skipShutdown)
return
}
func (c *VirtualMachineClient) Restart(ctx context.Context, group string, name string) (err error) {
err = c.internal.Stop(ctx, group, name)
if err != nil {
return
}
err = c.internal.Start(ctx, group, name)
return
}
func (c *VirtualMachineClient) Pause(ctx context.Context, group string, name string) (err error) {
err = c.internal.Pause(ctx, group, name)
return
}
func (c *VirtualMachineClient) Save(ctx context.Context, group string, name string) (err error) {
err = c.internal.Save(ctx, group, name)
return
}
func (c *VirtualMachineClient) RemoveIsoDisk(ctx context.Context, group string, name string) (err error) {
err = c.internal.RemoveIsoDisk(ctx, group, name)
return
}
func (c *VirtualMachineClient) RepairGuestAgent(ctx context.Context, group string, name string) (err error) {
err = c.internal.RepairGuestAgent(ctx, group, name)
return
}
// Validate methods invokes the validate Get method
func (c *VirtualMachineClient) Validate(ctx context.Context, group, name string) error {
return c.internal.Validate(ctx, group, name)
}
func (c *VirtualMachineClient) Resize(ctx context.Context, group string, name string, newSize compute.VirtualMachineSizeTypes, newCustomSize *compute.VirtualMachineCustomSize) (err error) {
return c.ResizeEx(ctx, group, name, newSize, newCustomSize, nil)
}
func (c *VirtualMachineClient) ResizeEx(ctx context.Context, group string, name string, newSize compute.VirtualMachineSizeTypes, newCustomSize *compute.VirtualMachineCustomSize, newVirtualMachineGPUs []*compute.VirtualMachineGPU) (err error) {
vms, err := c.Get(ctx, group, name)
if err != nil {
return
}
if vms == nil || len(*vms) == 0 {
err = errors.Wrapf(errors.NotFound, "%s", name)
return
}
vm := (*vms)[0]
if !isDifferentVmSize(vm.HardwareProfile.VMSize, newSize, vm.HardwareProfile.CustomSize, newCustomSize) && !isDifferentGpuList(vm.HardwareProfile.VirtualMachineGPUs, newVirtualMachineGPUs) {
// Nothing to do
return
}
vm.HardwareProfile.VMSize = newSize
vm.HardwareProfile.CustomSize = newCustomSize
vm.HardwareProfile.VirtualMachineGPUs = newVirtualMachineGPUs
_, err = c.CreateOrUpdate(ctx, group, name, &vm)
return
}
func (c *VirtualMachineClient) DiskAttach(ctx context.Context, group string, vmName, diskName string) (err error) {
vms, err := c.Get(ctx, group, vmName)
if err != nil {
return err
}
if vms == nil || len(*vms) == 0 {
return errors.Wrapf(errors.NotFound, "Unable to find Virtual Machine [%s]", vmName)
}
vm := (*vms)[0]
for _, disk := range *vm.StorageProfile.DataDisks {
if *disk.VhdName == diskName {
return errors.Wrapf(errors.AlreadyExists, "DataDisk [%s] is already attached to the VM [%s]", diskName, vmName)
}
}
*vm.StorageProfile.DataDisks = append(*vm.StorageProfile.DataDisks, compute.DataDisk{VhdName: &diskName})
_, err = c.CreateOrUpdate(ctx, group, vmName, &vm)
if err != nil {
return err
}
return
}
func (c *VirtualMachineClient) DiskDetach(ctx context.Context, group string, vmName, diskName string) (err error) {
vms, err := c.Get(ctx, group, vmName)
if err != nil {
return err
}
if vms == nil || len(*vms) == 0 {
return errors.Wrapf(errors.NotFound, "Unable to find Virtual Machine [%s]", vmName)
}
vm := (*vms)[0]
for i, element := range *vm.StorageProfile.DataDisks {
if *element.VhdName == diskName {
*vm.StorageProfile.DataDisks = append((*vm.StorageProfile.DataDisks)[:i], (*vm.StorageProfile.DataDisks)[i+1:]...)
break
}
}
_, err = c.CreateOrUpdate(ctx, group, vmName, &vm)
if err != nil {
return err
}
return
}
func (c *VirtualMachineClient) NetworkInterfaceAdd(ctx context.Context, group string, vmName, nicName string) (err error) {
vms, err := c.Get(ctx, group, vmName)
if err != nil {
return err
}
if vms == nil || len(*vms) == 0 {
return errors.Wrapf(errors.NotFound, "Unable to find Virtual Machine [%s]", vmName)
}
vm := (*vms)[0]
for _, nic := range *vm.NetworkProfile.NetworkInterfaces {
if *nic.VirtualNetworkInterfaceReference == nicName {
return errors.Wrapf(errors.AlreadyExists, "NetworkInterface [%s] is already attached to the VM [%s]", nicName, vmName)
}
}
*vm.NetworkProfile.NetworkInterfaces = append(*vm.NetworkProfile.NetworkInterfaces, compute.NetworkInterfaceReference{VirtualNetworkInterfaceReference: &nicName})
_, err = c.CreateOrUpdate(ctx, group, vmName, &vm)
if err != nil {
return err
}
return
}
func (c *VirtualMachineClient) NetworkInterfaceRemove(ctx context.Context, group string, vmName, nicName string) (err error) {
vms, err := c.Get(ctx, group, vmName)
if err != nil {
return err
}
if vms == nil || len(*vms) == 0 {
return errors.Wrapf(errors.NotFound, "Unable to find Virtual Machine [%s]", vmName)
}
vm := (*vms)[0]
for i, element := range *vm.NetworkProfile.NetworkInterfaces {
if *element.VirtualNetworkInterfaceReference == nicName {
*vm.NetworkProfile.NetworkInterfaces = append((*vm.NetworkProfile.NetworkInterfaces)[:i], (*vm.NetworkProfile.NetworkInterfaces)[i+1:]...)
break
}
}
_, err = c.CreateOrUpdate(ctx, group, vmName, &vm)
if err != nil {
return err
}
return
}
func (c *VirtualMachineClient) NetworkInterfaceList(ctx context.Context, group string, vmName string) (err error) {
vms, err := c.Get(ctx, group, vmName)
if err != nil {
return err
}
if vms == nil || len(*vms) == 0 {
return errors.Wrapf(errors.NotFound, "Unable to find Virtual Machine [%s]", vmName)
}
vm := (*vms)[0]
for _, element := range *vm.NetworkProfile.NetworkInterfaces {
log.Printf("%+v\n", marshal.ToString(element))
}
return
}
func (c *VirtualMachineClient) NetworkInterfaceShow(ctx context.Context, group string, vmName, nicName string) (err error) {
vms, err := c.Get(ctx, group, vmName)
if err != nil {
return err
}
if vms == nil || len(*vms) == 0 {
return errors.Wrapf(errors.NotFound, "Unable to find Virtual Machine [%s]", vmName)
}
vm := (*vms)[0]
for _, nic := range *vm.NetworkProfile.NetworkInterfaces {
if *nic.VirtualNetworkInterfaceReference == nicName {
// TODO - implement detailed show
log.Printf("%+v\n", marshal.ToString(nic))
break
}
}
return
}
func (c *VirtualMachineClient) RunCommand(ctx context.Context, group, vmName string, request *compute.VirtualMachineRunCommandRequest) (response *compute.VirtualMachineRunCommandResponse, err error) {
return c.internal.RunCommand(ctx, group, vmName, request)
}
func isDifferentVmSize(oldSizeType, newSizeType compute.VirtualMachineSizeTypes, oldCustomSize, newCustomSize *compute.VirtualMachineCustomSize) bool {
if oldSizeType != newSizeType {
return true
}
// same vm size type, check custom size
// Note: fields in compute.VirtualMachineCustomSize are pointers, deference to compare the values
switch newSizeType {
case compute.VirtualMachineSizeTypesCustomNK:
fallthrough
case compute.VirtualMachineSizeTypesCustomGpupv:
if *oldCustomSize.GpuCount != *newCustomSize.GpuCount {
return true
}
fallthrough
case compute.VirtualMachineSizeTypesCustom:
if *oldCustomSize.CpuCount != *newCustomSize.CpuCount {
return true
}
if *oldCustomSize.MemoryMB != *newCustomSize.MemoryMB {
return true
}
return false
default:
return false
}
}
func isDifferentGpuList(oldGpuList, newGpuList []*compute.VirtualMachineGPU) bool {
// simultaneous addtion and removal of GPU is not supported
return len(oldGpuList) != len(newGpuList)
}
func (c *VirtualMachineClient) GetHyperVVmId(ctx context.Context, group string, name string) (*compute.VirtualMachineHyperVVmId, error) {
return c.internal.GetHyperVVmId(ctx, group, name)
}
func (c *VirtualMachineClient) HasHyperVVm(ctx context.Context, vmName string) (bool, error) {
return c.internal.HasHyperVVm(ctx, vmName)
}