0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-01 11:02:59 +00:00
terraform-provider-proxmox/proxmox/nodes/vms/custom_virtualio_device.go
Pavel Boldyrev 580381f892
chore(api): refactor nodes/vms/vms_types.go: split into multiple files (#1368)
Split all `Custom*` structs and marshaling code into separate files from `vms_types.go`

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
2024-06-09 04:11:16 +00:00

63 lines
1.7 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 vms
import (
"fmt"
"net/url"
"strings"
"github.com/bpg/terraform-provider-proxmox/proxmox/types"
)
// CustomVirtualIODevice handles QEMU VirtIO device parameters.
type CustomVirtualIODevice struct {
AIO *string `json:"aio,omitempty" url:"aio,omitempty"`
BackupEnabled *types.CustomBool `json:"backup,omitempty" url:"backup,omitempty,int"`
Enabled bool `json:"-" url:"-"`
FileVolume string `json:"file" url:"file"`
}
// CustomVirtualIODevices handles QEMU VirtIO device parameters.
type CustomVirtualIODevices []CustomVirtualIODevice
// EncodeValues converts a CustomVirtualIODevice struct to a URL value.
func (r CustomVirtualIODevice) EncodeValues(key string, v *url.Values) error {
values := []string{
fmt.Sprintf("file=%s", r.FileVolume),
}
if r.AIO != nil {
values = append(values, fmt.Sprintf("aio=%s", *r.AIO))
}
if r.BackupEnabled != nil {
if *r.BackupEnabled {
values = append(values, "backup=1")
} else {
values = append(values, "backup=0")
}
}
v.Add(key, strings.Join(values, ","))
return nil
}
// EncodeValues converts a CustomVirtualIODevices array to multiple URL values.
func (r CustomVirtualIODevices) EncodeValues(key string, v *url.Values) error {
for i, d := range r {
if d.Enabled {
if err := d.EncodeValues(fmt.Sprintf("%s%d", key, i), v); err != nil {
return fmt.Errorf("error encoding virtual IO device %d: %w", i, err)
}
}
}
return nil
}