fix: refactor manual arguments to url.Values
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -53,8 +55,12 @@ func createAuthorizationString(username, token string) string {
|
|||||||
return "PVEAPIToken=" + username + "=" + token
|
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{}
|
client := &http.Client{}
|
||||||
|
body := &bytes.Buffer{}
|
||||||
|
if urlparams != nil {
|
||||||
|
body = bytes.NewBuffer([]byte(urlparams.Encode()))
|
||||||
|
}
|
||||||
|
|
||||||
request, err := http.NewRequest(method, url, body)
|
request, err := http.NewRequest(method, url, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
25
proxmox.go
25
proxmox.go
@@ -5,13 +5,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/platform-engineering-labs/formae/pkg/plugin"
|
"github.com/platform-engineering-labs/formae/pkg/plugin"
|
||||||
@@ -121,12 +121,18 @@ func (p *Plugin) Create(ctx context.Context, req *resource.CreateRequest) (*reso
|
|||||||
}, err
|
}, 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 != "" {
|
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 {
|
if err != nil {
|
||||||
return &resource.CreateResult{
|
return &resource.CreateResult{
|
||||||
@@ -272,10 +278,15 @@ func (p *Plugin) Update(ctx context.Context, req *resource.UpdateRequest) (*reso
|
|||||||
}, err
|
}, 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), urlparams)
|
||||||
_, err = authenticatedRequest(http.MethodPut, config.URL+"/api2/json/nodes/"+config.NODE+"/lxc/"+desir.VMID+"/config", createAuthorizationString(username, token), argumentBuffer)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &resource.UpdateResult{
|
return &resource.UpdateResult{
|
||||||
|
|||||||
Reference in New Issue
Block a user