mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-07-05 05:24:01 +00:00
feat(vm): support stopping (rather than shutting down) VMs on resource destroy (#783)
* Support stopping VMs on destroy Signed-off-by: Chase <31072505+ActualTrash@users.noreply.github.com> * Make the linter happy Signed-off-by: Chase <31072505+ActualTrash@users.noreply.github.com> --------- Signed-off-by: Chase <31072505+ActualTrash@users.noreply.github.com>
This commit is contained in:
parent
db270efd89
commit
6ebe8dcc60
@ -475,6 +475,7 @@ output "ubuntu_vm_public_key" {
|
|||||||
resource. You may use the `ignore_changes` lifecycle meta-argument to ignore
|
resource. You may use the `ignore_changes` lifecycle meta-argument to ignore
|
||||||
changes to this attribute.
|
changes to this attribute.
|
||||||
- `template` - (Optional) Whether to create a template (defaults to `false`).
|
- `template` - (Optional) Whether to create a template (defaults to `false`).
|
||||||
|
- `stop_on_destroy` - (Optional) Whether to stop rather than shutdown on VM destroy (defaults to `false`)
|
||||||
- `timeout_clone` - (Optional) Timeout for cloning a VM in seconds (defaults to
|
- `timeout_clone` - (Optional) Timeout for cloning a VM in seconds (defaults to
|
||||||
1800).
|
1800).
|
||||||
- `timeout_create` - (Optional) Timeout for creating a VM in seconds (defaults to
|
- `timeout_create` - (Optional) Timeout for creating a VM in seconds (defaults to
|
||||||
|
@ -137,7 +137,7 @@ const (
|
|||||||
dvResourceVirtualEnvironmentVMVGAMemory = 16
|
dvResourceVirtualEnvironmentVMVGAMemory = 16
|
||||||
dvResourceVirtualEnvironmentVMVGAType = "std"
|
dvResourceVirtualEnvironmentVMVGAType = "std"
|
||||||
dvResourceVirtualEnvironmentVMSCSIHardware = "virtio-scsi-pci"
|
dvResourceVirtualEnvironmentVMSCSIHardware = "virtio-scsi-pci"
|
||||||
|
dvResourceVirtualEnvironmentVMStopOnDestroy = false
|
||||||
dvResourceVirtualEnvironmentVMHookScript = ""
|
dvResourceVirtualEnvironmentVMHookScript = ""
|
||||||
|
|
||||||
maxResourceVirtualEnvironmentVMAudioDevices = 1
|
maxResourceVirtualEnvironmentVMAudioDevices = 1
|
||||||
@ -299,6 +299,7 @@ const (
|
|||||||
mkResourceVirtualEnvironmentVMVMID = "vm_id"
|
mkResourceVirtualEnvironmentVMVMID = "vm_id"
|
||||||
mkResourceVirtualEnvironmentVMSCSIHardware = "scsi_hardware"
|
mkResourceVirtualEnvironmentVMSCSIHardware = "scsi_hardware"
|
||||||
mkResourceVirtualEnvironmentVMHookScriptFileID = "hook_script_file_id"
|
mkResourceVirtualEnvironmentVMHookScriptFileID = "hook_script_file_id"
|
||||||
|
mkResourceVirtualEnvironmentVMStopOnDestroy = "stop_on_destroy"
|
||||||
)
|
)
|
||||||
|
|
||||||
// VM returns a resource that manages VMs.
|
// VM returns a resource that manages VMs.
|
||||||
@ -1576,6 +1577,12 @@ func VM() *schema.Resource {
|
|||||||
Optional: true,
|
Optional: true,
|
||||||
Default: dvResourceVirtualEnvironmentVMHookScript,
|
Default: dvResourceVirtualEnvironmentVMHookScript,
|
||||||
},
|
},
|
||||||
|
mkResourceVirtualEnvironmentVMStopOnDestroy: {
|
||||||
|
Type: schema.TypeBool,
|
||||||
|
Description: "Whether to stop rather than shutdown on VM destroy",
|
||||||
|
Optional: true,
|
||||||
|
Default: dvResourceVirtualEnvironmentVMStopOnDestroy,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
CreateContext: vmCreate,
|
CreateContext: vmCreate,
|
||||||
ReadContext: vmRead,
|
ReadContext: vmRead,
|
||||||
@ -1747,6 +1754,20 @@ func vmShutdown(ctx context.Context, vmAPI *vms.Client, d *schema.ResourceData)
|
|||||||
return diag.FromErr(vmAPI.WaitForVMState(ctx, "stopped", shutdownTimeout, 1))
|
return diag.FromErr(vmAPI.WaitForVMState(ctx, "stopped", shutdownTimeout, 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Forcefully stop the VM, then wait for it to actually stop.
|
||||||
|
func vmStop(ctx context.Context, vmAPI *vms.Client, d *schema.ResourceData) diag.Diagnostics {
|
||||||
|
tflog.Debug(ctx, "Stopping VM")
|
||||||
|
|
||||||
|
stopTimeout := d.Get(mkResourceVirtualEnvironmentVMTimeoutStopVM).(int)
|
||||||
|
|
||||||
|
e := vmAPI.StopVM(ctx, stopTimeout+30)
|
||||||
|
if e != nil {
|
||||||
|
return diag.FromErr(e)
|
||||||
|
}
|
||||||
|
|
||||||
|
return diag.FromErr(vmAPI.WaitForVMState(ctx, "stopped", stopTimeout, 1))
|
||||||
|
}
|
||||||
|
|
||||||
func vmCreateClone(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
|
func vmCreateClone(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
|
||||||
config := m.(proxmoxtf.ProviderConfiguration)
|
config := m.(proxmoxtf.ProviderConfiguration)
|
||||||
|
|
||||||
@ -6126,17 +6147,24 @@ func vmDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D
|
|||||||
|
|
||||||
vmAPI := api.Node(nodeName).VM(vmID)
|
vmAPI := api.Node(nodeName).VM(vmID)
|
||||||
|
|
||||||
// Shut down the virtual machine before deleting it.
|
// Stop or shut down the virtual machine before deleting it.
|
||||||
status, err := vmAPI.GetVMStatus(ctx)
|
status, err := vmAPI.GetVMStatus(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return diag.FromErr(err)
|
return diag.FromErr(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
stop := d.Get(mkResourceVirtualEnvironmentVMStopOnDestroy).(bool)
|
||||||
if status.Status != "stopped" {
|
if status.Status != "stopped" {
|
||||||
|
if stop {
|
||||||
|
if e := vmStop(ctx, vmAPI, d); e != nil {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if e := vmShutdown(ctx, vmAPI, d); e != nil {
|
if e := vmShutdown(ctx, vmAPI, d); e != nil {
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = vmAPI.DeleteVM(ctx)
|
err = vmAPI.DeleteVM(ctx)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user