Skip to content

Commit

Permalink
fixed message handling, removed support for bootstrapping and executi…
Browse files Browse the repository at this point in the history
…on messages
  • Loading branch information
purehyperbole committed Mar 9, 2017
1 parent 4815144 commit 3e62021
Showing 1 changed file with 42 additions and 53 deletions.
95 changes: 42 additions & 53 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,97 +6,86 @@ package main

import (
"encoding/json"
"log"
"strings"

"github.com/nats-io/nats"
)

type genericMessage struct {
ID string `json:"service"`
Components []interface{} `json:"components"`
type genericMessage map[string]interface{}

func (m *genericMessage) getServiceID() string {
id, ok := (*m)["service"].(string)
if ok {
return id
}
return ""
}

func (m *genericMessage) getServicePart() string {
pieces := strings.Split(m.getServiceID(), "-")
return pieces[len(pieces)-1]
}

func genericHandler(msg *nats.Msg) {
var msgLines []Message
var input genericMessage
var notification Notification

if err := json.Unmarshal(msg.Data, &input); err != nil {
return
}
if err := processNotification(&notification, msg); err != nil {
return
}
input.ID = notification.getServiceID()

parts := strings.Split(msg.Subject, ".")
component := parts[0]

switch component {
case "ebs_volumes", "ebs_volume":
var n EBSVolume
msgLines = n.Handle(msg.Subject, input.Components, msgLines)
var nt EBSVolume
msgLines = nt.Handle(msg.Subject, input, msgLines)
case "instances", "instance":
var n Instance
msgLines = n.Handle(msg.Subject, input.Components, msgLines)
var nt Instance
msgLines = nt.Handle(msg.Subject, input, msgLines)
case "networks", "network":
var n Network
msgLines = n.Handle(msg.Subject, input.Components, msgLines)
var nt Network
msgLines = nt.Handle(msg.Subject, input, msgLines)
case "firewalls", "firewall":
var n Firewall
msgLines = n.Handle(msg.Subject, input.Components, msgLines)
var nt Firewall
msgLines = nt.Handle(msg.Subject, input, msgLines)
case "nats", "nat":
var n Nat
msgLines = n.Handle(msg.Subject, input.Components, msgLines)
var nt Nat
msgLines = nt.Handle(msg.Subject, input, msgLines)
case "routers", "router":
var n Router
msgLines = n.Handle(msg.Subject, input.Components, msgLines)
var nt Router
msgLines = nt.Handle(msg.Subject, input, msgLines)
case "vpcs", "vpc":
var n Vpc
msgLines = n.Handle(msg.Subject, input.Components, msgLines)
var nt Vpc
msgLines = nt.Handle(msg.Subject, input, msgLines)
case "executions", "execution":
var n Execution
msgLines = n.Handle(msg.Subject, input.Components, msgLines)
var nt Execution
msgLines = nt.Handle(msg.Subject, input, msgLines)
case "bootstraps", "bootstrap":
var n Bootstrap
msgLines = n.Handle(msg.Subject, input.Components, msgLines)
var nt Bootstrap
msgLines = nt.Handle(msg.Subject, input, msgLines)
case "elbs", "elb":
var n ELB
msgLines = n.Handle(msg.Subject, input.Components, msgLines)
var nt ELB
msgLines = nt.Handle(msg.Subject, input, msgLines)
case "s3s", "s3":
var n S3Bucket
msgLines = n.Handle(msg.Subject, input.Components, msgLines)
var nt S3Bucket
msgLines = nt.Handle(msg.Subject, input, msgLines)
case "rds_clusters", "rds_cluster":
var n RDSCluster
msgLines = n.Handle(msg.Subject, input.Components, msgLines)
var nt RDSCluster
msgLines = nt.Handle(msg.Subject, input, msgLines)
case "rds_instances", "rds_instance":
var n RDSInstance
msgLines = n.Handle(msg.Subject, input.Components, msgLines)
var nt RDSInstance
msgLines = nt.Handle(msg.Subject, input, msgLines)
default:
switch msg.Subject {
case "executions.create.done":
msgLines = executionsCreateHandler(input.Components)
case "bootstraps.create.done":
msgLines = bootstrapsCreateHandler(input.Components)
case "bootstraps.create.error":
msgLines = genericErrorMessageHandler(input.Components, "Bootstraping", "")
case "executions.create.error":
msgLines = genericErrorMessageHandler(input.Components, "Execution", "")
}
log.Println("unsupported: " + msg.Subject)
}
for _, v := range msgLines {
publishMessage(input.ID, &v)
publishMessage(input.getServicePart(), &v)
}
}

func executionsCreateHandler(components []interface{}) (lines []Message) {
return append(lines, Message{Body: "Executions ran", Level: "INFO"})
}

func bootstrapsCreateHandler(components []interface{}) (lines []Message) {
return append(lines, Message{Body: "Bootstraps ran", Level: "INFO"})
}

func genericErrorMessageHandler(components []interface{}, cType, cAction string) (lines []Message) {
for _, c := range components {
component := c.(map[string]interface{})
Expand Down

0 comments on commit 3e62021

Please sign in to comment.