Skip to content

Commit

Permalink
Merge pull request #11 from hsorellana/fix/services-templates
Browse files Browse the repository at this point in the history
Add template field to services handling
  • Loading branch information
lazzurs authored Jul 6, 2020
2 parents 5a8fb7f + 0713ad8 commit e38c6bb
Show file tree
Hide file tree
Showing 16 changed files with 527 additions and 592 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/hsorellana/go-icinga2-api

go 1.13
12 changes: 3 additions & 9 deletions iapi/checkcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ func (server *Server) GetCheckcommand(name string) ([]CheckcommandStruct, error)
}

// CreateCheckcommand ...
func (server *Server) CreateCheckcommand(name, command string, command_arguments map[string]string) ([]CheckcommandStruct, error) {

func (server *Server) CreateCheckcommand(name, command string, commandArguments map[string]string) ([]CheckcommandStruct, error) {
var newAttrs CheckcommandAttrs
newAttrs.Command = []string{command}
newAttrs.Arguments = command_arguments
newAttrs.Arguments = commandArguments

var newCheckcommand CheckcommandStruct
newCheckcommand.Name = name
Expand All @@ -49,8 +48,6 @@ func (server *Server) CreateCheckcommand(name, command string, command_arguments
return nil, marshalErr
}

//fmt.Printf("<payload> %s\n", payloadJSON)

// Make the API request to create the hosts.
results, err := server.NewAPIRequest("PUT", "/objects/checkcommands/"+name, []byte(payloadJSON))
if err != nil {
Expand All @@ -63,21 +60,18 @@ func (server *Server) CreateCheckcommand(name, command string, command_arguments
}

return nil, fmt.Errorf("%s", results.ErrorString)

}

// DeleteCheckcommand ...
func (server *Server) DeleteCheckcommand(name string) error {

results, err := server.NewAPIRequest("DELETE", "/objects/checkcommands/"+name+"?cascade=1", nil)
if err != nil {
return err
}

if results.Code == 200 {
return nil
} else {
return fmt.Errorf("%s", results.ErrorString)
}

return fmt.Errorf("%s", results.ErrorString)
}
14 changes: 6 additions & 8 deletions iapi/checkcommands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,20 @@ func TestDeleteCheckcommand(t *testing.T) {
}

func TestCreateCheckcommandArgs(t *testing.T) {

name := "check-command-docker-args"
command := "/dev/null"
command_args := make(map[string]string)
command_args["-I"] = "Iarg"
command_args["-X"] = "Xarg"

_, err := Icinga2_Server.CreateCheckcommand(name, command, command_args)
commandArgs := make(map[string]string)
commandArgs["-I"] = "Iarg"
commandArgs["-X"] = "Xarg"

_, err := Icinga2_Server.CreateCheckcommand(name, command, commandArgs)
if err != nil {
t.Error(err)
}

// Delete check command after creating it.
deleteErr := Icinga2_Server.DeleteCheckcommand(name)
if deleteErr != nil {
err = Icinga2_Server.DeleteCheckcommand(name)
if err != nil {
t.Error(err)
}

Expand Down
15 changes: 6 additions & 9 deletions iapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ type Server struct {
httpClient *http.Client
}

// func New ...
func New(username, password, url string, allowUnverifiedSSL bool) (*Server, error) {
return &Server{username, password, url, allowUnverifiedSSL, nil}, nil
}

// func Config ...
func (server *Server) Config(username, password, url string, allowUnverifiedSSL bool) (*Server, error) {

// TODO : Add code to verify parameters
Expand Down Expand Up @@ -63,14 +61,11 @@ func (server *Server) Connect() error {
defer response.Body.Close()

return nil

}

// NewAPIRequest ...
func (server *Server) NewAPIRequest(method, APICall string, jsonString []byte) (*APIResult, error) {

var results APIResult

fullURL := server.BaseURL + APICall

t := &http.Transport{
Expand All @@ -95,14 +90,16 @@ func (server *Server) NewAPIRequest(method, APICall string, jsonString []byte) (

response, doErr := server.httpClient.Do(request)
if doErr != nil {
results.Code = 0
results.Status = "Error : Request to server failed : " + doErr.Error()
results.ErrorString = doErr.Error()
results := APIResult{
Code: 0,
Status: "Error : Request to server failed : " + doErr.Error(),
ErrorString: doErr.Error(),
}
return &results, doErr
}

defer response.Body.Close()

var results APIResult
if decodeErr := json.NewDecoder(response.Body).Decode(&results); decodeErr != nil {
return nil, decodeErr
}
Expand Down
7 changes: 5 additions & 2 deletions iapi/client_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package iapi

import "testing"
import "os"
import (
"os"
"testing"
)

var ICINGA2_API_PASSWORD = os.Getenv("ICINGA2_API_PASSWORD")

var Icinga2_Server = Server{"root", ICINGA2_API_PASSWORD, "https://127.0.0.1:5665/v1", true, nil}

//var Icinga2_Server = Server{"icinga-test", "icinga", "https://127.0.0.1:5665/v1", true, nil}

func TestConnect(t *testing.T) {
Expand Down
7 changes: 2 additions & 5 deletions iapi/hostgroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

// GetHostgroup ...
//
func (server *Server) GetHostgroup(name string) ([]HostgroupStruct, error) {

var hostgroups []HostgroupStruct
Expand All @@ -34,7 +33,7 @@ func (server *Server) GetHostgroup(name string) ([]HostgroupStruct, error) {
}

if len(hostgroups) != 1 {
return nil, errors.New("Found more than one matching hostgroup.")
return nil, errors.New("found more than one matching hostgroup")
}

return hostgroups, err
Expand Down Expand Up @@ -73,16 +72,14 @@ func (server *Server) CreateHostgroup(name, displayName string) ([]HostgroupStru

// DeleteHostgroup ...
func (server *Server) DeleteHostgroup(name string) error {

results, err := server.NewAPIRequest("DELETE", "/objects/hostgroups/"+name, nil)
if err != nil {
return err
}

if results.Code == 200 {
return nil
} else {
return fmt.Errorf("%s", results.ErrorString)
}

return fmt.Errorf("%s", results.ErrorString)
}
123 changes: 57 additions & 66 deletions iapi/hostgroups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,61 @@ import (
"testing"
)

func TestGetValidHostgroup(t *testing.T) {

name := "linux-servers"

_, err := Icinga2_Server.GetHostgroup(name)

if err != nil {
t.Error(err)
}

}

func TestGetInvalidHostgroup(t *testing.T) {

name := "irix-servers"

_, err := Icinga2_Server.GetHostgroup(name)
if err != nil {
t.Error(err)
}

}

func TestCreateHostgroup(t *testing.T) {

name := "docker-servers"
displayName := "Docker Host Servers"
_, err := Icinga2_Server.CreateHostgroup(name, displayName)

if err != nil {
t.Error(err)
}

}

// func TestDeleteHostgroup
// Delete Hostgroup created via API. Should succeed
func TestDeleteHostgroup(t *testing.T) {

name := "docker-servers"

err := Icinga2_Server.DeleteHostgroup(name)
if err != nil {
t.Error(err)
}
}

// func TestDeleteHostgroupNonAPI
func TestDeleteHostgroupNonAPI(t *testing.T) {

name := "linux-servers"

err := Icinga2_Server.DeleteHostgroup(name)
if err.Error() != "500 One or more objects could not be deleted" {
t.Error(err)
}
}

func TestDeleteHostgroupDNE(t *testing.T) {

name := "docker-servers"
err := Icinga2_Server.DeleteHostgroup(name)

if err.Error() != "No objects found." {
t.Error(err)
}
func TestHostgroups(t *testing.T) {
icingaServer := Server{"root", ICINGA2_API_PASSWORD, "https://127.0.0.1:5665/v1", true, nil}
t.Run("Create", func(t *testing.T) {
t.Run("Hostgroup", func(t *testing.T) {
name := "docker-servers"
displayName := "Docker Host Servers"
_, err := icingaServer.CreateHostgroup(name, displayName)
if err != nil {
t.Error(err)
}
})
})

t.Run("Read", func(t *testing.T) {
t.Run("ValidHostgroup", func(t *testing.T) {
name := "linux-servers"
_, err := icingaServer.GetHostgroup(name)
if err != nil {
t.Error(err)
}
})

t.Run("InvalidHostgroup", func(t *testing.T) {
name := "irix-servers"
_, err := icingaServer.GetHostgroup(name)
if err != nil {
t.Error(err)
}
})
})

t.Run("Delete", func(t *testing.T) {
// Delete Hostgroup created via API. Should succeed
t.Run("Hostgroup", func(t *testing.T) {
name := "docker-servers"
err := icingaServer.DeleteHostgroup(name)
if err != nil {
t.Error(err)
}
})

t.Run("HostgroupNonAPI", func(t *testing.T) {
name := "linux-servers"
err := icingaServer.DeleteHostgroup(name)
if err == nil {
t.Error(err)
}
})

t.Run("HostgroupDNE", func(t *testing.T) {
name := "docker-servers"
err := icingaServer.DeleteHostgroup(name)
if err.Error() != "No objects found." {
t.Error(err)
}
})
})
}
12 changes: 5 additions & 7 deletions iapi/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func (server *Server) CreateHost(hostname, address, checkCommand string, variabl
newAttrs.Vars = variables
newAttrs.Templates = templates

if groups == nil {
groups = []string{}
}
newAttrs.Groups = groups
if groups == nil {
groups = []string{}
}
newAttrs.Groups = groups

var newHost HostStruct
newHost.Name = hostname
Expand Down Expand Up @@ -76,16 +76,14 @@ func (server *Server) CreateHost(hostname, address, checkCommand string, variabl

// DeleteHost ...
func (server *Server) DeleteHost(hostname string) error {

results, err := server.NewAPIRequest("DELETE", "/objects/hosts/"+hostname+"?cascade=1", nil)
if err != nil {
return err
}

if results.Code == 200 {
return nil
} else {
return fmt.Errorf("%s", results.ErrorString)
}

return fmt.Errorf("%s", results.ErrorString)
}
32 changes: 16 additions & 16 deletions iapi/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestCreateSimpleHost(t *testing.T) {
IPAddress := "127.0.0.2"
CheckCommand := "hostalive"

_, err := Icinga2_Server.CreateHost(hostname, IPAddress, CheckCommand, nil, nil, nil )
_, err := Icinga2_Server.CreateHost(hostname, IPAddress, CheckCommand, nil, nil, nil)

if err != nil {
t.Error(err)
Expand Down Expand Up @@ -78,21 +78,21 @@ func TestCreateHostWithTemplates(t *testing.T) {
}

func TestCreateHostWithGroup(t *testing.T) {
hostname := "go-icinga2-api-2"
IPAddress := "127.0.0.3"
CheckCommand := "hostalive"
Group := []string{"linux-servers"}

_, err := Icinga2_Server.CreateHost(hostname, IPAddress, CheckCommand, nil, nil, Group)
if err != nil {
t.Error(err)
}

// Delete host after creating it.
deleteErr := Icinga2_Server.DeleteHost(hostname)
if deleteErr != nil {
t.Error(err)
}
hostname := "go-icinga2-api-2"
IPAddress := "127.0.0.3"
CheckCommand := "hostalive"
Group := []string{"linux-servers"}

_, err := Icinga2_Server.CreateHost(hostname, IPAddress, CheckCommand, nil, nil, Group)
if err != nil {
t.Error(err)
}

// Delete host after creating it.
deleteErr := Icinga2_Server.DeleteHost(hostname)
if deleteErr != nil {
t.Error(err)
}
}
func TestDeleteHost(t *testing.T) {

Expand Down
Loading

0 comments on commit e38c6bb

Please sign in to comment.