Skip to content

Commit

Permalink
Merge pull request #27 from sensu/feature/templates
Browse files Browse the repository at this point in the history
Example templating implementation
  • Loading branch information
portertech committed Mar 3, 2020
2 parents 65bdb5d + bb8d722 commit c2a0123
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,31 @@ import (

"github.com/bluele/slack"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu-community/sensu-plugin-sdk/templates"
corev2 "github.com/sensu/sensu-go/api/core/v2"
)

// HandlerConfig contains the Slack handler configuration
type HandlerConfig struct {
sensu.PluginConfig
slackwebHookURL string
slackChannel string
slackUsername string
slackIconURL string
slackwebHookURL string
slackChannel string
slackUsername string
slackIconURL string
slackDescriptionTemplate string
}

const (
webHookURL = "webhook-url"
channel = "channel"
username = "username"
iconURL = "icon-url"
webHookURL = "webhook-url"
channel = "channel"
username = "username"
iconURL = "icon-url"
descriptionTemplate = "description-template"

defaultChannel = "#general"
defaultIconURL = "https://www.sensu.io/img/sensu-logo.png"
defaultUsername = "sensu"
defaultTemplate = "{{ .Check.Output }}"
)

var (
Expand Down Expand Up @@ -75,6 +79,15 @@ var (
Usage: "A URL to an image to use as the user avatar",
Value: &config.slackIconURL,
},
{
Path: descriptionTemplate,
Env: "SLACK_DESCRIPTION_TEMPLATE",
Argument: descriptionTemplate,
Shorthand: "t",
Default: defaultTemplate,
Usage: "The Slack notification output template, in Golang text/template format",
Value: &config.slackDescriptionTemplate,
},
}
)

Expand Down Expand Up @@ -157,9 +170,13 @@ func messageStatus(event *corev2.Event) string {
}

func messageAttachment(event *corev2.Event) *slack.Attachment {
description, err := templates.EvalTemplate("description", config.slackDescriptionTemplate, event)
if err != nil {
fmt.Errorf("Error processing template: %s", err)
}
attachment := &slack.Attachment{
Title: "Description",
Text: event.Check.Output,
Text: description,
Fallback: formattedMessage(event),
Color: messageColor(event),
Fields: []*slack.AttachmentField{
Expand Down

0 comments on commit c2a0123

Please sign in to comment.