-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from micwiel/notifications
Notifications
- Loading branch information
Showing
5 changed files
with
512 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package iapi | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// GetNotification ... | ||
func (server *Server) GetNotification(name string) ([]NotificationStruct, error) { | ||
|
||
var notifications []NotificationStruct | ||
results, err := server.NewAPIRequest("GET", "/objects/notifications/"+name, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Contents of the results is an interface object. Need to convert it to json first. | ||
jsonStr, marshalErr := json.Marshal(results.Results) | ||
if marshalErr != nil { | ||
return nil, marshalErr | ||
} | ||
|
||
// then the JSON can be pushed into the appropriate struct. | ||
// Note : Results is a slice so much push into a slice. | ||
|
||
if unmarshalErr := json.Unmarshal(jsonStr, ¬ifications); unmarshalErr != nil { | ||
return nil, unmarshalErr | ||
} | ||
|
||
return notifications, err | ||
} | ||
|
||
// CreateNotification ... | ||
func (server *Server) CreateNotification(name, hostname, command, servicename string, interval int, users []string, vars map[string]string, templates []string) ([]NotificationStruct, error) { | ||
|
||
var newAttrs NotificationAttrs | ||
newAttrs.Command = command | ||
newAttrs.Users = users | ||
newAttrs.Servicename = servicename | ||
newAttrs.Interval = interval | ||
newAttrs.Vars = vars | ||
newAttrs.Templates = templates | ||
|
||
var newNotification NotificationStruct | ||
newNotification.Name = name | ||
newNotification.Type = "Notification" | ||
newNotification.Attrs = newAttrs | ||
|
||
// Create JSON from completed struct | ||
payloadJSON, marshalErr := json.Marshal(newNotification) | ||
if marshalErr != nil { | ||
return nil, marshalErr | ||
} | ||
|
||
//fmt.Printf("<payload> %s\n", payloadJSON) | ||
|
||
// Make the API request to create the hosts. | ||
results, err := server.NewAPIRequest("PUT", "/objects/notifications/"+name, []byte(payloadJSON)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if results.Code == 200 { | ||
notifications, err := server.GetNotification(name) | ||
return notifications, err | ||
} | ||
|
||
return nil, fmt.Errorf("%s", results.ErrorString) | ||
} | ||
|
||
// DeleteNotification ... | ||
func (server *Server) DeleteNotification(name string) error { | ||
|
||
results, err := server.NewAPIRequest("DELETE", "/objects/notifications/"+name+"?cascade=1", nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if results.Code == 200 { | ||
return nil | ||
} else { | ||
return fmt.Errorf("%s", results.ErrorString) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,233 @@ | ||
package iapi | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestGetValidNotification(t *testing.T) { | ||
|
||
name := "valid-notification" | ||
_, err := Icinga2_Server.GetNotification(name) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestGetInvalidNotification(t *testing.T) { | ||
|
||
name := "invalid-notification" | ||
|
||
_, err := Icinga2_Server.GetNotification(name) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestCreateNotificationCommandDNE(t *testing.T) { | ||
|
||
hostname := "host" | ||
notificationname := hostname+"!"+hostname | ||
command := "invalid-command" | ||
servicename := "" | ||
interval := 1800 | ||
|
||
_, err := Icinga2_Server.CreateNotification(notificationname, hostname, command, servicename, interval, nil, nil, nil) | ||
|
||
if !strings.Contains(err.Error(), "type 'NotificationCommand' does not exist.") { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
// func TestCreateNotificationHostDNE | ||
// Try and create a notification for a host that does not exist. | ||
// Should fail with an error about the host not existing. | ||
func TestCreateNotificationHostDNE(t *testing.T) { | ||
|
||
hostname := "host-dne" | ||
notificationname := hostname+"!"+hostname | ||
command := "mail-host-notification" | ||
servicename := "" | ||
interval := 1800 | ||
|
||
_, err := Icinga2_Server.CreateNotification(notificationname, hostname, command, servicename, interval, nil, nil, nil) | ||
|
||
if !strings.Contains(err.Error(), "type 'Host' does not exist.") { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestCreateNotificationInvalidName(t *testing.T) { | ||
|
||
hostname := "host-dne" | ||
notificationname := "invalid-name" | ||
command := "mail-host-notification" | ||
servicename := "" | ||
interval := 1800 | ||
|
||
_, err := Icinga2_Server.CreateNotification(notificationname, hostname, command, servicename, interval, nil, nil, nil) | ||
|
||
if !strings.Contains(err.Error(), "Invalid Notification name.") { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestCreateNotificationUserDNE(t *testing.T) { | ||
|
||
hostname := "host" | ||
group := []string{"linux-servers"} | ||
notificationname := hostname+"!"+hostname | ||
command := "mail-host-notification" | ||
servicename := "" | ||
interval := 1800 | ||
users := []string{"user-dne"} | ||
|
||
|
||
_, _ = Icinga2_Server.CreateHost(hostname, "127.0.0.1", "hostalive", nil, nil, group) | ||
_, err := Icinga2_Server.CreateNotification(notificationname, hostname, command, servicename, interval, users, nil, nil) | ||
|
||
if !strings.Contains(err.Error(), "type 'User' does not exist.") { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestCreateHostNotification(t *testing.T) { | ||
|
||
hostname := "host" | ||
groups := []string{"linux-servers"} | ||
notificationname := hostname+"!"+hostname | ||
command := "mail-host-notification" | ||
servicename := "" | ||
interval := 1800 | ||
username := "user" | ||
|
||
_, _ = Icinga2_Server.CreateHost(hostname, "127.0.0.1", "hostalive", nil, nil, groups) | ||
_, _ = Icinga2_Server.CreateUser(username, "[email protected]") | ||
_, err := Icinga2_Server.CreateNotification(notificationname, hostname, command, servicename, interval, []string{username}, nil, nil) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestCreateHostNotificationAlreadyExists(t *testing.T) { | ||
|
||
hostname := "host" | ||
notificationname := hostname+"!"+hostname | ||
command := "mail-host-notification" | ||
servicename := "" | ||
interval := 1800 | ||
username := "user" | ||
|
||
_, err := Icinga2_Server.CreateNotification(notificationname, hostname, command, servicename, interval, []string{username}, nil, nil) | ||
|
||
if !strings.HasSuffix(err.Error(), " already exists.") { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestCreateServiceNotification(t *testing.T) { | ||
|
||
hostname := "host" | ||
servicename := "test" | ||
check_command := "random" | ||
notificationname := hostname+"!"+servicename+"!"+hostname+"-"+servicename | ||
command := "mail-service-notification" | ||
interval := 1800 | ||
username := "user" | ||
|
||
_, _ = Icinga2_Server.CreateUser(username, "[email protected]") | ||
_, _ = Icinga2_Server.CreateService(servicename, hostname, check_command) | ||
_, err := Icinga2_Server.CreateNotification(notificationname, hostname, command, servicename, interval, []string{username}, nil, nil) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestCreateServiceNotificationAlreadyExists(t *testing.T) { | ||
|
||
hostname := "host" | ||
servicename := "test" | ||
check_command := "random" | ||
notificationname := hostname+"!"+servicename+"!"+hostname+"-"+servicename | ||
command := "mail-service-notification" | ||
interval := 1800 | ||
username := "user" | ||
|
||
_, _ = Icinga2_Server.CreateUser(username, "[email protected]") | ||
_, _ = Icinga2_Server.CreateService(servicename, hostname, check_command) | ||
_, err := Icinga2_Server.CreateNotification(notificationname, hostname, command, servicename, interval, []string{username}, nil, nil) | ||
|
||
if !strings.HasSuffix(err.Error(), " already exists.") { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestDeleteServiceNotification(t *testing.T) { | ||
|
||
hostname := "host" | ||
servicename := "test" | ||
notificationname := hostname+"!"+servicename+"!"+hostname+"-"+servicename | ||
|
||
err := Icinga2_Server.DeleteNotification(notificationname) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestDeleteServiceNotificationDNE(t *testing.T) { | ||
|
||
hostname := "host" | ||
servicename := "test" | ||
notificationname := hostname+"!"+servicename+"!"+hostname+"-"+servicename | ||
|
||
err := Icinga2_Server.DeleteNotification(notificationname) | ||
|
||
if err.Error() != "No objects found." { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestDeleteHostNotification(t *testing.T) { | ||
|
||
hostname := "host" | ||
notificationname := hostname+"!"+hostname | ||
username := "user" | ||
|
||
err := Icinga2_Server.DeleteNotification(notificationname) | ||
_ = Icinga2_Server.DeleteHost(hostname) | ||
_ = Icinga2_Server.DeleteUser(username) | ||
|
||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
func TestDeleteHostNotificationDNE(t *testing.T) { | ||
|
||
hostname := "host" | ||
notificationname := hostname+"!"+hostname | ||
|
||
err := Icinga2_Server.DeleteNotification(notificationname) | ||
|
||
if err.Error() != "No objects found." { | ||
t.Error(err) | ||
} | ||
} | ||
|
||
// func TestDeleteNotificationNonAPI | ||
// Notifications that were not created via the API, cannot be deleted via the API | ||
// Should get an error about not being created via the API | ||
func TestDeleteNotificationNonAPI(t *testing.T) { | ||
|
||
notificationname := "mail-icingaadmin" | ||
|
||
err := Icinga2_Server.DeleteNotification(notificationname) | ||
if err.Error() != "No objects found." { | ||
t.Error(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.