diff --git a/.linter.conf b/.linter.conf new file mode 100644 index 0000000..223c4ae --- /dev/null +++ b/.linter.conf @@ -0,0 +1,4 @@ +{ + "DisableAll": true, + "Enable": ["golint", "vet", "errcheck"] +} diff --git a/Makefile b/Makefile index cc93278..766ec29 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,8 @@ deps: dev-deps dev-deps: go get github.com/smartystreets/goconvey/convey - go get github.com/golang/lint/golint + go get github.com/alecthomas/gometalinter + gometalinter --install cover: go test -v ./... --cover @@ -21,5 +22,5 @@ build: go build -v ./... lint: - golint ./... - go vet ./... + gometalinter --config .linter.conf + diff --git a/c_bootstrap.go b/c_bootstrap.go index 2dc2911..10583a8 100644 --- a/c_bootstrap.go +++ b/c_bootstrap.go @@ -4,35 +4,32 @@ package main +// Bootstrap : .. type Bootstrap struct { } +// Handle : .. func (n *Bootstrap) Handle(subject string, components []interface{}, lines []Message) []Message { switch subject { - case "bootstraps.create": - lines = append(lines, Message{Body: "Running bootstraps:", Level: "INFO"}) - case "bootstraps.create.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Bootstrap ran", Level: "INFO"}) - case "bootstraps.create.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Bootstrap failed", Level: "INFO"}) + case "bootstrap.create.done", "bootstrap.create.error": + lines = n.getSingleDetail(components, "Bootstrap ran") } + return lines } -func (n *Bootstrap) getDetails(components []interface{}) (lines []Message) { - for _, v := range components { - r := v.(map[string]interface{}) - name, _ := r["name"].(string) - status, _ := r["status"].(string) - lines = append(lines, Message{Body: " - " + name, Level: ""}) - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - if status == "errored" { - err, _ := r["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) - } +func (n *Bootstrap) getSingleDetail(v interface{}, prefix string) (lines []Message) { + r := v.(map[string]interface{}) + name, _ := r["name"].(string) + if prefix != "" { + name = prefix + " " + name + } + status, _ := r["status"].(string) + lines = append(lines, Message{Body: " - " + name, Level: ""}) + lines = append(lines, Message{Body: " Status : " + status, Level: ""}) + if status == "errored" { + err, _ := r["error"].(string) + lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) } - return lines } diff --git a/c_ebs_volume.go b/c_ebs_volume.go index ddb6898..1aa3404 100644 --- a/c_ebs_volume.go +++ b/c_ebs_volume.go @@ -4,54 +4,39 @@ package main +// EBSVolume : ... type EBSVolume struct { } -func (v *EBSVolume) Handle(subject string, components []interface{}, lines []Message) []Message { +// Handle : ... +func (n *EBSVolume) Handle(subject string, components []interface{}, lines []Message) []Message { switch subject { - case "ebs_volumes.create": - return append(lines, Message{Body: "Creating ebs volumes:", Level: "INFO"}) - case "ebs_volumes.create.done": - lines = v.getDetails(components) - return append(lines, Message{Body: "EBS volumes successfully created", Level: "INFO"}) - case "ebs_volumes.create.error": - lines = v.getDetails(components) - return append(lines, Message{Body: "EBS volumes creation failed", Level: "INFO"}) - case "ebs_volumes.delete": - return append(lines, Message{Body: "Deleting ebs volumes:", Level: "INFO"}) - case "ebs_volumes.delete.done": - lines = v.getDetails(components) - return append(lines, Message{Body: "EBS volumes deleted", Level: "INFO"}) - case "ebs_volumes.delete.error": - lines = v.getDetails(components) - return append(lines, Message{Body: "EBS volumes deletion failed", Level: "INFO"}) - case "ebs_volumes.find": - return append(lines, Message{Body: "Importing ebs volumes:", Level: "INFO"}) - case "ebs_volumes.find.done": - lines = v.getDetails(components) - return append(lines, Message{Body: "EBS volumes successfully imported", Level: "INFO"}) - case "ebs_volumes.find.error": - lines = v.getDetails(components) - return append(lines, Message{Body: "EBS volumes import failed", Level: "INFO"}) + case "ebs_volume.create.done", "ebs_volume.create.error": + lines = n.getSingleDetail(components, "Created EBS volume ") + case "ebs_volume.delete.done", "ebs_volume.delete.error": + lines = n.getSingleDetail(components, "Deleted EBS volume ") + case "ebs_volume.find.done", "ebs_volume.find.error": + lines = n.getSingleDetail(components, "Found EBS volume ") } return lines } -func (v *EBSVolume) getDetails(components []interface{}) (lines []Message) { - for _, v := range components { - r := v.(map[string]interface{}) - name, _ := r["name"].(string) - status, _ := r["status"].(string) - lines = append(lines, Message{Body: " - " + name, Level: ""}) - id, _ := r["volume_aws_id"].(string) - if id != "" { - lines = append(lines, Message{Body: " AWS ID : " + id, Level: ""}) - } - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - if status == "errored" { - err, _ := r["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) - } +func (n *EBSVolume) getSingleDetail(v interface{}, prefix string) (lines []Message) { + r := v.(map[string]interface{}) + name, _ := r["name"].(string) + if prefix != "" { + name = prefix + " " + name + } + status, _ := r["status"].(string) + lines = append(lines, Message{Body: " - " + name, Level: ""}) + id, _ := r["volume_aws_id"].(string) + if id != "" { + lines = append(lines, Message{Body: " AWS ID : " + id, Level: ""}) + } + lines = append(lines, Message{Body: " Status : " + status, Level: ""}) + if status == "errored" { + err, _ := r["error"].(string) + lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) } return lines } diff --git a/c_elb.go b/c_elb.go index dd77508..b166732 100644 --- a/c_elb.go +++ b/c_elb.go @@ -4,66 +4,46 @@ package main +// ELB : ... type ELB struct { } +// Handle : ... func (n *ELB) Handle(subject string, components []interface{}, lines []Message) []Message { switch subject { - case "elbs.create": - lines = append(lines, Message{Body: "Creating ELBs:", Level: "INFO"}) - case "elbs.create.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "ELBs created", Level: "INFO"}) - case "elbs.create.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "ELBs creation failed", Level: "INFO"}) - case "elbs.update": - return append(lines, Message{Body: "Updating ELBs:", Level: "INFO"}) - case "elbs.update.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "ELBs updated", Level: "INFO"}) - case "elbs.update.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "ELBs modification failed", Level: "INFO"}) - case "elbs.delete": - return append(lines, Message{Body: "Deleting ELBs:", Level: "INFO"}) - case "elbs.delete.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "ELBs deleted", Level: "INFO"}) - case "elbs.delete.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "ELBs deletion failed", Level: "INFO"}) - case "elbs.find": - lines = append(lines, Message{Body: "Importing ELBs:", Level: "INFO"}) - case "elbs.find.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "ELBs imported", Level: "INFO"}) - case "elbs.find.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "ELBs import failed", Level: "INFO"}) + case "elb.create.done", "elb.create.error": + lines = n.getSingleDetail(components, "Created ELB") + case "elb.update.done", "elb.update.error": + lines = n.getSingleDetail(components, "Updated ELB") + case "elb.delete.done", "elb.delete.error": + lines = n.getSingleDetail(components, "Deleted ELB") + case "elb.find.done", "elb.find.error": + lines = n.getSingleDetail(components, "Found ELB") + } return lines } -func (n *ELB) getDetails(components []interface{}) (lines []Message) { - for _, v := range components { - r := v.(map[string]interface{}) - name, _ := r["name"].(string) - status, _ := r["status"].(string) - lines = append(lines, Message{Body: " - " + name, Level: ""}) - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - if r["dns_name"] != nil { - dnsName, _ := r["dns_name"].(string) - if dnsName != "" { - lines = append(lines, Message{Body: " DNS : " + dnsName, Level: ""}) - } - } - lines = append(lines) - if status == "errored" { - err, _ := r["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) +func (n *ELB) getSingleDetail(v interface{}, prefix string) (lines []Message) { + r := v.(map[string]interface{}) + name, _ := r["name"].(string) + if prefix != "" { + name = prefix + " " + name + } + status, _ := r["status"].(string) + lines = append(lines, Message{Body: " - " + name, Level: ""}) + lines = append(lines, Message{Body: " Status : " + status, Level: ""}) + if r["dns_name"] != nil { + dnsName, _ := r["dns_name"].(string) + if dnsName != "" { + lines = append(lines, Message{Body: " DNS : " + dnsName, Level: ""}) } } + lines = append(lines) + if status == "errored" { + err, _ := r["error"].(string) + lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) + } return lines } diff --git a/c_execution.go b/c_execution.go index d8441bd..5d25f67 100644 --- a/c_execution.go +++ b/c_execution.go @@ -4,35 +4,31 @@ package main +// Execution : ... type Execution struct { } +// Handle : ... func (n *Execution) Handle(subject string, components []interface{}, lines []Message) []Message { switch subject { - case "executions.create": - lines = append(lines, Message{Body: "Running executions:", Level: "INFO"}) - case "executions.create.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Executions ran", Level: "INFO"}) - case "executions.create.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Executions failed", Level: "INFO"}) + case "execution.create.done", "execution.create.error": + lines = n.getSingleDetail(components, "Ran execution") } return lines } -func (n *Execution) getDetails(components []interface{}) (lines []Message) { - for _, v := range components { - r := v.(map[string]interface{}) - name, _ := r["name"].(string) - status, _ := r["status"].(string) - lines = append(lines, Message{Body: " - " + name, Level: ""}) - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - if status == "errored" { - err, _ := r["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) - } +func (n *Execution) getSingleDetail(v interface{}, prefix string) (lines []Message) { + r := v.(map[string]interface{}) + name, _ := r["name"].(string) + if prefix != "" { + name = prefix + " " + name + } + status, _ := r["status"].(string) + lines = append(lines, Message{Body: " - " + name, Level: ""}) + lines = append(lines, Message{Body: " Status : " + status, Level: ""}) + if status == "errored" { + err, _ := r["error"].(string) + lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) } - return lines } diff --git a/c_firewall.go b/c_firewall.go index bfb5ead..badbeca 100644 --- a/c_firewall.go +++ b/c_firewall.go @@ -4,59 +4,37 @@ package main +// Firewall : ... type Firewall struct { } +// Handle : ... func (n *Firewall) Handle(subject string, components []interface{}, lines []Message) []Message { switch subject { - case "firewalls.create": - lines = append(lines, Message{Body: "Creating firewalls:", Level: "INFO"}) - case "firewalls.create.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Firewalls created", Level: "INFO"}) - case "firewalls.create.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Firewalls creation failed", Level: "INFO"}) - case "firewalls.update": - return append(lines, Message{Body: "Updating firewalls:", Level: "INFO"}) - case "firewalls.update.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Firewalls updated", Level: "INFO"}) - case "firewalls.update.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Firewalls modification failed", Level: "INFO"}) - case "firewalls.delete": - return append(lines, Message{Body: "Deleting firewalls:", Level: "INFO"}) - case "firewalls.delete.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Firewalls deleted", Level: "INFO"}) - case "firewalls.delete.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Firewalls deletion failed", Level: "INFO"}) - case "firewalls.find": - lines = append(lines, Message{Body: "Importing firewalls:", Level: "INFO"}) - case "firewalls.find.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Firewalls imported", Level: "INFO"}) - case "firewalls.find.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Firewalls import failed", Level: "INFO"}) + case "firewall.create.done", "firewall.create.error": + lines = n.getSingleDetail(components, "Firewall created") + case "firewall.update.done", "firewall.update.error": + lines = n.getSingleDetail(components, "Firewall updated") + case "firewall.delete.done", "firewall.delete.error": + lines = n.getSingleDetail(components, "Firewall deleted") + case "firewall.find.done", "firewall.find.error": + lines = n.getSingleDetail(components, "Firewall found") } return lines } -func (n *Firewall) getDetails(components []interface{}) (lines []Message) { - for _, v := range components { - r := v.(map[string]interface{}) - name, _ := r["name"].(string) - status, _ := r["status"].(string) - lines = append(lines, Message{Body: " - " + name, Level: ""}) - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - if status == "errored" { - err, _ := r["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) - } +func (n *Firewall) getSingleDetail(v interface{}, prefix string) (lines []Message) { + r := v.(map[string]interface{}) + name, _ := r["name"].(string) + if prefix != "" { + name = prefix + " " + name + } + status, _ := r["status"].(string) + lines = append(lines, Message{Body: " - " + name, Level: ""}) + lines = append(lines, Message{Body: " Status : " + status, Level: ""}) + if status == "errored" { + err, _ := r["error"].(string) + lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) } - return lines } diff --git a/c_instance.go b/c_instance.go index d71a4eb..8f3a699 100644 --- a/c_instance.go +++ b/c_instance.go @@ -4,69 +4,47 @@ package main +// Instance : ... type Instance struct { } +// Handle : ... func (n *Instance) Handle(subject string, components []interface{}, lines []Message) []Message { switch subject { - case "instances.create": - lines = append(lines, Message{Body: "Creating instances:", Level: "INFO"}) - case "instances.create.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Instances successfully created", Level: "INFO"}) - case "instances.create.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Instances creation failed", Level: "INFO"}) - case "instances.update": - return append(lines, Message{Body: "Updating instances:", Level: "INFO"}) - case "instances.update.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Instances successfully updated", Level: "INFO"}) - case "instances.update.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Instances modification failed", Level: "INFO"}) - case "instances.delete": - return append(lines, Message{Body: "Deleting instances:", Level: "INFO"}) - case "instances.delete.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Instances deleted", Level: "INFO"}) - case "instances.delete.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Instances deletion failed", Level: "INFO"}) - case "instances.find": - lines = append(lines, Message{Body: "Importing instances:", Level: "INFO"}) - case "instances.find.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Instances successfully imported", Level: "INFO"}) - case "instances.find.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Instances import failed", Level: "INFO"}) + case "instance.create.done", "instance.create.error": + lines = n.getSingleDetail(components, "Instance created") + case "instance.update.done", "instance.update.error": + lines = n.getSingleDetail(components, "Instance udpated") + case "instance.delete.done", "instance.delete.error": + lines = n.getSingleDetail(components, "Instance delete") + case "instance.find.done", "instance.find.error": + lines = n.getSingleDetail(components, "Instance find") } return lines } -func (n *Instance) getDetails(components []interface{}) (lines []Message) { - for _, v := range components { - r := v.(map[string]interface{}) - ip, _ := r["ip"].(string) - name, _ := r["name"].(string) - status, _ := r["status"].(string) - lines = append(lines, Message{Body: " - " + name, Level: ""}) - lines = append(lines, Message{Body: " IP : " + ip, Level: ""}) - public_ip, _ := r["public_ip"].(string) - if public_ip != "" { - lines = append(lines, Message{Body: " PUBLIC IP : " + public_ip, Level: ""}) - } - id, _ := r["instance_aws_id"].(string) - if id != "" { - lines = append(lines, Message{Body: " AWS ID : " + id, Level: ""}) - } - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - if status == "errored" { - err, _ := r["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) - } +func (n *Instance) getSingleDetail(v interface{}, prefix string) (lines []Message) { + r := v.(map[string]interface{}) + ip, _ := r["ip"].(string) + name, _ := r["name"].(string) + if prefix != "" { + name = prefix + " " + name + } + status, _ := r["status"].(string) + lines = append(lines, Message{Body: " - " + name, Level: ""}) + lines = append(lines, Message{Body: " IP : " + ip, Level: ""}) + publicIP, _ := r["public_ip"].(string) + if publicIP != "" { + lines = append(lines, Message{Body: " PUBLIC IP : " + publicIP, Level: ""}) + } + id, _ := r["instance_aws_id"].(string) + if id != "" { + lines = append(lines, Message{Body: " AWS ID : " + id, Level: ""}) + } + lines = append(lines, Message{Body: " Status : " + status, Level: ""}) + if status == "errored" { + err, _ := r["error"].(string) + lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) } - return lines } diff --git a/c_nat.go b/c_nat.go index 37a0496..15608be 100644 --- a/c_nat.go +++ b/c_nat.go @@ -4,59 +4,37 @@ package main +// Nat : ... type Nat struct { } +// Handle : ... func (n *Nat) Handle(subject string, components []interface{}, lines []Message) []Message { switch subject { - case "nats.create": - lines = append(lines, Message{Body: "Creating nats:", Level: "INFO"}) - case "nats.create.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Nats created", Level: "INFO"}) - case "nats.create.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Nats creation failed", Level: "INFO"}) - case "nats.update": - return append(lines, Message{Body: "Updating nats:", Level: "INFO"}) - case "nats.update.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Nats updated", Level: "INFO"}) - case "nats.update.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Nats modification failed", Level: "INFO"}) - case "nats.delete": - return append(lines, Message{Body: "Deleting nats:", Level: "INFO"}) - case "nats.delete.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Nats deleted", Level: "INFO"}) - case "nats.delete.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Nats deletion failed", Level: "INFO"}) - case "nats.find": - lines = append(lines, Message{Body: "Importing nats:", Level: "INFO"}) - case "nats.find.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Nats imported", Level: "INFO"}) - case "nats.find.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Nats import failed", Level: "INFO"}) + case "nat.create.done", "nat.create.error": + lines = n.getSingleDetail(components, "Nat created") + case "nat.update.done", "nat.update.error": + lines = n.getSingleDetail(components, "Nat updated") + case "nat.delete.done", "nat.delete.error": + lines = n.getSingleDetail(components, "Nat deleted") + case "nat.find.done", "nat.find.error": + lines = n.getSingleDetail(components, "Nat created") } return lines } -func (n *Nat) getDetails(components []interface{}) (lines []Message) { - for _, v := range components { - r := v.(map[string]interface{}) - name, _ := r["name"].(string) - status, _ := r["status"].(string) - lines = append(lines, Message{Body: " - " + name, Level: ""}) - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - if status == "errored" { - err, _ := r["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) - } +func (n *Nat) getSingleDetail(v interface{}, prefix string) (lines []Message) { + r := v.(map[string]interface{}) + name, _ := r["name"].(string) + if prefix != "" { + name = prefix + " " + name + } + status, _ := r["status"].(string) + lines = append(lines, Message{Body: " - " + name, Level: ""}) + lines = append(lines, Message{Body: " Status : " + status, Level: ""}) + if status == "errored" { + err, _ := r["error"].(string) + lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) } - return lines } diff --git a/c_network.go b/c_network.go index b7f6b3c..a3bdd83 100644 --- a/c_network.go +++ b/c_network.go @@ -4,56 +4,41 @@ package main +// Network : ... type Network struct { } +// Handle : ... func (n *Network) Handle(subject string, components []interface{}, lines []Message) []Message { switch subject { - case "networks.create": - return append(lines, Message{Body: "Creating networks:", Level: "INFO"}) - case "networks.create.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Networks successfully created", Level: "INFO"}) - case "networks.create.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Networks creation failed", Level: "INFO"}) - case "networks.delete": - return append(lines, Message{Body: "Deleting networks:", Level: "INFO"}) - case "networks.delete.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Networks deleted", Level: "INFO"}) - case "networks.delete.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Networks deletion failed", Level: "INFO"}) - case "networks.find": - return append(lines, Message{Body: "Importing networks:", Level: "INFO"}) - case "networks.find.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Networks successfully imported", Level: "INFO"}) - case "networks.find.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Networks import failed", Level: "INFO"}) + case "network.create.done", "network.create.error": + lines = n.getSingleDetail(components, "Network created") + case "network.delete.done", "network.delete.error": + lines = n.getSingleDetail(components, "Network deleted") + case "network.find.done", "network.find.error": + lines = n.getSingleDetail(components, "Network found") } return lines } -func (n *Network) getDetails(components []interface{}) (lines []Message) { - for _, v := range components { - r := v.(map[string]interface{}) - ip, _ := r["range"].(string) - name, _ := r["name"].(string) - status, _ := r["status"].(string) - lines = append(lines, Message{Body: " - " + name, Level: ""}) - lines = append(lines, Message{Body: " IP : " + ip, Level: ""}) - id, _ := r["network_aws_id"].(string) - if id != "" { - lines = append(lines, Message{Body: " AWS ID : " + id, Level: ""}) - } - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - if status == "errored" { - err, _ := r["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) - } +func (n *Network) getSingleDetail(v interface{}, prefix string) (lines []Message) { + r := v.(map[string]interface{}) + ip, _ := r["range"].(string) + name, _ := r["name"].(string) + if prefix != "" { + name = prefix + " " + name + } + status, _ := r["status"].(string) + lines = append(lines, Message{Body: " - " + name, Level: ""}) + lines = append(lines, Message{Body: " IP : " + ip, Level: ""}) + id, _ := r["network_aws_id"].(string) + if id != "" { + lines = append(lines, Message{Body: " AWS ID : " + id, Level: ""}) + } + lines = append(lines, Message{Body: " Status : " + status, Level: ""}) + if status == "errored" { + err, _ := r["error"].(string) + lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) } return lines } diff --git a/c_rds_cluster.go b/c_rds_cluster.go index d78e378..cca46c0 100644 --- a/c_rds_cluster.go +++ b/c_rds_cluster.go @@ -4,62 +4,40 @@ package main +// RDSCluster : ... type RDSCluster struct { } +// Handle : ... func (n *RDSCluster) Handle(subject string, components []interface{}, lines []Message) []Message { switch subject { - case "rds_clusters.create": - lines = append(lines, Message{Body: "Creating rds clusters:", Level: "INFO"}) - case "rds_clusters.create.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS clusters created", Level: "INFO"}) - case "rds_clusters.create.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS clusters creation failed", Level: "INFO"}) - case "rds_clusters.update": - lines = append(lines, Message{Body: "Updating rds clusters:", Level: "INFO"}) - case "rds_clusters.update.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS clusters modified", Level: "INFO"}) - case "rds_clusters.update.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS clusters modification failed", Level: "INFO"}) - case "rds_clusters.delete": - return append(lines, Message{Body: "Deleting rds clusters:", Level: "INFO"}) - case "rds_clusters.delete.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS clusters deleted", Level: "INFO"}) - case "rds_clusters.delete.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS clusters deletion failed", Level: "INFO"}) - case "rds_clusters.find": - lines = append(lines, Message{Body: "Importing rds clusters:", Level: "INFO"}) - case "rds_clusters.find.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS clusters imported", Level: "INFO"}) - case "rds_clusters.find.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS clusters import failed", Level: "INFO"}) + case "rds_cluster.create.done", "rds_cluster.create.error": + lines = n.getSingleDetail(components, "RDS cluster created") + case "rds_cluster.update.done", "rds_cluster.update.error": + lines = n.getSingleDetail(components, "RDS cluster updated") + case "rds_cluster.delete.done", "rds_cluster.delete.error": + lines = n.getSingleDetail(components, "RDS cluster deleted") + case "rds_cluster.find.done", "rds_cluster.find.error": + lines = n.getSingleDetail(components, "RDS cluster found") } return lines } -func (n *RDSCluster) getDetails(components []interface{}) (lines []Message) { - for _, v := range components { - r := v.(map[string]interface{}) - name, _ := r["name"].(string) - engine, _ := r["engine"].(string) - endpoint, _ := r["endpoint"].(string) - status, _ := r["status"].(string) - lines = append(lines, Message{Body: " - " + name, Level: ""}) - lines = append(lines, Message{Body: " Engine : " + engine, Level: ""}) - lines = append(lines, Message{Body: " Endpoint : " + endpoint, Level: ""}) - if status == "errored" { - err, _ := r["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) - } +func (n *RDSCluster) getSingleDetail(v interface{}, prefix string) (lines []Message) { + r := v.(map[string]interface{}) + name, _ := r["name"].(string) + if prefix != "" { + name = prefix + " " + name + } + engine, _ := r["engine"].(string) + endpoint, _ := r["endpoint"].(string) + status, _ := r["status"].(string) + lines = append(lines, Message{Body: " - " + name, Level: ""}) + lines = append(lines, Message{Body: " Engine : " + engine, Level: ""}) + lines = append(lines, Message{Body: " Endpoint : " + endpoint, Level: ""}) + if status == "errored" { + err, _ := r["error"].(string) + lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) } - return lines } diff --git a/c_rds_instance.go b/c_rds_instance.go index 4225b0a..55a7faf 100644 --- a/c_rds_instance.go +++ b/c_rds_instance.go @@ -4,63 +4,42 @@ package main +// RDSInstance : ... type RDSInstance struct { } +// Handle : ... func (n *RDSInstance) Handle(subject string, components []interface{}, lines []Message) []Message { switch subject { - case "rds_instances.create": - lines = append(lines, Message{Body: "Creating rds instances:", Level: "INFO"}) - case "rds_instances.create.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS instances created", Level: "INFO"}) - case "rds_instances.create.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS instances creation failed", Level: "INFO"}) - case "rds_instances.update": - lines = append(lines, Message{Body: "Updating rds instances:", Level: "INFO"}) - case "rds_instances.update.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS instances modified", Level: "INFO"}) - case "rds_instances.update.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS instances modification failed", Level: "INFO"}) - case "rds_instances.delete": - return append(lines, Message{Body: "Deleting rds instances:", Level: "INFO"}) - case "rds_instances.delete.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS instances deleted", Level: "INFO"}) - case "rds_instances.delete.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS instances deletion failed", Level: "INFO"}) - case "rds_instances.find": - lines = append(lines, Message{Body: "Importing rds instances:", Level: "INFO"}) - case "rds_instances.find.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS instances imported", Level: "INFO"}) - case "rds_instances.find.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "RDS instances import failed", Level: "INFO"}) + case "rds_instance.create.done", "rds_instance.create.error": + lines = n.getSingleDetail(components, "RDS instance created") + case "rds_instance.udpate.done", "rds_instance.update.error": + lines = n.getSingleDetail(components, "RDS instance updated") + case "rds_instance.delete.done", "rds_instance.delete.error": + lines = n.getSingleDetail(components, "RDS instance deleted") + case "rds_instance.find.done", "rds_instance.find.error": + lines = n.getSingleDetail(components, "RDS instance found") } return lines } -func (n *RDSInstance) getDetails(components []interface{}) (lines []Message) { - for _, v := range components { - r := v.(map[string]interface{}) - name, _ := r["name"].(string) - engine, _ := r["engine"].(string) - cluster, _ := r["cluster"].(string) - endpoint, _ := r["endpoint"].(string) - status, _ := r["status"].(string) - lines = append(lines, Message{Body: " - " + name, 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, _ := r["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) - } +func (n *RDSInstance) getSingleDetail(v interface{}, prefix string) (lines []Message) { + r := v.(map[string]interface{}) + name, _ := r["name"].(string) + if prefix != "" { + name = prefix + " " + name + } + engine, _ := r["engine"].(string) + cluster, _ := r["cluster"].(string) + endpoint, _ := r["endpoint"].(string) + status, _ := r["status"].(string) + lines = append(lines, Message{Body: " - " + name, 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, _ := r["error"].(string) + lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) } return lines diff --git a/c_router.go b/c_router.go index 9aafb02..cad7f48 100644 --- a/c_router.go +++ b/c_router.go @@ -4,43 +4,36 @@ package main +// Router : ... type Router struct { } +// Handle : ... func (n *Router) Handle(subject string, components []interface{}, lines []Message) []Message { switch subject { - case "routers.create": - lines = append(lines, Message{Body: "Creating routers:", Level: "INFO"}) - case "routers.create.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Routers created", Level: "INFO"}) - case "routers.create.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Routers creation failed", Level: "INFO"}) - case "routers.delete": - return append(lines, Message{Body: "Deleting routers:", Level: "INFO"}) - case "routers.delete.done": - return append(lines, Message{Body: "Routers deleted", Level: "INFO"}) - case "routers.delete.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Routers deletion failed", Level: "INFO"}) + + case "router.create.done", "router.create.error": + lines = n.getSingleDetail(components, "Created router") + case "router.delete.done", "router.delete.error": + lines = n.getSingleDetail(components, "Deleted router") } return lines } -func (n *Router) getDetails(components []interface{}) (lines []Message) { - for _, v := range components { - r := v.(map[string]interface{}) - name, _ := r["name"].(string) - status, _ := r["status"].(string) - ip, _ := r["ip"].(string) - lines = append(lines, Message{Body: " - " + name, Level: ""}) - lines = append(lines, Message{Body: " IP : " + ip, Level: ""}) - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - if status == "errored" { - err, _ := r["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) - } +func (n *Router) getSingleDetail(v interface{}, prefix string) (lines []Message) { + r := v.(map[string]interface{}) + name, _ := r["name"].(string) + if prefix != "" { + name = prefix + " " + name + } + status, _ := r["status"].(string) + ip, _ := r["ip"].(string) + lines = append(lines, Message{Body: " - " + name, Level: ""}) + lines = append(lines, Message{Body: " IP : " + ip, Level: ""}) + lines = append(lines, Message{Body: " Status : " + status, Level: ""}) + if status == "errored" { + err, _ := r["error"].(string) + lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) } return lines diff --git a/c_s3.go b/c_s3.go index 789fde0..035bf16 100644 --- a/c_s3.go +++ b/c_s3.go @@ -4,64 +4,42 @@ package main +// S3Bucket : ... type S3Bucket struct { } +// Handle : ... func (n *S3Bucket) Handle(subject string, components []interface{}, lines []Message) []Message { switch subject { - case "s3s.create": - lines = append(lines, Message{Body: "Creating s3 buckets:", Level: "INFO"}) - case "s3s.create.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "S3 buckets created", Level: "INFO"}) - case "s3s.create.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "S3 buckets creation failed", Level: "INFO"}) - case "s3s.update": - lines = append(lines, Message{Body: "Updating s3 buckets:", Level: "INFO"}) - case "s3s.update.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "S3 buckets modified", Level: "INFO"}) - case "s3s.update.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "S3 buckets modification failed", Level: "INFO"}) - case "s3s.delete": - return append(lines, Message{Body: "Deleting s3 buckets:", Level: "INFO"}) - case "s3s.delete.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "S3 buckets deleted", Level: "INFO"}) - case "s3s.delete.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "S3 buckets deletion failed", Level: "INFO"}) - case "s3s.find": - lines = append(lines, Message{Body: "Importing s3 buckets:", Level: "INFO"}) - case "s3s.find.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "S3 buckets imported", Level: "INFO"}) - case "s3s.find.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "S3 buckets import failed", Level: "INFO"}) + case "s3.create.done", "s3.create.error": + lines = n.getSingleDetail(components, "S3 bucket created") + case "s3.update.done", "s3.update.error": + lines = n.getSingleDetail(components, "S3 bucket updated") + case "s3.delete.done", "s3.delete.error": + lines = n.getSingleDetail(components, "S3 bucket deleted") + case "s3.find.done", "s3.find.error": + lines = n.getSingleDetail(components, "S3 bucket imported") } return lines } -func (n *S3Bucket) getDetails(components []interface{}) (lines []Message) { - for _, v := range components { - r := v.(map[string]interface{}) - name, _ := r["name"].(string) - acl, _ := r["acl"].(string) - if acl == "" { - acl = "by grantees" - } - status, _ := r["status"].(string) - lines = append(lines, Message{Body: " - " + name, Level: ""}) - lines = append(lines, Message{Body: " ACL : " + acl, Level: ""}) - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - if status == "errored" { - err, _ := r["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) - } +func (n *S3Bucket) getSingleDetail(v interface{}, prefix string) (lines []Message) { + r := v.(map[string]interface{}) + name, _ := r["name"].(string) + if prefix != "" { + name = prefix + " " + name + } + acl, _ := r["acl"].(string) + if acl == "" { + acl = "by grantees" + } + status, _ := r["status"].(string) + lines = append(lines, Message{Body: " - " + name, Level: ""}) + lines = append(lines, Message{Body: " ACL : " + acl, Level: ""}) + lines = append(lines, Message{Body: " Status : " + status, Level: ""}) + if status == "errored" { + err, _ := r["error"].(string) + lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) } - return lines } diff --git a/c_service.go b/c_service.go index 1142e7b..9455baa 100644 --- a/c_service.go +++ b/c_service.go @@ -4,9 +4,11 @@ package main +// Service : ... type Service struct { } +// Handle : ... func (n *Service) Handle(subject string, lines []Message) []Message { switch subject { case "service.create": diff --git a/c_vpc.go b/c_vpc.go index d9df0f0..a2b7326 100644 --- a/c_vpc.go +++ b/c_vpc.go @@ -4,52 +4,39 @@ package main +// Vpc : ... type Vpc struct { } +// Handle : ... func (n *Vpc) Handle(subject string, components []interface{}, lines []Message) []Message { switch subject { - case "vpcs.create": - lines = append(lines, Message{Body: "Creating Vpc:", Level: "INFO"}) - case "vpcs.create.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Vpc created", Level: "INFO"}) - case "vpcs.create.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Vpc creation failed", Level: "INFO"}) - case "vpcs.delete": - return append(lines, Message{Body: "Deleting vpcs:", Level: "INFO"}) - case "vpcs.delete.done": - return append(lines, Message{Body: "Vpc deleted", Level: "INFO"}) - case "vpcs.delete.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Vpc deletion failed", Level: "INFO"}) - case "vpcs.find": - lines = append(lines, Message{Body: "Importing Vpc:", Level: "INFO"}) - case "vpcs.find.done": - lines = n.getDetails(components) - return append(lines, Message{Body: "Vpc imported", Level: "INFO"}) - case "vpcs.find.error": - lines = n.getDetails(components) - return append(lines, Message{Body: "Vpc import failed", Level: "INFO"}) + case "vpc.create.done", "vpc.create.error": + lines = n.getSingleDetail(components, "VPC created") + case "vpc.update.done", "vpc.update.error": + lines = n.getSingleDetail(components, "VPC udpated") + case "vpc.delete.done", "vpc.delete.error": + lines = n.getSingleDetail(components, "VPC deleted") + case "vpc.find.done", "vpc.find.error": + lines = n.getSingleDetail(components, "VPC Found") } return lines } -func (n *Vpc) getDetails(components []interface{}) (lines []Message) { - for _, v := range components { - r := v.(map[string]interface{}) - id, _ := r["vpc_id"].(string) - subnet, _ := r["vpc_subnet"].(string) - status, _ := r["status"].(string) - lines = append(lines, Message{Body: " - " + id, Level: ""}) - lines = append(lines, Message{Body: " Subnet : " + subnet, Level: ""}) - lines = append(lines, Message{Body: " Status : " + status, Level: ""}) - if status == "errored" { - err, _ := r["error"].(string) - lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) - } +func (n *Vpc) getSingleDetail(v interface{}, prefix string) (lines []Message) { + r := v.(map[string]interface{}) + id, _ := r["vpc_id"].(string) + if prefix != "" { + id = prefix + " " + id + } + subnet, _ := r["vpc_subnet"].(string) + status, _ := r["status"].(string) + lines = append(lines, Message{Body: " - " + id, Level: ""}) + lines = append(lines, Message{Body: " Subnet : " + subnet, Level: ""}) + lines = append(lines, Message{Body: " Status : " + status, Level: ""}) + if status == "errored" { + err, _ := r["error"].(string) + lines = append(lines, Message{Body: " Error : " + err, Level: "ERROR"}) } - return lines } diff --git a/main.go b/main.go index 6fc6c35..cad7674 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ package main import ( "fmt" + "log" "net/http" "github.com/nats-io/nats" @@ -17,6 +18,7 @@ var s *sse.Server var host string var port string var secret string +var err error func main() { setup() @@ -33,20 +35,64 @@ func main() { mux.HandleFunc("/events", authMiddleware) // Start nats handler, subscribe to all events related with the monitor - n.Subscribe("monitor.user", natsHandler) - n.Subscribe("service.create", natsHandler) - n.Subscribe("service.delete", natsHandler) - n.Subscribe("service.create.done", natsHandler) - n.Subscribe("service.create.error", natsHandler) - n.Subscribe("service.delete.done", natsHandler) - n.Subscribe("service.delete.error", natsHandler) - n.Subscribe("service.import.done", natsHandler) - n.Subscribe("service.import.error", natsHandler) - - n.Subscribe("*.*", genericHandler) - n.Subscribe("*.*.*", genericHandler) + _, 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) + if err != nil { + log.Println(err) + return + } // Start Listening addr := fmt.Sprintf("%s:%s", host, port) - http.ListenAndServe(addr, mux) + _ = http.ListenAndServe(addr, mux) } diff --git a/main_test.go b/main_test.go index 46b467c..5bc0d53 100644 --- a/main_test.go +++ b/main_test.go @@ -99,7 +99,9 @@ func TestMain(t *testing.T) { s.CreateStream("test") time.Sleep(time.Millisecond * 10) - go cl.SubscribeChan("test", rcv) + go func() { + _ = cl.SubscribeChan("test", rcv) + }() time.Sleep(time.Millisecond * 10) Convey("It should publish a message to the stream", func() { @@ -120,7 +122,9 @@ func TestMain(t *testing.T) { time.Sleep(time.Millisecond * 10) - go cl.SubscribeChan("test", rcv) + go func() { + _ = cl.SubscribeChan("test", rcv) + }() time.Sleep(time.Millisecond * 10) Convey("It should not publish a message to the stream", func() { diff --git a/setup.go b/setup.go index f43dac2..d9bef62 100644 --- a/setup.go +++ b/setup.go @@ -42,7 +42,9 @@ func setup() { if err != nil { panic("Can't get monitor config") } - json.Unmarshal(msg.Data, &cfg) + if err := json.Unmarshal(msg.Data, &cfg); err != nil { + panic("Can't process monitor config") + } host = cfg.Host port = cfg.Port