0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-05 13:33:58 +00:00

refactor version retrieval / parsing / comparison

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
This commit is contained in:
Pavel Boldyrev 2025-06-17 08:51:17 -04:00
parent a005b7062a
commit 6edb78e111
No known key found for this signature in database
GPG Key ID: 637146A2A6804C59
4 changed files with 59 additions and 32 deletions

View File

@ -115,7 +115,7 @@ func (d *versionDatasource) Read(ctx context.Context, _ datasource.ReadRequest,
state.Release = types.StringValue(version.Release)
state.RepositoryID = types.StringValue(version.RepositoryID)
state.Version = types.StringValue(version.Version)
state.Version = types.StringValue(version.Version.String())
state.ID = types.StringValue("version")

View File

@ -0,0 +1,20 @@
/*
* 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 version
import "github.com/hashicorp/go-version"
// MinimumProxmoxVersion is the minimum supported Proxmox version by the provider.
//
//nolint:gochecknoglobals
var MinimumProxmoxVersion = ProxmoxVersion{*version.Must(version.NewVersion("8.0.0"))}
// SupportImportContentType checks if the Proxmox version supports the `import` content type when uploading disk images.
// See https://bugzilla.proxmox.com/show_bug.cgi?id=2424
func (v *ProxmoxVersion) SupportImportContentType() bool {
return v.GreaterThanOrEqual(version.Must(version.NewVersion("8.4.0")))
}

View File

@ -6,6 +6,12 @@
package version
import (
"fmt"
"github.com/hashicorp/go-version"
)
// ResponseBody contains the body from a version response.
type ResponseBody struct {
Data *ResponseData `json:"data,omitempty"`
@ -16,5 +22,21 @@ type ResponseData struct {
Console string `json:"console"`
Release string `json:"release"`
RepositoryID string `json:"repoid"`
Version string `json:"version"`
Version ProxmoxVersion `json:"version"`
}
type ProxmoxVersion struct {
version.Version
}
func (v *ProxmoxVersion) UnmarshalJSON(data []byte) error {
// Unmarshal the version string into a go-version Version object
ver, err := version.NewVersion(string(data))
if err != nil {
return fmt.Errorf("failed to parse version %q: %w", string(data), err)
}
v.Version = *ver
return nil
}

View File

@ -20,7 +20,6 @@ import (
"path/filepath"
"slices"
"sort"
"strconv"
"strings"
"time"
@ -31,6 +30,7 @@ import (
"github.com/bpg/terraform-provider-proxmox/proxmox"
"github.com/bpg/terraform-provider-proxmox/proxmox/api"
"github.com/bpg/terraform-provider-proxmox/proxmox/version"
"github.com/bpg/terraform-provider-proxmox/proxmoxtf"
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/validators"
"github.com/bpg/terraform-provider-proxmox/utils"
@ -408,7 +408,7 @@ func fileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag
"url": sourceFilePath,
})
version, e := api.GetMinTLSVersion(sourceFileMinTLS)
minTLSVersion, e := api.GetMinTLSVersion(sourceFileMinTLS)
if e != nil {
return diag.FromErr(e)
}
@ -416,7 +416,7 @@ func fileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag
httpClient := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
MinVersion: version,
MinVersion: minTLSVersion,
InsecureSkipVerify: sourceFileInsecure,
},
},
@ -624,29 +624,14 @@ func fileGetContentType(ctx context.Context, d *schema.ResourceData, c proxmox.C
sourceFile := d.Get(mkResourceVirtualEnvironmentFileSourceFile).([]interface{})
sourceRaw := d.Get(mkResourceVirtualEnvironmentFileSourceRaw).([]interface{})
releaseMajor := 0
releaseMinor := 0
version, err := c.Version().Version(ctx)
if err != nil {
tflog.Warn(ctx, "failed to determine Proxmox VE version", map[string]interface{}{
"error": err,
})
ver := version.MinimumProxmoxVersion
if versionResp, err := c.Version().Version(ctx); err == nil {
ver = versionResp.Version
} else {
release := strings.Split(version.Release, ".")
releaseMajor, err = strconv.Atoi(release[0])
if err != nil {
tflog.Warn(ctx, "failed to parse Proxmox VE version Major", map[string]interface{}{
tflog.Warn(ctx, fmt.Sprintf("failed to determine Proxmox VE version, assume %v", ver), map[string]interface{}{
"error": err,
})
}
releaseMinor, err = strconv.Atoi(release[1])
if err != nil {
tflog.Warn(ctx, "failed to parse Proxmox VE version Minor", map[string]interface{}{
"error": err,
})
}
}
sourceFilePath := ""
@ -668,8 +653,8 @@ func fileGetContentType(ctx context.Context, d *schema.ResourceData, c proxmox.C
if strings.HasSuffix(sourceFilePath, ".tar.gz") ||
strings.HasSuffix(sourceFilePath, ".tar.xz") {
contentType = "vztmpl"
// For Proxmox VE 8.4 and later, we can import VM images to the "import" content type.
} else if releaseMajor >= 8 && releaseMinor > 4 && (strings.HasSuffix(sourceFilePath, ".qcow2") ||
} else if ver.SupportImportContentType() &&
(strings.HasSuffix(sourceFilePath, ".qcow2") ||
strings.HasSuffix(sourceFilePath, ".raw") ||
strings.HasSuffix(sourceFilePath, ".vmdk")) {
contentType = "import"