Skip to content

Commit

Permalink
put in initial logic
Browse files Browse the repository at this point in the history
  • Loading branch information
akerl committed Mar 10, 2018
1 parent 8592c00 commit 7fbf3eb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 11 deletions.
12 changes: 7 additions & 5 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 55 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,75 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"os"

"github.com/akerl/go-lambda/cloudwatch"
"github.com/akerl/go-lambda/s3"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"gopkg.in/yaml.v2"
)

// TODO: Add config file structure
type configFile struct{}

var config *configFile

func handler(e events.SNSEvent) error {
log.Printf("%+v", e)
type configFile struct {
Alarms map[string]alarmConfig `json:"alarms"`
}

func (c *configFile) lookup(name string) (alarmConfig, error) {
conf, ok := c.Alarms[name]
if ok {
return conf, nil
}
conf, ok = c.Alarms["default"]
if ok {
return conf, nil
}
return conf, fmt.Errorf("No config found: %s", name)
}

type alarmConfig struct {
URL string `json:"url"`
}

func (ac *alarmConfig) notify(m cloudwatch.AlarmMessage) error {
slack := slackMsg{Text: m.Subject}
buf, err := json.Marshal(slack)
if err != nil {
return err
}
_, err = http.Post(ac.URL, "application/json", bytes.NewReader(buf))
return err
}

type slackMsg struct {
Text string `json:"text"`
}

func handler(e cloudwatch.SNSEvent) error {
if config == nil {
return fmt.Errorf("Config failed to load")
}

for _, r := range e.Records {
m, err := r.DecodedMessage()
if err != nil {
return err
}
ac, err := config.lookup(m.AlarmName)
if err != nil {
return err
}
err = ac.notify(m)
if err != nil {
return err
}
}

return nil
}

Expand Down

0 comments on commit 7fbf3eb

Please sign in to comment.