0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-29 18:21:10 +00:00
terraform-provider-proxmox/proxmox/nodes/vms/custom_rng_device.go
Pavel Boldyrev 3119194292
feat(vm): add RNG device support (#1774)
* feat(vm): add RNG device  support

This commit adds support for configuring the Random Number Generator (RNG) device for virtual machines in both the VM resource and datasource. The implementation includes:

- New schema and model for RNG configuration
- Support for setting RNG source, max_bytes, and period
- Updated documentation for RNG configuration
- Test cases for RNG configuration
- Integration with VM creation, update, and read operations

---------

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
2025-02-16 23:53:40 -05:00

87 lines
1.9 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 (
"encoding/json"
"fmt"
"net/url"
"strconv"
"strings"
)
// CustomRNGDevice represents a random number generator device configuration.
type CustomRNGDevice struct {
Source string `json:"source,omitempty" url:"source,omitempty"`
MaxBytes *int `json:"max_bytes,omitempty" url:"max_bytes,omitempty"`
Period *int `json:"period,omitempty" url:"period,omitempty"`
}
// EncodeValues converts a CustomRNGDevice struct to a URL value.
func (r *CustomRNGDevice) EncodeValues(key string, v *url.Values) error {
var values []string
if r.Source != "" {
values = append(values, fmt.Sprintf("source=%s", r.Source))
}
if r.MaxBytes != nil {
values = append(values, fmt.Sprintf("max_bytes=%d", *r.MaxBytes))
}
if r.Period != nil {
values = append(values, fmt.Sprintf("period=%d", *r.Period))
}
if len(values) > 0 {
v.Add(key, strings.Join(values, ","))
}
return nil
}
// UnmarshalJSON unmarshals a JSON object into a CustomRNGDevice struct.
func (r *CustomRNGDevice) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return fmt.Errorf("failed to unmarshal CustomRNGDevice: %w", err)
}
pairs := strings.Split(s, ",")
for _, p := range pairs {
v := strings.Split(strings.TrimSpace(p), "=")
if len(v) == 1 {
r.Source = v[0]
} else if len(v) == 2 {
switch v[0] {
case "source":
r.Source = v[1]
case "max_bytes":
maxBytes, err := strconv.Atoi(v[1])
if err != nil {
return fmt.Errorf("failed to parse max_bytes: %w", err)
}
r.MaxBytes = &maxBytes
case "period":
period, err := strconv.Atoi(v[1])
if err != nil {
return fmt.Errorf("failed to parse period: %w", err)
}
r.Period = &period
}
}
}
return nil
}