-
Notifications
You must be signed in to change notification settings - Fork 39
/
virtual_machine.go
43 lines (33 loc) · 1 KB
/
virtual_machine.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
package resourcestatus
import (
"context"
"errors"
"fmt"
"strings"
"github.com/Azure/go-autorest/autorest/azure"
"github.com/seal-io/walrus/pkg/dao/types/status"
)
func getVirtualMachine(ctx context.Context, resourceType, name string) (*status.Status, error) {
client, err := virtualMachineClient(ctx)
if err != nil {
return &status.Status{}, err
}
r, err := azure.ParseResourceID(name)
if err != nil {
return nil, err
}
resp, err := client.InstanceView(ctx, r.ResourceGroup, r.ResourceName, nil)
if err != nil {
return nil, fmt.Errorf("failed to get azure resource %s %s: %w", resourceType, r.ResourceName, err)
}
if resp.Statuses == nil || len(resp.Statuses) == 0 {
return nil, errors.New("not found")
}
for _, vmStatus := range resp.Statuses {
if strings.HasPrefix(*vmStatus.Code, "PowerState/") {
state := strings.TrimPrefix(*vmStatus.Code, "PowerState/")
return virtualMachineStatusConverter.Convert(state, *vmStatus.Message), nil
}
}
return nil, errors.New("not found")
}