Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for config file #2

Merged
merged 3 commits into from
May 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
# deploy
/dist

# config
/config.yml

# misc
/.data
95 changes: 95 additions & 0 deletions Gopkg.lock

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

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
name = "github.com/spf13/cobra"
version = "0.0.3"

[[constraint]]
name = "github.com/spf13/viper"
version = "1.3.2"

[[constraint]]
name = "github.com/sensu/sensu-go"
version = "5.7.0"
Expand Down
96 changes: 76 additions & 20 deletions cmd/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@ import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/sensu/sensu-go/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"sensu-sic-handler/handler"
"sensu-sic-handler/recipient"
)

var (
annotationPrefix string
handlerConfig = &handler.Config{}
)

// eventCmd represents the "event" command
var eventCmd = &cobra.Command{
Use: "event",
Expand Down Expand Up @@ -49,35 +46,48 @@ var eventCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(eventCmd)

eventCmd.PersistentFlags().StringVar(&annotationPrefix,
eventCmd.PersistentFlags().String(
"outputs",
"mail,slack,xmpp",
"The outputs to use, defaults to 'mail,slack,xmpp'")

eventCmd.PersistentFlags().String(
"annotation-prefix",
os.Getenv("ANNOTATION_PREFIX"),
"The annotation prefix to use, defaults to value of ANNOTATION_PREFIX env variable")
"sic.software",
"The annotation prefix to use")

eventCmd.PersistentFlags().StringVar(&handlerConfig.SMTPAddress,
eventCmd.PersistentFlags().String(
"smtp-address",
os.Getenv("SMTP_ADDRESS"),
"The address of the SMTP server to use, defaults to value of SMTP_ADDRESS env variable")
"localhost",
"The address of the SMTP server to use, defaults to localhost")

eventCmd.PersistentFlags().StringVar(&handlerConfig.MailFrom,
eventCmd.PersistentFlags().String(
"mail-from",
os.Getenv("MAIL_FROM"),
"The sender address for emails, defaults to value of MAIL_FROM env variable")
"",
"The sender address for emails")

eventCmd.PersistentFlags().StringVar(&handlerConfig.SlackWebhookURL,
eventCmd.PersistentFlags().String(
"slack-webhook-url",
os.Getenv("SLACK_WEBHOOK_URL"),
"The webhook url to send messages to, defaults to value of SLACK_WEBHOOK_URL env variable")
"",
"The webhook url to send messages to")

eventCmd.PersistentFlags().StringVar(&handlerConfig.SlackUsername,
eventCmd.PersistentFlags().String(
"slack-username",
"sensu",
"The username that messages will be sent as")

eventCmd.PersistentFlags().StringVar(&handlerConfig.SlackIconURL,
eventCmd.PersistentFlags().String(
"slack-icon-url",
"http://s3-us-west-2.amazonaws.com/sensuapp.org/sensu.png",
"A URL to an image to use as the user avatar")

_ = viper.BindPFlag("outputs", eventCmd.PersistentFlags().Lookup("outputs"))
_ = viper.BindPFlag("annotation-prefix", eventCmd.PersistentFlags().Lookup("annotation-prefix"))
_ = viper.BindPFlag("smtp-address", eventCmd.PersistentFlags().Lookup("smtp-address"))
_ = viper.BindPFlag("mail-from", eventCmd.PersistentFlags().Lookup("mail-from"))
_ = viper.BindPFlag("slack-webhook-url", eventCmd.PersistentFlags().Lookup("slack-webhook-url"))
_ = viper.BindPFlag("slack-username", eventCmd.PersistentFlags().Lookup("slack-username"))
_ = viper.BindPFlag("slack-icon-url", eventCmd.PersistentFlags().Lookup("slack-icon-url"))
}

func loadEvent() (*types.Event, error) {
Expand Down Expand Up @@ -125,9 +135,19 @@ func handleEvent(event *types.Event) error {
return nil
}

if val, ok := event.Entity.Annotations[fmt.Sprintf("%s/recipients", annotationPrefix)]; ok {
if val, ok := event.Entity.Annotations[fmt.Sprintf("%s/recipients", viper.GetString("annotation-prefix"))]; ok {
handlerConfig := &handler.Config{
SMTPAddress: viper.GetString("smtp-address"),
MailFrom: viper.GetString("mail-from"),
SlackWebhookURL: viper.GetString("slack-webhook-url"),
SlackUsername: viper.GetString("slack-username"),
SlackIconURL: viper.GetString("slack-icon-url"),
}

recipients := recipient.Parse(redisClient, val)

recipients = filterRecipients(recipients)

err := handler.Handle(recipients, event, handlerConfig)
if err != nil {
return err
Expand All @@ -136,3 +156,39 @@ func handleEvent(event *types.Event) error {

return nil
}

func filterRecipients(recipients []*recipient.Recipient) []*recipient.Recipient {
filtered := make([]*recipient.Recipient, 0)

useMail, useSlack, useXMPP := false, false, false

for _, output := range strings.Split(viper.GetString("outputs"), ",") {
switch output {
case "mail":
useMail = true
case "slack":
useSlack = true
case "xmpp":
useXMPP = true
}
}

for _, rcpt := range recipients {
switch rcpt.Type {
case recipient.HandlerTypeMail:
if useMail {
filtered = append(filtered, rcpt)
}
case recipient.HandlerTypeSlack:
if useSlack {
filtered = append(filtered, rcpt)
}
case recipient.HandlerTypeXMPP:
if useXMPP {
filtered = append(filtered, rcpt)
}
}
}

return filtered
}
23 changes: 10 additions & 13 deletions cmd/redmine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
)

var (
redmineURL string
redmineToken string
"github.com/spf13/viper"
)

// redmineCmd represents the "redmine" command
Expand All @@ -22,13 +16,16 @@ var redmineCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(redmineCmd)

redmineCmd.PersistentFlags().StringVar(&redmineURL,
redmineCmd.PersistentFlags().String(
"redmine-url",
os.Getenv("REDMINE_URL"),
"The redmine url to import data from, defaults to value of REDMINE_URL env variable")
"",
"The redmine url to import data from")

redmineCmd.PersistentFlags().StringVar(&redmineToken,
redmineCmd.PersistentFlags().String(
"redmine-token",
os.Getenv("REDMINE_TOKEN"),
"The redmine token used for authentication, defaults to value of REDMINE_TOKEN env variable")
"",
"The redmine token used for authentication")

_ = viper.BindPFlag("redmine-url", redmineCmd.PersistentFlags().Lookup("redmine-url"))
_ = viper.BindPFlag("redmine-token", redmineCmd.PersistentFlags().Lookup("redmine-token"))
}
7 changes: 4 additions & 3 deletions cmd/redmineImport.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cmd

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"

"sensu-sic-handler/redmine"
)
Expand All @@ -18,17 +19,17 @@ var redmineImportCmd = &cobra.Command{
return
}

if redmineURL == "" {
if viper.GetString("redmine-url") == "" {
terminateWithHelpAndMessage(cmd, "redmine url is empty")
return
}

if redmineToken == "" {
if viper.GetString("redmine-token") == "" {
terminateWithHelpAndMessage(cmd, "redmine token is empty")
return
}

err := redmine.Import(redmineURL, redmineToken, redisClient)
err := redmine.Import(viper.GetString("redmine-url"), viper.GetString("redmine-token"), redisClient)
if err != nil {
terminateWithHelpAndMessage(cmd, err.Error())
return
Expand Down
Loading