From 9a9739de95fd237f13f03ba58aca409248452d7d Mon Sep 17 00:00:00 2001 From: Boris Ershov Date: Sun, 16 Jun 2019 04:21:17 +0700 Subject: [PATCH] Added `HostUpdate` method --- host.go | 18 ++++++++++++++++++ host_test.go | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/host.go b/host.go index 9979697..221f785 100644 --- a/host.go +++ b/host.go @@ -178,6 +178,11 @@ type hostCreateResult struct { HostIDs []int `json:"hostids"` } +// Structure to store updation result +type hostUpdateResult struct { + HostIDs []int `json:"hostids"` +} + // Structure to store deletion result type hostDeleteResult struct { HostIDs []int `json:"hostids"` @@ -209,6 +214,19 @@ func (z *Context) HostCreate(params []HostObject) ([]int, int, error) { return result.HostIDs, status, nil } +// HostUpdate updates hosts +func (z *Context) HostUpdate(params []HostObject) ([]int, int, error) { + + var result hostUpdateResult + + status, err := z.request("host.update", params, &result) + if err != nil { + return nil, status, err + } + + return result.HostIDs, status, nil +} + // HostDelete deletes hosts func (z *Context) HostDelete(hostIDs []int) ([]int, int, error) { diff --git a/host_test.go b/host_test.go index bea2bdb..d5b20f0 100644 --- a/host_test.go +++ b/host_test.go @@ -32,6 +32,9 @@ func TestHostCRUD(t *testing.T) { hCreatedIDs := testHostCreate(t, z, hgCreatedIDs, tCreatedIDs) defer testHostDelete(t, z, hCreatedIDs) + // Update + testHostUpdate(t, z, hCreatedIDs) + // Get testHostGet(t, z, hCreatedIDs, tCreatedIDs, hgCreatedIDs) } @@ -91,6 +94,36 @@ func testHostCreate(t *testing.T, z Context, hgCreatedIDs, tCreatedIDs []int) [] return hCreatedIDs } +func testHostUpdate(t *testing.T, z Context, hCreatedIDs []int) []int { + + var hObjects []HostObject + + // Preparing host objects array to update + for _, i := range hCreatedIDs { + hObjects = append(hObjects, HostObject{ + HostID: i, + Name: testHostName + "_upd", + }) + } + + hUpdatedIDs, _, err := z.HostUpdate(hObjects) + if err != nil { + t.Fatal("Host update error:", err) + } + + if len(hUpdatedIDs) == 0 { + t.Fatal("Host update error: empty IDs array") + } + + if reflect.DeepEqual(hUpdatedIDs, hCreatedIDs) == false { + t.Fatal("Host update error: IDs arrays for created and updated hosts are mismatch") + } + + t.Logf("Host update: success") + + return hUpdatedIDs +} + func testHostDelete(t *testing.T, z Context, hCreatedIDs []int) []int { hDeletedIDs, _, err := z.HostDelete(hCreatedIDs) @@ -121,7 +154,7 @@ func testHostGet(t *testing.T, z Context, hCreatedIDs, tCreatedIDs, hgCreatedIDs GroupIDs: hgCreatedIDs, GetParameters: GetParameters{ Filter: map[string]interface{}{ - "name": testHostName, + "name": testHostName + "_upd", }, Output: SelectExtendedOutput, },