From 35a789d5d9cd30797e2123a864f6e2a1d24d8d9d Mon Sep 17 00:00:00 2001 From: ManInDark <61268856+ManInDark@users.noreply.github.com> Date: Tue, 3 Feb 2026 22:44:23 +0100 Subject: [PATCH] fix: moved authenticated requests to helper --- helper.go | 31 +++++++++++++++++++++ proxmox.go | 74 ++++--------------------------------------------- proxmox_test.go | 27 ++---------------- 3 files changed, 38 insertions(+), 94 deletions(-) diff --git a/helper.go b/helper.go index d828df7..6118771 100644 --- a/helper.go +++ b/helper.go @@ -3,6 +3,9 @@ package main import ( "encoding/json" "fmt" + "io" + "log" + "net/http" "os" ) @@ -45,3 +48,31 @@ func parseLXCProperties(data json.RawMessage) (*LXCProperties, error) { } return &props, nil } + +func createAuthorizationString(username, token string) string { + return "PVEAPIToken=" + username + "=" + token +} + +func authenticatedRequest(method, url, authorization string, body io.Reader) ([]byte, error) { + client := &http.Client{} + + request, err := http.NewRequest(method, url, body) + if err != nil { + return nil, err + } + request.Header.Set("Authorization", authorization) + + resp, err := client.Do(request) + if err != nil { + return nil, err + } + + data, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + log.Println("URL: ", method, "Status Code:", resp.Status, "Body: ", string(data)) + + return data, nil +} diff --git a/proxmox.go b/proxmox.go index 9f1b718..bfc2e9d 100644 --- a/proxmox.go +++ b/proxmox.go @@ -10,7 +10,6 @@ import ( "encoding/json" "errors" "fmt" - "io" "log" "net/http" "strconv" @@ -122,17 +121,12 @@ func (p *Plugin) Create(ctx context.Context, req *resource.CreateRequest) (*reso }, err } - client := &http.Client{} - arguments := "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 } - request, err := http.NewRequest("POST", config.URL+"/api2/json/nodes/"+config.NODE+"/lxc", bytes.NewBuffer([]byte(arguments))) - request.Header.Set("Authorization", "PVEAPIToken="+username+"="+token) - - resp, err := client.Do(request) + _, err = authenticatedRequest(http.MethodPost, config.URL+"/api2/json/nodes/"+config.NODE+"/lxc", createAuthorizationString(username, token), bytes.NewBuffer([]byte(arguments))) if err != nil { return &resource.CreateResult{ @@ -145,21 +139,6 @@ func (p *Plugin) Create(ctx context.Context, req *resource.CreateRequest) (*reso }, err } - body, err := io.ReadAll(resp.Body) - if err != nil { - return &resource.CreateResult{ - ProgressResult: &resource.ProgressResult{ - Operation: resource.OperationCreate, - OperationStatus: resource.OperationStatusFailure, - ErrorCode: resource.OperationErrorCodeInternalFailure, - StatusMessage: err.Error(), - }, - }, err - } - - log.Println("Response StatusCode: ", resp.Status) - log.Println("Response Body: ", string(body)) - return &resource.CreateResult{ ProgressResult: &resource.ProgressResult{ Operation: resource.OperationCreate, @@ -182,19 +161,12 @@ func (p *Plugin) Read(ctx context.Context, req *resource.ReadRequest) (*resource return &resource.ReadResult{}, nil } - client := &http.Client{} - - request, err := http.NewRequest("GET", config.URL+"/api2/json/nodes/"+config.NODE+"/lxc/"+req.NativeID+"/config", nil) + data, err := authenticatedRequest(http.MethodGet, config.URL+"/api2/json/nodes/"+config.NODE+"/lxc/"+req.NativeID+"/config", createAuthorizationString(username, token), nil) if err != nil { return &resource.ReadResult{ ErrorCode: resource.OperationErrorCodeNetworkFailure, }, err } - request.Header.Set("Authorization", "PVEAPIToken="+username+"="+token) - - resp, err := client.Do(request) - - data, err := io.ReadAll(resp.Body) var props StatusLXCConfigResponse @@ -300,16 +272,11 @@ func (p *Plugin) Update(ctx context.Context, req *resource.UpdateRequest) (*reso }, err } - client := &http.Client{} - - url := config.URL + "/api2/json/nodes/" + config.NODE + "/lxc/" + desir.VMID + "/config" arguments := "vmid=" + desir.VMID + "&hostname=" + desir.Hostname + "&description=" + desir.Description + "&cores=" + strconv.Itoa(desir.Cores) + "&memory=" + strconv.Itoa(desir.Memory) argumentBuffer := bytes.NewBuffer([]byte(arguments)) - request, err := http.NewRequest("PUT", url, argumentBuffer) - request.Header.Set("Authorization", "PVEAPIToken="+username+"="+token) + _, err = authenticatedRequest(http.MethodPut, config.URL+"/api2/json/nodes/"+config.NODE+"/lxc/"+desir.VMID+"/config", createAuthorizationString(username, token), argumentBuffer) - resp, err := client.Do(request) if err != nil { return &resource.UpdateResult{ ProgressResult: &resource.ProgressResult{ @@ -320,8 +287,6 @@ func (p *Plugin) Update(ctx context.Context, req *resource.UpdateRequest) (*reso }, }, err } - - log.Println("Response StatusCode: ", resp.Status) } result, err := p.Read(ctx, &resource.ReadRequest{ @@ -368,14 +333,7 @@ func (p *Plugin) Delete(ctx context.Context, req *resource.DeleteRequest) (*reso }, err } - client := &http.Client{} - - url := config.URL + "/api2/json/nodes/" + config.NODE + "/lxc/" + req.NativeID - - request, err := http.NewRequest("DELETE", url, nil) - request.Header.Set("Authorization", "PVEAPIToken="+username+"="+token) - - resp, err := client.Do(request) + _, err = authenticatedRequest(http.MethodDelete, config.URL+"/api2/json/nodes/"+config.NODE+"/lxc/"+req.NativeID, createAuthorizationString(username, token), nil) if err != nil { return &resource.DeleteResult{ @@ -388,21 +346,6 @@ func (p *Plugin) Delete(ctx context.Context, req *resource.DeleteRequest) (*reso }, err } - body, err := io.ReadAll(resp.Body) - if err != nil { - return &resource.DeleteResult{ - ProgressResult: &resource.ProgressResult{ - Operation: resource.OperationCreate, - OperationStatus: resource.OperationStatusFailure, - ErrorCode: resource.OperationErrorCodeInternalFailure, - StatusMessage: err.Error(), - }, - }, err - } - - log.Println("Response StatusCode: ", resp.Status) - log.Println("Response Body: ", string(body)) - return &resource.DeleteResult{ ProgressResult: &resource.ProgressResult{ Operation: resource.OperationCreate, @@ -452,21 +395,14 @@ func (p *Plugin) List(ctx context.Context, req *resource.ListRequest) (*resource }, err } - client := &http.Client{} - var props StatusGeneralResponse - request, err := http.NewRequest("GET", config.URL+"/api2/json/nodes/"+config.NODE+"/lxc", nil) + data, err := authenticatedRequest(http.MethodGet, config.URL+"/api2/json/nodes/"+config.NODE+"/lxc", createAuthorizationString(username, token), nil) if err != nil { return &resource.ListResult{ NativeIDs: []string{}, }, err } - request.Header.Set("Authorization", "PVEAPIToken="+username+"="+token) - - resp, err := client.Do(request) - - data, err := io.ReadAll(resp.Body) json.Unmarshal(data, &props) diff --git a/proxmox_test.go b/proxmox_test.go index 632a720..11ee157 100644 --- a/proxmox_test.go +++ b/proxmox_test.go @@ -3,7 +3,6 @@ package main import ( "context" "encoding/json" - "io" "log" "net/http" "strconv" @@ -55,20 +54,9 @@ func TestCreate(t *testing.T) { require.NotNil(t, result.ProgressResult, "Create should return ProgressResult") require.Eventually(t, func() bool { - client := &http.Client{} - var props StatusGeneralResponse - request, err := http.NewRequest("GET", config.URL+"/api2/json/nodes/"+config.NODE+"/lxc", nil) - if err != nil { - t.Logf("Something unexpected happened") - return false - } - request.Header.Set("Authorization", "PVEAPIToken="+username+"="+token) - - resp, err := client.Do(request) - - data, err := io.ReadAll(resp.Body) + data, _ := authenticatedRequest(http.MethodGet, config.URL+"/api2/json/nodes/"+config.NODE+"/lxc", createAuthorizationString(username, token), nil) json.Unmarshal(data, &props) @@ -206,20 +194,9 @@ func TestDelete(t *testing.T) { require.NotNil(t, result.ProgressResult, "Create should return ProgressResult") require.Eventually(t, func() bool { - client := &http.Client{} - var props StatusGeneralResponse - request, err := http.NewRequest("GET", config.URL+"/api2/json/nodes/"+config.NODE+"/lxc", nil) - if err != nil { - t.Logf("Something unexpected happened") - return false - } - request.Header.Set("Authorization", "PVEAPIToken="+username+"="+token) - - resp, err := client.Do(request) - - data, err := io.ReadAll(resp.Body) + data, _ := authenticatedRequest(http.MethodGet, config.URL+"/api2/json/nodes/"+config.NODE+"/lxc", createAuthorizationString(username, token), nil) json.Unmarshal(data, &props)