This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 523
/
compute.go
128 lines (105 loc) · 4.58 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package armhelpers
import (
"context"
"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) (VirtualMachineListResultPage, error) {
page, err := az.virtualMachinesClient.List(ctx, resourceGroup)
return &page, err
}
// GetVirtualMachine returns the specified machine in the specified resource group.
func (az *AzureClient) GetVirtualMachine(ctx context.Context, resourceGroup, name string) (compute.VirtualMachine, error) {
return az.virtualMachinesClient.Get(ctx, resourceGroup, name, "")
}
// 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) (VirtualMachineScaleSetListResultPage, error) {
page, err := az.virtualMachineScaleSetsClient.List(ctx, resourceGroup)
return &page, err
}
// RestartVirtualMachineScaleSets restarts the specified VMSS
func (az *AzureClient) RestartVirtualMachineScaleSets(ctx context.Context, resourceGroup string, virtualMachineScaleSet string, instanceIDs *compute.VirtualMachineScaleSetVMInstanceIDs) error {
future, err := az.virtualMachineScaleSetsClient.Restart(ctx, resourceGroup, virtualMachineScaleSet, instanceIDs)
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) (VirtualMachineScaleSetVMListResultPage, error) {
page, err := az.virtualMachineScaleSetVMsClient.List(ctx, resourceGroup, virtualMachineScaleSet, "", "", "")
return &page, 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 compute.Sku, location string) error {
future, err := az.virtualMachineScaleSetsClient.CreateOrUpdate(
ctx,
resourceGroup,
virtualMachineScaleSet,
compute.VirtualMachineScaleSet{
Location: &location,
Sku: &sku,
})
if err != nil {
return err
}
if err = future.WaitForCompletionRef(ctx, az.virtualMachineScaleSetsClient.Client); err != nil {
return err
}
_, err = future.Result(az.virtualMachineScaleSetsClient)
return err
}