zabbix-tools/go-zabbix

for v6.4.0

Ephemeraler opened this issue · 6 comments

user.log paramter had been changed, from user to username.

mr-ns commented

I think you mean user.login rather than user.log.

Technically this changed occurred in Zabbix 5.4; user was still allowed though, but deprecated. Then in 6.4 the deprecated user was removed.

I have a patch, by the way, that takes care of this in a backward-compatible way. (I am maintaining my own clone of this repo now because it seems to take a while for issues to get fixed.) Happy to pass along the patch if you like.

ksc98 commented

A one-liner patch, here's what we're using:

---
 session.go | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/session.go b/session.go
index 6ebe0d5..3b9c32e 100644
--- a/session.go
+++ b/session.go
@@ -52,7 +52,7 @@ func (c *Session) login(username, password string) error {

        // login to API
        params := map[string]string{
-               "user":     username,
+               "username": username,
                "password": password,
        }

--
mr-ns commented

I have to support both Zabbix < 5.4 and Zabbix >= 6.4 in my environment, so my patch is backward compatible:

diff --git a/go.mod b/go.mod
index db6a935..b74b8dc 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,5 @@
 module github.com/cavaliercoder/go-zabbix
 
 go 1.16
+
+require github.com/hashicorp/go-version v1.6.0
diff --git a/go.sum b/go.sum
index e69de29..805e323 100644
--- a/go.sum
+++ b/go.sum
@@ -0,0 +1,2 @@
+github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
+github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
diff --git a/session.go b/session.go
index d1b8e46..c13ded8 100644
--- a/session.go
+++ b/session.go
@@ -7,6 +7,8 @@ import (
 	"fmt"
 	"io/ioutil"
 	"net/http"
+
+	"github.com/hashicorp/go-version"
 )
 
 // ErrNotFound describes an empty result set for an API call.
@@ -51,9 +53,18 @@ func (c *Session) login(username, password string) error {
 		return fmt.Errorf("Failed to retrieve Zabbix API version: %v", err)
 	}
 
+	// "user" parameter changed to "username" in Zabbix 5.4. Use the
+	// new parameter unless we can unequivocally determine we are
+	// pre-5.4.
+	userParam := "username"
+	vZabbix, err := version.NewVersion(c.APIVersion)
+	if err == nil && vZabbix.LessThan(version.Must(version.NewVersion("5.4"))) {
+		userParam = "user"
+	}
+
 	// login to API
 	params := map[string]string{
-		"user":     username,
+		userParam:  username,
 		"password": password,
 	}
 
ksc98 commented

Looks good, I like it!

found the same,opened PR #55

I've fixed this in my fork of this library: https://github.com/fabiang/go-zabbix