Skip to content

Commit

Permalink
Merge pull request #6 from nixwiz/updates
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
Todd Campbell authored May 14, 2020
2 parents e455558 + 9afa9a2 commit 24f9b03
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 24 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

### Added
- Details field for parity with Sensu Enterprise handler
- Expansion of embedded \n newlines in description
- More test coverage

### Changed
- Minor README fixes
- Changed source from "sensuGo" to "Sensu Go"
- Fixed bug where evenPriority annotations weren't being checked correctly

## [0.4.1] - 2020-05-11

### Changed
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ If you're using an earlier version of sensuctl, you can find the asset on the [B
"env_vars": [
"OPSGENIE_AUTHTOKEN=SECRET",
"OPSGENIE_TEAM=TEAM_NAME",
"OPSGENIE_APIURL=https://api.eu.opsgenie.com"
"OPSGENIE_APIURL=https://api.opsgenie.com"
],
"timeout": 10,
"filters": [
Expand Down Expand Up @@ -168,7 +168,7 @@ either a complete URL or a "host[:port]", in which case the "http" scheme is ass
The easiest way to get this handler added to your Sensu environment, is to add it as an asset from Bonsai:

```sh
sensuctl asset add betorvs/sensu-opsgenie-handler --rename sensu-opsgenie-handler
sensuctl asset add nixwiz/sensu-opsgenie-handler --rename sensu-opsgenie-handler
```

See `sensuctl asset --help` for details on how to specify version.
Expand Down Expand Up @@ -200,7 +200,7 @@ See https://github.com/sensu/sensu-go/blob/master/CONTRIBUTING.md
[1]: https://github.com/sensu/sensu-go
[2]: https://www.opsgenie.com/
[3]: https://docs.sensu.io/sensu-go/5.0/reference/handlers/#how-do-sensu-handlers-work
[4]: https://github.com/betorvs/sensu-opsgenie-handler/releases
[4]: https://github.com/nixwiz/sensu-opsgenie-handler/releases
[5]: https://docs.opsgenie.com/docs/sensu-integration#section-add-sensu-integration-in-opsgenie
[6]: https://github.com/sensu/sensu-pagerduty-handler
[7]: https://docs.sensu.io/sensu-go/latest/reference/assets/
Expand Down
27 changes: 24 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

const (
notFound = "NOT FOUND"
source = "sensuGo"
source = "Sensu Go"
)

// Config represents the handler plugin config.
Expand Down Expand Up @@ -171,13 +171,33 @@ func parseDescription(event *types.Event) (description string) {
if err != nil {
return ""
}
// allow newlines to get expanded
description = strings.Replace(description, `\n`, "\n", -1)
return trim(description, plugin.DescriptionLimit)
}

// parseDetails func returns a map of string string with check information for the details field
func parseDetails(event *types.Event) map[string]string {
details := make(map[string]string)
details["output"] = event.Check.Output
details["command"] = event.Check.Command
details["proxy_entity_name"] = event.Check.ProxyEntityName
details["state"] = event.Check.State
details["status"] = fmt.Sprintf("%d", event.Check.Status)
details["ttl"] = fmt.Sprintf("%d", event.Check.Ttl)
details["interval"] = fmt.Sprintf("%d", event.Check.Interval)
details["occurrences"] = fmt.Sprintf("%d", event.Check.Occurrences)
details["occurrences_watermark"] = fmt.Sprintf("%d", event.Check.OccurrencesWatermark)
details["subscriptions"] = fmt.Sprintf("%v", event.Check.Subscriptions)
details["handlers"] = fmt.Sprintf("%v", event.Check.Handlers)

return details
}

// eventPriority func read priority in the event and return alerts.PX
// check.Annotations override Entity.Annotations
func eventPriority(event *types.Event) alertsv2.Priority {
if event.Check.Annotations != nil {
if event.Check.Annotations != nil && len(event.Check.Annotations["opsgenie_priority"]) > 0 {
switch event.Check.Annotations["opsgenie_priority"] {
case "P5":
return alerts.P5
Expand All @@ -198,7 +218,7 @@ func eventPriority(event *types.Event) alertsv2.Priority {
return alerts.P3

}
} else if event.Entity.Annotations != nil {
} else if event.Entity.Annotations != nil && len(event.Entity.Annotations["opsgenie_priority"]) > 0 {
switch event.Entity.Annotations["opsgenie_priority"] {
case "P5":
return alerts.P5
Expand Down Expand Up @@ -277,6 +297,7 @@ func createIncident(alertCli *ogcli.OpsGenieAlertV2Client, event *types.Event) e
Message: title,
Alias: alias,
Description: parseDescription(event),
Details: parseDetails(event),
Teams: teams,
Entity: event.Entity.Name,
Source: source,
Expand Down
98 changes: 80 additions & 18 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

alerts "github.com/opsgenie/opsgenie-go-sdk/alertsv2"
v2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/types"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -40,35 +39,98 @@ func TestParseDescription(t *testing.T) {
plugin.DescriptionTemplate = "{{.Check.Output}}"
plugin.DescriptionLimit = 100
description := parseDescription(event)
assert.Contains(t, description, "Check OK")
assert.Equal(t, description, "Check OK")
}

func TestParseDetails(t *testing.T) {
event := types.FixtureEvent("foo", "bar")
event.Check.Output = "Check OK"
event.Check.State = "passing"
event.Check.Status = 0
_, err := json.Marshal(event)
assert.NoError(t, err)
det := parseDetails(event)
assert.Equal(t, det["output"], "Check OK")
assert.Equal(t, det["state"], "passing")
assert.Equal(t, det["status"], "0")
}

func TestEventPriority(t *testing.T) {
event := v2.Event{
Entity: &v2.Entity{
ObjectMeta: v2.ObjectMeta{
Name: "test",
Namespace: "default",
testcases := []struct {
myPriority string
alertsPriority alerts.Priority
}{
{"P1", alerts.P1},
{"P2", alerts.P2},
{"P3", alerts.P3},
{"P4", alerts.P4},
{"P5", alerts.P5},
{"Default", alerts.P3},
}

for _, tc := range testcases {
assert := assert.New(t)
event := types.FixtureEvent("foo", "bar")
event.Check.Annotations["opsgenie_priority"] = tc.myPriority
priority := eventPriority(event)
expectedValue := tc.alertsPriority
assert.Equal(priority, expectedValue)
}

// The FixtureEntity in FixtureEvent lacks Annotations, hand roll event
for _, tc := range testcases {
assert := assert.New(t)
event := types.Event{
Entity: &types.Entity{
ObjectMeta: types.ObjectMeta{
Name: "test",
Namespace: "default",
Annotations: map[string]string{
"opsgenie_priority": tc.myPriority,
},
},
},
},
Check: &v2.Check{
ObjectMeta: v2.ObjectMeta{
Name: "test-check",
Annotations: map[string]string{
"opsgenie_priority": "P1",
Check: &types.Check{
ObjectMeta: types.ObjectMeta{
Name: "test-check",
},
Output: "test output",
},
Output: "test output",
},
}
priority := eventPriority(&event)
expectedValue := tc.alertsPriority
assert.Equal(priority, expectedValue)
}
priority := eventPriority(&event)
expectedValue := alerts.P1
assert.Contains(t, priority, expectedValue)

assert := assert.New(t)
event := types.FixtureEvent("foo", "bar")
priority := eventPriority(event)
expectedValue := alerts.P3
assert.Equal(priority, expectedValue)
}

func TestCheckArgs(t *testing.T) {
assert := assert.New(t)
event := types.FixtureEvent("entity1", "check1")
assert.Error(checkArgs(event))
plugin.AuthToken = "Testing"
assert.Error(checkArgs(event))
plugin.Team = "Testing"
assert.NoError(checkArgs(event))
}

func TestStringInSlice(t *testing.T) {
testSlice := []string{"foo", "bar", "test"}
testString := "test"
testResult := stringInSlice(testString, testSlice)
assert.True(t, testResult)
testString = "giraffe"
testResult = stringInSlice(testString, testSlice)
assert.False(t, testResult)
}

func TestTrim(t *testing.T) {
testString := "This string is 33 characters long"
assert.Equal(t, trim(testString, 40), testString)
assert.Equal(t, trim(testString, 4), "This")
}

0 comments on commit 24f9b03

Please sign in to comment.