From 5f716530bd0beb76b3c0234a353060b45c450d96 Mon Sep 17 00:00:00 2001 From: Todd Campbell Date: Thu, 29 Oct 2020 20:30:49 -0400 Subject: [PATCH] add UUIDFromBytes template function Signed-off-by: Todd Campbell --- go.mod | 1 + templates/event_test.go | 1 + templates/templates.go | 2 ++ templates/templates_test.go | 13 +++++++++++++ 4 files changed, 17 insertions(+) diff --git a/go.mod b/go.mod index aa2fd63..4f9c6aa 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/templates/event_test.go b/templates/event_test.go index 9062889..9897073 100644 --- a/templates/event_test.go +++ b/templates/event_test.go @@ -3,6 +3,7 @@ package templates var ( testEventBytes = []byte(` { + "id": "e60d1549-bd57-4281-8273-1a04409aa9fa", "timestamp": 1550816106, "entity": { "entity_class": "agent", diff --git a/templates/templates.go b/templates/templates.go index f0b3491..4503a64 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -3,6 +3,7 @@ package templates import ( "bytes" "fmt" + "github.com/google/uuid" "text/template" "time" ) @@ -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) diff --git a/templates/templates_test.go b/templates/templates_test.go index f068dbd..b2736b7 100644 --- a/templates/templates_test.go +++ b/templates/templates_test.go @@ -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" @@ -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 }} !" ) @@ -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{}