Skip to content

Remove running VM #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/sc_demo_vm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ resource "hypercore_nic" "vlan_all" {

resource "hypercore_vm_power_state" "demo_vm" {
vm_uuid = hypercore_vm.demo_vm.id
state = "SHUTOFF" # available states are: SHUTOFF, RUNNING, PAUSED
state = "RUNNING" # available states are: SHUTOFF, RUNNING, PAUSED
depends_on = [
hypercore_disk.os,
hypercore_disk.iso,
Expand Down
11 changes: 11 additions & 0 deletions internal/provider/hypercore_vm_power_state_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,17 @@ func (r *HypercoreVMPowerStateResource) Delete(ctx context.Context, req resource
}

// Extra implementation not needed
// We use this to ensure VM is shutdown, before it is deleted.
// After VM is shutdown, disks can be deleted.
// Code assumes there is no reason to remove hypercore_vm_power_state resource from Terraform plan,
// while keeping hypercore_vm resource.
restClient := *r.client
vm_uuid := data.VmUUID.ValueString()
err := ShutdownVM(ctx, vm_uuid, &restClient)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to shutdown VM before, got error: %s", err))
return
}

// If applicable, this is a great opportunity to initialize any necessary
// provider client data and make a call using it.
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/hypercore_vm_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,9 @@ func (r *HypercoreVMResource) Delete(ctx context.Context, req resource.DeleteReq

restClient := *r.client
vm_uuid := data.Id.ValueString()
err := shutdownVM(ctx, vm_uuid, &restClient)
err := ShutdownVM(ctx, vm_uuid, &restClient)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete shutdown VM, got error: %s", err))
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to shutdown VM, got error: %s", err))
return
}

Expand All @@ -520,7 +520,7 @@ func (r *HypercoreVMResource) ImportState(ctx context.Context, req resource.Impo
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func shutdownVM(ctx context.Context, vmUUID string, restClient *utils.RestClient) diag.Diagnostic {
func ShutdownVM(ctx context.Context, vmUUID string, restClient *utils.RestClient) diag.Diagnostic {
currentState, err := utils.GetVMPowerState(vmUUID, *restClient)
if err != nil {
return err
Expand Down
Loading