Skip to content

Commit

Permalink
Merge pull request #57 from nixwiz/feature/template_uuid
Browse files Browse the repository at this point in the history
Add UUIDFromBytes template function
  • Loading branch information
Todd Campbell authored Oct 30, 2020
2 parents 9bd92d3 + 6a451bb commit 244a3ae
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added
- Add template function to allow expansion of event ID

### Changed
- Updated included sample event to include event ID

## [0.8.1] - 2020-10-22

### Changed
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
- [Handler definition](#handler-definition)
- [Annotations](#annotations)
- [Templates](#templates)
- [Formatting Timestamps in Templates](#formatting-dates-in-templates)
- [Formatting Timestamps in Templates](#formatting-timestamps-in-templates)
- [Formatting the Event ID in Templates](#formatting-the-event-id-in-templates)
- [Installing from source and contributing](#installing-from-source-and-contributing)

## Overview
Expand Down Expand Up @@ -194,6 +195,20 @@ timestamps.
[...]
```
#### Formatting the Event ID in Templates
Each Sensu Go event contains a unique event ID (UUID) that is presented to
this handler in a non-printable format. To properly print this value as
part of a template, the function UUIDFromBytes is provided. The example
below shows its use:
```
[...]
<b>Event ID</b>: {{UUIDFromBytes .ID}}
<h3>Check Output Details</h3>
<b>Check Output</b>: {{.Check.Output}}
```
## Installing from source and contributing
Download the latest version of the sensu-email-handler from [releases][1],
Expand Down
3 changes: 2 additions & 1 deletion event.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"timestamp": 1598478983,
"id": "e60d1549-bd57-4281-8273-1a04409aa9fa",
"entity": {
"entity_class": "agent",
"system": {
Expand Down Expand Up @@ -118,5 +120,4 @@
"annotations": null
}
}
"timestamp": 1598478983
}
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
ttemplate "text/template"
"time"

"github.com/google/uuid"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
corev2 "github.com/sensu/sensu-go/api/core/v2"
)
Expand Down Expand Up @@ -364,12 +365,14 @@ func resolveTemplate(templateValue string, event *corev2.Event, contentType stri
if contentType == ContentHTML {
// parse using html/template
tmpl, err = htemplate.New("test").Funcs(htemplate.FuncMap{
"UnixTime": func(i int64) time.Time { return time.Unix(i, 0) },
"UnixTime": func(i int64) time.Time { return time.Unix(i, 0) },
"UUIDFromBytes": uuid.FromBytes,
}).Parse(templateValue)
} else {
// default parse using text/template
tmpl, err = ttemplate.New("test").Funcs(ttemplate.FuncMap{
"UnixTime": func(i int64) time.Time { return time.Unix(i, 0) },
"UnixTime": func(i int64) time.Time { return time.Unix(i, 0) },
"UUIDFromBytes": uuid.FromBytes,
}).Parse(templateValue)
}

Expand Down

0 comments on commit 244a3ae

Please sign in to comment.