forked from concourse/governance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailgun_state.go
85 lines (66 loc) · 1.54 KB
/
mailgun_state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package governance
import (
"context"
"fmt"
"log"
"os"
"github.com/mailgun/mailgun-go/v4"
)
func LoadMailgunState(domain string) (*MailgunState, error) {
ctx := context.Background()
mailgunAPIKey := os.Getenv("MAILGUN_API_KEY")
if mailgunAPIKey == "" {
log.Fatalln("no $MAILGUN_API_KEY provided")
}
mg := mailgun.NewMailgun(domain, mailgunAPIKey)
state := &MailgunState{}
iter := mg.ListRoutes(nil)
var routes []mailgun.Route
for iter.Next(ctx, &routes) {
for _, route := range routes {
state.Routes = append(state.Routes, MailgunRoute{
ID: route.Id,
Description: route.Description,
Expression: route.Expression,
Actions: route.Actions,
})
}
}
return state, nil
}
func (config *Config) DesiredMailgunState(domain string) *MailgunState {
state := &MailgunState{}
for _, team := range config.Teams {
if len(team.RawMembers) == 0 {
continue
}
route := MailgunRoute{
Description: fmt.Sprintf("mailgun_route.routes[%q]", team.Name),
Expression: fmt.Sprintf("match_recipient(%q)", team.Name+"@"+domain),
}
for _, member := range team.Members(config) {
if member.Email == "" {
continue
}
route.Actions = append(
route.Actions,
fmt.Sprintf("forward(%q)", member.Email),
)
}
route.Actions = append(
route.Actions,
"stop()",
)
state.Routes = append(state.Routes, route)
}
return state
}
type MailgunState struct {
Routes []MailgunRoute
}
type MailgunRoute struct {
ID string
Description string
Expression string
Actions []string
}