This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
hipchat.go
71 lines (58 loc) · 1.98 KB
/
hipchat.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
package main
import (
"fmt"
"github.com/andybons/hipchat"
)
const (
activeMessage = "Service %s is active"
failedMessage = "Service %s failed"
restartMessage = "Service %s has auto-restarted"
)
type HipchatClient interface {
PostMessage(req hipchat.MessageRequest) error
}
type Hipchat struct {
Room string `yaml:"room,omitempty"`
Token string `yaml:"token,omitempty"`
Active bool `yaml:"on_active,omitempty"`
Failed bool `yaml:"on_failed,omitempty"`
Restart bool `yaml:"on_restart,omitempty"`
}
func (h *Hipchat) Send(event *ServiceEvent) error {
client := &hipchat.Client{AuthToken: h.Token}
return h.SendWithClient(client, event)
}
func (h *Hipchat) SendWithClient(client HipchatClient, event *ServiceEvent) error {
switch {
case event.ActiveStatus == "active" && h.Active:
return h.sendActive(client, event)
case event.ActiveStatus == "failed" && h.Failed:
return h.sendFailed(client, event)
case event.ActiveStatus == "activating" && event.SubStatus == "auto-restart" && h.Restart:
return h.sendRestart(client, event)
}
return nil
}
func (h *Hipchat) sendActive(client HipchatClient, event *ServiceEvent) error {
msg := fmt.Sprintf(activeMessage, event.Service)
return h.send(client, hipchat.ColorGreen, hipchat.FormatHTML, msg, false)
}
func (h *Hipchat) sendFailed(client HipchatClient, event *ServiceEvent) error {
msg := fmt.Sprintf(failedMessage, event.Service)
return h.send(client, hipchat.ColorRed, hipchat.FormatHTML, msg, true)
}
func (h *Hipchat) sendRestart(client HipchatClient, event *ServiceEvent) error {
msg := fmt.Sprintf(restartMessage, event.Service)
return h.send(client, hipchat.ColorGreen, hipchat.FormatHTML, msg, true)
}
func (h *Hipchat) send(client HipchatClient, color, format, message string, notify bool) error {
req := hipchat.MessageRequest{
RoomId: h.Room,
From: "Systemd",
Message: message,
Color: color,
MessageFormat: format,
Notify: notify,
}
return client.PostMessage(req)
}