Skip to content

Commit

Permalink
add HTTP_HEADERS function
Browse files Browse the repository at this point in the history
  • Loading branch information
BenediktBertsch committed Oct 3, 2022
1 parent 454a406 commit 121d96b
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export PM_API_URL="https://xxxx.com:8006/api2/json"
export PM_USER=user@pam
export PM_PASS=password
export PM_OTP=otpcode (only if required)
export HTTP_HEADERS=Key,Value,Key1,Value1 (only if required)

./proxmox-api-go installQemu proxmox-node-name < qemu1.json

Expand Down
9 changes: 6 additions & 3 deletions cli/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func Execute() (err error) {
}

func NewClient() (c *proxmox.Client) {
c, err := Client("", "", "", "")
c, err := Client("", "", "", "", "")
LogFatalError(err)
return
}

func Client(apiUlr, userID, password, otp string) (c *proxmox.Client, err error) {
func Client(apiUlr, userID, password, otp string, http_headers string) (c *proxmox.Client, err error) {
insecure, _ := RootCmd.Flags().GetBool("insecure")
timeout, _ := RootCmd.Flags().GetInt("timeout")
proxyUrl, _ := RootCmd.Flags().GetString("proxyurl")
Expand All @@ -59,7 +59,10 @@ func Client(apiUlr, userID, password, otp string) (c *proxmox.Client, err error)
if otp == "" {
otp = os.Getenv("PM_OTP")
}
c, err = proxmox.NewClient(apiUlr, nil, tlsconf, proxyUrl, timeout)
if http_headers == "" {
otp = os.Getenv("HTTP_HEADERS")
}
c, err = proxmox.NewClient(apiUlr, nil, http_headers, tlsconf, proxyUrl, timeout)
LogFatalError(err)
if userRequiresAPIToken(userID) {
c.SetAPIToken(userID, password)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func main() {
if !*insecure {
tlsconf = nil
}
c, err := proxmox.NewClient(os.Getenv("PM_API_URL"), nil, tlsconf, *proxyURL, *taskTimeout)
c, err := proxmox.NewClient(os.Getenv("PM_API_URL"), nil, os.Getenv("HTTP_HEADERS"), tlsconf, *proxyURL, *taskTimeout)
failError(err)
if userRequiresAPIToken(os.Getenv("PM_USER")) {
c.SetAPIToken(os.Getenv("PM_USER"), os.Getenv("PM_PASS"))
Expand Down
13 changes: 9 additions & 4 deletions proxmox/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,18 @@ func NewVmRef(vmId int) (vmr *VmRef) {
return
}

func NewClient(apiUrl string, hclient *http.Client, tls *tls.Config, proxyString string, taskTimeout int) (client *Client, err error) {
func NewClient(apiUrl string, hclient *http.Client, http_headers string, tls *tls.Config, proxyString string, taskTimeout int) (client *Client, err error) {
var sess *Session
sess, err = NewSession(apiUrl, hclient, proxyString, tls)
if err == nil {
sess, err_s := NewSession(apiUrl, hclient, proxyString, tls)
sess, err = createHeaderList(http_headers, sess)
if err != nil {
return nil, err
}
if err_s == nil {
client = &Client{session: sess, ApiUrl: apiUrl, TaskTimeout: taskTimeout}
}
return client, err

return client, err_s
}

// SetAPIToken specifies a pair of user identifier and token UUID to use
Expand Down
2 changes: 1 addition & 1 deletion proxmox/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestClient_Login(t *testing.T) {
client, err := NewClient(os.Getenv("PM_API_URL"), nil, &tls.Config{InsecureSkipVerify: true}, "", 300)
client, err := NewClient(os.Getenv("PM_API_URL"), nil, os.Getenv("HTTP_HEADERS"), &tls.Config{InsecureSkipVerify: true}, "", 300)
assert.Nil(t, err)

err = client.Login(os.Getenv("PM_USER"), os.Getenv("PM_PASS"), "")
Expand Down
13 changes: 13 additions & 0 deletions proxmox/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,16 @@ func failError(err error) {
log.Fatal(err)
}
}

// Create list of http.Header out of string, separator is ","
func createHeaderList(header_string string, sess *Session) (*Session, error) {
header_string_split := strings.Split(header_string, ",")
err := ValidateArrayEven(header_string_split, "Header key(s) and value(s) not even. Check your HTTP_HEADERS.")
if err != nil {
return nil, err
}
for i := 0; i < len(header_string_split); i += 2 {
sess.Headers.Add(header_string_split[i], header_string_split[i+1])
}
return sess, nil
}
7 changes: 7 additions & 0 deletions proxmox/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ func ValidateArrayNotEmpty(array interface{}, text string) error {
return ErrorKeyEmpty(text)
}

func ValidateArrayEven(array interface{}, text string) error {
if len(array.([]string))%2 == 0 {
return nil
}
return ErrorKeyEmpty(text)
}

func ErrorKeyEmpty(text string) error {
return fmt.Errorf("error the value of key (%s) may not be empty", text)
}
Expand Down
13 changes: 7 additions & 6 deletions test/cli/shared_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ func (test *Test) StandardTest(t *testing.T) {
}

type LoginTest struct {
APIurl string
UserID string
Password string
OTP string
ReqErr bool //if an error is expected as output
APIurl string
UserID string
Password string
OTP string
HttpHeaders string
ReqErr bool //if an error is expected as output
}

func (test *LoginTest) Login(t *testing.T) {
_, err := cli.Client(test.APIurl, test.UserID, test.Password, test.OTP)
_, err := cli.Client(test.APIurl, test.UserID, test.Password, test.OTP, test.HttpHeaders)
if test.ReqErr {
require.Error(t, err)
} else {
Expand Down

0 comments on commit 121d96b

Please sign in to comment.