forked from Azure/aks-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute.go
162 lines (139 loc) · 5.52 KB
/
compute.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package azurestack
import (
"context"
"fmt"
"github.com/Azure/aks-engine/pkg/armhelpers"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2017-03-30/compute"
azcompute "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-10-01/compute"
)
// ListVirtualMachines returns (the first page of) the machines in the specified resource group.
func (az *AzureClient) ListVirtualMachines(ctx context.Context, resourceGroup string) (armhelpers.VirtualMachineListResultPage, error) {
page, err := az.virtualMachinesClient.List(ctx, resourceGroup)
c := VirtualMachineListResultPageClient{
vmlrp: page,
err: err,
}
return &c, err
}
// GetVirtualMachine returns the specified machine in the specified resource group.
func (az *AzureClient) GetVirtualMachine(ctx context.Context, resourceGroup, name string) (azcompute.VirtualMachine, error) {
vm, err := az.virtualMachinesClient.Get(ctx, resourceGroup, name, "")
azVM := azcompute.VirtualMachine{}
if err != nil {
return azVM, fmt.Errorf("fail to get virtual machine, %s", err)
}
err = DeepCopy(&azVM, vm)
if err != nil {
return azVM, fmt.Errorf("fail to convert virtual machine, %s", err)
}
return azVM, err
}
// RestartVirtualMachine restarts the specified virtual machine.
func (az *AzureClient) RestartVirtualMachine(ctx context.Context, resourceGroup, name string) error {
future, err := az.virtualMachinesClient.Restart(ctx, resourceGroup, name)
if err != nil {
return err
}
if err = future.WaitForCompletionRef(ctx, az.virtualMachinesClient.Client); err != nil {
return err
}
_, err = future.Result(az.virtualMachinesClient)
return err
}
// DeleteVirtualMachine handles deletion of a CRP/VMAS VM (aka, not a VMSS VM).
func (az *AzureClient) DeleteVirtualMachine(ctx context.Context, resourceGroup, name string) error {
future, err := az.virtualMachinesClient.Delete(ctx, resourceGroup, name)
if err != nil {
return err
}
if err = future.WaitForCompletionRef(ctx, az.virtualMachinesClient.Client); err != nil {
return err
}
_, err = future.Result(az.virtualMachinesClient)
return err
}
// ListVirtualMachineScaleSets returns (the first page of) the VMSS resources in the specified resource group.
func (az *AzureClient) ListVirtualMachineScaleSets(ctx context.Context, resourceGroup string) (armhelpers.VirtualMachineScaleSetListResultPage, error) {
page, err := az.virtualMachineScaleSetsClient.List(ctx, resourceGroup)
c := VirtualMachineScaleSetListResultPageClient{
vmsslrp: page,
err: err,
}
return &c, err
}
// RestartVirtualMachineScaleSets restarts the specified VMSS
func (az *AzureClient) RestartVirtualMachineScaleSets(ctx context.Context, resourceGroup string, virtualMachineScaleSet string, instanceIDs *azcompute.VirtualMachineScaleSetVMInstanceIDs) error {
ids := compute.VirtualMachineScaleSetVMInstanceIDs{}
err := DeepCopy(&ids, instanceIDs)
if err != nil {
return fmt.Errorf("fail to convert instance IDs, %v", err)
}
future, err := az.virtualMachineScaleSetsClient.Restart(ctx, resourceGroup, virtualMachineScaleSet, &ids)
if err != nil {
return err
}
if err = future.WaitForCompletionRef(ctx, az.virtualMachineScaleSetsClient.Client); err != nil {
return err
}
_, err = future.Result(az.virtualMachineScaleSetsClient)
return err
}
// ListVirtualMachineScaleSetVMs returns the list of VMs per VMSS
func (az *AzureClient) ListVirtualMachineScaleSetVMs(ctx context.Context, resourceGroup, virtualMachineScaleSet string) (armhelpers.VirtualMachineScaleSetVMListResultPage, error) {
page, err := az.virtualMachineScaleSetVMsClient.List(ctx, resourceGroup, virtualMachineScaleSet, "", "", "")
c := VirtualMachineScaleSetVMListResultPageClient{
vmssvlrp: page,
err: err,
}
return &c, err
}
// DeleteVirtualMachineScaleSetVM deletes a VM in a VMSS
func (az *AzureClient) DeleteVirtualMachineScaleSetVM(ctx context.Context, resourceGroup, virtualMachineScaleSet, instanceID string) error {
future, err := az.virtualMachineScaleSetVMsClient.Delete(ctx, resourceGroup, virtualMachineScaleSet, instanceID)
if err != nil {
return err
}
if err = future.WaitForCompletionRef(ctx, az.virtualMachineScaleSetVMsClient.Client); err != nil {
return err
}
_, err = future.Result(az.virtualMachineScaleSetVMsClient)
return err
}
// DeleteVirtualMachineScaleSet deletes an entire VM Scale Set.
func (az *AzureClient) DeleteVirtualMachineScaleSet(ctx context.Context, resourceGroup, vmssName string) error {
future, err := az.virtualMachineScaleSetsClient.Delete(ctx, resourceGroup, vmssName)
if err != nil {
return err
}
if err = future.WaitForCompletionRef(ctx, az.virtualMachineScaleSetsClient.Client); err != nil {
return err
}
_, err = future.Result(az.virtualMachineScaleSetsClient)
return err
}
// SetVirtualMachineScaleSetCapacity sets the VMSS capacity
func (az *AzureClient) SetVirtualMachineScaleSetCapacity(ctx context.Context, resourceGroup, virtualMachineScaleSet string, sku azcompute.Sku, location string) error {
s := compute.Sku{}
err := DeepCopy(&s, sku)
if err != nil {
return fmt.Errorf("fail to convert SKU, %v", err)
}
future, err := az.virtualMachineScaleSetsClient.CreateOrUpdate(
ctx,
resourceGroup,
virtualMachineScaleSet,
compute.VirtualMachineScaleSet{
Location: &location,
Sku: &s,
})
if err != nil {
return err
}
if err = future.WaitForCompletionRef(ctx, az.virtualMachineScaleSetsClient.Client); err != nil {
return err
}
_, err = future.Result(az.virtualMachineScaleSetsClient)
return err
}