Skip to content

Commit

Permalink
Zabbix 5.4+ dropped support for Application API
Browse files Browse the repository at this point in the history
  • Loading branch information
pdecat committed Oct 3, 2022
1 parent 94b2550 commit ac2a714
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 3 deletions.
2 changes: 2 additions & 0 deletions application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func testDeleteApplication(app *zapi.Application, t *testing.T) {
}

func TestApplications(t *testing.T) {
skipTestIfVersionGreaterThanOrEqual(t, "5.4", "dropped support for Application API")

api := testGetAPI(t)

group := testCreateHostGroup(t)
Expand Down
14 changes: 14 additions & 0 deletions base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

zapi "github.com/claranet/go-zabbix-api"
"github.com/hashicorp/go-version"
)

var (
Expand Down Expand Up @@ -62,6 +63,19 @@ func testGetAPI(t *testing.T) *zapi.API {
return _api
}

func skipTestIfVersionGreaterThanOrEqual(t *testing.T, comparedVersion, msg string) {
api := testGetAPI(t)
serverVersion, err := api.Version()
if err != nil {
t.Fatal(err)
}
sVersion, _ := version.NewVersion(serverVersion)
cVersion, _ := version.NewVersion(comparedVersion)
if sVersion.GreaterThanOrEqual(cVersion) {
t.Skipf("Zabbix version %s is greater than or equal to %s which %s, skipping test.", serverVersion, comparedVersion, msg)
}
}

func TestBadCalls(t *testing.T) {
api := testGetAPI(t)
res, err := api.Call("", nil)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/claranet/go-zabbix-api

go 1.18

require github.com/hashicorp/go-version v1.6.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
46 changes: 44 additions & 2 deletions item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,22 @@ import (
zapi "github.com/claranet/go-zabbix-api"
)

func testCreateItem(app *zapi.Application, t *testing.T) *zapi.Item {
func testCreateItem(host *zapi.Host, t *testing.T) *zapi.Item {
items := zapi.Items{{
HostID: host.HostID,
Key: "key.lala.laa",
Name: "name for key",
Type: zapi.ZabbixTrapper,
Delay: "0",
}}
err := testGetAPI(t).ItemsCreate(items)
if err != nil {
t.Fatal(err)
}
return &items[0]
}

func testCreateItemWithApplication(app *zapi.Application, t *testing.T) *zapi.Item {
items := zapi.Items{{
HostID: app.HostID,
Key: "key.lala.laa",
Expand Down Expand Up @@ -38,6 +53,33 @@ func TestItems(t *testing.T) {
host := testCreateHost(group, t)
defer testDeleteHost(host, t)

item := testCreateItem(host, t)

_, err := api.ItemGetByID(item.ItemID)
if err != nil {
t.Fatal(err)
}

item.Name = "another name"
err = api.ItemsUpdate(zapi.Items{*item})
if err != nil {
t.Error(err)
}

testDeleteItem(item, t)
}

func TestItemsWithApplication(t *testing.T) {
skipTestIfVersionGreaterThanOrEqual(t, "5.4", "dropped support for Application API")

api := testGetAPI(t)

group := testCreateHostGroup(t)
defer testDeleteHostGroup(group, t)

host := testCreateHost(group, t)
defer testDeleteHost(host, t)

app := testCreateApplication(host, t)
defer testDeleteApplication(app, t)

Expand All @@ -49,7 +91,7 @@ func TestItems(t *testing.T) {
t.Fatal("Found items")
}

item := testCreateItem(app, t)
item := testCreateItemWithApplication(app, t)

_, err = api.ItemGetByID(item.ItemID)
if err != nil {
Expand Down
36 changes: 35 additions & 1 deletion trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,44 @@ func TestTrigger(t *testing.T) {
host := testCreateHost(group, t)
defer testDeleteHost(host, t)

item := testCreateItem(host, t)
defer testDeleteItem(item, t)

triggerParam := zapi.Params{"hostids": host.HostID}
res, err := api.TriggersGet(triggerParam)
if err != nil {
t.Fatal(err)
}
if len(res) != 0 {
t.Fatal("Found items")
}

trigger := testCreateTrigger(item, host, t)

trigger.Description = "new trigger name"
err = api.TriggersUpdate(zapi.Triggers{*trigger})
if err != nil {
t.Error(err)
}

testDeleteTrigger(trigger, t)
}

func TestTriggerWithApplication(t *testing.T) {
skipTestIfVersionGreaterThanOrEqual(t, "5.4", "dropped support for Application API")

api := testGetAPI(t)

group := testCreateHostGroup(t)
defer testDeleteHostGroup(group, t)

host := testCreateHost(group, t)
defer testDeleteHost(host, t)

app := testCreateApplication(host, t)
defer testDeleteApplication(app, t)

item := testCreateItem(app, t)
item := testCreateItemWithApplication(app, t)
defer testDeleteItem(item, t)

triggerParam := zapi.Params{"hostids": host.HostID}
Expand Down

0 comments on commit ac2a714

Please sign in to comment.