Skip to content

Commit

Permalink
review: simplify how mail templates are compiled
Browse files Browse the repository at this point in the history
This is nominally more code, but the code in NewCluster() repeats itself
less.
  • Loading branch information
majewsky committed Feb 25, 2025
1 parent e3daee2 commit 2ae2d33
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
7 changes: 2 additions & 5 deletions internal/core/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package core
import (
"context"
"slices"
"text/template"
"time"

"github.com/gophercloud/gophercloud/v2"
Expand Down Expand Up @@ -81,16 +80,14 @@ func NewCluster(config ClusterConfiguration) (c *Cluster, errs errext.ErrorSet)
}

// Create mail templates
confirmTPl, err := template.New("confirm").Parse(config.MailTemplates.ConfirmedCommitments.Body)
err := c.Config.MailTemplates.ConfirmedCommitments.Compile()
if err != nil {
errs.Addf("could not parse confirmation mail template: %w", err)
}
c.Config.MailTemplates.ConfirmedCommitments.Compiled = confirmTPl
expireTpl, err := template.New("expire").Parse(config.MailTemplates.ExpiringCommitments.Body)
err = c.Config.MailTemplates.ExpiringCommitments.Compile()
if err != nil {
errs.Addf("could not parse expiration mail template: %w", err)
}
c.Config.MailTemplates.ExpiringCommitments.Compiled = expireTpl

return c, errs
}
Expand Down
1 change: 1 addition & 0 deletions internal/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ type AutogrowQuotaDistributionConfiguration struct {
UsageDataRetentionPeriod util.MarshalableTimeDuration `yaml:"usage_data_retention_period"`
}

// MailTemplateConfiguration appears in type Configuration.
type MailTemplateConfiguration struct {
ConfirmedCommitments MailTemplate `yaml:"confirmed_commitments"`
ExpiringCommitments MailTemplate `yaml:"expiring_commitments"`
Expand Down
10 changes: 10 additions & 0 deletions internal/core/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,22 @@ type CommitmentNotification struct {
Resource AZResourceLocation
}

// MailTemplate is a template for notification mails generated by Limes.
// It appears in type MailTemplateConfiguration.
type MailTemplate struct {
Subject string `yaml:"subject"`
Body string `yaml:"body"`
Compiled *template.Template `yaml:"-"` // filled during Config.Validate()
}

// Compile compiles the provided mail body template.
// This needs to be run once before any call to Render().
func (t *MailTemplate) Compile() (err error) {
t.Compiled, err = template.New("mail-body").Parse(t.Body)
return err
}

// Render generates a mail notification for a completed commitment workflow.
func (t MailTemplate) Render(m CommitmentGroupNotification, projectID db.ProjectID, now time.Time) (db.MailNotification, error) {
if len(m.Commitments) == 0 {
return db.MailNotification{}, fmt.Errorf("mail: no commitments provided for projectID: %v", projectID)
Expand Down

0 comments on commit 2ae2d33

Please sign in to comment.