From 3e62021659d19da6c73b32c205ebb6ba3debcbda Mon Sep 17 00:00:00 2001 From: Tom Bevan Date: Thu, 9 Mar 2017 17:16:10 +0000 Subject: [PATCH] fixed message handling, removed support for bootstrapping and execution messages --- generic.go | 95 ++++++++++++++++++++++++------------------------------ 1 file changed, 42 insertions(+), 53 deletions(-) diff --git a/generic.go b/generic.go index baa0d9c..c946e01 100644 --- a/generic.go +++ b/generic.go @@ -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(¬ification, 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{})