From 93c0384ba4bf22498c1d8fd794a0713bc18c88d8 Mon Sep 17 00:00:00 2001 From: sayakghosh Date: Wed, 15 May 2019 10:34:53 +0530 Subject: [PATCH 1/9] Initial commit --- Gopkg.lock | 23 ++++-- Gopkg.toml | 8 +- main.go | 204 +++++++++++++++++---------------------------------- main_test.go | 4 +- 4 files changed, 90 insertions(+), 149 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index ca40a829..4bcc1c81 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -118,7 +118,7 @@ revision = "2315d5715e36303a941d907f038da7f7c44c773b" [[projects]] - digest = "1:dafb48fd4fb82b3e0a15b789da6da152180e9c28b1b8846058903eff5eadc01d" + digest = "1:be364f871284e2e8f787235fe31d904cf9958f64b7b06fd52f97bf9af2672f91" name = "github.com/sensu/sensu-go" packages = [ "api/core/v2", @@ -127,7 +127,18 @@ "util/strings", ] pruneopts = "UT" - revision = "2daf9d442deec0afd2c6f53c183460e879a10646" + revision = "5.7.0" + +[[projects]] + digest = "1:ad992f22b75d0056f09fc95b1765afd5e1c5409e2360d282485ecbe351c240ec" + name = "github.com/sensu/sensu-plugins-go-library" + packages = [ + "args", + "sensu", + ] + pruneopts = "UT" + revision = "8849554ccf2effb05b6534240d87483ee4ac72a6" + version = "0.2.0-alpha1" [[projects]] digest = "1:645cabccbb4fa8aab25a956cbcbdf6a6845ca736b2c64e197ca7cbb9d210b939" @@ -146,15 +157,15 @@ version = "v1.0.3" [[projects]] - digest = "1:c40d65817cdd41fac9aa7af8bed56927bb2d6d47e4fea566a74880f5c2b1c41e" + digest = "1:5da8ce674952566deae4dbc23d07c85caafc6cfa815b0b3e03e41979cedb8750" name = "github.com/stretchr/testify" packages = [ "assert", "require", ] pruneopts = "UT" - revision = "f35b8ab0b5a2cef36673838d662e249dd9c94686" - version = "v1.2.2" + revision = "ffdc059bfe9ce6a4e144ba849dbedead332c6053" + version = "v1.3.0" [[projects]] branch = "master" @@ -263,7 +274,7 @@ input-imports = [ "github.com/bluele/slack", "github.com/sensu/sensu-go/types", - "github.com/spf13/cobra", + "github.com/sensu/sensu-plugins-go-library/sensu", "github.com/stretchr/testify/assert", "github.com/stretchr/testify/require", ] diff --git a/Gopkg.toml b/Gopkg.toml index f119e207..20d5d994 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -30,12 +30,8 @@ name = "github.com/bluele/slack" [[constraint]] - name = "github.com/sensu/sensu-go" - revision = "2daf9d442deec0afd2c6f53c183460e879a10646" - -[[constraint]] - name = "github.com/spf13/cobra" - version = "0.0.3" + name = "github.com/sensu/sensu-plugins-go-library" + version = "0.2.0-alpha1" [[constraint]] name = "github.com/stretchr/testify" diff --git a/main.go b/main.go index 41123fd0..6ea7b1ee 100644 --- a/main.go +++ b/main.go @@ -1,18 +1,14 @@ package main import ( - "encoding/json" "errors" "fmt" - "io/ioutil" - "log" + "github.com/sensu/sensu-go/types" + "github.com/sensu/sensu-plugins-go-library/sensu" "os" - "path" "strings" "github.com/bluele/slack" - "github.com/sensu/sensu-go/types" - "github.com/spf13/cobra" ) type HandlerConfigOption struct { @@ -22,144 +18,106 @@ type HandlerConfigOption struct { } type HandlerConfig struct { + sensu.PluginConfig SlackWebhookUrl HandlerConfigOption SlackChannel HandlerConfigOption SlackUsername HandlerConfigOption SlackIconUrl HandlerConfigOption - Timeout int - Keyspace string } var ( stdin *os.File config = HandlerConfig{ - // default values - SlackWebhookUrl: HandlerConfigOption{Path: "webhook-url", Env: "SENSU_SLACK_WEHBOOK_URL"}, - SlackChannel: HandlerConfigOption{Path: "channel", Env: "SENSU_SLACK_CHANNEL"}, - SlackUsername: HandlerConfigOption{Path: "username", Env: "SENSU_SLACK_USERNAME"}, - SlackIconUrl: HandlerConfigOption{Path: "icon-url", Env: "SENSU_SLACK_ICON_URL"}, - Timeout: 10, - Keyspace: "sensu.io/plugins/slack/config", + PluginConfig: sensu.PluginConfig{ + Name: "sensu-slack-handler", + Short: "The Sensu Go Slack handler for notifying a channel", + Timeout: 10, + Keyspace: "sensu.io/plugins/slack/config", + }, } - options = []*HandlerConfigOption{ - // iterable slice of user-overridable configuration options - &config.SlackWebhookUrl, - &config.SlackChannel, - &config.SlackUsername, - &config.SlackIconUrl, + + slackConfigOptions = []*sensu.PluginConfigOption{ + { + Path: "webhook-url", + Env: "SENSU_SLACK_WEHBOOK_URL", + Argument: "webhook-url", + Shorthand: "w", + Default: "", + Usage: "The webhook url to send messages to, defaults to value of SLACK_WEBHOOK_URL env variable", + Value: &config.SlackWebhookUrl.Value, + }, + { + Path: "channel", + Env: "SENSU_SLACK_CHANNEL", + Argument: "channel", + Shorthand: "c", + Default: "#general", + Usage: "The channel to post messages to", + Value: &config.SlackChannel.Value, + }, + { + Path: "username", + Env: "SENSU_SLACK_USERNAME", + Argument: "username", + Shorthand: "u", + Default: "sensu", + Usage: "The username that messages will be sent as", + Value: &config.SlackUsername.Value, + }, + { + Path: "icon-url", + Env: "SENSU_SLACK_ICON_URL", + Argument: "icon-url", + Shorthand: "i", + Default: "http://s3-us-west-2.amazonaws.com/sensuapp.org/sensu.png", + Usage: "A URL to an image to use as the user avatar", + Value: &config.SlackIconUrl.Value, + }, } ) func main() { - rootCmd := configureRootCommand() - if err := rootCmd.Execute(); err != nil { - log.Fatal(err) + goHandler, _ := sensu.NewGoHandler(&config.PluginConfig, slackConfigOptions, checkArgs, executeHandler) + err := goHandler.Execute() + if err != nil { + fmt.Printf("Error executing plugin: %s", err) } } -func configureRootCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "sensu-slack-handler", - Short: "The Sensu Go Slack handler for notifying a channel", - RunE: run, +func checkArgs(event *types.Event) error { + if len(config.SlackWebhookUrl.Value) == 0 { + return fmt.Errorf("--webhook-url or SENSU_SLACK_WEHBOOK_URL environment variable is required") } - /* - Sensitive flags - default to using envvar value - do not mark as required - manually test for empty value - */ - cmd.Flags().StringVarP(&config.SlackWebhookUrl.Value, - "webhook-url", - "w", - os.Getenv("SLACK_WEBHOOK_URL"), - "The webhook url to send messages to, defaults to value of SLACK_WEBHOOK_URL env variable") - - cmd.Flags().StringVarP(&config.SlackChannel.Value, - "channel", - "c", - "#general", - "The channel to post messages to") - - cmd.Flags().StringVarP(&config.SlackUsername.Value, - "username", - "u", - "sensu", - "The username that messages will be sent as") - - cmd.Flags().StringVarP(&config.SlackIconUrl.Value, - "icon-url", - "i", - "http://s3-us-west-2.amazonaws.com/sensuapp.org/sensu.png", - "A URL to an image to use as the user avatar") - - cmd.Flags().IntVarP(&config.Timeout, - "timeout", - "t", - 10, - "The amount of seconds to wait before terminating the handler") - - return cmd -} - -func run(cmd *cobra.Command, args []string) error { - if len(args) != 0 { - _ = cmd.Help() - return errors.New("invalid argument(s) received") + if event.Timestamp <= 0 { + return errors.New("timestamp is missing or must be greater than zero") } - // load & parse stdin - if stdin == nil { - stdin = os.Stdin - } - eventJSON, err := ioutil.ReadAll(stdin) - if err != nil { - return fmt.Errorf("failed to read stdin: %s", err.Error()) - } - event := &types.Event{} - err = json.Unmarshal(eventJSON, event) - if err != nil { - return fmt.Errorf("failed to unmarshal stdin data: %s", eventJSON) + if event.Entity == nil { + return errors.New("entity is missing from event") } - // configuration validation & overrides - if config.SlackWebhookUrl.Value == "" { - _ = cmd.Help() - return fmt.Errorf("webhook url is empty") + if !event.HasCheck() { + return errors.New("check is missing from event") } - configurationOverrides(&config, options, event) - - if err = validateEvent(event); err != nil { - return errors.New(err.Error()) + if err := event.Entity.Validate(); err != nil { + return err } - if err = sendMessage(event); err != nil { - return errors.New(err.Error()) + if err := event.Check.Validate(); err != nil { + return err } return nil } -func configurationOverrides(config *HandlerConfig, options []*HandlerConfigOption, event *types.Event) { - if config.Keyspace == "" { - return - } - for _, opt := range options { - if opt.Path != "" { - // compile the Annotation keyspace to look for configuration overrides - k := path.Join(config.Keyspace, opt.Path) - switch { - case event.Check.Annotations[k] != "": - opt.Value = event.Check.Annotations[k] - log.Printf("Overriding default handler configuration with value of \"Check.Annotations.%s\" (\"%s\")\n", k, event.Check.Annotations[k]) - case event.Entity.Annotations[k] != "": - opt.Value = event.Entity.Annotations[k] - log.Printf("Overriding default handler configuration with value of \"Entity.Annotations.%s\" (\"%s\")\n", k, event.Entity.Annotations[k]) - } - } +func executeHandler(event *types.Event) error { + if err := sendMessage(event); err != nil { + return errors.New(err.Error()) } + + return nil } func formattedEventAction(event *types.Event) string { @@ -249,27 +207,3 @@ func sendMessage(event *types.Event) error { Username: config.SlackUsername.Value, }) } - -func validateEvent(event *types.Event) error { - if event.Timestamp <= 0 { - return errors.New("timestamp is missing or must be greater than zero") - } - - if event.Entity == nil { - return errors.New("entity is missing from event") - } - - if !event.HasCheck() { - return errors.New("check is missing from event") - } - - if err := event.Entity.Validate(); err != nil { - return err - } - - if err := event.Check.Validate(); err != nil { - return err - } - - return nil -} diff --git a/main_test.go b/main_test.go index 694be29b..56a95dc5 100644 --- a/main_test.go +++ b/main_test.go @@ -122,7 +122,7 @@ func TestSendMessage(t *testing.T) { assert.NoError(err) } -func TestMain(t *testing.T) { +func TestMain(m *testing.M) { assert := assert.New(t) file, _ := ioutil.TempFile(os.TempDir(), "sensu-handler-slack-") defer func() { @@ -132,7 +132,7 @@ func TestMain(t *testing.T) { event := types.FixtureEvent("entity1", "check1") eventJSON, _ := json.Marshal(event) _, err := file.WriteString(string(eventJSON)) - require.NoError(t, err) + require.NoError(m, err) require.NoError(t, file.Sync()) _, err = file.Seek(0, 0) require.NoError(t, err) From 1a44efd56c8a475e96d8e6e7badc27bf9ff6ce51 Mon Sep 17 00:00:00 2001 From: sayakghosh Date: Wed, 15 May 2019 10:43:17 +0530 Subject: [PATCH 2/9] Revert testcase changes --- main_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main_test.go b/main_test.go index 56a95dc5..694be29b 100644 --- a/main_test.go +++ b/main_test.go @@ -122,7 +122,7 @@ func TestSendMessage(t *testing.T) { assert.NoError(err) } -func TestMain(m *testing.M) { +func TestMain(t *testing.T) { assert := assert.New(t) file, _ := ioutil.TempFile(os.TempDir(), "sensu-handler-slack-") defer func() { @@ -132,7 +132,7 @@ func TestMain(m *testing.M) { event := types.FixtureEvent("entity1", "check1") eventJSON, _ := json.Marshal(event) _, err := file.WriteString(string(eventJSON)) - require.NoError(m, err) + require.NoError(t, err) require.NoError(t, file.Sync()) _, err = file.Seek(0, 0) require.NoError(t, err) From 5efd6727223a340230bf2999cdd724d7e035cb92 Mon Sep 17 00:00:00 2001 From: sayakghosh Date: Wed, 15 May 2019 21:40:32 +0530 Subject: [PATCH 3/9] test case fix --- main_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main_test.go b/main_test.go index 694be29b..3c994ec2 100644 --- a/main_test.go +++ b/main_test.go @@ -136,7 +136,7 @@ func TestMain(t *testing.T) { require.NoError(t, file.Sync()) _, err = file.Seek(0, 0) require.NoError(t, err) - stdin = file + os.Stdin = file requestReceived := false var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { From 0f37d31d631940795237b1150a56b528eac22a82 Mon Sep 17 00:00:00 2001 From: sayakghosh Date: Wed, 15 May 2019 21:41:15 +0530 Subject: [PATCH 4/9] test case fix --- main.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/main.go b/main.go index 6ea7b1ee..ccbafd96 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/sensu/sensu-go/types" "github.com/sensu/sensu-plugins-go-library/sensu" - "os" "strings" "github.com/bluele/slack" @@ -26,7 +25,6 @@ type HandlerConfig struct { } var ( - stdin *os.File config = HandlerConfig{ PluginConfig: sensu.PluginConfig{ Name: "sensu-slack-handler", From 67fcc2e536228a358b4b8f5b9a10c680bdc3f7ec Mon Sep 17 00:00:00 2001 From: sayakghosh Date: Thu, 16 May 2019 09:25:06 +0530 Subject: [PATCH 5/9] Removing unnecessary event check from main.go. These checks are already being performed in sensu-plugins-go-library --- main.go | 22 +--------------------- 1 file changed, 1 insertion(+), 21 deletions(-) diff --git a/main.go b/main.go index ccbafd96..2c27de75 100644 --- a/main.go +++ b/main.go @@ -82,31 +82,11 @@ func main() { } } -func checkArgs(event *types.Event) error { +func checkArgs(_ *types.Event) error { if len(config.SlackWebhookUrl.Value) == 0 { return fmt.Errorf("--webhook-url or SENSU_SLACK_WEHBOOK_URL environment variable is required") } - if event.Timestamp <= 0 { - return errors.New("timestamp is missing or must be greater than zero") - } - - if event.Entity == nil { - return errors.New("entity is missing from event") - } - - if !event.HasCheck() { - return errors.New("check is missing from event") - } - - if err := event.Entity.Validate(); err != nil { - return err - } - - if err := event.Check.Validate(); err != nil { - return err - } - return nil } From 22a77babf478bbf9e6d7bc1f5c7520f06cd32d58 Mon Sep 17 00:00:00 2001 From: sayakghosh Date: Thu, 16 May 2019 20:46:38 +0530 Subject: [PATCH 6/9] Changes based on PR Feedback --- Gopkg.lock | 9 +++++---- Gopkg.toml | 2 +- main.go | 32 +++++++++++++------------------- main_test.go | 4 ++-- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index 4bcc1c81..ff6c1fd1 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -127,18 +127,19 @@ "util/strings", ] pruneopts = "UT" - revision = "5.7.0" + revision = "32aea478ae74c3753710887d2ce2556b598d82bb" + version = "5.7.0" [[projects]] - digest = "1:ad992f22b75d0056f09fc95b1765afd5e1c5409e2360d282485ecbe351c240ec" + digest = "1:887b1d559ae6493b3873336a87df2ea0cfe18835706987bec6c7fea3df6002f2" name = "github.com/sensu/sensu-plugins-go-library" packages = [ "args", "sensu", ] pruneopts = "UT" - revision = "8849554ccf2effb05b6534240d87483ee4ac72a6" - version = "0.2.0-alpha1" + revision = "a614f92aa018bd69a21d8d6e2d07244b8d8fbb05" + version = "0.2.0-alpha4" [[projects]] digest = "1:645cabccbb4fa8aab25a956cbcbdf6a6845ca736b2c64e197ca7cbb9d210b939" diff --git a/Gopkg.toml b/Gopkg.toml index 20d5d994..dfd3adcb 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -31,7 +31,7 @@ [[constraint]] name = "github.com/sensu/sensu-plugins-go-library" - version = "0.2.0-alpha1" + version = "0.2.0-alpha4" [[constraint]] name = "github.com/stretchr/testify" diff --git a/main.go b/main.go index 2c27de75..64a90723 100644 --- a/main.go +++ b/main.go @@ -10,18 +10,12 @@ import ( "github.com/bluele/slack" ) -type HandlerConfigOption struct { - Value string - Path string - Env string -} - type HandlerConfig struct { sensu.PluginConfig - SlackWebhookUrl HandlerConfigOption - SlackChannel HandlerConfigOption - SlackUsername HandlerConfigOption - SlackIconUrl HandlerConfigOption + SlackWebhookUrl string + SlackChannel string + SlackUsername string + SlackIconUrl string } var ( @@ -42,7 +36,7 @@ var ( Shorthand: "w", Default: "", Usage: "The webhook url to send messages to, defaults to value of SLACK_WEBHOOK_URL env variable", - Value: &config.SlackWebhookUrl.Value, + Value: &config.SlackWebhookUrl, }, { Path: "channel", @@ -51,7 +45,7 @@ var ( Shorthand: "c", Default: "#general", Usage: "The channel to post messages to", - Value: &config.SlackChannel.Value, + Value: &config.SlackChannel, }, { Path: "username", @@ -60,7 +54,7 @@ var ( Shorthand: "u", Default: "sensu", Usage: "The username that messages will be sent as", - Value: &config.SlackUsername.Value, + Value: &config.SlackUsername, }, { Path: "icon-url", @@ -69,7 +63,7 @@ var ( Shorthand: "i", Default: "http://s3-us-west-2.amazonaws.com/sensuapp.org/sensu.png", Usage: "A URL to an image to use as the user avatar", - Value: &config.SlackIconUrl.Value, + Value: &config.SlackIconUrl, }, } ) @@ -83,7 +77,7 @@ func main() { } func checkArgs(_ *types.Event) error { - if len(config.SlackWebhookUrl.Value) == 0 { + if len(config.SlackWebhookUrl) == 0 { return fmt.Errorf("--webhook-url or SENSU_SLACK_WEHBOOK_URL environment variable is required") } @@ -177,11 +171,11 @@ func messageAttachment(event *types.Event) *slack.Attachment { } func sendMessage(event *types.Event) error { - hook := slack.NewWebHook(config.SlackWebhookUrl.Value) + hook := slack.NewWebHook(config.SlackWebhookUrl) return hook.PostMessage(&slack.WebHookPostPayload{ Attachments: []*slack.Attachment{messageAttachment(event)}, - Channel: config.SlackChannel.Value, - IconUrl: config.SlackIconUrl.Value, - Username: config.SlackUsername.Value, + Channel: config.SlackChannel, + IconUrl: config.SlackIconUrl, + Username: config.SlackUsername, }) } diff --git a/main_test.go b/main_test.go index 3c994ec2..2ca5de12 100644 --- a/main_test.go +++ b/main_test.go @@ -116,8 +116,8 @@ func TestSendMessage(t *testing.T) { require.NoError(t, err) })) - config.SlackWebhookUrl.Value = apiStub.URL - config.SlackChannel.Value = "#test" + config.SlackWebhookUrl = apiStub.URL + config.SlackChannel = "#test" err := sendMessage(event) assert.NoError(err) } From 3b0d0d3a07e1a344cd1db26f64bf02bbf7f2a275 Mon Sep 17 00:00:00 2001 From: sayakghosh Date: Sat, 25 May 2019 16:55:43 +0530 Subject: [PATCH 7/9] Changes based on review comments: 1) Upgrade of sensu-plugins-go-library to version 0.2.0 2) Use of "github.com/sensu/sensu-go/api/core/v2" library instead of "github.com/sensu/sensu-go/types" to maintain consistency with sensu-go code --- Gopkg.lock | 6 ++--- Gopkg.toml | 2 +- main.go | 67 ++++++++++++++++++++++++---------------------------- main_test.go | 18 +++++++------- 4 files changed, 44 insertions(+), 49 deletions(-) diff --git a/Gopkg.lock b/Gopkg.lock index ff6c1fd1..4a544d93 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -131,15 +131,15 @@ version = "5.7.0" [[projects]] - digest = "1:887b1d559ae6493b3873336a87df2ea0cfe18835706987bec6c7fea3df6002f2" + digest = "1:8cd72fbcf8f18cf753ca9c40723eaef3356e166437f33d219d977581b7024d14" name = "github.com/sensu/sensu-plugins-go-library" packages = [ "args", "sensu", ] pruneopts = "UT" - revision = "a614f92aa018bd69a21d8d6e2d07244b8d8fbb05" - version = "0.2.0-alpha4" + revision = "a4d9fcd58e069a1c9e51984b9f4b466accd366c8" + version = "0.2.0" [[projects]] digest = "1:645cabccbb4fa8aab25a956cbcbdf6a6845ca736b2c64e197ca7cbb9d210b939" diff --git a/Gopkg.toml b/Gopkg.toml index dfd3adcb..91814838 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -31,7 +31,7 @@ [[constraint]] name = "github.com/sensu/sensu-plugins-go-library" - version = "0.2.0-alpha4" + version = "0.2.0" [[constraint]] name = "github.com/stretchr/testify" diff --git a/main.go b/main.go index 64a90723..df3fcc7a 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,8 @@ package main import ( - "errors" "fmt" - "github.com/sensu/sensu-go/types" + corev2 "github.com/sensu/sensu-go/api/core/v2" "github.com/sensu/sensu-plugins-go-library/sensu" "strings" @@ -18,6 +17,13 @@ type HandlerConfig struct { SlackIconUrl string } +const ( + webHookUrl = "webhook-url" + channel = "channel" + userName = "username" + iconUrl = "icon-url" +) + var ( config = HandlerConfig{ PluginConfig: sensu.PluginConfig{ @@ -30,36 +36,36 @@ var ( slackConfigOptions = []*sensu.PluginConfigOption{ { - Path: "webhook-url", + Path: webHookUrl, Env: "SENSU_SLACK_WEHBOOK_URL", - Argument: "webhook-url", + Argument: webHookUrl, Shorthand: "w", Default: "", Usage: "The webhook url to send messages to, defaults to value of SLACK_WEBHOOK_URL env variable", Value: &config.SlackWebhookUrl, }, { - Path: "channel", + Path: channel, Env: "SENSU_SLACK_CHANNEL", - Argument: "channel", + Argument: channel, Shorthand: "c", Default: "#general", Usage: "The channel to post messages to", Value: &config.SlackChannel, }, { - Path: "username", + Path: userName, Env: "SENSU_SLACK_USERNAME", - Argument: "username", + Argument: userName, Shorthand: "u", Default: "sensu", Usage: "The username that messages will be sent as", Value: &config.SlackUsername, }, { - Path: "icon-url", + Path: iconUrl, Env: "SENSU_SLACK_ICON_URL", - Argument: "icon-url", + Argument: iconUrl, Shorthand: "i", Default: "http://s3-us-west-2.amazonaws.com/sensuapp.org/sensu.png", Usage: "A URL to an image to use as the user avatar", @@ -69,30 +75,19 @@ var ( ) func main() { - goHandler, _ := sensu.NewGoHandler(&config.PluginConfig, slackConfigOptions, checkArgs, executeHandler) - err := goHandler.Execute() - if err != nil { - fmt.Printf("Error executing plugin: %s", err) - } + goHandler := sensu.NewGoHandler(&config.PluginConfig, slackConfigOptions, checkArgs, sendMessage) + goHandler.Execute() } -func checkArgs(_ *types.Event) error { +func checkArgs(_ *corev2.Event) error { if len(config.SlackWebhookUrl) == 0 { - return fmt.Errorf("--webhook-url or SENSU_SLACK_WEHBOOK_URL environment variable is required") - } - - return nil -} - -func executeHandler(event *types.Event) error { - if err := sendMessage(event); err != nil { - return errors.New(err.Error()) + return fmt.Errorf("--webhook-url or SENSU_SLACK_WEBHOOK_URL environment variable is required") } return nil } -func formattedEventAction(event *types.Event) string { +func formattedEventAction(event *corev2.Event) string { switch event.Check.Status { case 0: return "RESOLVED" @@ -105,11 +100,11 @@ func chomp(s string) string { return strings.Trim(strings.Trim(strings.Trim(s, "\n"), "\r"), "\r\n") } -func eventKey(event *types.Event) string { +func eventKey(event *corev2.Event) string { return fmt.Sprintf("%s/%s", event.Entity.Name, event.Check.Name) } -func eventSummary(event *types.Event, maxLength int) string { +func eventSummary(event *corev2.Event, maxLength int) string { output := chomp(event.Check.Output) if len(event.Check.Output) > maxLength { output = output[0:maxLength] + "..." @@ -117,11 +112,11 @@ func eventSummary(event *types.Event, maxLength int) string { return fmt.Sprintf("%s:%s", eventKey(event), output) } -func formattedMessage(event *types.Event) string { +func formattedMessage(event *corev2.Event) string { return fmt.Sprintf("%s - %s", formattedEventAction(event), eventSummary(event, 100)) } -func messageColor(event *types.Event) string { +func messageColor(event *corev2.Event) string { switch event.Check.Status { case 0: return "good" @@ -132,7 +127,7 @@ func messageColor(event *types.Event) string { } } -func messageStatus(event *types.Event) string { +func messageStatus(event *corev2.Event) string { switch event.Check.Status { case 0: return "Resolved" @@ -143,24 +138,24 @@ func messageStatus(event *types.Event) string { } } -func messageAttachment(event *types.Event) *slack.Attachment { +func messageAttachment(event *corev2.Event) *slack.Attachment { attachment := &slack.Attachment{ Title: "Description", Text: event.Check.Output, Fallback: formattedMessage(event), Color: messageColor(event), Fields: []*slack.AttachmentField{ - &slack.AttachmentField{ + { Title: "Status", Value: messageStatus(event), Short: false, }, - &slack.AttachmentField{ + { Title: "Entity", Value: event.Entity.Name, Short: true, }, - &slack.AttachmentField{ + { Title: "Check", Value: event.Check.Name, Short: true, @@ -170,7 +165,7 @@ func messageAttachment(event *types.Event) *slack.Attachment { return attachment } -func sendMessage(event *types.Event) error { +func sendMessage(event *corev2.Event) error { hook := slack.NewWebHook(config.SlackWebhookUrl) return hook.PostMessage(&slack.WebHookPostPayload{ Attachments: []*slack.Attachment{messageAttachment(event)}, diff --git a/main_test.go b/main_test.go index 2ca5de12..5c2102b3 100644 --- a/main_test.go +++ b/main_test.go @@ -8,14 +8,14 @@ import ( "os" "testing" - "github.com/sensu/sensu-go/types" + corev2 "github.com/sensu/sensu-go/api/core/v2" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestFormattedEventAction(t *testing.T) { assert := assert.New(t) - event := types.FixtureEvent("entity1", "check1") + event := corev2.FixtureEvent("entity1", "check1") action := formattedEventAction(event) assert.Equal("RESOLVED", action) @@ -43,14 +43,14 @@ func TestChomp(t *testing.T) { func TestEventKey(t *testing.T) { assert := assert.New(t) - event := types.FixtureEvent("entity1", "check1") + event := corev2.FixtureEvent("entity1", "check1") eventKey := eventKey(event) assert.Equal("entity1/check1", eventKey) } func TestEventSummary(t *testing.T) { assert := assert.New(t) - event := types.FixtureEvent("entity1", "check1") + event := corev2.FixtureEvent("entity1", "check1") event.Check.Output = "disk is full" eventKey := eventSummary(event, 100) @@ -62,7 +62,7 @@ func TestEventSummary(t *testing.T) { func TestFormattedMessage(t *testing.T) { assert := assert.New(t) - event := types.FixtureEvent("entity1", "check1") + event := corev2.FixtureEvent("entity1", "check1") event.Check.Output = "disk is full" event.Check.Status = 1 formattedMsg := formattedMessage(event) @@ -71,7 +71,7 @@ func TestFormattedMessage(t *testing.T) { func TestMessageColor(t *testing.T) { assert := assert.New(t) - event := types.FixtureEvent("entity1", "check1") + event := corev2.FixtureEvent("entity1", "check1") event.Check.Status = 0 color := messageColor(event) @@ -88,7 +88,7 @@ func TestMessageColor(t *testing.T) { func TestMessageStatus(t *testing.T) { assert := assert.New(t) - event := types.FixtureEvent("entity1", "check1") + event := corev2.FixtureEvent("entity1", "check1") event.Check.Status = 0 status := messageStatus(event) @@ -105,7 +105,7 @@ func TestMessageStatus(t *testing.T) { func TestSendMessage(t *testing.T) { assert := assert.New(t) - event := types.FixtureEvent("entity1", "check1") + event := corev2.FixtureEvent("entity1", "check1") var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { body, _ := ioutil.ReadAll(r.Body) @@ -129,7 +129,7 @@ func TestMain(t *testing.T) { _ = os.Remove(file.Name()) }() - event := types.FixtureEvent("entity1", "check1") + event := corev2.FixtureEvent("entity1", "check1") eventJSON, _ := json.Marshal(event) _, err := file.WriteString(string(eventJSON)) require.NoError(t, err) From 81b2ba0570d9fe72e41ddc332e21e3e9d971b62c Mon Sep 17 00:00:00 2001 From: sayakghosh Date: Sat, 25 May 2019 16:57:45 +0530 Subject: [PATCH 8/9] Changes based on review comments: 3) Rectification of spelling error --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index df3fcc7a..5c2210d5 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,7 @@ var ( slackConfigOptions = []*sensu.PluginConfigOption{ { Path: webHookUrl, - Env: "SENSU_SLACK_WEHBOOK_URL", + Env: "SENSU_SLACK_WEBHOOK_URL", Argument: webHookUrl, Shorthand: "w", Default: "", From a83ba0a7e2941ec7723ec4a2eec5be88d99a995e Mon Sep 17 00:00:00 2001 From: sayakghosh Date: Sat, 8 Jun 2019 21:05:27 +0530 Subject: [PATCH 9/9] Adding direct dependency on github.com/sensu/sensu-go library --- Gopkg.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Gopkg.toml b/Gopkg.toml index 91814838..7948859f 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -29,6 +29,10 @@ branch = "master" name = "github.com/bluele/slack" +[[constraint]] + name = "github.com/sensu/sensu-go" + version = "5.9.0" + [[constraint]] name = "github.com/sensu/sensu-plugins-go-library" version = "0.2.0"