All checks were successful
continuous-integration/drone/push Build is passing
Reject malformed addresses up-front instead of letting them surface as a runtime SMTP failure during Collect.
71 lines
1.6 KiB
Go
71 lines
1.6 KiB
Go
package checker
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/mail"
|
|
"strings"
|
|
"time"
|
|
|
|
sdk "git.happydns.org/checker-sdk-go/checker"
|
|
)
|
|
|
|
func Provider() sdk.ObservationProvider {
|
|
return &happyDeliverProvider{}
|
|
}
|
|
|
|
type happyDeliverProvider struct{}
|
|
|
|
func (p *happyDeliverProvider) Key() sdk.ObservationKey {
|
|
return ObservationKeyHappyDeliver
|
|
}
|
|
|
|
func (p *happyDeliverProvider) Definition() *sdk.CheckerDefinition {
|
|
return Definition()
|
|
}
|
|
|
|
// ValidateOptions runs on the host before Collect so bad options are rejected
|
|
// up-front rather than surfacing as a runtime SMTP failure.
|
|
func (p *happyDeliverProvider) ValidateOptions(opts sdk.CheckerOptions) error {
|
|
raw, ok := opts["from_address"]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
s, ok := raw.(string)
|
|
if !ok {
|
|
return fmt.Errorf("from_address must be a string")
|
|
}
|
|
s = strings.TrimSpace(s)
|
|
if s == "" {
|
|
return nil
|
|
}
|
|
if _, err := mail.ParseAddress(s); err != nil {
|
|
return fmt.Errorf("invalid from_address: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *happyDeliverProvider) ExtractMetrics(ctx sdk.ReportContext, collectedAt time.Time) ([]sdk.CheckMetric, error) {
|
|
var data HappyDeliverData
|
|
if err := json.Unmarshal(ctx.Data(), &data); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(data.Scores) == 0 {
|
|
return nil, nil
|
|
}
|
|
metrics := make([]sdk.CheckMetric, 0, len(AllSections))
|
|
for _, section := range AllSections {
|
|
score, ok := data.Scores[section]
|
|
if !ok {
|
|
continue
|
|
}
|
|
metrics = append(metrics, sdk.CheckMetric{
|
|
Name: "happydeliver_score",
|
|
Value: float64(score),
|
|
Unit: "points",
|
|
Labels: map[string]string{"section": section},
|
|
Timestamp: collectedAt,
|
|
})
|
|
}
|
|
return metrics, nil
|
|
}
|