0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-08-23 03:48:35 +00:00

fix: Non-default VM disk format is not preserved in TF state (#134)

Fix disk format parsing from the volume string.
Add basic unit tests for disk volume string parsing.
This commit is contained in:
Pavel Boldyrev 2022-10-14 18:27:30 -04:00 committed by GitHub
parent 5ad5f7a168
commit b09389f0a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 68 additions and 1 deletions

View File

@ -18,6 +18,7 @@ resource "proxmox_virtual_environment_vm" "example_template" {
# interface = "scsi1"
# discard = "ignore"
# iothread = true
# file_format = "raw"
# }
initialization {

4
go.mod
View File

@ -9,12 +9,14 @@ require (
github.com/hashicorp/terraform-plugin-log v0.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.23.0
github.com/pkg/sftp v1.13.5
github.com/stretchr/testify v1.7.2
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d
)
require (
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
@ -40,6 +42,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
@ -52,4 +55,5 @@ require (
google.golang.org/grpc v1.48.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"net/url"
"path/filepath"
"strconv"
"strings"
)
@ -1555,6 +1556,11 @@ func (r *CustomStorageDevice) UnmarshalJSON(b []byte) error {
if len(v) == 1 {
r.FileVolume = v[0]
ext := filepath.Ext(v[0])
if ext != "" {
format := string([]byte(ext)[1:])
r.Format = &format
}
} else if len(v) == 2 {
switch v[0] {
case "aio":

View File

@ -0,0 +1,56 @@
package proxmox
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestCustomStorageDevice_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
line string
want *CustomStorageDevice
wantErr bool
}{
{
name: "simple volume",
line: `"local-lvm:vm-2041-disk-0,discard=on,iothread=1,size=8G"`,
want: &CustomStorageDevice{
Discard: strPtr("on"),
Enabled: true,
FileVolume: "local-lvm:vm-2041-disk-0",
IOThread: boolPtr(true),
Size: strPtr("8G"),
},
},
{
name: "raw volume type",
line: `"nfs:2041/vm-2041-disk-0.raw,discard=ignore,iothread=1,size=8G"`,
want: &CustomStorageDevice{
Discard: strPtr("ignore"),
Enabled: true,
FileVolume: "nfs:2041/vm-2041-disk-0.raw",
Format: strPtr("raw"),
IOThread: boolPtr(true),
Size: strPtr("8G"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := &CustomStorageDevice{}
if err := r.UnmarshalJSON([]byte(tt.line)); (err != nil) != tt.wantErr {
t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
}
require.Equal(t, tt.want, r)
})
}
}
func strPtr(s string) *string {
return &s
}
func boolPtr(s bool) *CustomBool {
customBool := CustomBool(s)
return &customBool
}

View File

@ -10,5 +10,5 @@ const (
TerraformProviderName = "terraform-provider-proxmox"
// TerraformProviderVersion specifies the version number.
TerraformProviderVersion = "0.6.0"
TerraformProviderVersion = "0.6.3"
)