0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-05 05:24:01 +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.Release = types.StringValue(version.Release)
state.RepositoryID = types.StringValue(version.RepositoryID) state.RepositoryID = types.StringValue(version.RepositoryID)
state.Version = types.StringValue(version.Version) state.Version = types.StringValue(version.Version.String())
state.ID = types.StringValue("version") 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 package version
import (
"fmt"
"github.com/hashicorp/go-version"
)
// ResponseBody contains the body from a version response. // ResponseBody contains the body from a version response.
type ResponseBody struct { type ResponseBody struct {
Data *ResponseData `json:"data,omitempty"` Data *ResponseData `json:"data,omitempty"`
@ -13,8 +19,24 @@ type ResponseBody struct {
// ResponseData contains the data from a version response. // ResponseData contains the data from a version response.
type ResponseData struct { type ResponseData struct {
Console string `json:"console"` Console string `json:"console"`
Release string `json:"release"` Release string `json:"release"`
RepositoryID string `json:"repoid"` 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" "path/filepath"
"slices" "slices"
"sort" "sort"
"strconv"
"strings" "strings"
"time" "time"
@ -31,6 +30,7 @@ import (
"github.com/bpg/terraform-provider-proxmox/proxmox" "github.com/bpg/terraform-provider-proxmox/proxmox"
"github.com/bpg/terraform-provider-proxmox/proxmox/api" "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"
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/validators" "github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/validators"
"github.com/bpg/terraform-provider-proxmox/utils" "github.com/bpg/terraform-provider-proxmox/utils"
@ -408,7 +408,7 @@ func fileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag
"url": sourceFilePath, "url": sourceFilePath,
}) })
version, e := api.GetMinTLSVersion(sourceFileMinTLS) minTLSVersion, e := api.GetMinTLSVersion(sourceFileMinTLS)
if e != nil { if e != nil {
return diag.FromErr(e) return diag.FromErr(e)
} }
@ -416,7 +416,7 @@ func fileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag
httpClient := http.Client{ httpClient := http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
TLSClientConfig: &tls.Config{ TLSClientConfig: &tls.Config{
MinVersion: version, MinVersion: minTLSVersion,
InsecureSkipVerify: sourceFileInsecure, InsecureSkipVerify: sourceFileInsecure,
}, },
}, },
@ -624,28 +624,13 @@ func fileGetContentType(ctx context.Context, d *schema.ResourceData, c proxmox.C
sourceFile := d.Get(mkResourceVirtualEnvironmentFileSourceFile).([]interface{}) sourceFile := d.Get(mkResourceVirtualEnvironmentFileSourceFile).([]interface{})
sourceRaw := d.Get(mkResourceVirtualEnvironmentFileSourceRaw).([]interface{}) sourceRaw := d.Get(mkResourceVirtualEnvironmentFileSourceRaw).([]interface{})
releaseMajor := 0 ver := version.MinimumProxmoxVersion
releaseMinor := 0 if versionResp, err := c.Version().Version(ctx); err == nil {
ver = versionResp.Version
version, err := c.Version().Version(ctx) } else {
if err != nil { tflog.Warn(ctx, fmt.Sprintf("failed to determine Proxmox VE version, assume %v", ver), map[string]interface{}{
tflog.Warn(ctx, "failed to determine Proxmox VE version", map[string]interface{}{
"error": err, "error": err,
}) })
} 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{}{
"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 := "" sourceFilePath := ""
@ -668,10 +653,10 @@ func fileGetContentType(ctx context.Context, d *schema.ResourceData, c proxmox.C
if strings.HasSuffix(sourceFilePath, ".tar.gz") || if strings.HasSuffix(sourceFilePath, ".tar.gz") ||
strings.HasSuffix(sourceFilePath, ".tar.xz") { strings.HasSuffix(sourceFilePath, ".tar.xz") {
contentType = "vztmpl" contentType = "vztmpl"
// For Proxmox VE 8.4 and later, we can import VM images to the "import" content type. } else if ver.SupportImportContentType() &&
} else if releaseMajor >= 8 && releaseMinor > 4 && (strings.HasSuffix(sourceFilePath, ".qcow2") || (strings.HasSuffix(sourceFilePath, ".qcow2") ||
strings.HasSuffix(sourceFilePath, ".raw") || strings.HasSuffix(sourceFilePath, ".raw") ||
strings.HasSuffix(sourceFilePath, ".vmdk")) { strings.HasSuffix(sourceFilePath, ".vmdk")) {
contentType = "import" contentType = "import"
} else { } else {
ext := strings.TrimLeft(strings.ToLower(filepath.Ext(sourceFilePath)), ".") ext := strings.TrimLeft(strings.ToLower(filepath.Ext(sourceFilePath)), ".")