Skip to content

Commit

Permalink
Merge pull request #45 from sensu-community/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 bbf3094 + 5f71653 commit f007aff
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/sensu-community/sensu-plugin-sdk
go 1.13

require (
github.com/google/uuid v1.1.1
github.com/sensu/sensu-go/api/core/v2 v2.3.0
github.com/sensu/sensu-go/types v0.3.0
github.com/sensu/sensu-licensing v0.1.2
Expand Down
1 change: 1 addition & 0 deletions templates/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package templates
var (
testEventBytes = []byte(`
{
"id": "e60d1549-bd57-4281-8273-1a04409aa9fa",
"timestamp": 1550816106,
"entity": {
"entity_class": "agent",
Expand Down
2 changes: 2 additions & 0 deletions templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package templates
import (
"bytes"
"fmt"
"github.com/google/uuid"
"text/template"
"time"
)
Expand All @@ -17,6 +18,7 @@ func EvalTemplate(templName, templStr string, templSrc interface{}) (string, err

templ, err := template.New(templName).Funcs(template.FuncMap{
"UnixTime": func(i int64) time.Time { return time.Unix(i, 0) },
"UUIDFromBytes": uuid.FromBytes,
}).Parse(templStr)
if err != nil {
return "", fmt.Errorf("Error building template: %s", err)
Expand Down
13 changes: 13 additions & 0 deletions templates/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package templates

import (
"encoding/json"
"github.com/google/uuid"
"github.com/sensu/sensu-go/types"
"github.com/stretchr/testify/assert"
"testing"
Expand All @@ -11,6 +12,7 @@ import (
var (
templateOk = "Check: {{ .Check.Name }} Entity: {{ .Entity.Name }} !"
templateOkUnixTime = "Check: {{ .Check.Name }} Entity: {{ .Entity.Name }} Executed: {{(UnixTime .Check.Executed).Format \"2 Jan 2006 15:04:05\"}} !"
templateOkUUID = "Check: {{ .Check.Name }} Entity: {{ .Entity.Name }} Event ID: {{UUIDFromBytes .ID}} !"
templateVarNotFound = "Check: {{ .Check.NameZZZ }} Entity: {{ .Entity.Name }} !"
templateInvalid = "Check: {{ .Check.Name Entity: {{ .Entity.Name }} !"
)
Expand All @@ -36,6 +38,17 @@ func TestEvalTemplateUnixTime_Valid(t *testing.T) {
assert.Equal(t, "Check: check-nginx Entity: webserver01 Executed: " + executed + " !", result)
}

// Valid test - UUID
func TestEvalTemplateUUIDValid(t *testing.T) {
event := &types.Event{}
_ = json.Unmarshal(testEventBytes, event)

uuidFromEvent, _ := uuid.FromBytes(event.ID)
result, err := EvalTemplate("templOk", templateOkUUID, event)
assert.Nil(t, err)
assert.Equal(t, "Check: check-nginx Entity: webserver01 Event ID: " + uuidFromEvent.String() + " !", result)
}

// Variable not found
func TestEvalTemplate_VarNotFound(t *testing.T) {
event := &types.Event{}
Expand Down

0 comments on commit f007aff

Please sign in to comment.