Skip to content
Open
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
1 change: 1 addition & 0 deletions proxmox/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ func (c *Client) DeleteVm(ctx context.Context, vmr *VmRef) (exitStatus string, e
return c.DeleteVmParams(ctx, vmr, nil)
}

// Deprecated: use VmRef.Delete() instead.
func (c *Client) DeleteVmParams(ctx context.Context, vmr *VmRef, params map[string]interface{}) (exitStatus string, err error) {
err = c.CheckVmRef(ctx, vmr)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions proxmox/client__api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
// in the future we might put the interface even lower, but for now this is sufficient
type clientApiInterface interface {
createHaRule(ctx context.Context, params map[string]any) error
deleteGuest(ctx context.Context, vmr *VmRef, purge bool) error
deleteHaResource(ctx context.Context, id GuestID) error
deleteHaRule(ctx context.Context, id HaRuleID) error
getGuestConfig(ctx context.Context, vmr *VmRef) (map[string]any, error)
Expand Down
8 changes: 8 additions & 0 deletions proxmox/client__api__mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type mockClientAPI struct {
createHaRuleFunc func(ctx context.Context, params map[string]any) error
deleteGuestFunc func(ctx context.Context, vmr *VmRef, purge bool) error
deleteHaResourceFunc func(ctx context.Context, id GuestID) error
deleteHaRuleFunc func(ctx context.Context, id HaRuleID) error
getGuestConfigFunc func(ctx context.Context, vmr *VmRef) (map[string]any, error)
Expand Down Expand Up @@ -33,6 +34,13 @@ func (m *mockClientAPI) createHaRule(ctx context.Context, params map[string]any)
return m.createHaRuleFunc(ctx, params)
}

func (m *mockClientAPI) deleteGuest(ctx context.Context, vmr *VmRef, purge bool) error {
if m.deleteGuestFunc == nil {
m.panic("deleteGuestFunc")
}
return m.deleteGuestFunc(ctx, vmr, purge)
}

func (m *mockClientAPI) deleteHaResource(ctx context.Context, id GuestID) error {
if m.deleteHaResourceFunc == nil {
m.panic("deleteHaResourceFunc")
Expand Down
13 changes: 13 additions & 0 deletions proxmox/client__api__reusable.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ func (c *clientAPI) delete(ctx context.Context, url string) (err error) {
return
}

// Makes a DELETE request and waits on proxmox for the task to complete.
// It returns the HTTP error as 'err'.
func (c *clientAPI) deleteTask(ctx context.Context, url string) error {
var resp *http.Response
resp, err := c.session.delete(ctx, url, nil, nil)
if err != nil {
_ = c.handleTaskError(resp)
return err
}
_, err = c.checkTask(ctx, resp)
return err
}

func (c *clientAPI) getMap(ctx context.Context, url, text, message string, ignore errorIgnore) (map[string]any, error) {
data, err := c.getRootMap(ctx, url, text, message, ignore)
if err != nil {
Expand Down
46 changes: 29 additions & 17 deletions proxmox/vmref.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,6 @@ func (c *clientNew) guestDelete(ctx context.Context, vmr *VmRef) error {
if !ok {
return errorMsg{}.guestDoesNotExist(vmr.vmId)
}
if haState := rawGuest.GetHaState(); haState != "" {
if _, err = guestID.deleteHaResource(ctx, ca); err != nil {
return err
}
}

guestType := rawGuest.GetType()
vmr.node = rawGuest.GetNode()
Expand Down Expand Up @@ -142,16 +137,15 @@ func (c *clientNew) guestDelete(ctx context.Context, vmr *VmRef) error {
return err
}

haEnabled := rawGuest.GetHaState() != ""
if rawGuest.GetStatus() != PowerStateStopped { // Check if guest is running
for {
var guestStatus RawGuestStatus
guestStatus, err = vmr.getRawGuestStatus_Unsafe(ctx, c.oldClient)
if err != nil {
if haEnabled {
if _, err = guestID.deleteHaResource(ctx, ca); err != nil { // It's faster to delete HA resource first, instead of stopping via HA
return err
}
if guestStatus.GetState() == PowerStateStopped {
break
}
haEnabled = false
}
for {
if version.Encode() >= version_8_0_0 { // Try to force stop the guest if supported
err = vmr.forceStop_Unsafe(ctx, ca)
} else {
Expand All @@ -160,21 +154,36 @@ func (c *clientNew) guestDelete(ctx context.Context, vmr *VmRef) error {
if err != nil {
return err
}
var guestStatus RawGuestStatus
guestStatus, err = vmr.getRawGuestStatus_Unsafe(ctx, c.oldClient)
if err != nil {
return err
}
if guestStatus.GetState() == PowerStateStopped {
break
}
}
}
return vmr.delete_Unsafe(ctx, c.oldClient)
return vmr.delete_Unsafe(ctx, c.apiGet(), haEnabled)
}

func (vmr VmRef) DeleteNoCheck(ctx context.Context, c *Client) error {
if err := c.checkInitialized(); err != nil {
return err
}
return vmr.delete_Unsafe(ctx, c)
return vmr.delete_Unsafe(ctx, c.new().apiGet(), false)
}

func (vmr *VmRef) delete_Unsafe(ctx context.Context, c *Client) error {
_, err := c.DeleteVmParams(ctx, vmr, nil) // TODO use a more optimized version
return err
func (vmr *VmRef) delete_Unsafe(ctx context.Context, c clientApiInterface, purge bool) error {
return c.deleteGuest(ctx, vmr, purge)
}

func (c *clientAPI) deleteGuest(ctx context.Context, vmr *VmRef, purge bool) error {
var urlOptions string
if purge {
urlOptions = "?purge=1"
}
return c.deleteTask(ctx, "/nodes/"+vmr.node.String()+"/"+vmr.vmType.String()+"/"+vmr.vmId.String()+urlOptions)
}
Comment on lines +181 to 187
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The deleteGuest function lacks documentation. Consider adding a comment explaining what the function does and what the purge parameter means. For example: "deleteGuest deletes the guest VM. When purge is true, it also removes the VM from HA resources and related configurations."

Copilot uses AI. Check for mistakes.

// ForceStop stops the guest immediately without a graceful shutdown and cancels any stop/shutdown operations in progress.
Expand All @@ -197,6 +206,9 @@ func (c *clientNew) guestStopForce(ctx context.Context, vmr *VmRef) error {
return vmr.forceStop_Unsafe(ctx, c.apiGet())
}

// forceStop stops the guest immediately without a graceful shutdown and cancels any stop/shutdown operations in progress.
// This function requires Proxmox VE 8.0 or later.
// Gives error when HA is enabled.
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The grammar in this comment is awkward. Consider rephrasing to "Returns an error when HA is enabled" or "Will error if HA is enabled" for better clarity.

Suggested change
// Gives error when HA is enabled.
// Returns an error when HA is enabled.

Copilot uses AI. Check for mistakes.
func (vmr *VmRef) forceStop_Unsafe(ctx context.Context, c clientApiInterface) error {
return c.updateGuestStatus(ctx, vmr, "stop", map[string]any{"overrule-shutdown": int(1)})
}
Expand Down