diff --git a/Makefile b/Makefile index 80f37a8..48201f7 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ deps: go get gopkg.in/redis.v3 go get github.com/nats-io/nats go get github.com/dgrijalva/jwt-go + go get github.com/r3labs/pattern dev-deps: deps go get github.com/smartystreets/goconvey/convey diff --git a/auth.go b/auth.go index 48e05e5..c712b87 100644 --- a/auth.go +++ b/auth.go @@ -51,5 +51,5 @@ func authMiddleware(w http.ResponseWriter, r *http.Request) { } // Pass to sse server - s.HTTPHandler(w, r) + ss.HTTPHandler(w, r) } diff --git a/c_ebs_volume.go b/c_ebs_volume.go deleted file mode 100644 index 48a9e37..0000000 --- a/c_ebs_volume.go +++ /dev/null @@ -1,56 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// EBSVolume : ... -type EBSVolume struct { -} - -// Handle : ... -func (n *EBSVolume) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "ebs_volume.create": - lines = n.getSingleDetail(c, "Created EBS volume ") - case "ebs_volume.delete": - lines = n.getSingleDetail(c, "Deleted EBS volume ") - case "ebs_volumes.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found EBS volume")...) - } - } - return lines -} - -func (n *EBSVolume) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["volume_aws_id"].(string) - if id != "" { - lines = append(lines, Message{Body: " AWS ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_elb.go b/c_elb.go deleted file mode 100644 index 95e6d24..0000000 --- a/c_elb.go +++ /dev/null @@ -1,62 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// ELB : ... -type ELB struct { -} - -// Handle : ... -func (n *ELB) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "elb.create": - lines = n.getSingleDetail(c, "Created ELB") - case "elb.update": - lines = n.getSingleDetail(c, "Updated ELB") - case "elb.delete": - lines = n.getSingleDetail(c, "Deleted ELB") - case "elbs.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found ELB")...) - } - } - return lines -} - -func (n *ELB) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if c["dns_name"] != nil { - dnsName, _ := c["dns_name"].(string) - if dnsName != "" { - lines = append(lines, Message{Body: " DNS : " + dnsName, Level: ""}) - } - } - lines = append(lines) - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - - return lines -} diff --git a/c_firewall.go b/c_firewall.go deleted file mode 100644 index 56a1518..0000000 --- a/c_firewall.go +++ /dev/null @@ -1,54 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// Firewall : ... -type Firewall struct { -} - -// Handle : ... -func (n *Firewall) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "firewall.create": - lines = n.getSingleDetail(c, "Created Firewall") - case "firewall.update": - lines = n.getSingleDetail(c, "Updated Firewall") - case "firewall.delete": - lines = n.getSingleDetail(c, "Deleted Firewall") - case "firewalls.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Firewall")...) - } - } - return lines -} - -func (n *Firewall) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_instance.go b/c_instance.go deleted file mode 100644 index eb855e3..0000000 --- a/c_instance.go +++ /dev/null @@ -1,64 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// Instance : ... -type Instance struct { -} - -// Handle : ... -func (n *Instance) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "instance.create": - lines = n.getSingleDetail(c, "Created Instance") - case "instance.update": - lines = n.getSingleDetail(c, "Updated Instance") - case "instance.delete": - lines = n.getSingleDetail(c, "Deleted Instance") - case "instances.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Instance")...) - } - } - return lines -} - -func (n *Instance) getSingleDetail(c component, prefix string) (lines []Message) { - ip, _ := c["ip"].(string) - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - lines = append(lines, Message{Body: " IP : " + ip, Level: ""}) - publicIP, _ := c["public_ip"].(string) - if publicIP != "" { - lines = append(lines, Message{Body: " PUBLIC IP : " + publicIP, Level: ""}) - } - id, _ := c["instance_aws_id"].(string) - if id != "" { - lines = append(lines, Message{Body: " AWS ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_internet_gateway.go b/c_internet_gateway.go deleted file mode 100644 index d8bf615..0000000 --- a/c_internet_gateway.go +++ /dev/null @@ -1,56 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// InternetGateway : ... -type InternetGateway struct { -} - -// Handle : ... -func (n *InternetGateway) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "internet_gateway.create": - lines = n.getSingleDetail(c, "Created Internet Gateway") - case "internet_gateway.delete": - lines = n.getSingleDetail(c, "Deleted Internet Gateway") - case "internet_gateways.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Internet Gateway")...) - } - } - return lines -} - -func (n *InternetGateway) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["internet_gateway_aws_id"].(string) - if id != "" { - lines = append(lines, Message{Body: " AWS ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_loadbalancer.go b/c_loadbalancer.go deleted file mode 100644 index e9b3cfa..0000000 --- a/c_loadbalancer.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// Lb : ... -type Lb struct { -} - -// Handle : ... -func (n *Lb) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "lb.create": - lines = n.getSingleDetail(c, "Created Loadbalancer") - case "lb.update": - lines = n.getSingleDetail(c, "Updated Loadbalancer") - case "lb.delete": - lines = n.getSingleDetail(c, "Deleted Loadbalancer") - case "lbs.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Loadbalancer")...) - } - } - return lines -} - -func (n *Lb) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_loadbalancer_backend_address_pool.go b/c_loadbalancer_backend_address_pool.go deleted file mode 100644 index 821c9c4..0000000 --- a/c_loadbalancer_backend_address_pool.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// LbBackendAddressPool : ... -type LbBackendAddressPool struct { -} - -// Handle : ... -func (n *LbBackendAddressPool) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "lb_backend_address_pool.create": - lines = n.getSingleDetail(c, "Created Loadbalancer") - case "lb_backend_address_pool.update": - lines = n.getSingleDetail(c, "Updated Loadbalancer") - case "lb_backend_address_pool.delete": - lines = n.getSingleDetail(c, "Deleted Loadbalancer") - case "lb_backend_address_pools.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Loadbalancer backend address pool")...) - } - } - return lines -} - -func (n *LbBackendAddressPool) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_loadbalancer_probe.go b/c_loadbalancer_probe.go deleted file mode 100644 index 2ad9541..0000000 --- a/c_loadbalancer_probe.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// LbProbe : ... -type LbProbe struct { -} - -// Handle : ... -func (n *LbProbe) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "lb_probe.create": - lines = n.getSingleDetail(c, "Created Loadbalancer") - case "lb_probe.update": - lines = n.getSingleDetail(c, "Updated Loadbalancer") - case "lb_probe.delete": - lines = n.getSingleDetail(c, "Deleted Loadbalancer") - case "lb_probes.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Loadbalancer probe")...) - } - } - return lines -} - -func (n *LbProbe) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_loadbalancer_rule.go b/c_loadbalancer_rule.go deleted file mode 100644 index 1c96787..0000000 --- a/c_loadbalancer_rule.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// LbRule : ... -type LbRule struct { -} - -// Handle : ... -func (n *LbRule) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "lb_rule.create": - lines = n.getSingleDetail(c, "Created Loadbalancer") - case "lb_rule.update": - lines = n.getSingleDetail(c, "Updated Loadbalancer") - case "lb_rule.delete": - lines = n.getSingleDetail(c, "Deleted Loadbalancer") - case "lb_rules.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Loadbalancer Rule")...) - } - } - return lines -} - -func (n *LbRule) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_local_network_gateway.go b/c_local_network_gateway.go deleted file mode 100644 index 3fe8cb8..0000000 --- a/c_local_network_gateway.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// LocalNetworkGateway : ... -type LocalNetworkGateway struct { -} - -// Handle : ... -func (n *LocalNetworkGateway) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "local_network_gateway.create": - lines = n.getSingleDetail(c, "Created Local Network Gateway") - case "local_network_gateway.update": - lines = n.getSingleDetail(c, "Updated Local Network Gateway") - case "local_network_gateway.delete": - lines = n.getSingleDetail(c, "Deleted Local Network Gateway") - case "local_network_gateways.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Local Network Gateway")...) - } - } - return lines -} - -func (n *LocalNetworkGateway) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_nat.go b/c_nat.go deleted file mode 100644 index 5568d2b..0000000 --- a/c_nat.go +++ /dev/null @@ -1,54 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// Nat : ... -type Nat struct { -} - -// Handle : ... -func (n *Nat) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "nat.create": - lines = n.getSingleDetail(c, "Created Nat") - case "nat.update": - lines = n.getSingleDetail(c, "Updated Nat") - case "nat.delete": - lines = n.getSingleDetail(c, "Deleted Nat") - case "nats.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Nat")...) - } - } - return lines -} - -func (n *Nat) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_network.go b/c_network.go deleted file mode 100644 index 3097280..0000000 --- a/c_network.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// Network : ... -type Network struct { -} - -// Handle : ... -func (n *Network) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "network.create": - lines = n.getSingleDetail(c, "Created Network") - case "network.delete": - lines = n.getSingleDetail(c, "Deleted Network") - case "networks.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Network")...) - } - } - return lines -} - -func (n *Network) getSingleDetail(c component, prefix string) (lines []Message) { - ip, _ := c["range"].(string) - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - lines = append(lines, Message{Body: " Subnet : " + ip, Level: ""}) - id, _ := c["network_aws_id"].(string) - if id != "" { - lines = append(lines, Message{Body: " AWS ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_network_interface.go b/c_network_interface.go deleted file mode 100644 index 111f2d6..0000000 --- a/c_network_interface.go +++ /dev/null @@ -1,60 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import ( - "strings" -) - -// NetworkInterface : ... -type NetworkInterface struct { -} - -// Handle : ... -func (n *NetworkInterface) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "network_interface.create": - lines = n.getSingleDetail(c, "Created Network Interface") - case "network_interface.delete": - lines = n.getSingleDetail(c, "Deleted Network Interface") - case "network_interfaces.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Network Interface")...) - } - } - return lines -} - -func (n *NetworkInterface) getSingleDetail(c component, prefix string) (lines []Message) { - ip, _ := c["private_ip_address"].(string) - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - lines = append(lines, Message{Body: " IP : " + ip, Level: ""}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_network_security_group.go b/c_network_security_group.go deleted file mode 100644 index 349677d..0000000 --- a/c_network_security_group.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// NetworkSecurityGroup : ... -type NetworkSecurityGroup struct { -} - -// Handle : ... -func (n *NetworkSecurityGroup) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "security_group.create": - lines = n.getSingleDetail(c, "Created Network Security Group") - case "security_group.update": - lines = n.getSingleDetail(c, "Updated Network Security Group") - case "security_group.delete": - lines = n.getSingleDetail(c, "Deleted Network Security Group") - case "security_groups.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Network Security Group")...) - } - } - return lines -} - -func (n *NetworkSecurityGroup) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_public_ip.go b/c_public_ip.go deleted file mode 100644 index d8f0023..0000000 --- a/c_public_ip.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// PublicIP : ... -type PublicIP struct { -} - -// Handle : ... -func (n *PublicIP) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "public_ip.create": - lines = n.getSingleDetail(c, "Created Public IP") - case "public_ip.delete": - lines = n.getSingleDetail(c, "Deleted Public IP") - case "public_ips.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Public IP")...) - } - } - return lines -} - -func (n *PublicIP) getSingleDetail(c component, prefix string) (lines []Message) { - ip, _ := c["ip_address"].(string) - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - lines = append(lines, Message{Body: " IP : " + ip, Level: ""}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_rds_cluster.go b/c_rds_cluster.go deleted file mode 100644 index 133e920..0000000 --- a/c_rds_cluster.go +++ /dev/null @@ -1,55 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// RDSCluster : ... -type RDSCluster struct { -} - -// Handle : ... -func (n *RDSCluster) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "rds_cluster.create": - lines = n.getSingleDetail(c, "Created RDS Cluster") - case "rds_cluster.update": - lines = n.getSingleDetail(c, "Updated RDS Cluster") - case "rds_cluster.delete": - lines = n.getSingleDetail(c, "Deleted RDS Cluster") - case "rds_clusters.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found RDS Cluster")...) - } - } - return lines -} - -func (n *RDSCluster) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - engine, _ := c["engine"].(string) - endpoint, _ := c["endpoint"].(string) - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - lines = append(lines, Message{Body: " Engine : " + engine, Level: ""}) - lines = append(lines, Message{Body: " Endpoint : " + endpoint, Level: ""}) - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_rds_instance.go b/c_rds_instance.go deleted file mode 100644 index 7c0c5b5..0000000 --- a/c_rds_instance.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// RDSInstance : ... -type RDSInstance struct { -} - -// Handle : ... -func (n *RDSInstance) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "rds_instance.create": - lines = n.getSingleDetail(c, "Created RDS Instance") - case "rds_instance.udpate": - lines = n.getSingleDetail(c, "Updated RDS Instance") - case "rds_instance.delete": - lines = n.getSingleDetail(c, "Deleted RDS Instance") - case "rds_instances.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found RDS Instance")...) - } - } - return lines -} - -func (n *RDSInstance) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - engine, _ := c["engine"].(string) - cluster, _ := c["cluster"].(string) - endpoint, _ := c["endpoint"].(string) - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - lines = append(lines, Message{Body: " Engine : " + engine, Level: ""}) - lines = append(lines, Message{Body: " Cluster : " + cluster, Level: ""}) - lines = append(lines, Message{Body: " Endpoint : " + endpoint, Level: ""}) - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - - return lines -} diff --git a/c_resource_group.go b/c_resource_group.go deleted file mode 100644 index b02c6b7..0000000 --- a/c_resource_group.go +++ /dev/null @@ -1,56 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// ResourceGroup : ... -type ResourceGroup struct { -} - -// Handle : ... -func (n *ResourceGroup) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "resource_group.create": - lines = n.getSingleDetail(c, "Created Resource Group") - case "resource_group.delete": - lines = n.getSingleDetail(c, "Deleted Resource Group") - case "resource_groups.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Resource Group")...) - } - } - return lines -} - -func (n *ResourceGroup) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_router.go b/c_router.go deleted file mode 100644 index e359f96..0000000 --- a/c_router.go +++ /dev/null @@ -1,48 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// Router : ... -type Router struct { -} - -// Handle : ... -func (n *Router) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - - case "router.create", "router.update": - lines = n.getSingleDetail(c, "Configured Router") - } - return lines -} - -func (n *Router) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - - return lines -} diff --git a/c_s3.go b/c_s3.go deleted file mode 100644 index 4f1f6d7..0000000 --- a/c_s3.go +++ /dev/null @@ -1,59 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// S3Bucket : ... -type S3Bucket struct { -} - -// Handle : ... -func (n *S3Bucket) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "s3.create": - lines = n.getSingleDetail(c, "Created S3 Bucket") - case "s3.update": - lines = n.getSingleDetail(c, "Updated S3 Bucket") - case "s3.delete": - lines = n.getSingleDetail(c, "Deleted S3 Bucket") - case "s3s.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found S3 Bucket")...) - } - } - return lines -} - -func (n *S3Bucket) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - acl, _ := c["acl"].(string) - if acl == "" { - acl = "by grantees" - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - lines = append(lines, Message{Body: " ACL : " + acl, Level: ""}) - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_service.go b/c_service.go deleted file mode 100644 index 3b6d078..0000000 --- a/c_service.go +++ /dev/null @@ -1,38 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -// Service : ... -type Service struct { -} - -// Handle : ... -func (n *Service) Handle(subject string, lines []Message) []Message { - switch subject { - case "service.create": - lines = append(lines, Message{Body: "Applying your definition", Level: "INFO"}) - case "service.delete": - lines = append(lines, Message{Body: "Starting environment deletion", Level: "INFO"}) - case "service.create.done": - lines = append(lines, Message{Body: "SUCCESS: rules successfully applied", Level: "SUCCESS"}) - lines = append(lines, Message{Body: "error", Level: "ERROR"}) - case "service.create.error": - lines = append(lines, Message{Body: "\nOops! Something went wrong. Please manually fix any errors shown above and re-apply your definition.", Level: "INFO"}) - lines = append(lines, Message{Body: "error", Level: "ERROR"}) - case "service.delete.done": - lines = append(lines, Message{Body: "SUCCESS: your environment has been successfully deleted", Level: "SUCCESS"}) - lines = append(lines, Message{Body: "success", Level: "SUCCESS"}) - case "service.delete.error": - lines = append(lines, Message{Body: "\nOops! Something went wrong. Please manually fix any errors shown above and re-apply your service deletion.", Level: "INFO"}) - lines = append(lines, Message{Body: "error", Level: "ERROR"}) - case "service.import.done": - lines = append(lines, Message{Body: "SUCCESS: service successfully imported", Level: "SUCCESS"}) - lines = append(lines, Message{Body: "error", Level: "ERROR"}) - case "service.import.error": - lines = append(lines, Message{Body: "\nOops! Something went wrong. Please manually fix any errors shown above and re-apply your definition.", Level: "INFO"}) - lines = append(lines, Message{Body: "error", Level: "ERROR"}) - } - return lines -} diff --git a/c_sql_database.go b/c_sql_database.go deleted file mode 100644 index e54144a..0000000 --- a/c_sql_database.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// SQLDatabase : ... -type SQLDatabase struct { -} - -// Handle : ... -func (n *SQLDatabase) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "sql_database.create": - lines = n.getSingleDetail(c, "Created SQL Database") - case "sql_database.update": - lines = n.getSingleDetail(c, "Updated SQL Database") - case "sql_database.delete": - lines = n.getSingleDetail(c, "Deleted SQL Database") - case "sql_databases.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found SQL Database")...) - } - } - return lines -} - -func (n *SQLDatabase) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_sql_firewall_rules.go b/c_sql_firewall_rules.go deleted file mode 100644 index b6a95ed..0000000 --- a/c_sql_firewall_rules.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// SQLFirewallRule : ... -type SQLFirewallRule struct { -} - -// Handle : ... -func (n *SQLFirewallRule) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "sql_firewall_rule.create": - lines = n.getSingleDetail(c, "Created SQL Firewall Rule") - case "sql_firewall_rule.update": - lines = n.getSingleDetail(c, "Updated SQL Firewall Rule") - case "sql_firewall_rule.delete": - lines = n.getSingleDetail(c, "Deleted SQL Firewall Rule") - case "sql_firewall_rules.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found SQL Firewall Rule")...) - } - } - return lines -} - -func (n *SQLFirewallRule) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_sql_server.go b/c_sql_server.go deleted file mode 100644 index 343a94c..0000000 --- a/c_sql_server.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// SQLServer : ... -type SQLServer struct { -} - -// Handle : ... -func (n *SQLServer) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "sql_server.create": - lines = n.getSingleDetail(c, "Created SQL Server") - case "sql_server.update": - lines = n.getSingleDetail(c, "Updated SQL Server") - case "sql_server.delete": - lines = n.getSingleDetail(c, "Deleted SQL Server") - case "sql_servers.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found SQL Server")...) - } - } - return lines -} - -func (n *SQLServer) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_storage_account.go b/c_storage_account.go deleted file mode 100644 index 29cc387..0000000 --- a/c_storage_account.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// StorageAccount : ... -type StorageAccount struct { -} - -// Handle : ... -func (n *StorageAccount) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "storage_account.create": - lines = n.getSingleDetail(c, "Created Storage Account") - case "storage_account.update": - lines = n.getSingleDetail(c, "Updated Storage Account") - case "storage_account.delete": - lines = n.getSingleDetail(c, "Deleted Storage Account") - case "storage_accounts.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Storage Account")...) - } - } - return lines -} - -func (n *StorageAccount) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_storage_container.go b/c_storage_container.go deleted file mode 100644 index 463d540..0000000 --- a/c_storage_container.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// StorageContainer : ... -type StorageContainer struct { -} - -// Handle : ... -func (n *StorageContainer) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "storage_container.create": - lines = n.getSingleDetail(c, "Created Storage Container") - case "storage_container.update": - lines = n.getSingleDetail(c, "Updated Storage Container") - case "storage_container.delete": - lines = n.getSingleDetail(c, "Deleted Storage Container") - case "storage_containers.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Storage Container")...) - } - } - return lines -} - -func (n *StorageContainer) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_subnet.go b/c_subnet.go deleted file mode 100644 index cdfe037..0000000 --- a/c_subnet.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// Subnet : ... -type Subnet struct { -} - -// Handle : ... -func (n *Subnet) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "subnet.create": - lines = n.getSingleDetail(c, "Created Subnet") - case "subnet.delete": - lines = n.getSingleDetail(c, "Deleted Subnet") - case "subnets.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Subnet")...) - } - } - return lines -} - -func (n *Subnet) getSingleDetail(c component, prefix string) (lines []Message) { - ip, _ := c["address_prefix"].(string) - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - lines = append(lines, Message{Body: " Address Prefix : " + ip, Level: ""}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_virtual_machine.go b/c_virtual_machine.go deleted file mode 100644 index f506624..0000000 --- a/c_virtual_machine.go +++ /dev/null @@ -1,58 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// VirtualMachine : ... -type VirtualMachine struct { -} - -// Handle : ... -func (n *VirtualMachine) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "virtual_machine.create": - lines = n.getSingleDetail(c, "Created Virtual Machine") - case "virtual_machine.update": - lines = n.getSingleDetail(c, "Updated Virtual Machine") - case "virtual_machine.delete": - lines = n.getSingleDetail(c, "Deleted Virtual Machine") - case "virtual_machines.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Virtual Machine")...) - } - } - return lines -} - -func (n *VirtualMachine) getSingleDetail(c component, prefix string) (lines []Message) { - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_virtual_network.go b/c_virtual_network.go deleted file mode 100644 index 7745fd4..0000000 --- a/c_virtual_network.go +++ /dev/null @@ -1,65 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import ( - "strings" -) - -// VirtualNetwork : ... -type VirtualNetwork struct { -} - -// Handle : ... -func (n *VirtualNetwork) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "virtual_network.create": - lines = n.getSingleDetail(c, "Created Virtual Network") - case "virtual_network.delete": - lines = n.getSingleDetail(c, "Deleted Virtual Network") - case "virtual_networks.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found Virtual Network")...) - } - } - return lines -} - -func (n *VirtualNetwork) getSingleDetail(c component, prefix string) (lines []Message) { - addressSpace := c["address_space"].([]interface{}) - var netlist []string - for _, a := range addressSpace { - netlist = append(netlist, a.(string)) - } - networks := strings.Join(netlist, ", ") - name, _ := c["name"].(string) - if prefix != "" { - name = prefix + " " + name - } - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + name, Level: level}) - lines = append(lines, Message{Body: " Address Space : " + networks, Level: ""}) - id, _ := c["id"].(string) - if id != "" { - lines = append(lines, Message{Body: " ID : " + id, Level: ""}) - } - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/c_vpc.go b/c_vpc.go deleted file mode 100644 index c8ccd41..0000000 --- a/c_vpc.go +++ /dev/null @@ -1,56 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import "strings" - -// Vpc : ... -type Vpc struct { -} - -// Handle : ... -func (n *Vpc) Handle(subject string, c component, lines []Message) []Message { - parts := strings.Split(subject, ".") - subject = parts[0] + "." + parts[1] - switch subject { - case "vpc.create": - lines = n.getSingleDetail(c, "Created VPC") - case "vpc.update": - lines = n.getSingleDetail(c, "Updated VPC") - case "vpc.delete": - lines = n.getSingleDetail(c, "Deleted VPC") - case "vpcs.find": - for _, cx := range c.getFoundComponents() { - lines = append(lines, n.getSingleDetail(cx, "Found VPC")...) - } - } - return lines -} - -func (n *Vpc) getSingleDetail(c component, prefix string) (lines []Message) { - id, _ := c["vpc_id"].(string) - if prefix != "" { - id = prefix + " " + id - } - subnet, _ := c["subnet"].(string) - status, _ := c["_state"].(string) - level := "INFO" - if status == "errored" { - level = "ERROR" - } - if status != "errored" && status != "completed" && status != "" { - return lines - } - lines = append(lines, Message{Body: " " + id, Level: level}) - lines = append(lines, Message{Body: " Subnet : " + subnet, Level: ""}) - if status != "" { - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - } - if status == "errored" { - err, _ := c["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: ""}) - } - return lines -} diff --git a/component.go b/component.go new file mode 100644 index 0000000..18c14f4 --- /dev/null +++ b/component.go @@ -0,0 +1,61 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package main + +import ( + "encoding/json" + "log" + "strings" + + "github.com/nats-io/nats" + "github.com/r3labs/sse" +) + +// Component : holds component values +type Component struct { + ID string `json:"_component_id"` + Subject string `json:"_subject"` + Type string `json:"_component"` + State string `json:"_state"` + Action string `json:"_action"` + Provider string `json:"_provider"` + Name string `json:"name"` + Error string `json:"error,omitempty"` + Service string `json:"service,omitempty"` + Components []Component `json:"components,omitempty"` +} + +func processComponent(msg *nats.Msg) { + var c Component + + c.Subject = msg.Subject + + if err := json.Unmarshal(msg.Data, &c); err != nil { + log.Println(err) + return + } + + id := c.getID() + data, err := json.Marshal(c) + if err != nil { + log.Println(err) + return + } + + if ss.StreamExists(id) { + ss.Publish(id, &sse.Event{Data: data}) + } +} + +func (c *Component) getID() string { + if strings.Contains(c.Service, "-") { + var pieces []string + pieces = strings.Split(c.Service, "-") + + return pieces[len(pieces)-1] + } + + return c.Service +} diff --git a/generic.go b/generic.go deleted file mode 100644 index eeecac7..0000000 --- a/generic.go +++ /dev/null @@ -1,163 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import ( - "encoding/json" - "log" - "strings" - - "github.com/nats-io/nats" -) - -type component map[string]interface{} - -func (m *component) getServiceID() string { - id, ok := (*m)["service"].(string) - if ok { - return id - } - return "" -} - -func (m *component) getServicePart() string { - pieces := strings.Split(m.getServiceID(), "-") - return pieces[len(pieces)-1] -} - -func (m *component) getFoundComponents() []component { - var c []component - - components, ok := (*m)["components"].([]interface{}) - if ok { - for _, x := range components { - c = append(c, x.(map[string]interface{})) - } - } - - return c -} - -func genericHandler(msg *nats.Msg) { - var msgLines []Message - var c component - - if err := json.Unmarshal(msg.Data, &c); err != nil { - return - } - - parts := strings.Split(msg.Subject, ".") - component := parts[0] - - switch component { - case "ebs_volumes", "ebs_volume": - var nt EBSVolume - msgLines = nt.Handle(msg.Subject, c, msgLines) - case "instances", "instance": - var nt Instance - msgLines = nt.Handle(msg.Subject, c, msgLines) - case "networks", "network": - var nt Network - msgLines = nt.Handle(msg.Subject, c, msgLines) - case "firewalls", "firewall": - var nt Firewall - msgLines = nt.Handle(msg.Subject, c, msgLines) - case "nats", "nat": - var nt Nat - msgLines = nt.Handle(msg.Subject, c, msgLines) - case "routers", "router": - var nt Router - msgLines = nt.Handle(msg.Subject, c, msgLines) - case "vpcs", "vpc": - var nt Vpc - msgLines = nt.Handle(msg.Subject, c, msgLines) - case "elbs", "elb": - var nt ELB - msgLines = nt.Handle(msg.Subject, c, msgLines) - case "s3s", "s3": - var nt S3Bucket - msgLines = nt.Handle(msg.Subject, c, msgLines) - case "rds_clusters", "rds_cluster": - var nt RDSCluster - msgLines = nt.Handle(msg.Subject, c, msgLines) - case "rds_instances", "rds_instance": - var nt RDSInstance - msgLines = nt.Handle(msg.Subject, c, msgLines) - case "internet_gateway", "internet_gateways": - var nt InternetGateway - msgLines = nt.Handle(msg.Subject, c, msgLines) - case "public_ip", "public_ips": - var h PublicIP - msgLines = h.Handle(msg.Subject, c, msgLines) - case "virtual_network", "virtual_networks": - var h VirtualNetwork - msgLines = h.Handle(msg.Subject, c, msgLines) - case "resource_group", "resource_groups": - var h ResourceGroup - msgLines = h.Handle(msg.Subject, c, msgLines) - case "subnet", "subnets": - var h Subnet - msgLines = h.Handle(msg.Subject, c, msgLines) - case "network_interface", "network_interfaces": - var h NetworkInterface - msgLines = h.Handle(msg.Subject, c, msgLines) - case "storage_account", "storage_accounts": - var h StorageAccount - msgLines = h.Handle(msg.Subject, c, msgLines) - case "storage_container", "storage_containers": - var h StorageContainer - msgLines = h.Handle(msg.Subject, c, msgLines) - case "virtual_machine", "virtual_machines": - var h VirtualMachine - msgLines = h.Handle(msg.Subject, c, msgLines) - case "lb", "lbs": - var h Lb - msgLines = h.Handle(msg.Subject, c, msgLines) - case "lb_rule", "lb_rules": - var h LbRule - msgLines = h.Handle(msg.Subject, c, msgLines) - case "lb_probe", "lb_probes": - var h LbProbe - msgLines = h.Handle(msg.Subject, c, msgLines) - case "lb_backend_address_pool", "lb_backend_address_pools": - var h LbProbe - msgLines = h.Handle(msg.Subject, c, msgLines) - case "sql_server", "sql_servers": - var h SQLServer - msgLines = h.Handle(msg.Subject, c, msgLines) - case "local_network_gateway", "local_network_gateways": - var h LocalNetworkGateway - msgLines = h.Handle(msg.Subject, c, msgLines) - case "security_group", "security_groups": - var h NetworkSecurityGroup - msgLines = h.Handle(msg.Subject, c, msgLines) - case "sql_database", "sql_databases": - var h SQLDatabase - msgLines = h.Handle(msg.Subject, c, msgLines) - case "sql_firewall_rule", "sql_firewall_rules": - var h SQLFirewallRule - msgLines = h.Handle(msg.Subject, c, msgLines) - default: - log.Println("unsupported: " + msg.Subject) - } - for _, v := range msgLines { - publishMessage(c.getServicePart(), &v) - } -} - -func genericErrorMessageHandler(components []interface{}, cType, cAction string) (lines []Message) { - for _, c := range components { - component := c.(map[string]interface{}) - if component["status"].(string) == "errored" { - name := component["name"].(string) - msg := component["error"].(string) - msg = strings.Replace(msg, ":", " -", -1) - line := cType + " " + name + " " + cAction + " failed with: \n" + msg - lines = append(lines, Message{Body: line, Level: "ERROR"}) - } - } - - return lines -} diff --git a/main.go b/main.go index 1abe54c..bdc2fc2 100644 --- a/main.go +++ b/main.go @@ -13,8 +13,8 @@ import ( "github.com/r3labs/sse" ) -var n *nats.Conn -var s *sse.Server +var nc *nats.Conn +var ss *sse.Server var host string var port string var secret string @@ -22,71 +22,20 @@ var err error func main() { setup() - defer n.Close() + defer nc.Close() // Create new SSE server - s = sse.New() - s.AutoStream = true - s.EncodeBase64 = true - defer s.Close() + ss = sse.New() + ss.AutoStream = true + ss.EncodeBase64 = true + defer ss.Close() // Create new HTTP Server and add the route handler mux := http.NewServeMux() mux.HandleFunc("/events", authMiddleware) - // Start nats handler, subscribe to all events related with the monitor - _, err = n.Subscribe("monitor.user", natsHandler) - if err != nil { - log.Println(err) - return - } - _, err = n.Subscribe("service.create", natsHandler) - if err != nil { - log.Println(err) - return - } - _, err = n.Subscribe("service.delete", natsHandler) - if err != nil { - log.Println(err) - return - } - _, err = n.Subscribe("service.create.done", natsHandler) - if err != nil { - log.Println(err) - return - } - _, err = n.Subscribe("service.create.error", natsHandler) - if err != nil { - log.Println(err) - return - } - _, err = n.Subscribe("service.delete.done", natsHandler) - if err != nil { - log.Println(err) - return - } - _, err = n.Subscribe("service.delete.error", natsHandler) - if err != nil { - log.Println(err) - return - } - _, err = n.Subscribe("service.import.done", natsHandler) - if err != nil { - log.Println(err) - return - } - _, err = n.Subscribe("service.import.error", natsHandler) - if err != nil { - log.Println(err) - return - } - - _, err = n.Subscribe("*.*.*", genericHandler) - if err != nil { - log.Println(err) - return - } - _, err = n.Subscribe("*.*.*.*", genericHandler) + // Subscribe to subjects + _, err = nc.Subscribe(">", natsHandler) if err != nil { log.Println(err) return diff --git a/main_test.go b/main_test.go index 5bc0d53..beacfbb 100644 --- a/main_test.go +++ b/main_test.go @@ -33,43 +33,43 @@ func TestMain(t *testing.T) { Convey("Given a new server", t, func() { // New Server - s = sse.New() - defer s.Close() + ss = sse.New() + defer ss.Close() mux := http.NewServeMux() - mux.HandleFunc("/events", s.HTTPHandler) + mux.HandleFunc("/events", ss.HTTPHandler) hs := httptest.NewServer(mux) url := hs.URL + "/events" Convey("When listening for NATS messages", func() { - createEvents := []string{"service.create", "service.delete"} + createEvents := []string{"service.create", "service.delete", "service.import"} for _, event := range createEvents { Convey("On receiving "+event, func() { - msg := nats.Msg{Subject: event, Data: []byte(`{"service": "test"}`)} + msg := nats.Msg{Subject: event, Data: []byte(`{"id": "test"}`)} natsHandler(&msg) time.Sleep(time.Millisecond * 10) Convey("It should create a stream for the service", func() { - So(s.StreamExists("test"), ShouldBeTrue) + So(ss.StreamExists("test"), ShouldBeTrue) }) }) } - deleteEvents := []string{"service.create.done", "service.delete.done", "service.create.error", "service.delete.error"} + deleteEvents := []string{"service.create.done", "service.delete.done", "service.import.done", "service.create.error", "service.delete.error", "service.import.error"} for _, event := range deleteEvents { - s.CreateStream("test") + ss.CreateStream("test") time.Sleep(time.Millisecond * 10) Convey("On receiving "+event, func() { - msg := nats.Msg{Subject: event, Data: []byte(`{"service": "test"}`)} + msg := nats.Msg{Subject: event, Data: []byte(`{"id": "test"}`)} natsHandler(&msg) time.Sleep(time.Millisecond * 1500) Convey("It should remove the services stream", func() { - So(s.StreamExists("test"), ShouldBeFalse) + So(ss.StreamExists("test"), ShouldBeFalse) }) }) @@ -77,26 +77,26 @@ func TestMain(t *testing.T) { Convey("On receiving an unknown message", func() { // Clean server - s.RemoveStream("test") + ss.RemoveStream("test") time.Sleep(time.Millisecond * 10) - msg := nats.Msg{Subject: "test.event", Data: []byte(`{"service": "test"}`)} + msg := nats.Msg{Subject: "test.event", Data: []byte(`{"id": "test"}`)} natsHandler(&msg) Convey("It should not create a stream", func() { - So(s.StreamExists("test"), ShouldBeFalse) + So(ss.StreamExists("test"), ShouldBeFalse) }) }) - Convey("When receiving monitor.user", func() { - testEvent := `{"service": "test", "messages":[{"body": "test", "color": "blue"}]}` - msg := nats.Msg{Subject: "monitor.user", Data: []byte(testEvent)} + Convey("When receiving component event network.create.aws.done", func() { + testEvent := `{"service": "test", "name": "network"}` + msg := nats.Msg{Subject: "network.create.aws.done", Data: []byte(testEvent)} Convey("And a stream exists", func() { rcv := make(chan *sse.Event) cl := sse.NewClient(url) - s.CreateStream("test") + ss.CreateStream("test") time.Sleep(time.Millisecond * 10) go func() { @@ -107,14 +107,19 @@ func TestMain(t *testing.T) { Convey("It should publish a message to the stream", func() { natsHandler(&msg) - event, err := wait(rcv, time.Millisecond*100) + for { + event, err := wait(rcv, time.Millisecond*100) + So(err, ShouldBeNil) - So(err, ShouldBeNil) - So(string(event.Data), ShouldEqual, `{"body":"test","level":""}`) + if len(event.Data) > 0 { + So(string(event.Data), ShouldEqual, `{"_component_id":"","_subject":"network.create.aws.done","_component":"","_state":"","_action":"","_provider":"","name":"network","service":"test"}`) + break + } + } }) }) - s.RemoveStream("test") + ss.RemoveStream("test") Convey("And a stream doesn't exist", func() { rcv := make(chan *sse.Event) @@ -122,19 +127,9 @@ func TestMain(t *testing.T) { time.Sleep(time.Millisecond * 10) - go func() { - _ = cl.SubscribeChan("test", rcv) - }() - time.Sleep(time.Millisecond * 10) - - Convey("It should not publish a message to the stream", func() { - natsHandler(&msg) - event, err := wait(rcv, time.Millisecond*100) - - So(err, ShouldBeNil) - if err != nil { - So(string(event.Data), ShouldNotEqual, `{"body":"test","color":"blue"}`) - } + Convey("It should error when connecting to the stream", func() { + err := cl.SubscribeChan("test", rcv) + So(err, ShouldNotBeNil) }) }) }) diff --git a/nats.go b/nats.go index fc58b98..1f590c7 100644 --- a/nats.go +++ b/nats.go @@ -6,43 +6,33 @@ package main import ( "github.com/nats-io/nats" - "github.com/r3labs/sse" - "log" - "time" + "github.com/r3labs/pattern" ) func natsHandler(msg *nats.Msg) { - var notification Notification - if err := processNotification(¬ification, msg); err != nil { - return + var services = []string{ + "service.create", + "service.create.*", + "service.delete", + "service.delete.*", + "service.import", + "service.import.*", + } + var components = []string{ + "*.create.*", + "*.create.*.*", + "*.update.*", + "*.update.*.*", + "*.delete.*", + "*.delete.*.*", + "*.find.*", + "*.find.*.*", } - switch msg.Subject { - case "monitor.user": - // Publish messages to subscribers - for _, nm := range notification.Messages { - publishMessage(notification.getServiceID(), &nm) - } - case "service.create", "service.delete": - var handler Service - // Create a new stream - log.Println("Creating stream for", notification.getServiceID()) - s.CreateStream(notification.getServiceID()) - lines := handler.Handle(msg.Subject, notification.Messages) - for _, nm := range lines { - publishMessage(notification.getServiceID(), &nm) - } - case "service.create.done", "service.create.error", "service.delete.done", "service.delete.error", "service.import.done", "service.import.error": - var handler Service - lines := handler.Handle(msg.Subject, notification.Messages) - for _, nm := range lines { - publishMessage(notification.getServiceID(), &nm) - } - time.Sleep(10 * time.Millisecond) - // Remove a new stream when the build completes - log.Println("Closing stream for", notification.getServiceID()) - go func(s *sse.Server) { - s.RemoveStream(notification.getServiceID()) - }(s) + switch { + case pattern.Match(msg.Subject, services...): + processService(msg) + case pattern.Match(msg.Subject, components...): + processComponent(msg) } } diff --git a/notification.go b/notification.go deleted file mode 100644 index b544df0..0000000 --- a/notification.go +++ /dev/null @@ -1,57 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -package main - -import ( - "encoding/json" - "log" - "strings" - - "github.com/nats-io/nats" - "github.com/r3labs/sse" -) - -// Messages holds a collection of the type Message -type Messages []Message - -// Message stores the data of a notification -type Message struct { - Body string `json:"body"` - Level string `json:"level"` -} - -// Notification stores any user output sent from the FSM -type Notification struct { - ID string `json:"id"` - Service string `json:"service"` - Messages Messages `json:"messages"` -} - -func (n *Notification) getServiceID() string { - var pieces []string - - if n.Service != "" { - pieces = strings.Split(n.Service, "-") - } else { - pieces = strings.Split(n.ID, "-") - } - - return pieces[len(pieces)-1] -} - -func processNotification(notification *Notification, msg *nats.Msg) error { - return json.Unmarshal(msg.Data, ¬ification) -} - -func publishMessage(service string, msg *Message) { - data, err := json.Marshal(msg) - - if err != nil { - log.Println("Could not encode message: ") - log.Println(err) - } else { - s.Publish(service, &sse.Event{Data: data}) - } -} diff --git a/service.go b/service.go new file mode 100644 index 0000000..336139b --- /dev/null +++ b/service.go @@ -0,0 +1,64 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package main + +import ( + "encoding/json" + "log" + "strings" + "time" + + "github.com/nats-io/nats" + "github.com/r3labs/sse" +) + +// Service : holds service values +type Service struct { + ID string `json:"id"` + Name string `json:"name"` + Subject string `json:"_subject"` + Changes []Component `json:"changes"` +} + +func processService(msg *nats.Msg) { + var s Service + + s.Subject = msg.Subject + + if err := json.Unmarshal(msg.Data, &s); err != nil { + log.Println(err) + return + } + + id := s.getID() + + data, err := json.Marshal(s) + if err != nil { + log.Println(err) + return + } + + switch msg.Subject { + case "service.create", "service.delete", "service.import": + log.Println("Creating stream: ", id) + ss.CreateStream(id) + ss.Publish(id, &sse.Event{Data: data}) + case "service.create.done", "service.create.error", "service.delete.done", "service.delete.error", "service.import.done", "service.import.error": + ss.Publish(id, &sse.Event{Data: data}) + go func(ss *sse.Server) { + // Wait for any late connecting clients before closing stream + time.Sleep(1 * time.Second) + log.Println("Closing stream: ", id) + ss.RemoveStream(id) + }(ss) + } +} + +func (s *Service) getID() string { + var pieces []string + pieces = strings.Split(s.ID, "-") + + return pieces[len(pieces)-1] +} diff --git a/setup.go b/setup.go index d9bef62..53ff013 100644 --- a/setup.go +++ b/setup.go @@ -21,7 +21,7 @@ type monitorConfig struct { func setup() { var err error // Open Nats connection - n, err = nats.Connect(os.Getenv("NATS_URI")) + nc, err = nats.Connect(os.Getenv("NATS_URI")) if err != nil { log.Println("Could not connect to nats") return @@ -29,8 +29,8 @@ func setup() { secret = os.Getenv("JWT_SECRET") if secret == "" { - token, err := n.Request("config.get.jwt_token", []byte(""), 1*time.Second) - if err != nil { + token, aerr := nc.Request("config.get.jwt_token", []byte(""), 1*time.Second) + if aerr != nil { panic("Can't get jwt_config config") } @@ -38,7 +38,7 @@ func setup() { } cfg := monitorConfig{} - msg, err := n.Request("config.get.monitor", []byte(""), 1*time.Second) + msg, err := nc.Request("config.get.monitor", []byte(""), 1*time.Second) if err != nil { panic("Can't get monitor config") }