-
Notifications
You must be signed in to change notification settings - Fork 78
/
vm.go
66 lines (57 loc) · 2.31 KB
/
vm.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package client
import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"github.com/gardener/gardener-extension-provider-azure/pkg/internal"
)
// VirtualMachinesClient is an implementation of Vm for a virtual machine k8sClient.
type VirtualMachinesClient struct {
client *armcompute.VirtualMachinesClient
}
// NewVMClient creates a new VM client
func NewVMClient(auth internal.ClientAuth, tc azcore.TokenCredential, opts *arm.ClientOptions) (*VirtualMachinesClient, error) {
client, err := armcompute.NewVirtualMachinesClient(auth.SubscriptionID, tc, opts)
return &VirtualMachinesClient{client}, err
}
// Get will get virtual machines in a resource group.
func (c *VirtualMachinesClient) Get(ctx context.Context, resourceGroupName string, resource string, opts *armcompute.InstanceViewTypes) (*armcompute.VirtualMachine, error) {
var getOpts *armcompute.VirtualMachinesClientGetOptions
if opts != nil {
getOpts = &armcompute.VirtualMachinesClientGetOptions{
Expand: opts,
}
}
vm, err := c.client.Get(ctx, resourceGroupName, resource, getOpts)
if err != nil {
return nil, FilterNotFoundError(err)
}
return &vm.VirtualMachine, nil
}
// CreateOrUpdate will Create a virtual machine or update an existing one.
func (c *VirtualMachinesClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, name string, parameters armcompute.VirtualMachine) (*armcompute.VirtualMachine, error) {
future, err := c.client.BeginCreateOrUpdate(ctx, resourceGroupName, name, parameters, nil)
if err != nil {
return nil, err
}
res, err := future.PollUntilDone(ctx, nil)
if err != nil {
return nil, err
}
return &res.VirtualMachine, nil
}
// Delete will delete a virtual machine.
func (c *VirtualMachinesClient) Delete(ctx context.Context, resourceGroupName, name string, forceDeletion *bool) error {
future, err := c.client.BeginDelete(ctx, resourceGroupName, name, &armcompute.VirtualMachinesClientBeginDeleteOptions{
ForceDeletion: forceDeletion,
})
if err != nil {
return FilterNotFoundError(err)
}
_, err = future.PollUntilDone(ctx, nil)
return err
}