Skip to content

Commit

Permalink
Merge pull request #372 from openinfradev/snr_fix
Browse files Browse the repository at this point in the history
feature. support sending email to multiple targets
  • Loading branch information
ktkfree authored Apr 11, 2024
2 parents 5b8a434 + 057829e commit 5809be9
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 40 deletions.
11 changes: 4 additions & 7 deletions internal/mail/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func MakeVerityIdentityMessage(ctx context.Context, to, code string) (*MessageIn

m := &MessageInfo{
From: from,
To: to,
To: []string{to},
Subject: subject,
Body: tpl.String(),
}
Expand All @@ -54,7 +54,7 @@ func MakeTemporaryPasswordMessage(ctx context.Context, to, organizationId, accou

m := &MessageInfo{
From: from,
To: to,
To: []string{to},
Subject: subject,
Body: tpl.String(),
}
Expand Down Expand Up @@ -90,18 +90,15 @@ func MakeGeneratingOrganizationMessage(

m := &MessageInfo{
From: from,
To: to,
To: []string{to},
Subject: subject,
Body: tpl.String(),
}

return m, nil
}

func MakeSystemNotificationMessage(
ctx context.Context,
organizationId string, title string,
to string) (*MessageInfo, error) {
func MakeSystemNotificationMessage(ctx context.Context, organizationId string, title string, to []string) (*MessageInfo, error) {
subject := "[TKS] 시스템 알림이 발생하였습니다."

tmpl, err := template.ParseFS(templateFS, "contents/system_notification.html")
Expand Down
2 changes: 1 addition & 1 deletion internal/mail/ses.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type AwsMailer struct {
func (a *AwsMailer) SendMail(ctx context.Context) error {
input := &awsSes.SendEmailInput{
Destination: &types.Destination{
ToAddresses: []string{a.message.To},
ToAddresses: a.message.To,
},
Message: &types.Message{
Subject: &types.Content{
Expand Down
15 changes: 9 additions & 6 deletions internal/mail/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Mailer interface {

type MessageInfo struct {
From string
To string
To []string
Subject string
Body string
}
Expand All @@ -50,14 +50,17 @@ type SmtpMailer struct {

func (s *SmtpMailer) SendMail(ctx context.Context) error {
s.client.SetHeader("From", s.message.From)
s.client.SetHeader("To", s.message.To)
s.client.SetHeader("Subject", s.message.Subject)
s.client.SetBody("text/html", s.message.Body)

d := NewDialer(s.Host, s.Port, s.Username, s.Password)
if err := d.DialAndSend(s.client); err != nil {
log.Errorf(ctx, "failed to send email, %v", err)
return err

for _, to := range s.message.To {
s.client.SetHeader("To", to)

if err := d.DialAndSend(s.client); err != nil {
log.Errorf(ctx, "failed to send email, %v", err)
continue
}
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/usecase/app-group.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (u *AppGroupUsecase) Create(ctx context.Context, dto model.AppGroup) (id do
"app_group_id=" + dto.ID.String(),
"keycloak_url=" + strings.TrimSuffix(viper.GetString("keycloak-address"), "/auth"),
"console_url=" + viper.GetString("console-address"),
"alert_tks=" + viper.GetString("external-address") + "/system-api/1.0/alerts",
"alert_tks=" + viper.GetString("external-address") + "/system-api/1.0/system-notifications",
"alert_slack=" + viper.GetString("alert-slack"),
"cloud_account_id=" + tksCloudAccountId,
"object_store=" + tksObjectStore,
Expand Down
61 changes: 42 additions & 19 deletions internal/usecase/system-notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/openinfradev/tks-api/internal/mail"
"github.com/openinfradev/tks-api/internal/middleware/auth/request"
"github.com/openinfradev/tks-api/internal/model"
"github.com/openinfradev/tks-api/internal/pagination"
Expand All @@ -31,18 +32,20 @@ type ISystemNotificationUsecase interface {
}

type SystemNotificationUsecase struct {
repo repository.ISystemNotificationRepository
clusterRepo repository.IClusterRepository
organizationRepo repository.IOrganizationRepository
appGroupRepo repository.IAppGroupRepository
repo repository.ISystemNotificationRepository
clusterRepo repository.IClusterRepository
organizationRepo repository.IOrganizationRepository
appGroupRepo repository.IAppGroupRepository
systemNotificationRuleRepo repository.ISystemNotificationRuleRepository
}

func NewSystemNotificationUsecase(r repository.Repository) ISystemNotificationUsecase {
return &SystemNotificationUsecase{
repo: r.SystemNotification,
clusterRepo: r.Cluster,
appGroupRepo: r.AppGroup,
organizationRepo: r.Organization,
repo: r.SystemNotification,
clusterRepo: r.Cluster,
appGroupRepo: r.AppGroup,
organizationRepo: r.Organization,
systemNotificationRuleRepo: r.SystemNotificationRule,
}
}

Expand Down Expand Up @@ -117,20 +120,40 @@ func (u *SystemNotificationUsecase) Create(ctx context.Context, input domain.Cre

_, err = u.repo.Create(ctx, dto)
if err != nil {
log.Error(ctx, "Failed to create systemNotification ", err)
continue
}

/*
for _, user := range dto.System
message, err := mail.MakeSystemNotificationMessage(ctx, organizationId, "test", dto.Users, user.AccountId, randomPassword)
if err != nil {
return nil, httpErrors.NewInternalServerError(err, "", "")
}
mailer := mail.New(message)
if err := mailer.SendMail(ctx); err != nil {
return nil, httpErrors.NewInternalServerError(err, "", "")
}
*/
if systemNotification.Annotations.SystemNotificationRuleId == "" {
log.Error(ctx, "Invalid systemNotificationRuleId ")
continue
}

systemNotificationRuleId, err := uuid.Parse(systemNotification.Annotations.SystemNotificationRuleId)
if err != nil {
log.Error(ctx, "Failed to parse uuid ", err)
continue
}
rule, err := u.systemNotificationRuleRepo.Get(ctx, systemNotificationRuleId)
if err != nil {
log.Error(ctx, "Failed to get systemNotificationRule ", err)
continue
}

to := []string{}
for _, user := range rule.TargetUsers {
to = append(to, user.Email)
}
message, err := mail.MakeSystemNotificationMessage(ctx, organizationId, systemNotification.Annotations.Message, to)
if err != nil {
log.Error(ctx, fmt.Sprintf("Failed to make email content. err : %s", err.Error()))
continue
}
mailer := mail.New(message)
if err := mailer.SendMail(ctx); err != nil {
log.Error(ctx, fmt.Sprintf("Failed to send email to %s. err : %s", to, err.Error()))
continue
}
}

return nil
Expand Down
13 changes: 7 additions & 6 deletions pkg/domain/system-notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ type SystemNotificationRequest struct {
TacoCluster string `json:"taco_cluster"`
} `json:"labels"`
Annotations struct {
Message string `json:"message"`
Summary string `json:"summary"`
Description string `json:"description"`
Checkpoint string `json:"Checkpoint"`
Discriminative string `json:"discriminative"`
AlertType string `json:"alertType"`
Message string `json:"message"`
Summary string `json:"summary"`
Description string `json:"description"`
Checkpoint string `json:"Checkpoint"`
Discriminative string `json:"discriminative"`
AlertType string `json:"alertType"`
SystemNotificationRuleId string `json:"systemNotificationRuleId"`
} `json:"annotations"`
}

Expand Down

0 comments on commit 5809be9

Please sign in to comment.