Skip to content

Commit

Permalink
Merge pull request #279 from FredrickB/add-detaching-disks-functional…
Browse files Browse the repository at this point in the history
…ity-to-client

feat: add support for unlinking disks from VM
  • Loading branch information
mleone87 authored Dec 6, 2023
2 parents 49c9bc2 + d062c3c commit 566d203
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ export PM_HTTP_HEADERS=Key,Value,Key1,Value1 (only if required)

./proxmox-api-go node shutdown proxmox-node-name

./proxmox-api-go unlink 123 proxmox-node-name "virtio1,virtio2,virtioN" [false|true]

```
## Proxy server support
Expand Down
28 changes: 28 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,34 @@ func main() {
failError(err)
fmt.Println(string(dnsList))

case "unlink":
if len(flag.Args()) < 4 {
failError(fmt.Errorf("error: invoke with <vmID> <node> <diskID [<forceRemoval: false|true>]"))
}

vmIdUnparsed := flag.Args()[1]
node := flag.Args()[2]
vmId, err := strconv.Atoi(vmIdUnparsed)
if err != nil {
failError(fmt.Errorf("Failed to convert vmId: %s to a string, error: %+v", vmIdUnparsed, err))
}

disks := flag.Args()[3]
forceRemoval := false
if len(flag.Args()) > 4 {
forceRemovalUnparsed := flag.Args()[4]
forceRemoval, err = strconv.ParseBool(forceRemovalUnparsed)
if err != nil {
failError(fmt.Errorf("Failed to convert <forceRemoval>: %s to a bool, error: %+v", forceRemovalUnparsed, err))
}
}

exitStatus, err := c.Unlink(node, vmId, disks, forceRemoval)
if err != nil {
failError(fmt.Errorf("error: %+v\n api error: %s", err, exitStatus))
}
log.Printf("Unlinked disks: %s from vmId: %d. Disks removed: %t", disks, vmId, forceRemoval)

default:
fmt.Printf("unknown action, try start|stop vmid\n")
}
Expand Down
19 changes: 19 additions & 0 deletions proxmox/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,25 @@ func (c *Client) MoveQemuDiskToVM(vmrSource *VmRef, disk string, vmrTarget *VmRe
return
}

// Unlink - Unlink (detach) a set of disks from a VM.
// Reference: https://pve.proxmox.com/pve-docs/api-viewer/index.html#/nodes/{node}/qemu/{vmid}/unlink
func (c *Client) Unlink(node string, vmId int, diskIds string, forceRemoval bool) (exitStatus string, err error) {
url := fmt.Sprintf("/nodes/%s/qemu/%d/unlink", node, vmId)
data := ParamsToBody(map[string]interface{}{
"idlist": diskIds,
"force": forceRemoval,
})
resp, err := c.session.Put(url, nil, nil, &data)
if err != nil {
return c.HandleTaskError(resp), err
}
json, err := ResponseJSON(resp)
if err != nil {
return "", err
}
return c.WaitForCompletion(json)
}

// GetNextID - Get next free VMID
func (c *Client) GetNextID(currentID int) (nextID int, err error) {
var data map[string]interface{}
Expand Down

0 comments on commit 566d203

Please sign in to comment.