Skip to content

Commit

Permalink
added error email
Browse files Browse the repository at this point in the history
  • Loading branch information
chandanpasunoori committed Oct 18, 2021
1 parent 9955c1a commit aca1e41
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ app.json
app2.json
cloudbuild.yaml
dist/
dns-check
dns-sync
deployment.yaml
cronjob.yaml
Makefile
bin/
ignored.json
36 changes: 21 additions & 15 deletions pkg/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var logger = log.Logger{
func (d *Domain) getCNAMERecords() string {
cname, err := net.LookupCNAME(d.Name)
if err != nil {
logger.Errorf("Error looking up CNAME for %s: %s", d.Name, err)
logger.Errorf("error looking up CNAME for %s: %s", d.Name, err)
return ""
}
return cname
Expand All @@ -42,14 +42,18 @@ func (d *Domain) Check() bool {
}

func checkDNSTarget(domain Domain, ses SES) {
logger.Infof("Checking %s", domain.Name)
logger.Infof("checking %s", domain.Name)
if !domain.Check() {
//@todo send email with aws ses
logger.Errorf("%s is not pointing to %s", domain.Name, domain.Target)
sendEmail(domain, ses)
sendEmail(Subject(domain, ses), HtmlBody(domain, ses), TextBody(domain, ses), ses)
}
}

func errorEmail(ses SES, err error) {
errorMessage := fmt.Sprintf("Error in DNS Check(%s)", err.Error())
sendEmail(errorMessage, errorMessage, errorMessage, ses)
}

const (
CharSet = "UTF-8"
)
Expand All @@ -64,19 +68,20 @@ func TextBody(d Domain, ses SES) string {
return fmt.Sprintf(ses.Body, d.Name)
}

func sendEmail(d Domain, sesc SES) {
func sendEmail(subject, htmlBody, body string, s SES) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(sesc.Region)},
Region: aws.String(s.Region)},
)
if err != nil {
logger.Errorf("Error creating session: %s", err)
logger.Errorf("error creating session: %s", err)
errorEmail(s, err)
return
}

svc := ses.New(sess, &aws.Config{
Credentials: credentials.NewStaticCredentials(
sesc.AccessKey,
sesc.SecretKey,
s.AccessKey,
s.SecretKey,
"",
),
})
Expand All @@ -85,26 +90,26 @@ func sendEmail(d Domain, sesc SES) {
Destination: &ses.Destination{
CcAddresses: []*string{},
ToAddresses: []*string{
aws.String(sesc.Recipient),
aws.String(s.Recipient),
},
},
Message: &ses.Message{
Body: &ses.Body{
Html: &ses.Content{
Charset: aws.String(CharSet),
Data: aws.String(HtmlBody(d, sesc)),
Data: aws.String(htmlBody),
},
Text: &ses.Content{
Charset: aws.String(CharSet),
Data: aws.String(TextBody(d, sesc)),
Data: aws.String(body),
},
},
Subject: &ses.Content{
Charset: aws.String(CharSet),
Data: aws.String(Subject(d, sesc)),
Data: aws.String(subject),
},
},
Source: aws.String(sesc.Sender),
Source: aws.String(s.Sender),
}
result, err := svc.SendEmail(input)

Expand All @@ -123,8 +128,9 @@ func sendEmail(d Domain, sesc SES) {
} else {
logger.Println(err.Error())
}
errorEmail(s, err)
return
}
logger.Println("Email Sent to address: " + sesc.Recipient)
logger.Println("email sent to address: " + s.Recipient)
logger.Println(result.MessageId)
}

0 comments on commit aca1e41

Please sign in to comment.