mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-08-23 03:48:35 +00:00
81 lines
2.5 KiB
Go
81 lines
2.5 KiB
Go
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
package proxmox
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/url"
|
|
)
|
|
|
|
// CreateVM creates an virtual machine.
|
|
func (c *VirtualEnvironmentClient) CreateVM(nodeName string, d *VirtualEnvironmentVMCreateRequestBody) error {
|
|
return c.DoRequest(hmPOST, fmt.Sprintf("nodes/%s/qemu", url.PathEscape(nodeName)), d, nil)
|
|
}
|
|
|
|
// DeleteVM deletes an virtual machine.
|
|
func (c *VirtualEnvironmentClient) DeleteVM(nodeName string, vmID int) error {
|
|
return c.DoRequest(hmDELETE, fmt.Sprintf("nodes/%s/qemu/%d", url.PathEscape(nodeName), vmID), nil, nil)
|
|
}
|
|
|
|
// GetVM retrieves an virtual machine.
|
|
func (c *VirtualEnvironmentClient) GetVM(nodeName string, vmID int) (*VirtualEnvironmentVMGetResponseData, error) {
|
|
resBody := &VirtualEnvironmentVMGetResponseBody{}
|
|
err := c.DoRequest(hmGET, fmt.Sprintf("nodes/%s/qemu/%d/config", url.PathEscape(nodeName), vmID), nil, resBody)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if resBody.Data == nil {
|
|
return nil, errors.New("The server did not include a data object in the response")
|
|
}
|
|
|
|
return resBody.Data, nil
|
|
}
|
|
|
|
// GetVMID retrieves the next available VM identifier.
|
|
func (c *VirtualEnvironmentClient) GetVMID() (*int, error) {
|
|
nodes, err := c.ListNodes()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
vmID := 100
|
|
|
|
VMID:
|
|
for vmID <= 2147483637 {
|
|
for _, n := range nodes {
|
|
err := c.DoRequest(hmGET, fmt.Sprintf("nodes/%s/qemu/%d/status/current", url.PathEscape(n.Name), vmID), nil, nil)
|
|
|
|
if err == nil {
|
|
vmID += 5
|
|
|
|
continue VMID
|
|
}
|
|
}
|
|
|
|
return &vmID, nil
|
|
}
|
|
|
|
return nil, errors.New("Unable to retrieve the next available VM identifier")
|
|
}
|
|
|
|
// ListVMs retrieves a list of virtual machines.
|
|
func (c *VirtualEnvironmentClient) ListVMs() ([]*VirtualEnvironmentVMListResponseData, error) {
|
|
return nil, errors.New("Not implemented")
|
|
}
|
|
|
|
// UpdateVM updates a virtual machine.
|
|
func (c *VirtualEnvironmentClient) UpdateVM(nodeName string, vmID int, d *VirtualEnvironmentVMUpdateRequestBody) error {
|
|
return c.DoRequest(hmPUT, fmt.Sprintf("nodes/%s/qemu/%d/config", url.PathEscape(nodeName), vmID), d, nil)
|
|
}
|
|
|
|
// UpdateVMAsync updates a virtual machine asynchronously.
|
|
func (c *VirtualEnvironmentClient) UpdateVMAsync(nodeName string, vmID int, d *VirtualEnvironmentVMUpdateRequestBody) error {
|
|
return c.DoRequest(hmPOST, fmt.Sprintf("nodes/%s/qemu/%d/config", url.PathEscape(nodeName), vmID), d, nil)
|
|
}
|