From b31c1ba575f3f98aa3c471dbcb8266de2336c8b2 Mon Sep 17 00:00:00 2001 From: Andrew Ashraf Sabry Date: Sun, 2 Jan 2022 00:59:53 +0200 Subject: [PATCH] refactor GetProviders --- provider/provider.go | 23 ++++++++++++++- provider/providers.json | 18 ++++++++++++ util/util.go | 63 ++++++++++++++++++++++------------------- 3 files changed, 74 insertions(+), 30 deletions(-) create mode 100644 provider/providers.json diff --git a/provider/provider.go b/provider/provider.go index fc1d6f79..e9d9675c 100644 --- a/provider/provider.go +++ b/provider/provider.go @@ -1,6 +1,9 @@ package provider -import "github.com/abahmed/kwatch/event" +import ( + "github.com/abahmed/kwatch/event" + "github.com/spf13/viper" +) const ( footer = "" @@ -14,3 +17,21 @@ type Provider interface { SendEvent(*event.Event) error SendMessage(string) error } + +// New returns a new Provider object +func New(name string) Provider { + switch name { + case "slack": + return NewSlack(viper.GetString("alert.slack.webhook")) + case "discord": + return NewDiscord(viper.GetString("alert.discord.webhook")) + case "pagerduty": + return NewPagerDuty(viper.GetString("alert.pagerduty.integrationKey")) + case "telegram": + return NewTelegram(viper.GetString("alert.telegram.token"), viper.GetString("alert.telegram.chatId")) + case "teams": + return NewTeams(viper.GetString("alert.teams.webhook")) + default: + return nil + } +} diff --git a/provider/providers.json b/provider/providers.json new file mode 100644 index 00000000..3d61fd97 --- /dev/null +++ b/provider/providers.json @@ -0,0 +1,18 @@ +{ + "slack": { + "webhook": "" + }, + "pagerduty": { + "integrationkey": "" + }, + "discord": { + "webhook": "" + }, + "telegram": { + "token": "", + "chatid": "" + }, + "teams": { + "webhook": "" + } +} \ No newline at end of file diff --git a/util/util.go b/util/util.go index 6c556f68..a085b0b0 100644 --- a/util/util.go +++ b/util/util.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "io/ioutil" "strings" "github.com/abahmed/kwatch/event" @@ -96,33 +97,26 @@ func getPodEvents(c kubernetes.Interface, name, namespace string) (*v1.EventList // GetProviders returns slice of provider objects after parsing config func GetProviders() []provider.Provider { - var providers []provider.Provider - const isPresent = false - telegram := []bool{isPresent, isPresent} + pAttributes := populateProvidersAttributes() + var providers []provider.Provider for key, value := range viper.Get("alert").(map[string]interface{}) { + // checks if the provider name exists + att, found := pAttributes[key] + if !found { + continue + } + + // checks if the corresponding attributes exists for c, v := range value.(map[string]interface{}) { - if key == "slack" && c == "webhook" && len(strings.TrimSpace(v.(string))) > 0 { - providers = append(providers, provider.NewSlack(viper.GetString("alert.slack.webhook"))) - } - if key == "pagerduty" && c == "integrationkey" && len(strings.TrimSpace(v.(string))) > 0 { - providers = append(providers, provider.NewPagerDuty(viper.GetString("alert.pagerduty.integrationKey"))) - } - if key == "discord" && c == "webhook" && len(strings.TrimSpace(v.(string))) > 0 { - providers = append(providers, provider.NewDiscord(viper.GetString("alert.discord.webhook"))) - } - if key == "telegram" && c == "token" && len(strings.TrimSpace(v.(string))) > 0 { - telegram[0] = true - } - if key == "telegram" && c == "chatid" && len(strings.TrimSpace(v.(string))) > 0 { - telegram[1] = true - } - if key == "teams" && c == "webhook" && len(strings.TrimSpace(v.(string))) > 0 { - providers = append(providers, provider.NewTeams(viper.GetString("alert.teams.webhook"))) + if !IsStrInSlice(c, att) || len(strings.TrimSpace(v.(string))) <= 0 { + found = false } } - if key == "telegram" && isListAllBool(true, telegram) { - providers = append(providers, provider.NewTelegram(viper.GetString("alert.telegram.token"), viper.GetString("alert.telegram.chatId"))) + + // add a new object from corresponding provider + if found { + providers = append(providers, provider.New(key)) } } @@ -173,13 +167,24 @@ func IsStrInSlice(str string, strList []string) bool { return false } -// checks if all elements in a boolean list have the same value -func isListAllBool(v bool, l []bool) bool { - - for _, x := range l { - if x != v { - return false +// populateProvidersAttributes populates and maps providers names and required attributes from json file +func populateProvidersAttributes() map[string][]string { + js, err := ioutil.ReadFile("provider/providers.json") + if err != nil { + logrus.Error(err) + } + activeProviders := make(map[string]interface{}) + parseErr := json.Unmarshal(js, &activeProviders) + if parseErr != nil { + logrus.Error(parseErr) + } + pAttributes := make(map[string][]string) + for key, value := range activeProviders { + var attList []string + for att := range value.(map[string]interface{}) { + attList = append(attList, att) } + pAttributes[key] = attList } - return true + return pAttributes }