Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add query parameters support for GET method #177

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions proxmox.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -146,6 +147,42 @@ func (c *Client) Get(ctx context.Context, p string, v interface{}) error {
return c.Req(ctx, http.MethodGet, p, nil, v)
}

// GetWithParams is a helper function to append query parameters to the URL
func (c *Client) GetWithParams(ctx context.Context, p string, d interface{}, v interface{}) error {
// Parse data and append to URL
if d != nil {
queryString, err := dataParserForURL(d)
if err != nil {
return err
}
p = p + "?" + queryString
}
return c.Req(ctx, http.MethodGet, p, nil, v)
}

// dataParserForUrl parses the data and appends it to the URL as a query string
func dataParserForURL(d interface{}) (string, error) {
jsonBytes, err := json.Marshal(d)
if err != nil {
return "", err
}

var m map[string]interface{}
err = json.Unmarshal(jsonBytes, &m)
if err != nil {
return "", err
}

values := url.Values{}
for key, value := range m {
strValue := fmt.Sprintf("%v", value)
values.Set(key, strValue)
}

return values.Encode(), nil

}

func (c *Client) Post(ctx context.Context, p string, d interface{}, v interface{}) error {
var data []byte
if d != nil {
Expand Down
10 changes: 10 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1611,3 +1611,13 @@ type VzdumpConfig struct {
IPConfig8 string `json:"ipconfig8,omitempty"`
IPConfig9 string `json:"ipconfig9,omitempty"`
}

type PendingConfiguration []PendingConfigItem

type PendingConfigItem struct {
Key string `json:"key,omitempty"`
Delete *int `json:"delete,omitempty"`
// Proxmox API doc says "Pending" & "Value" fields return string but in reality it could be anything
Pending interface{} `json:"pending,omitempty"`
Value interface{} `json:"value,omitempty"`
}
5 changes: 5 additions & 0 deletions virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,8 @@ func (v *VirtualMachine) UnmountCloudInitISO(ctx context.Context, device string)
}
return nil
}

func (v *VirtualMachine) Pending(ctx context.Context) (pending *PendingConfiguration, err error) {
err = v.client.Get(ctx, fmt.Sprintf("/nodes/%s/qemu/%d/pending", v.Node, v.VMID), &pending)
return
}