/
notify.go
105 lines (83 loc) · 2.27 KB
/
notify.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package notify
import (
"bytes"
"context"
"embed"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/slack-go/slack"
"golang.org/x/xerrors"
)
const (
ExitCodeOK = iota
ExitCodeErr
)
type App struct {
CloudWatch CloudWatch
ChannelID string
Config *Config
Slack Slack
}
//go:embed widget/*.json
var static embed.FS
const WidgetFileName = "natureremo.json"
func New(conf *Config) *App {
return &App{Config: conf}
}
func (app *App) initLazy(ctx context.Context) error {
if app.CloudWatch != nil && app.Slack != nil && app.ChannelID != "" {
return nil
}
appConf := app.Config
awsConf := new(aws.Config)
if appConf.Region != "" {
awsConf.Region = aws.String(appConf.Region)
}
sessOpt := session.Options{
SharedConfigState: session.SharedConfigEnable,
Config: *awsConf,
}
sess := session.Must(session.NewSessionWithOptions(sessOpt))
if app.CloudWatch == nil {
app.CloudWatch = NewCloudWatch(cloudwatch.New(sess))
}
if app.Slack != nil && app.ChannelID != "" {
return nil
}
vs, err := NewSSM(ssm.New(sess)).GetParameters(ctx, appConf.SlackTokenKey, appConf.ChannelIDKey)
if err != nil {
return xerrors.Errorf("failed to get parameters: %w", err)
}
if app.Slack == nil {
app.Slack = NewSlack(slack.New(vs[appConf.SlackTokenKey]))
}
if app.ChannelID == "" {
app.ChannelID = vs[appConf.ChannelIDKey]
}
return nil
}
func (app *App) Handle(ctx context.Context) error {
return app.RunWithContext(ctx)
}
func (app *App) Run() error {
return app.RunWithContext(context.Background())
}
func (app *App) RunWithContext(ctx context.Context) error {
if err := app.initLazy(ctx); err != nil {
return xerrors.Errorf("failed to init lazy: %w", err)
}
widget, err := static.ReadFile(WidgetFileName)
if err != nil {
return xerrors.Errorf("failed to read file. name: %s: %w", WidgetFileName, err)
}
image, err := app.CloudWatch.GetMetricWidgetImage(ctx, string(widget))
if err != nil {
return xerrors.Errorf("failed to get metric widget image: %w", err)
}
if err = app.Slack.UploadImage(ctx, app.ChannelID, bytes.NewReader(image)); err != nil {
return xerrors.Errorf("failed to upload image: %w", err)
}
return nil
}