From c73a661437f49837a656038c69901129a228bac5 Mon Sep 17 00:00:00 2001 From: D074360 Date: Mon, 28 Oct 2024 14:19:08 +0100 Subject: [PATCH] Add identifier data to create uuid in events --- cmd/gcpPublishEvent.go | 2 +- pkg/events/events.go | 16 ++++++++++++++-- pkg/events/events_test.go | 21 ++++++++++++++++++--- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/cmd/gcpPublishEvent.go b/cmd/gcpPublishEvent.go index 83baab8cf2..95a5493e93 100644 --- a/cmd/gcpPublishEvent.go +++ b/cmd/gcpPublishEvent.go @@ -68,7 +68,7 @@ func runGcpPublishEvent(utils gcpPublishEventUtils) error { } func createNewEvent(config *gcpPublishEventOptions) ([]byte, error) { - event, err := events.NewEvent(config.EventType, config.EventSource).CreateWithJSONData(config.EventData) + event, err := events.NewEvent(config.EventType, config.EventSource, "").CreateWithJSONData(config.EventData) if err != nil { return []byte{}, errors.Wrap(err, "failed to create new event") } diff --git a/pkg/events/events.go b/pkg/events/events.go index 700337e9f4..ed95f10268 100644 --- a/pkg/events/events.go +++ b/pkg/events/events.go @@ -21,12 +21,14 @@ type Event struct { cloudEvent cloudevents.Event eventType string eventSource string + uuidData string } -func NewEvent(eventType, eventSource string) Event { +func NewEvent(eventType, eventSource string, uuidString string) Event { return Event{ eventType: eventType, eventSource: eventSource, + uuidData: uuidString, } } @@ -45,8 +47,14 @@ func (e Event) CreateWithJSONData(data string, opts ...Option) (Event, error) { func (e Event) Create(data any, opts ...Option) Event { e.cloudEvent = cloudevents.NewEvent("1.0") + + if e.uuidData != "" { + e.cloudEvent.SetID(GetUUID(e.uuidData)) + } else { + e.cloudEvent.SetID(uuid.New().String()) + } + // set default values - e.cloudEvent.SetID(uuid.New().String()) e.cloudEvent.SetType(e.eventType) e.cloudEvent.SetTime(time.Now()) e.cloudEvent.SetSource(e.eventSource) @@ -58,6 +66,10 @@ func (e Event) Create(data any, opts ...Option) Event { return e } +func GetUUID(pipelineIdentifier string) string { + return uuid.NewMD5(uuid.NameSpaceOID, []byte(pipelineIdentifier)).String() +} + func (e Event) ToBytes() ([]byte, error) { data, err := json.Marshal(e.cloudEvent) if err != nil { diff --git a/pkg/events/events_test.go b/pkg/events/events_test.go index 87e4742ef8..4ab9a40bbe 100644 --- a/pkg/events/events_test.go +++ b/pkg/events/events_test.go @@ -11,7 +11,7 @@ func TestEventCreation(t *testing.T) { t.Run("success", func(t *testing.T) { // init // test - event := NewEvent(mock.Anything, mock.Anything).Create(nil) + event := NewEvent(mock.Anything, mock.Anything, "").Create(nil) // asserts assert.Equal(t, mock.Anything, event.cloudEvent.Type()) assert.Equal(t, mock.Anything, event.cloudEvent.Source()) @@ -21,7 +21,7 @@ func TestEventCreation(t *testing.T) { // init testData := `{"testKey":"testValue"}` // test - event, err := NewEvent(mock.Anything, mock.Anything).CreateWithJSONData(testData) + event, err := NewEvent(mock.Anything, mock.Anything, "").CreateWithJSONData(testData) // asserts assert.NoError(t, err) assert.Equal(t, string(event.cloudEvent.Data()), testData) @@ -32,10 +32,25 @@ func TestEventCreation(t *testing.T) { testData := `{"testKey": "testValue"}` additionalData := `{"additionalKey": "additionalValue"}` // test - event, err := NewEvent(mock.Anything, mock.Anything).CreateWithJSONData(testData) + event, err := NewEvent(mock.Anything, mock.Anything, "").CreateWithJSONData(testData) event.AddToCloudEventData(additionalData) // asserts assert.NoError(t, err) assert.Equal(t, string(event.cloudEvent.Data()), `{"additionalKey":"additionalValue","testKey":"testValue"}`) }) } + +func TestGetUUID(t *testing.T) { + pipelineIdentifier := "pipelineIdentifier" + uuid := GetUUID(pipelineIdentifier) + + if uuid == "" { + t.Fatalf("expected a UUID but got none") + } + + uuid2 := GetUUID(pipelineIdentifier) + if uuid != uuid2 { + t.Fatalf("expected the same UUID but got different ones") + } + +}