Skip to content

Commit

Permalink
Email recipient pointers (flyteorg#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
wild-endeavor authored Feb 6, 2020
1 parent ae30b44 commit 001347a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
15 changes: 10 additions & 5 deletions pkg/async/notifications/implementations/aws_emailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ type AwsEmailer struct {
awsEmail sesiface.SESAPI
}

func (e *AwsEmailer) SendEmail(ctx context.Context, email admin.EmailMessage) error {
func FlyteEmailToSesEmailInput(email admin.EmailMessage) ses.SendEmailInput {
var toAddress []*string
for _, toEmail := range email.RecipientsEmail {
toAddress = append(toAddress, &toEmail)
// SES email input takes an array of pointers to strings so we have to create a new one for each email
//nolint:unconvert
e := string(toEmail)
toAddress = append(toAddress, &e)
}

emailInput := ses.SendEmailInput{
return ses.SendEmailInput{
Destination: &ses.Destination{
ToAddresses: toAddress,
},
Expand All @@ -62,17 +65,19 @@ func (e *AwsEmailer) SendEmail(ctx context.Context, email admin.EmailMessage) er
},
},
}
}

func (e *AwsEmailer) SendEmail(ctx context.Context, email admin.EmailMessage) error {
emailInput := FlyteEmailToSesEmailInput(email)
_, err := e.awsEmail.SendEmail(&emailInput)
e.systemMetrics.SendTotal.Inc()

if err != nil {
// TODO: If we see a certain set of AWS errors consistently, we can break the errors down based on type.
logger.Errorf(ctx, "error in sending email [%s] via ses mailer with err: %s", email.String(), err)
e.systemMetrics.SendError.Inc()
return errors.NewFlyteAdminErrorf(codes.Internal, "errors were seen while sending emails")
}

logger.Debugf(ctx, "Sent email to %s sub: %s", email.RecipientsEmail, email.SubjectLine)
e.systemMetrics.SendSuccess.Inc()
return nil
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/async/notifications/implementations/aws_emailer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ func TestAwsEmailer_SendEmail(t *testing.T) {
assert.Nil(t, testEmail.SendEmail(context.Background(), emailNotification))
}

func TestFlyteEmailToSesEmailInput(t *testing.T) {
emailNotification := admin.EmailMessage{
SubjectLine: "Notice: Execution \"name\" has succeeded in \"domain\".",
SenderEmail: "[email protected]",
RecipientsEmail: []string{
"[email protected]",
"[email protected]",
},
Body: "Execution \"name\" has succeeded in \"domain\". View details at " +
"<a href=\"https://example.com/executions/T/B/D\">" +
"https://example.com/executions/T/B/D</a>.",
}

sesEmailInput := FlyteEmailToSesEmailInput(emailNotification)
assert.Equal(t, *sesEmailInput.Destination.ToAddresses[0], emailNotification.RecipientsEmail[0])
assert.Equal(t, *sesEmailInput.Destination.ToAddresses[1], emailNotification.RecipientsEmail[1])
assert.Equal(t, *sesEmailInput.Message.Subject.Data, "Notice: Execution \"name\" has succeeded in \"domain\".")
}

func TestAwsEmailer_SendEmailError(t *testing.T) {
mockAwsEmail := mocks.SESClient{}
var awsSES sesiface.SESAPI
Expand Down

0 comments on commit 001347a

Please sign in to comment.