From e71d964f2cc241f16cd34cfbb4a7c7b7cfc32138 Mon Sep 17 00:00:00 2001 From: ManInDark <61268856+ManInDark@users.noreply.github.com> Date: Tue, 3 Feb 2026 23:01:16 +0100 Subject: [PATCH] fix: refactor manual arguments to url.Values --- helper.go | 8 +++++++- proxmox.go | 25 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/helper.go b/helper.go index 6118771..f5796a8 100644 --- a/helper.go +++ b/helper.go @@ -1,11 +1,13 @@ package main import ( + "bytes" "encoding/json" "fmt" "io" "log" "net/http" + "net/url" "os" ) @@ -53,8 +55,12 @@ func createAuthorizationString(username, token string) string { return "PVEAPIToken=" + username + "=" + token } -func authenticatedRequest(method, url, authorization string, body io.Reader) ([]byte, error) { +func authenticatedRequest(method, url, authorization string, urlparams url.Values) ([]byte, error) { client := &http.Client{} + body := &bytes.Buffer{} + if urlparams != nil { + body = bytes.NewBuffer([]byte(urlparams.Encode())) + } request, err := http.NewRequest(method, url, body) if err != nil { diff --git a/proxmox.go b/proxmox.go index bfc2e9d..28eb4b9 100644 --- a/proxmox.go +++ b/proxmox.go @@ -5,13 +5,13 @@ package main import ( - "bytes" "context" "encoding/json" "errors" "fmt" "log" "net/http" + "net/url" "strconv" "github.com/platform-engineering-labs/formae/pkg/plugin" @@ -121,12 +121,18 @@ func (p *Plugin) Create(ctx context.Context, req *resource.CreateRequest) (*reso }, err } - arguments := "vmid=" + props.VMID + "&ostemplate=" + props.OSTemplate + "&hostname=" + props.Hostname + "&cores=" + strconv.Itoa(props.Cores) + "&memory=" + strconv.Itoa(props.Memory) + urlparams := url.Values{ + "vmid": {props.VMID}, + "ostemplate": {props.OSTemplate}, + "hostname": {props.Hostname}, + "cores": {strconv.Itoa(props.Cores)}, + "memory": {strconv.Itoa(props.Memory)}, + } if props.Description != "" { - arguments += "&description=" + props.Description + urlparams.Add("description", props.Description) } - _, err = authenticatedRequest(http.MethodPost, config.URL+"/api2/json/nodes/"+config.NODE+"/lxc", createAuthorizationString(username, token), bytes.NewBuffer([]byte(arguments))) + _, err = authenticatedRequest(http.MethodPost, config.URL+"/api2/json/nodes/"+config.NODE+"/lxc", createAuthorizationString(username, token), urlparams) if err != nil { return &resource.CreateResult{ @@ -272,10 +278,15 @@ func (p *Plugin) Update(ctx context.Context, req *resource.UpdateRequest) (*reso }, err } - arguments := "vmid=" + desir.VMID + "&hostname=" + desir.Hostname + "&description=" + desir.Description + "&cores=" + strconv.Itoa(desir.Cores) + "&memory=" + strconv.Itoa(desir.Memory) + urlparams := url.Values{ + "vmid": {desir.VMID}, + "hostname": {desir.Hostname}, + "cores": {strconv.Itoa(desir.Cores)}, + "memory": {strconv.Itoa(desir.Memory)}, + "description": {desir.Description}, + } - argumentBuffer := bytes.NewBuffer([]byte(arguments)) - _, err = authenticatedRequest(http.MethodPut, config.URL+"/api2/json/nodes/"+config.NODE+"/lxc/"+desir.VMID+"/config", createAuthorizationString(username, token), argumentBuffer) + _, err = authenticatedRequest(http.MethodPut, config.URL+"/api2/json/nodes/"+config.NODE+"/lxc/"+desir.VMID+"/config", createAuthorizationString(username, token), urlparams) if err != nil { return &resource.UpdateResult{