fix: refactor manual arguments to url.Values
Some checks failed
CI / build (push) Failing after 3m24s
CI / lint (push) Failing after 2m51s
CI / pkl-validate (push) Successful in 10s
CI / integration-tests (push) Has been skipped
CI / conformance-tests (latest) (push) Has been skipped

This commit is contained in:
2026-02-03 23:01:16 +01:00
parent 35a789d5d9
commit e71d964f2c
2 changed files with 25 additions and 8 deletions

View File

@@ -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 {

View File

@@ -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{