/
vm_state.go
148 lines (129 loc) · 3.76 KB
/
vm_state.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
package vms
import (
"context"
"fmt"
"time"
"github.com/awlsring/proxmox-go/proxmox"
vt "github.com/awlsring/terraform-provider-proxmox/proxmox/qemu/vms/types"
"github.com/awlsring/terraform-provider-proxmox/proxmox/utils"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/r3labs/diff/v3"
)
var stateSensitiveProperties = []string{
"BIOS",
"CPU",
"Memory",
"Disks",
"NetworkInterfaces",
"PCIDevices",
"CloudInit",
"MachineType",
"KVMArguments",
}
func isSensitivePropertyChanged(ctx context.Context, state *vt.VirtualMachineResourceModel, plan *vt.VirtualMachineResourceModel) (bool, error) {
diff, err := diff.Diff(state, plan)
if err != nil {
tflog.Debug(ctx, "Error determining running state, defaulting to running")
return false, nil
}
for _, d := range diff {
field := d.Path[0]
tflog.Debug(ctx, fmt.Sprintf("Checking property '%v'", field))
if utils.ListContains(stateSensitiveProperties, field) {
tflog.Debug(ctx, fmt.Sprintf("Property '%v' is state sensitive", field))
return true, nil
}
}
return false, nil
}
func (r *virtualMachineResource) stopIfSensitivePropertyChanged(ctx context.Context, state *vt.VirtualMachineResourceModel, plan *vt.VirtualMachineResourceModel) (bool, error) {
node := state.Node.ValueString()
vmId := int(state.ID.ValueInt64())
running := false
status, err := r.client.GetVirtualMachineStatus(ctx, node, vmId)
if err != nil {
return false, err
}
if status.Status == proxmox.VIRTUALMACHINESTATUS_RUNNING {
running = true
}
isSensitive, err := isSensitivePropertyChanged(ctx, state, plan)
if err != nil {
return false, err
}
if isSensitive {
if running {
tflog.Debug(ctx, "Property is state sensitive, stopping VM")
err = r.stopVm(ctx, node, vmId)
if err != nil {
return false, err
}
return true, nil
}
}
return false, nil
}
func (r *virtualMachineResource) waitForStateChange(ctx context.Context, node string, vmId int, timeout int64, endState proxmox.VirtualMachineStatus) error {
tflog.Debug(ctx, "waiting for state change...")
deadline := setDeadline(timeout)
for {
status, err := r.client.GetVirtualMachineStatus(ctx, node, vmId)
if err != nil {
return err
}
if status.Status == endState {
break
}
if time.Now().After(deadline) {
return fmt.Errorf("timeout waiting for state change")
}
tflog.Debug(ctx, "state is still "+string(status.Status)+", waiting 5 seconds...")
time.Sleep(5 * time.Second)
}
tflog.Debug(ctx, "state changed to "+string(endState))
return nil
}
func (r *virtualMachineResource) startVm(ctx context.Context, node string, id int) error {
tflog.Debug(ctx, "Starting virtual machine")
err := r.client.StartVirtualMachine(ctx, node, id)
if err != nil {
return err
}
err = r.waitForStateChange(ctx, node, id, r.timeouts.Start, proxmox.VIRTUALMACHINESTATUS_RUNNING)
if err != nil {
return err
}
return nil
}
func (r *virtualMachineResource) stopVm(ctx context.Context, node string, id int) error {
tflog.Debug(ctx, "Stopping virtual machine")
err := r.client.StopVirtualMachine(ctx, node, id)
if err != nil {
return err
}
err = r.waitForStateChange(ctx, node, id, r.timeouts.Stop, proxmox.VIRTUALMACHINESTATUS_STOPPED)
if err != nil {
return err
}
return nil
}
func (r *virtualMachineResource) deleteVm(ctx context.Context, node string, id int) error {
tflog.Debug(ctx, "Deleting virtual machine")
err := r.client.DeleteVirtualMachine(ctx, node, id)
if err != nil {
return err
}
deadline := setDeadline(r.timeouts.Delete)
for {
_, err := r.client.GetVirtualMachineStatus(ctx, node, id)
if err != nil {
break
}
if time.Now().After(deadline) {
return fmt.Errorf("timeout waiting for state change")
}
tflog.Debug(ctx, "VM still exists, waiting 5 seconds...")
time.Sleep(5 * time.Second)
}
return nil
}