Skip to content

Commit

Permalink
Added HostUpdate method
Browse files Browse the repository at this point in the history
  • Loading branch information
borisershov committed Jun 15, 2019
1 parent b4d40d3 commit 9a9739d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
18 changes: 18 additions & 0 deletions host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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) {

Expand Down
35 changes: 34 additions & 1 deletion host_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
},
Expand Down

0 comments on commit 9a9739d

Please sign in to comment.