-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.go
64 lines (54 loc) · 1.74 KB
/
slack.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
package ecstaskmonitoring
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"io/ioutil"
)
// https://golang.org/doc/faq#guarantee_satisfies_interface
var _ Slack = (*Cluster)(nil)
var _ Slack = (*ParallelNotify)(nil)
// Slack ... Slack operation.
type Slack interface {
// NewSlackAttachmentMessage ... Generate a message to send to slack
NewSlackAttachmentMessage(message string) *Attachment
}
// PostSlackMessage ... Verify the revision number and notify the message
func (a *Attachment) PostSlackMessage(incomingWebhook string) {
s := SlackMessage{
Attachments: []*Attachment{a},
}
msg, _ := json.Marshal(s)
resp, err := http.PostForm(
incomingWebhook,
url.Values{"payload": {string(msg)}},
)
if err != nil {
log.sugar.Warnf("cannot post slack: %v: url: %s", err, incomingWebhook)
return
}
defer resp.Body.Close()
if _, err := ioutil.ReadAll(resp.Body); err != nil {
log.sugar.Warnf("cannot post slack: %v", err)
return
}
}
// NewSlackAttachmentMessage ... Initialize attachment data of slack for cluster messages
func (c *Cluster) NewSlackAttachmentMessage(message string) *Attachment {
return &Attachment{
Color: ColorRED,
Title: "ECS cluster task count threshold has been exceeded",
Text: fmt.Sprintf("current: %s > threshold: %d", message, c.TaskThreshold),
Footer: fmt.Sprintf("%s: %s cluster: %s", c.AwsProfile, c.Name, c.AwsRegion),
}
}
// NewSlackAttachmentMessage ... Initialize attachment data of slack for Parallel messages
func (p *ParallelNotify) NewSlackAttachmentMessage(_ string) *Attachment {
return &Attachment{
Color: ColorRED,
Title: "ECS Task Parallel",
Text: fmt.Sprintf("```\n%s```", p.Message),
Footer: fmt.Sprintf("%s: %s cluster: %s", p.AwsProfile, p.ClusterName, p.AwsRegion),
}
}