Skip to content

Commit b131d11

Browse files
committed
Reverted formdata change and fixed router file paths
1 parent 15a79b9 commit b131d11

3 files changed

Lines changed: 9 additions & 65 deletions

File tree

internal/cloning/networking.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cloning
22

33
import (
44
"fmt"
5-
"log"
65
"math"
76
"regexp"
87
"time"
@@ -33,13 +32,10 @@ func (cs *CloningService) configurePodRouter(podNumber int, node string, vmid in
3332
break // Agent is responding
3433
}
3534

36-
log.Printf("Agent ping failed for VMID %d: %s", vmid, err)
3735
time.Sleep(backoff)
3836
backoff = time.Duration(math.Min(float64(backoff*2), float64(maxBackoff)))
3937
}
4038

41-
log.Printf("QEMU agent is responding for VMID %d, proceeding with configuration", vmid)
42-
4339
// Configure router WAN IP to have correct third octet using qemu agent API call
4440
reqBody := map[string]any{
4541
"command": []string{
@@ -48,22 +44,17 @@ func (cs *CloningService) configurePodRouter(podNumber int, node string, vmid in
4844
},
4945
}
5046

51-
log.Printf("Request Body: %+v", reqBody)
52-
5347
execReq := tools.ProxmoxAPIRequest{
5448
Method: "POST",
5549
Endpoint: fmt.Sprintf("/nodes/%s/qemu/%d/agent/exec", node, vmid),
5650
RequestBody: reqBody,
57-
UseFormData: true, // Use form data for QEMU agent requests
5851
}
5952

60-
response, err := cs.ProxmoxService.GetRequestHelper().MakeRequest(execReq)
53+
_, err := cs.ProxmoxService.GetRequestHelper().MakeRequest(execReq)
6154
if err != nil {
6255
return fmt.Errorf("failed to make IP change request: %v", err)
6356
}
6457

65-
log.Printf("WAN IP configuration response: %s", string(response))
66-
6758
// Send agent exec request to change VIP subnet
6859
vipReqBody := map[string]any{
6960
"command": []string{
@@ -76,16 +67,13 @@ func (cs *CloningService) configurePodRouter(podNumber int, node string, vmid in
7667
Method: "POST",
7768
Endpoint: fmt.Sprintf("/nodes/%s/qemu/%d/agent/exec", node, vmid),
7869
RequestBody: vipReqBody,
79-
UseFormData: true, // Use form data for QEMU agent requests
8070
}
8171

82-
vipResponse, err := cs.ProxmoxService.GetRequestHelper().MakeRequest(vipExecReq)
72+
_, err = cs.ProxmoxService.GetRequestHelper().MakeRequest(vipExecReq)
8373
if err != nil {
8474
return fmt.Errorf("failed to make VIP change request: %v", err)
8575
}
8676

87-
log.Printf("VIP configuration response: %s", string(vipResponse))
88-
log.Printf("Successfully configured router for pod %d on node %s, VMID %d", podNumber, node, vmid)
8977
return nil
9078
}
9179

internal/cloning/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ type Config struct {
1919
CloneTimeout time.Duration `envconfig:"CLONE_TIMEOUT" default:"3m"`
2020
RouterWaitTimeout time.Duration `envconfig:"ROUTER_WAIT_TIMEOUT" default:"120s"`
2121
SDNApplyTimeout time.Duration `envconfig:"SDN_APPLY_TIMEOUT" default:"30s"`
22-
WANScriptPath string `envconfig:"WAN_SCRIPT_PATH" default:"/home/change-wan-ip.sh"`
23-
VIPScriptPath string `envconfig:"VIP_SCRIPT_PATH" default:"/home/change-vip-subnet.sh"`
22+
WANScriptPath string `envconfig:"WAN_SCRIPT_PATH" default:"/home/update-wan-ip.sh"`
23+
VIPScriptPath string `envconfig:"VIP_SCRIPT_PATH" default:"/home/update-wan-vip.sh"`
2424
WANIPBase string `envconfig:"WAN_IP_BASE" default:"172.16."`
2525
UseFullClones bool `envconfig:"USE_FULL_CLONES" default:"false"`
2626
}

internal/tools/requests.go

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"log"
98
"net/http"
10-
"net/url"
11-
"strings"
129
)
1310

1411
// ProxmoxAPIRequest represents a request to the Proxmox API
1512
type ProxmoxAPIRequest struct {
1613
Method string // GET, POST, PUT, DELETE
1714
Endpoint string // The API endpoint (e.g., "/nodes", "/nodes/node1/status")
1815
RequestBody any // Optional request body for POST/PUT requests
19-
UseFormData bool // Whether to send as form data instead of JSON
2016
}
2117

2218
// ProxmoxAPIResponse represents the generic Proxmox API response structure
@@ -43,59 +39,21 @@ func NewProxmoxRequestHelper(baseURL, apiToken string, httpClient *http.Client)
4339
// MakeRequest performs an HTTP request to the Proxmox API and returns the raw response data
4440
func (prh *ProxmoxRequestHelper) MakeRequest(req ProxmoxAPIRequest) (json.RawMessage, error) {
4541
var reqBody io.Reader
46-
var contentType string
4742

4843
// Prepare request body for POST/PUT requests
4944
if req.Method == "POST" || req.Method == "PUT" {
5045
var bodyData any
5146
if req.RequestBody != nil {
52-
log.Printf("Request Body: %+v", req.RequestBody)
5347
bodyData = req.RequestBody
5448
} else {
5549
bodyData = map[string]any{}
5650
}
5751

58-
if req.UseFormData {
59-
// Convert to form data
60-
formData := url.Values{}
61-
if bodyMap, ok := bodyData.(map[string]any); ok {
62-
for key, value := range bodyMap {
63-
switch v := value.(type) {
64-
case string:
65-
formData.Set(key, v)
66-
case []string:
67-
// For arrays, Proxmox expects multiple form fields with same name
68-
for _, item := range v {
69-
formData.Add(key, item)
70-
}
71-
case []any:
72-
// Handle generic interface slice (convert to strings)
73-
for _, item := range v {
74-
if str, ok := item.(string); ok {
75-
formData.Add(key, str)
76-
} else {
77-
formData.Add(key, fmt.Sprintf("%v", item))
78-
}
79-
}
80-
default:
81-
// Convert to JSON string for complex types
82-
jsonBytes, _ := json.Marshal(v)
83-
formData.Set(key, string(jsonBytes))
84-
}
85-
}
86-
}
87-
reqBody = strings.NewReader(formData.Encode())
88-
contentType = "application/x-www-form-urlencoded"
89-
log.Printf("Form data: %s", formData.Encode())
90-
} else {
91-
// Send as JSON
92-
jsonData, err := json.Marshal(bodyData)
93-
if err != nil {
94-
return nil, fmt.Errorf("failed to marshal request body: %w", err)
95-
}
96-
reqBody = bytes.NewBuffer(jsonData)
97-
contentType = "application/json"
52+
jsonData, err := json.Marshal(bodyData)
53+
if err != nil {
54+
return nil, fmt.Errorf("failed to marshal request body: %w", err)
9855
}
56+
reqBody = bytes.NewBuffer(jsonData)
9957
}
10058

10159
// Create the full URL
@@ -109,9 +67,7 @@ func (prh *ProxmoxRequestHelper) MakeRequest(req ProxmoxAPIRequest) (json.RawMes
10967

11068
// Set headers
11169
httpReq.Header.Add("Authorization", "PVEAPIToken="+prh.APIToken)
112-
if req.Method == "POST" || req.Method == "PUT" {
113-
httpReq.Header.Add("Content-Type", contentType)
114-
}
70+
httpReq.Header.Add("Content-Type", "application/json")
11571

11672
// Execute the request
11773
resp, err := prh.HTTPClient.Do(httpReq)

0 commit comments

Comments
 (0)