-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Message creation to support subject fields + additional data, ensure …
…tenant id exists in events (#112) * rework message creation to take functional arguments, support subject fields + additional data. add tenant to messages in additional subject urns and subject fields. Signed-off-by: E Camden Fisher <efisher@equinix.com> * add tenant to subject fields Signed-off-by: E Camden Fisher <efisher@equinix.com> * Update internal/pubsub/message.go Co-authored-by: Matt Siwiec <rizzza@users.noreply.github.com> Signed-off-by: E Camden Fisher <fish@fishnix.net> --------- Signed-off-by: E Camden Fisher <efisher@equinix.com> Signed-off-by: E Camden Fisher <fish@fishnix.net> Co-authored-by: Matt Siwiec <rizzza@users.noreply.github.com>
- Loading branch information
Showing
24 changed files
with
376 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package pubsub | ||
|
||
import ( | ||
"time" | ||
|
||
"go.infratographer.com/x/pubsubx" | ||
) | ||
|
||
const ( | ||
// DefaultMessageSource is the default source for messages | ||
DefaultMessageSource = "load-balancer-api" | ||
) | ||
|
||
// NewMessage functionally generates a new pubsub message and appends the tenantURN | ||
// to the list of additional subject urns | ||
func NewMessage(tenantURN string, opts ...MsgOption) (*pubsubx.Message, error) { | ||
msg := pubsubx.Message{ | ||
Timestamp: time.Now().UTC(), | ||
Source: DefaultMessageSource, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(&msg) | ||
} | ||
|
||
msg.AdditionalSubjectURNs = append(msg.AdditionalSubjectURNs, tenantURN) | ||
|
||
if msg.SubjectFields == nil { | ||
msg.SubjectFields = make(map[string]string) | ||
} | ||
|
||
msg.SubjectFields["tenant_urn"] = tenantURN | ||
|
||
if err := validatePubsubMessage(&msg); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &msg, nil | ||
} | ||
|
||
// MsgOption is a functional argument for NewMessage | ||
type MsgOption func(m *pubsubx.Message) | ||
|
||
// WithEventType sets the event type of the message | ||
func WithEventType(e string) MsgOption { | ||
return func(m *pubsubx.Message) { | ||
m.EventType = e | ||
} | ||
} | ||
|
||
// WithSource sets the source of the message | ||
func WithSource(s string) MsgOption { | ||
return func(m *pubsubx.Message) { | ||
m.Source = s | ||
} | ||
} | ||
|
||
// WithActorURN sets the actor urn of the message | ||
func WithActorURN(u string) MsgOption { | ||
return func(m *pubsubx.Message) { | ||
m.ActorURN = u | ||
} | ||
} | ||
|
||
// WithSubjectURN sets the subject urn of the message | ||
func WithSubjectURN(s string) MsgOption { | ||
return func(m *pubsubx.Message) { | ||
m.SubjectURN = s | ||
} | ||
} | ||
|
||
// WithAdditionalSubjectURNs sets the additional subject urns of the message | ||
func WithAdditionalSubjectURNs(a ...string) MsgOption { | ||
return func(m *pubsubx.Message) { | ||
m.AdditionalSubjectURNs = a | ||
} | ||
} | ||
|
||
// WithSubjectFields sets the subject fields of the message | ||
func WithSubjectFields(f map[string]string) MsgOption { | ||
return func(m *pubsubx.Message) { | ||
m.SubjectFields = f | ||
} | ||
} | ||
|
||
// WithAdditionalData sets the additional data of the message | ||
func WithAdditionalData(d map[string]interface{}) MsgOption { | ||
return func(m *pubsubx.Message) { | ||
m.AdditionalData = d | ||
} | ||
} | ||
|
||
// validatePubsubMessage validates a pubsub message for required fields | ||
func validatePubsubMessage(msg *pubsubx.Message) error { | ||
if msg.SubjectURN == "" { | ||
return ErrMissingEventSubjectURN | ||
} | ||
|
||
if msg.ActorURN == "" { | ||
return ErrMissingEventActorURN | ||
} | ||
|
||
if msg.Source == "" { | ||
return ErrMissingEventSource | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package pubsub | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.infratographer.com/x/pubsubx" | ||
) | ||
|
||
func Test_validatePubsubMessage(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
msg *pubsubx.Message | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid message", | ||
msg: &pubsubx.Message{ | ||
EventType: "test", | ||
Source: "test", | ||
SubjectURN: "foo", | ||
ActorURN: "bar", | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "missing source", | ||
msg: &pubsubx.Message{ | ||
EventType: "test", | ||
SubjectURN: "foo", | ||
ActorURN: "bar", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "missing subject urn", | ||
msg: &pubsubx.Message{ | ||
EventType: "test", | ||
Source: "test", | ||
ActorURN: "bar", | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "missing actor urn", | ||
msg: &pubsubx.Message{ | ||
EventType: "test", | ||
Source: "test", | ||
SubjectURN: "foo", | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := validatePubsubMessage(tt.msg) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
return | ||
} | ||
assert.NoError(t, err) | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.