feat: add http as smtp provider (#8545)

# Which Problems Are Solved

Send Email messages as a HTTP call to a relay, for own logic on handling
different Email providers

# How the Problems Are Solved

Create endpoints under Email provider to manage SMTP and HTTP in the
notification handlers.

# Additional Changes

Clean up old logic in command and query side to handle the general Email
providers with deactivate, activate and remove.

# Additional Context

Partially closes #8270

---------

Co-authored-by: Livio Spring <livio.a@gmail.com>
This commit is contained in:
Stefan Benz
2024-09-12 06:27:29 +02:00
committed by GitHub
parent d8a71d217c
commit 21c38b061d
28 changed files with 3575 additions and 1152 deletions

View File

@@ -10,16 +10,19 @@ import (
const (
smtpConfigPrefix = "smtp.config."
httpConfigPrefix = "http."
SMTPConfigAddedEventType = instanceEventTypePrefix + smtpConfigPrefix + "added"
SMTPConfigChangedEventType = instanceEventTypePrefix + smtpConfigPrefix + "changed"
SMTPConfigPasswordChangedEventType = instanceEventTypePrefix + smtpConfigPrefix + "password.changed"
SMTPConfigHTTPAddedEventType = instanceEventTypePrefix + smtpConfigPrefix + httpConfigPrefix + "added"
SMTPConfigHTTPChangedEventType = instanceEventTypePrefix + smtpConfigPrefix + httpConfigPrefix + "changed"
SMTPConfigRemovedEventType = instanceEventTypePrefix + smtpConfigPrefix + "removed"
SMTPConfigActivatedEventType = instanceEventTypePrefix + smtpConfigPrefix + "activated"
SMTPConfigDeactivatedEventType = instanceEventTypePrefix + smtpConfigPrefix + "deactivated"
)
type SMTPConfigAddedEvent struct {
eventstore.BaseEvent `json:"-"`
*eventstore.BaseEvent `json:"-"`
ID string `json:"id,omitempty"`
Description string `json:"description,omitempty"`
@@ -45,7 +48,7 @@ func NewSMTPConfigAddedEvent(
password *crypto.CryptoValue,
) *SMTPConfigAddedEvent {
return &SMTPConfigAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
BaseEvent: eventstore.NewBaseEventForPush(
ctx,
aggregate,
SMTPConfigAddedEventType,
@@ -61,6 +64,9 @@ func NewSMTPConfigAddedEvent(
Password: password,
}
}
func (e *SMTPConfigAddedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
e.BaseEvent = event
}
func (e *SMTPConfigAddedEvent) Payload() interface{} {
return e
@@ -70,29 +76,21 @@ func (e *SMTPConfigAddedEvent) UniqueConstraints() []*eventstore.UniqueConstrain
return nil
}
func SMTPConfigAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
smtpConfigAdded := &SMTPConfigAddedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(smtpConfigAdded)
if err != nil {
return nil, zerrors.ThrowInternal(err, "IAM-39fks", "unable to unmarshal smtp config added")
}
return smtpConfigAdded, nil
type SMTPConfigChangedEvent struct {
*eventstore.BaseEvent `json:"-"`
ID string `json:"id,omitempty"`
Description *string `json:"description,omitempty"`
FromAddress *string `json:"senderAddress,omitempty"`
FromName *string `json:"senderName,omitempty"`
ReplyToAddress *string `json:"replyToAddress,omitempty"`
TLS *bool `json:"tls,omitempty"`
Host *string `json:"host,omitempty"`
User *string `json:"user,omitempty"`
Password *crypto.CryptoValue `json:"password,omitempty"`
}
type SMTPConfigChangedEvent struct {
eventstore.BaseEvent `json:"-"`
ID string `json:"id,omitempty"`
Description *string `json:"description,omitempty"`
FromAddress *string `json:"senderAddress,omitempty"`
FromName *string `json:"senderName,omitempty"`
ReplyToAddress *string `json:"replyToAddress,omitempty"`
TLS *bool `json:"tls,omitempty"`
Host *string `json:"host,omitempty"`
User *string `json:"user,omitempty"`
Password *crypto.CryptoValue `json:"password,omitempty"`
func (e *SMTPConfigChangedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
e.BaseEvent = event
}
func (e *SMTPConfigChangedEvent) Payload() interface{} {
@@ -113,7 +111,7 @@ func NewSMTPConfigChangeEvent(
return nil, zerrors.ThrowPreconditionFailed(nil, "IAM-o0pWf", "Errors.NoChangesFound")
}
changeEvent := &SMTPConfigChangedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
BaseEvent: eventstore.NewBaseEventForPush(
ctx,
aggregate,
SMTPConfigChangedEventType,
@@ -182,23 +180,10 @@ func ChangeSMTPConfigSMTPPassword(password *crypto.CryptoValue) func(event *SMTP
}
}
func SMTPConfigChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
e := &SMTPConfigChangedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(e)
if err != nil {
return nil, zerrors.ThrowInternal(err, "IAM-m09oo", "unable to unmarshal smtp changed")
}
return e, nil
}
type SMTPConfigPasswordChangedEvent struct {
eventstore.BaseEvent `json:"-"`
ID string `json:"id,omitempty"`
Password *crypto.CryptoValue `json:"password,omitempty"`
*eventstore.BaseEvent `json:"-"`
ID string `json:"id,omitempty"`
Password *crypto.CryptoValue `json:"password,omitempty"`
}
func NewSMTPConfigPasswordChangedEvent(
@@ -208,7 +193,7 @@ func NewSMTPConfigPasswordChangedEvent(
password *crypto.CryptoValue,
) *SMTPConfigPasswordChangedEvent {
return &SMTPConfigPasswordChangedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
BaseEvent: eventstore.NewBaseEventForPush(
ctx,
aggregate,
SMTPConfigPasswordChangedEventType,
@@ -217,6 +202,10 @@ func NewSMTPConfigPasswordChangedEvent(
}
}
func (e *SMTPConfigPasswordChangedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
e.BaseEvent = event
}
func (e *SMTPConfigPasswordChangedEvent) Payload() interface{} {
return e
}
@@ -225,21 +214,109 @@ func (e *SMTPConfigPasswordChangedEvent) UniqueConstraints() []*eventstore.Uniqu
return nil
}
func SMTPConfigPasswordChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
smtpConfigPasswordChanged := &SMTPConfigPasswordChangedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(smtpConfigPasswordChanged)
if err != nil {
return nil, zerrors.ThrowInternal(err, "IAM-99iNF", "unable to unmarshal smtp config password changed")
}
type SMTPConfigHTTPAddedEvent struct {
*eventstore.BaseEvent `json:"-"`
return smtpConfigPasswordChanged, nil
ID string `json:"id,omitempty"`
Description string `json:"description,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
}
func NewSMTPConfigHTTPAddedEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
id, description string,
endpoint string,
) *SMTPConfigHTTPAddedEvent {
return &SMTPConfigHTTPAddedEvent{
BaseEvent: eventstore.NewBaseEventForPush(
ctx,
aggregate,
SMTPConfigHTTPAddedEventType,
),
ID: id,
Description: description,
Endpoint: endpoint,
}
}
func (e *SMTPConfigHTTPAddedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
e.BaseEvent = event
}
func (e *SMTPConfigHTTPAddedEvent) Payload() interface{} {
return e
}
func (e *SMTPConfigHTTPAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
type SMTPConfigHTTPChangedEvent struct {
*eventstore.BaseEvent `json:"-"`
ID string `json:"id,omitempty"`
Description *string `json:"description,omitempty"`
Endpoint *string `json:"endpoint,omitempty"`
}
func (e *SMTPConfigHTTPChangedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
e.BaseEvent = event
}
func (e *SMTPConfigHTTPChangedEvent) Payload() interface{} {
return e
}
func (e *SMTPConfigHTTPChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func NewSMTPConfigHTTPChangeEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
id string,
changes []SMTPConfigHTTPChanges,
) (*SMTPConfigHTTPChangedEvent, error) {
if len(changes) == 0 {
return nil, zerrors.ThrowPreconditionFailed(nil, "IAM-o0pWf", "Errors.NoChangesFound")
}
changeEvent := &SMTPConfigHTTPChangedEvent{
BaseEvent: eventstore.NewBaseEventForPush(
ctx,
aggregate,
SMTPConfigHTTPChangedEventType,
),
ID: id,
}
for _, change := range changes {
change(changeEvent)
}
return changeEvent, nil
}
type SMTPConfigHTTPChanges func(event *SMTPConfigHTTPChangedEvent)
func ChangeSMTPConfigHTTPID(id string) func(event *SMTPConfigHTTPChangedEvent) {
return func(e *SMTPConfigHTTPChangedEvent) {
e.ID = id
}
}
func ChangeSMTPConfigHTTPDescription(description string) func(event *SMTPConfigHTTPChangedEvent) {
return func(e *SMTPConfigHTTPChangedEvent) {
e.Description = &description
}
}
func ChangeSMTPConfigHTTPEndpoint(endpoint string) func(event *SMTPConfigHTTPChangedEvent) {
return func(e *SMTPConfigHTTPChangedEvent) {
e.Endpoint = &endpoint
}
}
type SMTPConfigActivatedEvent struct {
eventstore.BaseEvent `json:"-"`
ID string `json:"id,omitempty"`
*eventstore.BaseEvent `json:"-"`
ID string `json:"id,omitempty"`
}
func NewSMTPConfigActivatedEvent(
@@ -248,7 +325,7 @@ func NewSMTPConfigActivatedEvent(
id string,
) *SMTPConfigActivatedEvent {
return &SMTPConfigActivatedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
BaseEvent: eventstore.NewBaseEventForPush(
ctx,
aggregate,
SMTPConfigActivatedEventType,
@@ -257,6 +334,10 @@ func NewSMTPConfigActivatedEvent(
}
}
func (e *SMTPConfigActivatedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
e.BaseEvent = event
}
func (e *SMTPConfigActivatedEvent) Payload() interface{} {
return e
}
@@ -265,21 +346,9 @@ func (e *SMTPConfigActivatedEvent) UniqueConstraints() []*eventstore.UniqueConst
return nil
}
func SMTPConfigActivatedEventMapper(event eventstore.Event) (eventstore.Event, error) {
smtpConfigActivated := &SMTPConfigActivatedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(smtpConfigActivated)
if err != nil {
return nil, zerrors.ThrowInternal(err, "IAM-KPr5t", "unable to unmarshal smtp config removed")
}
return smtpConfigActivated, nil
}
type SMTPConfigDeactivatedEvent struct {
eventstore.BaseEvent `json:"-"`
ID string `json:"id,omitempty"`
*eventstore.BaseEvent `json:"-"`
ID string `json:"id,omitempty"`
}
func NewSMTPConfigDeactivatedEvent(
@@ -288,7 +357,7 @@ func NewSMTPConfigDeactivatedEvent(
id string,
) *SMTPConfigDeactivatedEvent {
return &SMTPConfigDeactivatedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
BaseEvent: eventstore.NewBaseEventForPush(
ctx,
aggregate,
SMTPConfigDeactivatedEventType,
@@ -297,6 +366,10 @@ func NewSMTPConfigDeactivatedEvent(
}
}
func (e *SMTPConfigDeactivatedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
e.BaseEvent = event
}
func (e *SMTPConfigDeactivatedEvent) Payload() interface{} {
return e
}
@@ -305,21 +378,9 @@ func (e *SMTPConfigDeactivatedEvent) UniqueConstraints() []*eventstore.UniqueCon
return nil
}
func SMTPConfigDeactivatedEventMapper(event eventstore.Event) (eventstore.Event, error) {
smtpConfigDeactivated := &SMTPConfigDeactivatedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(smtpConfigDeactivated)
if err != nil {
return nil, zerrors.ThrowInternal(err, "IAM-KPr5t", "unable to unmarshal smtp config removed")
}
return smtpConfigDeactivated, nil
}
type SMTPConfigRemovedEvent struct {
eventstore.BaseEvent `json:"-"`
ID string `json:"id,omitempty"`
*eventstore.BaseEvent `json:"-"`
ID string `json:"id,omitempty"`
}
func NewSMTPConfigRemovedEvent(
@@ -328,7 +389,7 @@ func NewSMTPConfigRemovedEvent(
id string,
) *SMTPConfigRemovedEvent {
return &SMTPConfigRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
BaseEvent: eventstore.NewBaseEventForPush(
ctx,
aggregate,
SMTPConfigRemovedEventType,
@@ -337,6 +398,9 @@ func NewSMTPConfigRemovedEvent(
}
}
func (e *SMTPConfigRemovedEvent) SetBaseEvent(event *eventstore.BaseEvent) {
e.BaseEvent = event
}
func (e *SMTPConfigRemovedEvent) Payload() interface{} {
return e
}
@@ -344,15 +408,3 @@ func (e *SMTPConfigRemovedEvent) Payload() interface{} {
func (e *SMTPConfigRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func SMTPConfigRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
smtpConfigRemoved := &SMTPConfigRemovedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(smtpConfigRemoved)
if err != nil {
return nil, zerrors.ThrowInternal(err, "IAM-DVw1s", "unable to unmarshal smtp config removed")
}
return smtpConfigRemoved, nil
}