initial fix

This commit is contained in:
yesoreyeram
2025-01-03 09:18:37 +00:00
parent dbcc008489
commit 374c83d17d
5 changed files with 143 additions and 30 deletions

View File

@@ -494,13 +494,7 @@ func (ds *Zabbix) GetAllGroups(ctx context.Context) ([]Group, error) {
params := ZabbixAPIParams{
"output": []string{"name", "groupid"},
"sortfield": "name",
}
// Zabbix v7.0 and later removed `real_hosts` parameter and replaced it with `with_hosts`
if ds.version < 70 {
params["real_hosts"] = true
} else {
params["with_hosts"] = true
"with_hosts": true,
}
result, err := ds.Request(ctx, &ZabbixAPIRequest{Method: "hostgroup.get", Params: params})

View File

@@ -0,0 +1,84 @@
package zabbixapi
import (
"context"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
)
func normalizeParams(ctx context.Context, method string, params ZabbixAPIParams, version int) ZabbixAPIParams {
logger := log.New().FromContext(ctx)
logger.Debug("performing query params migration", "method", method, "version", version)
switch method {
case "trigger.get":
newKey := "selectHostGroups"
deprecatedKey := "selectGroups"
deprecatedKeyValue, deprecatedKeyExists := params[deprecatedKey]
newKeyValue, newKeyExists := params[newKey]
if version < 70 && newKeyExists {
if deprecatedKeyExists {
delete(params, newKey)
}
if !deprecatedKeyExists {
params[deprecatedKey] = newKeyValue
delete(params, newKey)
}
}
if version >= 70 && deprecatedKeyExists {
if newKeyExists {
delete(params, deprecatedKey)
}
if !newKeyExists {
params[newKey] = deprecatedKeyValue
delete(params, deprecatedKey)
}
}
case "event.get":
newKey := "selectAcknowledges"
deprecatedKey := "select_acknowledges"
deprecatedKeyValue, deprecatedKeyExists := params[deprecatedKey]
newKeyValue, newKeyExists := params[newKey]
if version < 70 && newKeyExists {
if deprecatedKeyExists {
delete(params, newKey)
}
if !deprecatedKeyExists {
params[deprecatedKey] = newKeyValue
delete(params, newKey)
}
}
if version >= 70 && deprecatedKeyExists {
if newKeyExists {
delete(params, deprecatedKey)
}
if !newKeyExists {
params[newKey] = deprecatedKeyValue
delete(params, deprecatedKey)
}
}
case "hostgroup.get":
newKey := "with_hosts"
deprecatedKey := "real_hosts"
deprecatedKeyValue, deprecatedKeyExists := params[deprecatedKey]
newKeyValue, newKeyExists := params[newKey]
if version < 70 && newKeyExists {
if deprecatedKeyExists {
delete(params, newKey)
}
if !deprecatedKeyExists {
params[deprecatedKey] = newKeyValue
delete(params, newKey)
}
}
if version >= 70 && deprecatedKeyExists {
if newKeyExists {
delete(params, deprecatedKey)
}
if !newKeyExists {
params[newKey] = deprecatedKeyValue
delete(params, deprecatedKey)
}
}
}
return params
}

View File

@@ -92,7 +92,7 @@ func (api *ZabbixAPI) request(ctx context.Context, method string, params ZabbixA
"jsonrpc": "2.0",
"id": 2,
"method": method,
"params": params,
"params": normalizeParams(ctx, method, params, version),
}
// Zabbix v7.2 and later deprecated `auth` parameter and replaced it with using Auth header
@@ -115,7 +115,7 @@ func (api *ZabbixAPI) request(ctx context.Context, method string, params ZabbixA
if auth != "" {
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", auth))
}
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "Grafana/grafana-zabbix")