From f635ed6ab9bc94175cdcb2b458b2a9946fa5f763 Mon Sep 17 00:00:00 2001 From: Dmitry Redkin Date: Fri, 11 Nov 2022 11:34:51 +0500 Subject: [PATCH 01/10] refactor: Issue https://github.com/moira-alert/moira/issues/797 Use constructor function NewSender instead of Init method, remove Init method from Sender interface. --- integration_tests/notifier/notifier_test.go | 79 ++++++++++++--------- interfaces.go | 1 - mock/moira-alert/sender.go | 15 ---- notifier/notifier_test.go | 7 +- notifier/registrator.go | 49 +++++++------ senders/discord/init.go | 18 +++-- senders/discord/init_test.go | 14 ++-- senders/mail/mail.go | 19 +++-- senders/mattermost/sender.go | 16 +++-- senders/mattermost/sender_internal_test.go | 11 +-- senders/mattermost/sender_manual_test.go | 7 +- senders/mattermost/sender_test.go | 18 ++--- senders/msteams/msteams.go | 16 +++-- senders/msteams/msteams_test.go | 20 +++--- senders/opsgenie/init.go | 18 +++-- senders/opsgenie/init_test.go | 42 ++++++----- senders/pagerduty/init.go | 14 ++-- senders/pagerduty/init_test.go | 32 +++++---- senders/pushover/pushover.go | 14 ++-- senders/pushover/pushover_test.go | 22 +++--- senders/script/script.go | 33 +++++---- senders/script/script_test.go | 19 +++-- senders/selfstate/selfstate.go | 17 +++-- senders/slack/slack.go | 14 ++-- senders/slack/slack_test.go | 23 +++--- senders/telegram/init.go | 15 ++-- senders/telegram/init_test.go | 17 ++--- senders/twilio/twilio.go | 41 ++++++----- senders/twilio/twilio_test.go | 35 +++++---- senders/victorops/init.go | 15 ++-- senders/victorops/init_test.go | 43 ++++++----- senders/webhook/webhook.go | 18 +++-- senders/webhook/webhook_test.go | 15 ++-- 33 files changed, 401 insertions(+), 336 deletions(-) diff --git a/integration_tests/notifier/notifier_test.go b/integration_tests/notifier/notifier_test.go index cb69629a5..79d7e1746 100644 --- a/integration_tests/notifier/notifier_test.go +++ b/integration_tests/notifier/notifier_test.go @@ -17,6 +17,7 @@ import ( "github.com/moira-alert/moira/notifier" "github.com/moira-alert/moira/notifier/events" "github.com/moira-alert/moira/notifier/notifications" + . "github.com/smartystreets/goconvey/convey" ) var senderSettings = map[string]string{ @@ -78,41 +79,51 @@ var event = moira.NotificationEvent{ func TestNotifier(t *testing.T) { mockCtrl = gomock.NewController(t) defer mockCtrl.Finish() - database := redis.NewTestDatabase(logger) - metricsSourceProvider := metricSource.CreateMetricSourceProvider(local.Create(database), nil) - database.SaveContact(&contact) //nolint - database.SaveSubscription(&subscription) //nolint - database.SaveTrigger(trigger.ID, &trigger) //nolint - database.PushNotificationEvent(&event, true) //nolint - notifier2 := notifier.NewNotifier(database, logger, notifierConfig, notifierMetrics, metricsSourceProvider, map[string]moira.ImageStore{}) - sender := mock_moira_alert.NewMockSender(mockCtrl) - sender.EXPECT().Init(senderSettings, logger, location, dateTimeFormat).Return(nil) - notifier2.RegisterSender(senderSettings, sender) //nolint - sender.EXPECT().SendEvents(gomock.Any(), contact, triggerData, gomock.Any(), false).Return(nil).Do(func(arg0, arg1, arg2, arg3, arg4 interface{}) { - fmt.Print("SendEvents called. End test") - close(shutdown) - }) - fetchEventsWorker := events.FetchEventsWorker{ - Database: database, - Logger: logger, - Metrics: notifierMetrics, - Scheduler: notifier.NewScheduler(database, logger, notifierMetrics), - } - - fetchNotificationsWorker := notifications.FetchNotificationsWorker{ - Database: database, - Logger: logger, - Notifier: notifier2, - } - - fetchEventsWorker.Start() - fetchNotificationsWorker.Start() - - waitTestEnd() - - fetchEventsWorker.Stop() //nolint - fetchNotificationsWorker.Stop() //nolint + Convey("TestNotifier", t, func() { + database := redis.NewTestDatabase(logger) + metricsSourceProvider := metricSource.CreateMetricSourceProvider(local.Create(database), nil) + err := database.SaveContact(&contact) + So(err, ShouldBeNil) + err = database.SaveSubscription(&subscription) + So(err, ShouldBeNil) + err = database.SaveTrigger(trigger.ID, &trigger) + So(err, ShouldBeNil) + err = database.PushNotificationEvent(&event, true) + So(err, ShouldBeNil) + + notifier2 := notifier.NewNotifier(database, logger, notifierConfig, notifierMetrics, metricsSourceProvider, map[string]moira.ImageStore{}) + sender := mock_moira_alert.NewMockSender(mockCtrl) + err = notifier2.RegisterSender(senderSettings, sender) + So(err, ShouldBeNil) + sender.EXPECT().SendEvents(gomock.Any(), contact, triggerData, gomock.Any(), false).Return(nil).Do(func(arg0, arg1, arg2, arg3, arg4 interface{}) { + fmt.Print("SendEvents called. End test") + close(shutdown) + }) + + fetchEventsWorker := events.FetchEventsWorker{ + Database: database, + Logger: logger, + Metrics: notifierMetrics, + Scheduler: notifier.NewScheduler(database, logger, notifierMetrics), + } + + fetchNotificationsWorker := notifications.FetchNotificationsWorker{ + Database: database, + Logger: logger, + Notifier: notifier2, + } + + fetchEventsWorker.Start() + fetchNotificationsWorker.Start() + + waitTestEnd() + + err = fetchEventsWorker.Stop() + So(err, ShouldBeNil) + err = fetchNotificationsWorker.Stop() + So(err, ShouldBeNil) + }) } func waitTestEnd() { diff --git a/interfaces.go b/interfaces.go index 4fc22666e..0e72ce96a 100644 --- a/interfaces.go +++ b/interfaces.go @@ -194,7 +194,6 @@ type Logger interface { type Sender interface { // TODO refactor: https://github.com/moira-alert/moira/issues/794 SendEvents(events NotificationEvents, contact ContactData, trigger TriggerData, plot [][]byte, throttled bool) error - Init(senderSettings map[string]string, logger Logger, location *time.Location, dateTimeFormat string) error } // ImageStore is the interface for image storage providers diff --git a/mock/moira-alert/sender.go b/mock/moira-alert/sender.go index f74004a44..cf8371b9a 100644 --- a/mock/moira-alert/sender.go +++ b/mock/moira-alert/sender.go @@ -6,7 +6,6 @@ package mock_moira_alert import ( reflect "reflect" - time "time" gomock "github.com/golang/mock/gomock" moira "github.com/moira-alert/moira" @@ -35,20 +34,6 @@ func (m *MockSender) EXPECT() *MockSenderMockRecorder { return m.recorder } -// Init mocks base method. -func (m *MockSender) Init(arg0 map[string]string, arg1 moira.Logger, arg2 *time.Location, arg3 string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Init", arg0, arg1, arg2, arg3) - ret0, _ := ret[0].(error) - return ret0 -} - -// Init indicates an expected call of Init. -func (mr *MockSenderMockRecorder) Init(arg0, arg1, arg2, arg3 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Init", reflect.TypeOf((*MockSender)(nil).Init), arg0, arg1, arg2, arg3) -} - // SendEvents mocks base method. func (m *MockSender) SendEvents(arg0 moira.NotificationEvents, arg1 moira.ContactData, arg2 moira.TriggerData, arg3 [][]byte, arg4 bool) error { m.ctrl.T.Helper() diff --git a/notifier/notifier_test.go b/notifier/notifier_test.go index f7cbb3434..cc50eac96 100644 --- a/notifier/notifier_test.go +++ b/notifier/notifier_test.go @@ -219,11 +219,10 @@ func configureNotifier(t *testing.T) { "type": "test", } - sender.EXPECT().Init(senderSettings, logger, location, "15:04 02.01.2006").Return(nil) - - notif.RegisterSender(senderSettings, sender) //nolint - Convey("Should return one sender", t, func() { + err := notif.RegisterSender(senderSettings, sender) + So(err, ShouldBeNil) + So(notif.GetSenders(), ShouldResemble, map[string]bool{"test": true}) }) } diff --git a/notifier/registrator.go b/notifier/registrator.go index cd57737e4..fac5e09a9 100644 --- a/notifier/registrator.go +++ b/notifier/registrator.go @@ -40,52 +40,60 @@ const ( mattermostSender = "mattermost" ) -// RegisterSenders watch on senders config and register all configured senders +// RegisterSenders creates all senders and registers them. func (notifier *StandardNotifier) RegisterSenders(connector moira.Database) error { //nolint var err error for _, senderSettings := range notifier.config.Senders { senderSettings["front_uri"] = notifier.config.FrontURL + + var sender moira.Sender + switch senderSettings["type"] { case mailSender: - err = notifier.RegisterSender(senderSettings, &mail.Sender{}) + sender, err = mail.NewSender(senderSettings, notifier.logger, notifier.config.Location, notifier.config.DateTimeFormat) case pushoverSender: - err = notifier.RegisterSender(senderSettings, &pushover.Sender{}) + sender, err = pushover.NewSender(senderSettings, notifier.logger, notifier.config.Location) case scriptSender: - err = notifier.RegisterSender(senderSettings, &script.Sender{}) + sender, err = script.NewSender(senderSettings, notifier.logger) case discordSender: - err = notifier.RegisterSender(senderSettings, &discord.Sender{DataBase: connector}) + sender, err = discord.NewSender(senderSettings, notifier.logger, notifier.config.Location, connector) case slackSender: - err = notifier.RegisterSender(senderSettings, &slack.Sender{}) + sender, err = slack.NewSender(senderSettings, notifier.logger, notifier.config.Location) case telegramSender: - err = notifier.RegisterSender(senderSettings, &telegram.Sender{DataBase: connector}) + sender, err = telegram.NewSender(senderSettings, notifier.logger, notifier.config.Location, connector) case msTeamsSender: - err = notifier.RegisterSender(senderSettings, &msteams.Sender{}) + sender, err = msteams.NewSender(senderSettings, notifier.logger, notifier.config.Location) case pagerdutySender: - err = notifier.RegisterSender(senderSettings, &pagerduty.Sender{ImageStores: notifier.imageStores}) + sender = pagerduty.NewSender(senderSettings, notifier.logger, notifier.config.Location, notifier.imageStores) case twilioSmsSender, twilioVoiceSender: - err = notifier.RegisterSender(senderSettings, &twilio.Sender{}) + sender, err = twilio.NewSender(senderSettings, notifier.logger, notifier.config.Location) case webhookSender: - err = notifier.RegisterSender(senderSettings, &webhook.Sender{}) + sender, err = webhook.NewSender(senderSettings, notifier.logger) case opsgenieSender: - err = notifier.RegisterSender(senderSettings, &opsgenie.Sender{ImageStores: notifier.imageStores}) + sender, err = opsgenie.NewSender(senderSettings, notifier.logger, notifier.config.Location, notifier.imageStores) case victoropsSender: - err = notifier.RegisterSender(senderSettings, &victorops.Sender{ImageStores: notifier.imageStores}) + sender, err = victorops.NewSender(senderSettings, notifier.logger, notifier.config.Location, notifier.imageStores) case mattermostSender: - err = notifier.RegisterSender(senderSettings, &mattermost.Sender{}) + sender, err = mattermost.NewSender(senderSettings, notifier.config.Location) // case "email": - // err = notifier.RegisterSender(senderSettings, &kontur.MailSender{}) + // sender = kontur.NewMailSender(senderSettings, notifier.logger, notifier.config.Location, notifier.config.DateTimeFormat) // case "phone": - // err = notifier.RegisterSender(senderSettings, &kontur.SmsSender{}) + // sender = kontur.NewSmsSender(senderSettings, notifier.logger, notifier.config.Location) default: return fmt.Errorf("unknown sender type [%s]", senderSettings["type"]) } if err != nil { - return err + return fmt.Errorf("failed to initialize sender [%s], err [%s]", senderSettings["type"], err.Error()) + } + err = notifier.RegisterSender(senderSettings, sender) + if err != nil { + return fmt.Errorf("failed to register sender [%s], err [%s]", senderSettings["type"], err.Error()) } } if notifier.config.SelfStateEnabled { selfStateSettings := map[string]string{"type": selfStateSender} - if err = notifier.RegisterSender(selfStateSettings, &selfstate.Sender{Database: connector}); err != nil { + sender := selfstate.NewSender(notifier.logger, connector) + if err = notifier.RegisterSender(selfStateSettings, sender); err != nil { notifier.logger.Warningf("failed to register selfstate sender: %s", err.Error()) } } @@ -101,10 +109,7 @@ func (notifier *StandardNotifier) RegisterSender(senderSettings map[string]strin default: senderIdent = senderSettings["type"] } - err := sender.Init(senderSettings, notifier.logger, notifier.config.Location, notifier.config.DateTimeFormat) - if err != nil { - return fmt.Errorf("failed to initialize sender [%s], err [%s]", senderIdent, err.Error()) - } + eventsChannel := make(chan NotificationPackage) notifier.senders[senderIdent] = eventsChannel notifier.metrics.SendersOkMetrics.RegisterMeter(senderIdent, getGraphiteSenderIdent(senderIdent), "sends_ok") diff --git a/senders/discord/init.go b/senders/discord/init.go index d857ada69..82ecdeed1 100644 --- a/senders/discord/init.go +++ b/senders/discord/init.go @@ -16,7 +16,8 @@ const ( workerName = "DiscordBot" ) -// Sender implements moira sender interface for discord +// Sender implements moira sender interface for Discord. +// Use NewSender to create instance. type Sender struct { DataBase moira.Database logger moira.Logger @@ -26,16 +27,20 @@ type Sender struct { botUserID string } -// Init reads the yaml config -func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) error { +// NewSender creates Sender instance. +func NewSender(senderSettings map[string]string, logger moira.Logger, location *time.Location, db moira.Database) (*Sender, error) { + sender := &Sender{ + DataBase: db, + } + var err error token := senderSettings["token"] if token == "" { - return fmt.Errorf("cannot read the discord token from the config") + return nil, fmt.Errorf("cannot read the discord token from the config") } sender.session, err = discordgo.New("Bot " + token) if err != nil { - return fmt.Errorf("error creating discord session: %s", err) + return nil, fmt.Errorf("error creating discord session: %s", err) } sender.logger = logger sender.frontURI = senderSettings["front_uri"] @@ -56,7 +61,8 @@ func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger sender.session.AddHandler(handleMsg) go sender.runBot() - return nil + + return sender, nil } func (sender *Sender) runBot() { diff --git a/senders/discord/init_test.go b/senders/discord/init_test.go index a00315d13..04abfd116 100644 --- a/senders/discord/init_test.go +++ b/senders/discord/init_test.go @@ -18,22 +18,21 @@ type MockLock struct { moira.Lock } -func (lock *MockLock) Acquire(stop <-chan struct{}) (lost <-chan struct{}, error error) { +func (lock *MockLock) Acquire(<-chan struct{}) (lost <-chan struct{}, error error) { return lost, nil } -func (db *MockDB) NewLock(name string, ttl time.Duration) moira.Lock { +func (db *MockDB) NewLock(string, time.Duration) moira.Lock { return &MockLock{} } -func TestInit(t *testing.T) { +func TestNewSender(t *testing.T) { logger, _ := logging.ConfigureLog("stdout", "debug", "test", true) location, _ := time.LoadLocation("UTC") Convey("Init tests", t, func() { - sender := Sender{DataBase: &MockDB{}} Convey("Empty map", func() { - err := sender.Init(map[string]string{}, logger, nil, "") + sender, err := NewSender(map[string]string{}, logger, nil, &MockDB{}) So(err, ShouldResemble, fmt.Errorf("cannot read the discord token from the config")) - So(sender, ShouldResemble, Sender{DataBase: &MockDB{}}) + So(sender, ShouldBeNil) }) Convey("Has settings", func() { @@ -41,7 +40,8 @@ func TestInit(t *testing.T) { "token": "123", "front_uri": "http://moira.uri", } - sender.Init(senderSettings, logger, location, "15:04") //nolint + sender, err := NewSender(senderSettings, logger, location, &MockDB{}) + So(err, ShouldBeNil) So(sender.frontURI, ShouldResemble, "http://moira.uri") So(sender.session.Token, ShouldResemble, "Bot 123") So(sender.logger, ShouldResemble, logger) diff --git a/senders/mail/mail.go b/senders/mail/mail.go index 55955507e..e206147fe 100644 --- a/senders/mail/mail.go +++ b/senders/mail/mail.go @@ -12,7 +12,8 @@ import ( "github.com/moira-alert/moira" ) -// Sender implements moira sender interface via pushover +// Sender implements moira sender interface via Email. +// Use NewSender to create instance. type Sender struct { From string SMTPHello string @@ -30,18 +31,24 @@ type Sender struct { dateTimeFormat string } -// Init read yaml config -func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) error { +// NewSender creates Sender instance. +func NewSender(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) (*Sender, error) { + sender := &Sender{} + err := sender.fillSettings(senderSettings, logger, location, dateTimeFormat) if err != nil { - return err + return nil, err } sender.TemplateName, sender.Template, err = parseTemplate(sender.TemplateFile) if err != nil { - return err + return nil, err } err = sender.tryDial() - return err + if err != nil { + return nil, err + } + + return sender, nil } func (sender *Sender) fillSettings(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) error { diff --git a/senders/mattermost/sender.go b/senders/mattermost/sender.go index 99408e9b7..bc672d6be 100644 --- a/senders/mattermost/sender.go +++ b/senders/mattermost/sender.go @@ -23,17 +23,19 @@ type Sender struct { client Client } -// Init configures Sender. -func (sender *Sender) Init(senderSettings map[string]string, _ moira.Logger, location *time.Location, _ string) error { +// NewSender creates Sender instance. +func NewSender(senderSettings map[string]string, location *time.Location) (*Sender, error) { + sender := &Sender{} + url := senderSettings["url"] if url == "" { - return fmt.Errorf("can not read Mattermost url from config") + return nil, fmt.Errorf("can not read Mattermost url from config") } client := model.NewAPIv4Client(url) insecureTLS, err := strconv.ParseBool(senderSettings["insecure_tls"]) if err != nil { - return fmt.Errorf("can not parse insecure_tls: %v", err) + return nil, fmt.Errorf("can not parse insecure_tls: %v", err) } client.HTTPClient = &http.Client{ Transport: &http.Transport{ @@ -46,18 +48,18 @@ func (sender *Sender) Init(senderSettings map[string]string, _ moira.Logger, loc token := senderSettings["api_token"] if token == "" { - return fmt.Errorf("can not read Mattermost api_token from config") + return nil, fmt.Errorf("can not read Mattermost api_token from config") } sender.client.SetToken(token) frontURI := senderSettings["front_uri"] if frontURI == "" { - return fmt.Errorf("can not read Mattermost front_uri from config") + return nil, fmt.Errorf("can not read Mattermost front_uri from config") } sender.frontURI = frontURI sender.location = location - return nil + return sender, nil } // SendEvents implements moira.Sender interface. diff --git a/senders/mattermost/sender_internal_test.go b/senders/mattermost/sender_internal_test.go index aeadfdc7d..997593aab 100644 --- a/senders/mattermost/sender_internal_test.go +++ b/senders/mattermost/sender_internal_test.go @@ -9,15 +9,11 @@ import ( "github.com/moira-alert/moira" "github.com/golang/mock/gomock" - logging "github.com/moira-alert/moira/logging/zerolog_adapter" mock "github.com/moira-alert/moira/mock/notifier/mattermost" . "github.com/smartystreets/goconvey/convey" ) func TestSendEvents(t *testing.T) { - logger, _ := logging.ConfigureLog("stdout", "debug", "test", true) - sender := &Sender{} - Convey("Given configured sender", t, func() { senderSettings := map[string]string{ // redundant, but necessary config "url": "qwerty", @@ -25,7 +21,7 @@ func TestSendEvents(t *testing.T) { "front_uri": "qwerty", "insecure_tls": "true", } - err := sender.Init(senderSettings, logger, nil, "") + sender, err := NewSender(senderSettings, nil) So(err, ShouldBeNil) Convey("When client return error, SendEvents should return error", func() { @@ -53,9 +49,6 @@ func TestSendEvents(t *testing.T) { } func TestBuildMessage(t *testing.T) { - logger, _ := logging.ConfigureLog("stdout", "debug", "test", true) - sender := &Sender{} - Convey("Given configured sender", t, func() { senderSettings := map[string]string{ "url": "qwerty", "api_token": "qwerty", // redundant, but necessary config @@ -63,7 +56,7 @@ func TestBuildMessage(t *testing.T) { "insecure_tls": "true", } location, _ := time.LoadLocation("UTC") - err := sender.Init(senderSettings, logger, location, "") + sender, err := NewSender(senderSettings, location) So(err, ShouldBeNil) event := moira.NotificationEvent{ diff --git a/senders/mattermost/sender_manual_test.go b/senders/mattermost/sender_manual_test.go index 5dd419acc..bf59ef657 100644 --- a/senders/mattermost/sender_manual_test.go +++ b/senders/mattermost/sender_manual_test.go @@ -9,14 +9,11 @@ import ( "github.com/moira-alert/moira" "github.com/moira-alert/moira/senders/mattermost" - logging "github.com/moira-alert/moira/logging/zerolog_adapter" . "github.com/smartystreets/goconvey/convey" ) //TestSender is integration manual test. Paste your Url, Token and Channel ID and check message in Mattermost. func TestSender(t *testing.T) { - logger, _ := logging.ConfigureLog("stdout", "debug", "test", true) - const ( url = "http://localhost:8065" apiToken = "8pdo6yoiutgidgxs9qxhbo7w4h" @@ -24,8 +21,6 @@ func TestSender(t *testing.T) { ) Convey("Init tests", t, func() { - sender := &mattermost.Sender{} - Convey("With url and apiToken", func() { senderSettings := map[string]string{ "url": url, @@ -34,7 +29,7 @@ func TestSender(t *testing.T) { "insecure_tls": "true", } location, _ := time.LoadLocation("UTC") - err := sender.Init(senderSettings, logger, location, "") + sender, err := mattermost.NewSender(senderSettings, location) So(err, ShouldBeNil) event := moira.NotificationEvent{ diff --git a/senders/mattermost/sender_test.go b/senders/mattermost/sender_test.go index 78273b380..216efcd4b 100644 --- a/senders/mattermost/sender_test.go +++ b/senders/mattermost/sender_test.go @@ -5,22 +5,18 @@ import ( "github.com/moira-alert/moira/senders/mattermost" - logging "github.com/moira-alert/moira/logging/zerolog_adapter" . "github.com/smartystreets/goconvey/convey" ) func TestInit(t *testing.T) { - logger, _ := logging.ConfigureLog("stdout", "debug", "test", true) Convey("Init tests", t, func() { - sender := &mattermost.Sender{} - Convey("No url", func() { senderSettings := map[string]string{ "api_token": "qwerty", "front_uri": "qwerty", "insecure_tls": "true", } - err := sender.Init(senderSettings, logger, nil, "") + _, err := mattermost.NewSender(senderSettings, nil) So(err, ShouldNotBeNil) }) @@ -31,31 +27,31 @@ func TestInit(t *testing.T) { "front_uri": "qwerty", "insecure_tls": "true", } - err := sender.Init(senderSettings, logger, nil, "") + _, err := mattermost.NewSender(senderSettings, nil) So(err, ShouldNotBeNil) }) Convey("No api_token", func() { senderSettings := map[string]string{"url": "qwerty", "front_uri": "qwerty"} - err := sender.Init(senderSettings, logger, nil, "") + _, err := mattermost.NewSender(senderSettings, nil) So(err, ShouldNotBeNil) }) Convey("Empty api_token", func() { senderSettings := map[string]string{"url": "qwerty", "front_uri": "qwerty", "api_token": ""} - err := sender.Init(senderSettings, logger, nil, "") + _, err := mattermost.NewSender(senderSettings, nil) So(err, ShouldNotBeNil) }) Convey("No front_uri", func() { senderSettings := map[string]string{"url": "qwerty", "api_token": "qwerty"} - err := sender.Init(senderSettings, logger, nil, "") + _, err := mattermost.NewSender(senderSettings, nil) So(err, ShouldNotBeNil) }) Convey("Empty front_uri", func() { senderSettings := map[string]string{"url": "qwerty", "api_token": "qwerty", "front_uri": ""} - err := sender.Init(senderSettings, logger, nil, "") + _, err := mattermost.NewSender(senderSettings, nil) So(err, ShouldNotBeNil) }) @@ -66,7 +62,7 @@ func TestInit(t *testing.T) { "front_uri": "qwerty", "insecure_tls": "true", } - err := sender.Init(senderSettings, logger, nil, "") + _, err := mattermost.NewSender(senderSettings, nil) So(err, ShouldBeNil) }) }) diff --git a/senders/msteams/msteams.go b/senders/msteams/msteams.go index 25e645e66..e57693bb0 100644 --- a/senders/msteams/msteams.go +++ b/senders/msteams/msteams.go @@ -35,7 +35,8 @@ var headers = map[string]string{ "Content-Type": "application/json", } -// Sender implements moira sender interface via MS Teams +// Sender implements moira sender interface via MS Teams. +// Use NewSender to create instance. type Sender struct { frontURI string maxEvents int @@ -44,24 +45,27 @@ type Sender struct { client *http.Client } -// Init initialises settings required for full functionality -func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) error { +// NewSender creates Sender instance. +func NewSender(senderSettings map[string]string, logger moira.Logger, location *time.Location) (*Sender, error) { + sender := &Sender{} + sender.logger = logger sender.location = location sender.frontURI = senderSettings["front_uri"] maxEvents, err := strconv.Atoi(senderSettings["max_events"]) if err != nil { - return fmt.Errorf("max_events should be an integer: %w", err) + return nil, fmt.Errorf("max_events should be an integer: %w", err) } sender.maxEvents = maxEvents sender.client = &http.Client{ Timeout: time.Duration(30) * time.Second, //nolint } - return nil + + return sender, nil } // SendEvents implements Sender interface Send -func (sender *Sender) SendEvents(events moira.NotificationEvents, contact moira.ContactData, trigger moira.TriggerData, plots [][]byte, throttled bool) error { +func (sender *Sender) SendEvents(events moira.NotificationEvents, contact moira.ContactData, trigger moira.TriggerData, _ [][]byte, throttled bool) error { err := sender.isValidWebhookURL(contact.Value) if err != nil { return err diff --git a/senders/msteams/msteams_test.go b/senders/msteams/msteams_test.go index 15b5beef3..8761f48c6 100644 --- a/senders/msteams/msteams_test.go +++ b/senders/msteams/msteams_test.go @@ -11,20 +11,20 @@ import ( "gopkg.in/h2non/gock.v1" ) -func TestInit(t *testing.T) { +func TestNewSender(t *testing.T) { logger, _ := logging.ConfigureLog("stdout", "debug", "test", true) Convey("Init tests", t, func() { - sender := Sender{} senderSettings := map[string]string{ "max_events": "-1", } Convey("Empty map should fail", func() { - err := sender.Init(map[string]string{}, logger, nil, "") - So(err, ShouldNotResemble, nil) + sender, err := NewSender(map[string]string{}, logger, nil) + So(sender, ShouldBeNil) + So(err, ShouldNotBeNil) }) Convey("Minimal settings", func() { - err := sender.Init(senderSettings, logger, nil, "") - So(err, ShouldResemble, nil) + sender, err := NewSender(senderSettings, logger, nil) + So(err, ShouldBeNil) So(sender, ShouldNotResemble, Sender{}) So(sender.maxEvents, ShouldResemble, -1) }) @@ -32,12 +32,11 @@ func TestInit(t *testing.T) { } func TestMSTeamsHttpResponse(t *testing.T) { - sender := Sender{} logger, _ := logging.ConfigureLog("stdout", "info", "test", true) location, _ := time.LoadLocation("UTC") - _ = sender.Init(map[string]string{ + senderSettings := map[string]string{ "max_events": "-1", - }, logger, location, "") + } event := moira.NotificationEvent{ TriggerID: "TriggerID", Values: map[string]float64{"t1": 123}, @@ -59,6 +58,9 @@ some other text _italic text_`, } Convey("When HTTP Response", t, func() { + sender, err := NewSender(senderSettings, logger, location) + So(err, ShouldBeNil) + Convey("is 200 and body is '1' there should be no error", func() { defer gock.Off() gock.New("https://outlook.office.com/webhook/foo"). diff --git a/senders/opsgenie/init.go b/senders/opsgenie/init.go index b64fc2d8b..3e81a8cd2 100644 --- a/senders/opsgenie/init.go +++ b/senders/opsgenie/init.go @@ -10,7 +10,8 @@ import ( "github.com/opsgenie/opsgenie-go-sdk-v2/client" ) -// Sender implements the Sender interface for opsgenie +// Sender implements the Sender interface for Opsgenie. +// Use NewSender to create instance. type Sender struct { apiKey string client *alert.Client @@ -23,12 +24,16 @@ type Sender struct { frontURI string } -// Init initializes the opsgenie sender -func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) error { +// NewSender creates Sender instance. +func NewSender(senderSettings map[string]string, logger moira.Logger, location *time.Location, imageStores map[string]moira.ImageStore) (*Sender, error) { + sender := &Sender{ + ImageStores: imageStores, + } + var ok bool if sender.apiKey, ok = senderSettings["api_key"]; !ok { - return fmt.Errorf("cannot read the api_key from the sender settings") + return nil, fmt.Errorf("cannot read the api_key from the sender settings") } sender.imageStoreID, sender.imageStore, sender.imageStoreConfigured = @@ -39,11 +44,12 @@ func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger ApiKey: sender.apiKey, }) if err != nil { - return fmt.Errorf("error while creating opsgenie client: %s", err) + return nil, fmt.Errorf("error while creating opsgenie client: %s", err) } sender.frontURI = senderSettings["front_uri"] sender.logger = logger sender.location = location - return nil + + return sender, nil } diff --git a/senders/opsgenie/init_test.go b/senders/opsgenie/init_test.go index 744390b6f..94aa4375f 100644 --- a/senders/opsgenie/init_test.go +++ b/senders/opsgenie/init_test.go @@ -13,35 +13,36 @@ import ( . "github.com/smartystreets/goconvey/convey" ) -func TestInit(t *testing.T) { +func TestNewSender(t *testing.T) { logger, _ := logging.ConfigureLog("stdout", "debug", "test", true) location, _ := time.LoadLocation("UTC") mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() - imageStore := mock_moira_alert.NewMockImageStore(mockCtrl) Convey("Init tests", t, func() { - sender := Sender{ImageStores: map[string]moira.ImageStore{ - "s3": imageStore, - }} - Convey("Empty map", func() { - err := sender.Init(map[string]string{}, logger, nil, "") + imageStore := mock_moira_alert.NewMockImageStore(mockCtrl) + imageStores := map[string]moira.ImageStore{ + "s3": imageStore, + } + sender, err := NewSender(map[string]string{}, logger, nil, imageStores) So(err, ShouldResemble, fmt.Errorf("cannot read the api_key from the sender settings")) - So(sender, ShouldResemble, Sender{ - ImageStores: map[string]moira.ImageStore{ - "s3": imageStore, - }}) + So(sender, ShouldBeNil) }) Convey("Has settings", func() { + imageStore := mock_moira_alert.NewMockImageStore(mockCtrl) imageStore.EXPECT().IsEnabled().Return(true) + imageStores := map[string]moira.ImageStore{ + "s3": imageStore, + } senderSettings := map[string]string{ "api_key": "testkey", "front_uri": "http://moira.uri", "image_store": "s3", } - sender.Init(senderSettings, logger, location, "15:04") //nolint + sender, err := NewSender(senderSettings, logger, location, imageStores) + So(err, ShouldBeNil) So(sender.apiKey, ShouldResemble, "testkey") So(sender.frontURI, ShouldResemble, "http://moira.uri") So(sender.logger, ShouldResemble, logger) @@ -49,27 +50,34 @@ func TestInit(t *testing.T) { }) Convey("Wrong image_store name", func() { + imageStore := mock_moira_alert.NewMockImageStore(mockCtrl) + imageStores := map[string]moira.ImageStore{ + "s3": imageStore, + } senderSettings := map[string]string{ "front_uri": "http://moira.uri", "api_key": "testkey", "image_store": "s4", } - sender.Init(senderSettings, logger, location, "15:04") //nolint + sender, err := NewSender(senderSettings, logger, location, imageStores) + So(err, ShouldBeNil) So(sender.imageStoreConfigured, ShouldResemble, false) So(sender.imageStore, ShouldResemble, nil) }) Convey("image store not configured", func() { + imageStore := mock_moira_alert.NewMockImageStore(mockCtrl) + imageStores := map[string]moira.ImageStore{ + "s3": imageStore, + } imageStore.EXPECT().IsEnabled().Return(false) senderSettings := map[string]string{ "api_key": "testkey", "front_uri": "http://moira.uri", "image_store": "s3", } - sender := Sender{ImageStores: map[string]moira.ImageStore{ - "s3": imageStore, - }} - sender.Init(senderSettings, logger, location, "15:04") //nolint + sender, err := NewSender(senderSettings, logger, location, imageStores) + So(err, ShouldBeNil) So(sender.imageStoreConfigured, ShouldResemble, false) So(sender.imageStore, ShouldResemble, nil) }) diff --git a/senders/pagerduty/init.go b/senders/pagerduty/init.go index 3bccc5411..21819fb80 100644 --- a/senders/pagerduty/init.go +++ b/senders/pagerduty/init.go @@ -7,7 +7,8 @@ import ( "github.com/moira-alert/moira/senders" ) -// Sender implements moira sender interface for pagerduty +// Sender implements moira sender interface for PagerDuty. +// Use NewSender to create instance. type Sender struct { ImageStores map[string]moira.ImageStore imageStoreID string @@ -18,8 +19,12 @@ type Sender struct { location *time.Location } -// Init loads yaml config, configures the pagerduty client -func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) error { +// NewSender creates Sender instance. +func NewSender(senderSettings map[string]string, logger moira.Logger, location *time.Location, imageStores map[string]moira.ImageStore) *Sender { + sender := &Sender{ + ImageStores: imageStores, + } + sender.frontURI = senderSettings["front_uri"] sender.imageStoreID, sender.imageStore, sender.imageStoreConfigured = @@ -27,5 +32,6 @@ func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger sender.logger = logger sender.location = location - return nil + + return sender } diff --git a/senders/pagerduty/init_test.go b/senders/pagerduty/init_test.go index 59c8195c8..e87aaec33 100644 --- a/senders/pagerduty/init_test.go +++ b/senders/pagerduty/init_test.go @@ -12,7 +12,7 @@ import ( . "github.com/smartystreets/goconvey/convey" ) -func TestInit(t *testing.T) { +func TestNewSender(t *testing.T) { logger, _ := logging.ConfigureLog("stdout", "debug", "test", true) location, _ := time.LoadLocation("UTC") mockCtrl := gomock.NewController(t) @@ -20,17 +20,16 @@ func TestInit(t *testing.T) { imageStore := mock_moira_alert.NewMockImageStore(mockCtrl) Convey("Init tests", t, func() { - sender := Sender{ImageStores: map[string]moira.ImageStore{ - "s3": imageStore, - }} - Convey("Has settings", func() { imageStore.EXPECT().IsEnabled().Return(true) senderSettings := map[string]string{ "front_uri": "http://moira.uri", "image_store": "s3", } - sender.Init(senderSettings, logger, location, "15:04") //nolint + imageStores := map[string]moira.ImageStore{ + "s3": imageStore, + } + sender := NewSender(senderSettings, logger, location, imageStores) So(sender.frontURI, ShouldResemble, "http://moira.uri") So(sender.logger, ShouldResemble, logger) So(sender.location, ShouldResemble, location) @@ -42,9 +41,13 @@ func TestInit(t *testing.T) { "front_uri": "http://moira.uri", "image_store": "s4", } - sender.Init(senderSettings, logger, location, "15:04") //nolint - So(sender.imageStoreConfigured, ShouldResemble, false) - So(sender.imageStore, ShouldResemble, nil) + imageStores := map[string]moira.ImageStore{ + "s3": imageStore, + } + sender := NewSender(senderSettings, logger, location, imageStores) + So(sender, ShouldNotBeNil) + So(sender.imageStoreConfigured, ShouldEqual, false) + So(sender.imageStore, ShouldBeNil) }) Convey("image store not configured", func() { imageStore.EXPECT().IsEnabled().Return(false) @@ -52,12 +55,13 @@ func TestInit(t *testing.T) { "front_uri": "http://moira.uri", "image_store": "s3", } - sender := Sender{ImageStores: map[string]moira.ImageStore{ + imageStores := map[string]moira.ImageStore{ "s3": imageStore, - }} - sender.Init(senderSettings, logger, location, "15:04") //nolint - So(sender.imageStoreConfigured, ShouldResemble, false) - So(sender.imageStore, ShouldResemble, nil) + } + sender := NewSender(senderSettings, logger, location, imageStores) + So(sender, ShouldNotBeNil) + So(sender.imageStoreConfigured, ShouldEqual, false) + So(sender.imageStore, ShouldBeNil) }) }) } diff --git a/senders/pushover/pushover.go b/senders/pushover/pushover.go index b8b241128..7c5ffde71 100644 --- a/senders/pushover/pushover.go +++ b/senders/pushover/pushover.go @@ -14,7 +14,8 @@ const printEventsCount int = 5 const titleLimit = 250 const urlLimit = 512 -// Sender implements moira sender interface via pushover +// Sender implements moira sender interface via Pushover. +// Use NewSender to create instance. type Sender struct { logger moira.Logger location *time.Location @@ -24,17 +25,20 @@ type Sender struct { frontURI string } -// Init read yaml config -func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) error { +// NewSender creates Sender instance. +func NewSender(senderSettings map[string]string, logger moira.Logger, location *time.Location) (*Sender, error) { + sender := &Sender{} + sender.apiToken = senderSettings["api_token"] if sender.apiToken == "" { - return fmt.Errorf("can not read pushover api_token from config") + return nil, fmt.Errorf("can not read pushover api_token from config") } sender.client = pushover.New(sender.apiToken) sender.logger = logger sender.frontURI = senderSettings["front_uri"] sender.location = location - return nil + + return sender, nil } // SendEvents implements pushover build and send message functionality diff --git a/senders/pushover/pushover_test.go b/senders/pushover/pushover_test.go index 8b34d8ab1..b5e492e55 100644 --- a/senders/pushover/pushover_test.go +++ b/senders/pushover/pushover_test.go @@ -12,28 +12,25 @@ import ( . "github.com/smartystreets/goconvey/convey" ) -func TestSender_Init(t *testing.T) { +func TestSender_NewSender(t *testing.T) { logger, _ := logging.ConfigureLog("stdout", "debug", "test", true) Convey("Empty map", t, func() { - sender := Sender{} - err := sender.Init(map[string]string{}, logger, nil, "") + sender, err := NewSender(map[string]string{}, logger, nil) So(err, ShouldResemble, fmt.Errorf("can not read pushover api_token from config")) - So(sender, ShouldResemble, Sender{}) + So(sender, ShouldBeNil) }) Convey("Settings has api_token", t, func() { - sender := Sender{} - err := sender.Init(map[string]string{"api_token": "123"}, logger, nil, "") + sender, err := NewSender(map[string]string{"api_token": "123"}, logger, nil) So(err, ShouldBeNil) - So(sender, ShouldResemble, Sender{apiToken: "123", client: pushover.New("123"), logger: logger}) + So(sender, ShouldResemble, &Sender{apiToken: "123", client: pushover.New("123"), logger: logger}) }) Convey("Settings has all data", t, func() { - sender := Sender{} location, _ := time.LoadLocation("UTC") - err := sender.Init(map[string]string{"api_token": "123", "front_uri": "321"}, logger, location, "") + sender, err := NewSender(map[string]string{"api_token": "123", "front_uri": "321"}, logger, location) So(err, ShouldBeNil) - So(sender, ShouldResemble, Sender{apiToken: "123", client: pushover.New("123"), frontURI: "321", logger: logger, location: location}) + So(sender, ShouldResemble, &Sender{apiToken: "123", client: pushover.New("123"), frontURI: "321", logger: logger, location: location}) }) } @@ -176,7 +173,8 @@ func TestMakePushoverMessage(t *testing.T) { Title: "ERROR TriggerName [tag1][tag2] (1)", Message: "02:40: Metric = 123 (OK to ERROR)\n", } - expected.AddAttachment(bytes.NewReader([]byte{1, 0, 1})) //nolint - So(sender.makePushoverMessage(event, trigger, [][]byte{[]byte{1, 0, 1}}, false), ShouldResemble, expected) + err := expected.AddAttachment(bytes.NewReader([]byte{1, 0, 1})) + So(err, ShouldBeNil) + So(sender.makePushoverMessage(event, trigger, [][]byte{{1, 0, 1}}, false), ShouldResemble, expected) }) } diff --git a/senders/script/script.go b/senders/script/script.go index 9065fb656..a0bd4eb6b 100644 --- a/senders/script/script.go +++ b/senders/script/script.go @@ -7,41 +7,44 @@ import ( "os" "os/exec" "strings" - "time" "github.com/moira-alert/moira" ) -// Sender implements moira sender interface via script execution +// Sender implements moira sender interface via script execution. +// Use NewSender to create instance. type Sender struct { exec string logger moira.Logger } -type scriptNotification struct { - Events []moira.NotificationEvent `json:"events"` - Trigger moira.TriggerData `json:"trigger"` - Contact moira.ContactData `json:"contact"` - Throttled bool `json:"throttled"` - Timestamp int64 `json:"timestamp"` -} +// NewSender creates Sender instance. +func NewSender(senderSettings map[string]string, logger moira.Logger) (*Sender, error) { + sender := &Sender{} -// Init read yaml config -func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) error { if senderSettings["name"] == "" { - return fmt.Errorf("required name for sender type script") + return nil, fmt.Errorf("required name for sender type script") } _, _, err := parseExec(senderSettings["exec"]) if err != nil { - return err + return nil, err } sender.exec = senderSettings["exec"] sender.logger = logger - return nil + + return sender, nil +} + +type scriptNotification struct { + Events []moira.NotificationEvent `json:"events"` + Trigger moira.TriggerData `json:"trigger"` + Contact moira.ContactData `json:"contact"` + Throttled bool `json:"throttled"` + Timestamp int64 `json:"timestamp"` } // SendEvents implements Sender interface Send -func (sender *Sender) SendEvents(events moira.NotificationEvents, contact moira.ContactData, trigger moira.TriggerData, plots [][]byte, throttled bool) error { +func (sender *Sender) SendEvents(events moira.NotificationEvents, contact moira.ContactData, trigger moira.TriggerData, _ [][]byte, throttled bool) error { scriptFile, args, scriptBody, err := sender.buildCommandData(events, contact, trigger, throttled) if err != nil { return err diff --git a/senders/script/script_test.go b/senders/script/script_test.go index 100897e9a..578deae38 100644 --- a/senders/script/script_test.go +++ b/senders/script/script_test.go @@ -44,36 +44,35 @@ var execStringTestCases = []execStringTestCase{ }, } -func TestInit(t *testing.T) { +func TestNewSender(t *testing.T) { logger, _ := logging.ConfigureLog("stdout", "debug", "test", true) Convey("Init tests", t, func() { - sender := Sender{} settings := map[string]string{} Convey("Empty map", func() { - err := sender.Init(settings, logger, nil, "") + sender, err := NewSender(settings, logger) So(err, ShouldResemble, fmt.Errorf("required name for sender type script")) - So(sender, ShouldResemble, Sender{}) + So(sender, ShouldBeNil) }) settings["name"] = "script_name" Convey("Empty exec", func() { - err := sender.Init(settings, logger, nil, "") + sender, err := NewSender(settings, logger) So(err, ShouldResemble, fmt.Errorf("file not found")) - So(sender, ShouldResemble, Sender{}) + So(sender, ShouldBeNil) }) Convey("Exec with not exists file", func() { settings["exec"] = "./test_file1" - err := sender.Init(settings, logger, nil, "") + sender, err := NewSender(settings, logger) So(err, ShouldResemble, fmt.Errorf("file ./test_file1 not found")) - So(sender, ShouldResemble, Sender{}) + So(sender, ShouldBeNil) }) Convey("Exec with exists file", func() { settings["exec"] = "script.go" - err := sender.Init(settings, logger, nil, "") + sender, err := NewSender(settings, logger) So(err, ShouldBeNil) - So(sender, ShouldResemble, Sender{exec: "script.go", logger: logger}) + So(sender, ShouldResemble, &Sender{exec: "script.go", logger: logger}) }) }) } diff --git a/senders/selfstate/selfstate.go b/senders/selfstate/selfstate.go index 79c72fbc8..495af2e3a 100644 --- a/senders/selfstate/selfstate.go +++ b/senders/selfstate/selfstate.go @@ -2,25 +2,30 @@ package selfstate import ( "fmt" - "time" "github.com/moira-alert/moira" ) -// Sender implements moira sender interface via selfstate +// Sender implements moira sender interface via selfstate. +// Use NewSender to create instance. type Sender struct { Database moira.Database logger moira.Logger } -// Init read yaml config -func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) error { +// NewSender creates Sender instance. +func NewSender(logger moira.Logger, db moira.Database) *Sender { + sender := &Sender{ + Database: db, + } + sender.logger = logger - return nil + + return sender } // SendEvents implements Sender interface Send -func (sender *Sender) SendEvents(events moira.NotificationEvents, contact moira.ContactData, trigger moira.TriggerData, plots [][]byte, throttled bool) error { +func (sender *Sender) SendEvents(events moira.NotificationEvents, _ moira.ContactData, trigger moira.TriggerData, _ [][]byte, _ bool) error { selfState, err := sender.Database.GetNotifierState() if err != nil { return fmt.Errorf("failed to get notifier state: %s", err.Error()) diff --git a/senders/slack/slack.go b/senders/slack/slack.go index 9940b2e46..d270dca55 100644 --- a/senders/slack/slack.go +++ b/senders/slack/slack.go @@ -40,7 +40,8 @@ var stateEmoji = map[moira.State]string{ moira.StateTEST: testEmoji, } -// Sender implements moira sender interface via slack +// Sender implements moira sender interface via Slack. +// Use NewSender to create instance. type Sender struct { frontURI string useEmoji bool @@ -49,18 +50,21 @@ type Sender struct { client *slack.Client } -// Init read yaml config -func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) error { +// NewSender creates Sender instance. +func NewSender(senderSettings map[string]string, logger moira.Logger, location *time.Location) (*Sender, error) { + sender := &Sender{} + apiToken := senderSettings["api_token"] if apiToken == "" { - return fmt.Errorf("can not read slack api_token from config") + return nil, fmt.Errorf("can not read slack api_token from config") } sender.useEmoji, _ = strconv.ParseBool(senderSettings["use_emoji"]) sender.logger = logger sender.frontURI = senderSettings["front_uri"] sender.location = location sender.client = slack.New(apiToken) - return nil + + return sender, nil } // SendEvents implements Sender interface Send diff --git a/senders/slack/slack_test.go b/senders/slack/slack_test.go index e8b48672f..181c921ae 100644 --- a/senders/slack/slack_test.go +++ b/senders/slack/slack_test.go @@ -12,15 +12,14 @@ import ( . "github.com/smartystreets/goconvey/convey" ) -func TestInit(t *testing.T) { +func TestNewSender(t *testing.T) { logger, _ := logging.ConfigureLog("stdout", "debug", "test", true) Convey("Init tests", t, func() { - sender := Sender{} senderSettings := map[string]string{} Convey("Empty map", func() { - err := sender.Init(senderSettings, logger, nil, "") + sender, err := NewSender(senderSettings, logger, nil) So(err, ShouldResemble, fmt.Errorf("can not read slack api_token from config")) - So(sender, ShouldResemble, Sender{}) + So(sender, ShouldBeNil) }) Convey("has api_token", func() { @@ -28,30 +27,30 @@ func TestInit(t *testing.T) { client := slack.New("123") Convey("use_emoji not set", func() { - err := sender.Init(senderSettings, logger, nil, "") + sender, err := NewSender(senderSettings, logger, nil) So(err, ShouldBeNil) - So(sender, ShouldResemble, Sender{logger: logger, client: client}) + So(sender, ShouldResemble, &Sender{logger: logger, client: client}) }) Convey("use_emoji set to false", func() { senderSettings["use_emoji"] = "false" - err := sender.Init(senderSettings, logger, nil, "") + sender, err := NewSender(senderSettings, logger, nil) So(err, ShouldBeNil) - So(sender, ShouldResemble, Sender{logger: logger, client: client}) + So(sender, ShouldResemble, &Sender{logger: logger, client: client}) }) Convey("use_emoji set to true", func() { senderSettings["use_emoji"] = "true" - err := sender.Init(senderSettings, logger, nil, "") + sender, err := NewSender(senderSettings, logger, nil) So(err, ShouldBeNil) - So(sender, ShouldResemble, Sender{logger: logger, useEmoji: true, client: client}) + So(sender, ShouldResemble, &Sender{logger: logger, useEmoji: true, client: client}) }) Convey("use_emoji set to something wrong", func() { senderSettings["use_emoji"] = "123" - err := sender.Init(senderSettings, logger, nil, "") + sender, err := NewSender(senderSettings, logger, nil) So(err, ShouldBeNil) - So(sender, ShouldResemble, Sender{logger: logger, useEmoji: false, client: client}) + So(sender, ShouldResemble, &Sender{logger: logger, useEmoji: false, client: client}) }) }) }) diff --git a/senders/telegram/init.go b/senders/telegram/init.go index a2f6fb9e3..9dd437b0c 100644 --- a/senders/telegram/init.go +++ b/senders/telegram/init.go @@ -37,11 +37,15 @@ type Sender struct { location *time.Location } -// Init loads yaml config, configures and starts telegram bot -func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) error { +// NewSender creates Sender instance, configures and starts Telegram bot. +func NewSender(senderSettings map[string]string, logger moira.Logger, location *time.Location, db moira.Database) (*Sender, error) { + sender := &Sender{ + DataBase: db, + } + apiToken := senderSettings["api_token"] if apiToken == "" { - return fmt.Errorf("can not read telegram api_token from config") + return nil, fmt.Errorf("can not read telegram api_token from config") } sender.apiToken = apiToken @@ -54,7 +58,7 @@ func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger Poller: &telebot.LongPoller{Timeout: pollerTimeout}, }) if err != nil { - return err + return nil, err } sender.bot.Handle(telebot.OnText, func(message *telebot.Message) { @@ -63,7 +67,8 @@ func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger } }) go sender.runTelebot() - return nil + + return sender, nil } // runTelebot starts telegram bot and manages bot subscriptions diff --git a/senders/telegram/init_test.go b/senders/telegram/init_test.go index 24c8aac3f..ae645ce62 100644 --- a/senders/telegram/init_test.go +++ b/senders/telegram/init_test.go @@ -9,27 +9,24 @@ import ( . "github.com/smartystreets/goconvey/convey" ) -func TestInit(t *testing.T) { +func TestNewSender(t *testing.T) { logger, _ := logging.ConfigureLog("stdout", "debug", "test", true) location, _ := time.LoadLocation("UTC") Convey("Init tests", t, func() { - sender := Sender{} Convey("Empty map", func() { - err := sender.Init(map[string]string{}, logger, nil, "") + sender, err := NewSender(map[string]string{}, logger, nil, nil) So(err, ShouldResemble, fmt.Errorf("can not read telegram api_token from config")) - So(sender, ShouldResemble, Sender{}) + So(sender, ShouldBeNil) }) - Convey("Has settings", func() { + Convey("Incorrect api token", func() { senderSettings := map[string]string{ "api_token": "123", "front_uri": "http://moira.uri", } - sender.Init(senderSettings, logger, location, "15:04") //nolint - So(sender.apiToken, ShouldResemble, "123") - So(sender.frontURI, ShouldResemble, "http://moira.uri") - So(sender.logger, ShouldResemble, logger) - So(sender.location, ShouldResemble, location) + sender, err := NewSender(senderSettings, logger, location, nil) + So(sender, ShouldBeNil) + So(err, ShouldNotBeNil) }) }) } diff --git a/senders/twilio/twilio.go b/senders/twilio/twilio.go index bfff2ab38..b624dc3af 100644 --- a/senders/twilio/twilio.go +++ b/senders/twilio/twilio.go @@ -8,39 +8,31 @@ import ( "github.com/moira-alert/moira" ) -// Sender implements moira sender interface via twilio +// Sender implements moira sender interface via Twilio. +// Use NewSender to create instance. type Sender struct { sender sendEventsTwilio } -type sendEventsTwilio interface { - SendEvents(events moira.NotificationEvents, contact moira.ContactData, trigger moira.TriggerData, plots [][]byte, throttled bool) error -} +// NewSender creates Sender instance. +func NewSender(senderSettings map[string]string, logger moira.Logger, location *time.Location) (*Sender, error) { + sender := &Sender{} -type twilioSender struct { - client *twilio.TwilioClient - APIFromPhone string - logger moira.Logger - location *time.Location -} - -// Init read yaml config -func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) error { apiType := senderSettings["type"] apiASID := senderSettings["api_asid"] if apiASID == "" { - return fmt.Errorf("can not read [%s] api_sid param from config", apiType) + return nil, fmt.Errorf("can not read [%s] api_sid param from config", apiType) } apiAuthToken := senderSettings["api_authtoken"] if apiAuthToken == "" { - return fmt.Errorf("can not read [%s] api_authtoken param from config", apiType) + return nil, fmt.Errorf("can not read [%s] api_authtoken param from config", apiType) } apiFromPhone := senderSettings["api_fromphone"] if apiFromPhone == "" { - return fmt.Errorf("can not read [%s] api_fromphone param from config", apiType) + return nil, fmt.Errorf("can not read [%s] api_fromphone param from config", apiType) } twilioClient := twilio.NewClient(apiASID, apiAuthToken) @@ -61,7 +53,7 @@ func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger voiceURL := senderSettings["voiceurl"] if voiceURL == "" && !twimletsEcho { - return fmt.Errorf("can not read [%s] voiceurl param from config", apiType) + return nil, fmt.Errorf("can not read [%s] voiceurl param from config", apiType) } sender.sender = &twilioSenderVoice{ @@ -72,10 +64,21 @@ func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger } default: - return fmt.Errorf("wrong twilio type: %s", apiType) + return nil, fmt.Errorf("wrong twilio type: %s", apiType) } - return nil + return sender, nil +} + +type sendEventsTwilio interface { + SendEvents(events moira.NotificationEvents, contact moira.ContactData, trigger moira.TriggerData, plots [][]byte, throttled bool) error +} + +type twilioSender struct { + client *twilio.TwilioClient + APIFromPhone string + logger moira.Logger + location *time.Location } // SendEvents implements Sender interface Send diff --git a/senders/twilio/twilio_test.go b/senders/twilio/twilio_test.go index 8d886a788..517ef41fa 100644 --- a/senders/twilio/twilio_test.go +++ b/senders/twilio/twilio_test.go @@ -10,47 +10,46 @@ import ( . "github.com/smartystreets/goconvey/convey" ) -func TestInit(t *testing.T) { +func TestNewSender(t *testing.T) { Convey("Tests init twilio sender", t, func() { - sender := Sender{} logger, _ := logging.ConfigureLog("stdout", "debug", "test", true) location, _ := time.LoadLocation("UTC") settings := map[string]string{} Convey("no api asid", func() { - err := sender.Init(settings, logger, nil, "15:04") + sender, err := NewSender(settings, logger, nil) So(err, ShouldResemble, fmt.Errorf("can not read [%s] api_sid param from config", "")) - So(sender, ShouldResemble, Sender{}) + So(sender, ShouldBeNil) }) settings["api_asid"] = "123" Convey("no api authtoken", func() { - err := sender.Init(settings, logger, nil, "15:04") + sender, err := NewSender(settings, logger, nil) So(err, ShouldResemble, fmt.Errorf("can not read [%s] api_authtoken param from config", "")) - So(sender, ShouldResemble, Sender{}) + So(sender, ShouldBeNil) }) settings["api_authtoken"] = "321" Convey("no api fromphone", func() { - err := sender.Init(settings, logger, nil, "15:04") + sender, err := NewSender(settings, logger, nil) So(err, ShouldResemble, fmt.Errorf("can not read [%s] api_fromphone param from config", "")) - So(sender, ShouldResemble, Sender{}) + So(sender, ShouldBeNil) }) settings["api_fromphone"] = "12345678989" Convey("no api type", func() { - err := sender.Init(settings, logger, nil, "15:04") + sender, err := NewSender(settings, logger, nil) So(err, ShouldResemble, fmt.Errorf("wrong twilio type: %s", "")) - So(sender, ShouldResemble, Sender{}) + So(sender, ShouldBeNil) }) Convey("config sms", func() { settings["type"] = "twilio sms" - err := sender.Init(settings, logger, location, "15:04") + sender, err := NewSender(settings, logger, location) So(err, ShouldBeNil) - So(sender, ShouldResemble, Sender{sender: &twilioSenderSms{ + So(sender, ShouldResemble, &Sender{sender: &twilioSenderSms{ twilioSender{ client: twilio.NewClient("123", "321"), APIFromPhone: "12345678989", @@ -63,18 +62,18 @@ func TestInit(t *testing.T) { Convey("config voice", func() { settings["type"] = "twilio voice" Convey("no voice url", func() { - err := sender.Init(settings, logger, location, "15:04") + sender, err := NewSender(settings, logger, nil) So(err, ShouldResemble, fmt.Errorf("can not read [%s] voiceurl param from config", "twilio voice")) - So(sender, ShouldResemble, Sender{}) + So(sender, ShouldBeNil) }) Convey("has voice url", func() { settings["voiceurl"] = "url here" Convey("append_message == true", func() { settings["append_message"] = "true" - err := sender.Init(settings, logger, location, "15:04") + sender, err := NewSender(settings, logger, location) So(err, ShouldBeNil) - So(sender, ShouldResemble, Sender{sender: &twilioSenderVoice{ + So(sender, ShouldResemble, &Sender{sender: &twilioSenderVoice{ twilioSender: twilioSender{ client: twilio.NewClient("123", "321"), APIFromPhone: "12345678989", @@ -88,9 +87,9 @@ func TestInit(t *testing.T) { Convey("append_message is something another string", func() { settings["append_message"] = "something another string" - err := sender.Init(settings, logger, location, "15:04") + sender, err := NewSender(settings, logger, location) So(err, ShouldBeNil) - So(sender, ShouldResemble, Sender{sender: &twilioSenderVoice{ + So(sender, ShouldResemble, &Sender{sender: &twilioSenderVoice{ twilioSender: twilioSender{ client: twilio.NewClient("123", "321"), APIFromPhone: "12345678989", diff --git a/senders/victorops/init.go b/senders/victorops/init.go index 7c7b7c2eb..101856c14 100644 --- a/senders/victorops/init.go +++ b/senders/victorops/init.go @@ -9,7 +9,8 @@ import ( "github.com/moira-alert/moira" ) -// Sender implements moira sender interface for victorops +// Sender implements moira sender interface for VictorOps. +// Use NewSender to create instance. type Sender struct { DataBase moira.Database ImageStores map[string]moira.ImageStore @@ -24,12 +25,16 @@ type Sender struct { client *api.Client } -// Init loads yaml config, configures the victorops sender -func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) error { +// NewSender creates Sender instance. +func NewSender(senderSettings map[string]string, logger moira.Logger, location *time.Location, imageStores map[string]moira.ImageStore) (*Sender, error) { + sender := &Sender{ + ImageStores: imageStores, + } + var ok bool sender.routingURL, ok = senderSettings["routing_url"] if !ok { - return fmt.Errorf("cannot read the routing url from the yaml config") + return nil, fmt.Errorf("cannot read the routing url from the yaml config") } sender.imageStoreID, ok = senderSettings["image_store"] @@ -50,5 +55,5 @@ func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger sender.logger = logger sender.location = location - return nil + return sender, nil } diff --git a/senders/victorops/init_test.go b/senders/victorops/init_test.go index b3f0d5b3c..6a0b7de79 100644 --- a/senders/victorops/init_test.go +++ b/senders/victorops/init_test.go @@ -14,61 +14,72 @@ import ( . "github.com/smartystreets/goconvey/convey" ) -func TestInit(t *testing.T) { +func TestNewSender(t *testing.T) { logger, _ := logging.ConfigureLog("stdout", "debug", "test", true) location, _ := time.LoadLocation("UTC") mockCtrl := gomock.NewController(t) defer mockCtrl.Finish() - imageStore := mock_moira_alert.NewMockImageStore(mockCtrl) Convey("Init tests", t, func() { - sender := Sender{ImageStores: map[string]moira.ImageStore{ - "s3": imageStore, - }} Convey("Empty map", func() { - err := sender.Init(map[string]string{}, logger, nil, "") + imageStore := mock_moira_alert.NewMockImageStore(mockCtrl) + imageStores := map[string]moira.ImageStore{ + "s3": imageStore, + } + sender, err := NewSender(map[string]string{}, logger, nil, imageStores) So(err, ShouldResemble, fmt.Errorf("cannot read the routing url from the yaml config")) - So(sender, ShouldResemble, Sender{ - ImageStores: map[string]moira.ImageStore{ - "s3": imageStore, - }}) + So(sender, ShouldBeNil) }) Convey("Has settings", func() { + imageStore := mock_moira_alert.NewMockImageStore(mockCtrl) imageStore.EXPECT().IsEnabled().Return(true) + imageStores := map[string]moira.ImageStore{ + "s3": imageStore, + } senderSettings := map[string]string{ "routing_url": "https://testurl.com", "front_uri": "http://moira.uri", "image_store": "s3", } - sender.Init(senderSettings, logger, location, "15:04") //nolint + sender, err := NewSender(senderSettings, logger, location, imageStores) + So(err, ShouldBeNil) So(sender.routingURL, ShouldResemble, "https://testurl.com") So(sender.frontURI, ShouldResemble, "http://moira.uri") So(sender.logger, ShouldResemble, logger) So(sender.location, ShouldResemble, location) So(sender.client, ShouldResemble, api.NewClient("https://testurl.com", nil)) }) + Convey("Wrong image_store name", func() { + imageStore := mock_moira_alert.NewMockImageStore(mockCtrl) + imageStores := map[string]moira.ImageStore{ + "s3": imageStore, + } senderSettings := map[string]string{ "front_uri": "http://moira.uri", "routing_url": "https://testurl.com", "image_store": "s4", } - sender.Init(senderSettings, logger, location, "15:04") //nolint + sender, err := NewSender(senderSettings, logger, location, imageStores) + So(err, ShouldBeNil) So(sender.imageStoreConfigured, ShouldResemble, false) So(sender.imageStore, ShouldResemble, nil) }) + Convey("image store not configured", func() { + imageStore := mock_moira_alert.NewMockImageStore(mockCtrl) imageStore.EXPECT().IsEnabled().Return(false) + imageStores := map[string]moira.ImageStore{ + "s3": imageStore, + } senderSettings := map[string]string{ "front_uri": "http://moira.uri", "routing_url": "https://testurl.com", "image_store": "s3", } - sender := Sender{ImageStores: map[string]moira.ImageStore{ - "s3": imageStore, - }} - sender.Init(senderSettings, logger, location, "15:04") //nolint + sender, err := NewSender(senderSettings, logger, location, imageStores) + So(err, ShouldBeNil) So(sender.imageStoreConfigured, ShouldResemble, false) So(sender.imageStore, ShouldResemble, nil) }) diff --git a/senders/webhook/webhook.go b/senders/webhook/webhook.go index db4abfe28..f0d697fd9 100644 --- a/senders/webhook/webhook.go +++ b/senders/webhook/webhook.go @@ -10,7 +10,8 @@ import ( "github.com/moira-alert/moira" ) -// Sender implements moira sender interface via webhook +// Sender implements moira sender interface via Webhook. +// Use NewSender to create instance. type Sender struct { url string user string @@ -20,15 +21,17 @@ type Sender struct { log moira.Logger } -// Init read yaml config -func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger, location *time.Location, dateTimeFormat string) error { +// NewSender creates Sender instance. +func NewSender(senderSettings map[string]string, logger moira.Logger) (*Sender, error) { + sender := &Sender{} + if senderSettings["name"] == "" { - return fmt.Errorf("required name for sender type webhook") + return nil, fmt.Errorf("required name for sender type webhook") } sender.url = senderSettings["url"] if sender.url == "" { - return fmt.Errorf("can not read url from config") + return nil, fmt.Errorf("can not read url from config") } sender.user, sender.password = senderSettings["user"], senderSettings["password"] @@ -43,7 +46,7 @@ func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger var err error timeout, err = strconv.Atoi(timeoutRaw) if err != nil { - return fmt.Errorf("can not read timeout from config: %s", err.Error()) + return nil, fmt.Errorf("can not read timeout from config: %s", err.Error()) } } @@ -52,7 +55,8 @@ func (sender *Sender) Init(senderSettings map[string]string, logger moira.Logger Timeout: time.Duration(timeout) * time.Second, Transport: &http.Transport{DisableKeepAlives: true}, } - return nil + + return sender, nil } // SendEvents implements Sender interface Send diff --git a/senders/webhook/webhook_test.go b/senders/webhook/webhook_test.go index 9918711e9..3e6c2a7d0 100644 --- a/senders/webhook/webhook_test.go +++ b/senders/webhook/webhook_test.go @@ -10,10 +10,8 @@ import ( "net/url" "strings" "testing" - "time" "github.com/moira-alert/moira" - logging "github.com/moira-alert/moira/logging/zerolog_adapter" . "github.com/smartystreets/goconvey/convey" ) @@ -33,16 +31,19 @@ func TestSender_SendEvents(t *testing.T) { status, err := testRequestURL(r) if err != nil { w.WriteHeader(status) - w.Write([]byte(err.Error())) //nolint + _, err = w.Write([]byte(err.Error())) + So(err, ShouldBeNil) } status, err = testRequestHeaders(r) if err != nil { w.WriteHeader(status) - w.Write([]byte(err.Error())) //nolint + _, err = w.Write([]byte(err.Error())) + So(err, ShouldBeNil) } status, err = testRequestBody(r) if err != nil { - w.Write([]byte(err.Error())) //nolint + _, err = w.Write([]byte(err.Error())) //nolint + So(err, ShouldBeNil) w.WriteHeader(status) } w.WriteHeader(status) @@ -57,8 +58,8 @@ func TestSender_SendEvents(t *testing.T) { "user": testUser, "password": testPass, } - sender := Sender{} - err := sender.Init(senderSettings, logger, time.UTC, "") + + sender, err := NewSender(senderSettings, logger) So(err, ShouldBeNil) err = sender.SendEvents(testEvents, testContact, testTrigger, testPlot, false) From c5233674d3cdd465ed8ba67b3c1a7cfb00dc222b Mon Sep 17 00:00:00 2001 From: Dmitry Redkin Date: Fri, 11 Nov 2022 12:30:59 +0500 Subject: [PATCH 02/10] refactor: Rename files Rename `init.go` files to `sender.go`, because files no more contain Init method. --- senders/discord/{init.go => sender.go} | 0 senders/discord/{init_test.go => sender_test.go} | 0 senders/opsgenie/{init.go => sender.go} | 0 senders/opsgenie/{init_test.go => sender_test.go} | 0 senders/pagerduty/{init.go => sender.go} | 0 senders/pagerduty/{init_test.go => sender_test.go} | 0 senders/telegram/{init.go => sender.go} | 0 senders/telegram/{init_test.go => sender_test.go} | 7 ++++--- senders/victorops/{init.go => sender.go} | 0 senders/victorops/{init_test.go => sender_test.go} | 0 10 files changed, 4 insertions(+), 3 deletions(-) rename senders/discord/{init.go => sender.go} (100%) rename senders/discord/{init_test.go => sender_test.go} (100%) rename senders/opsgenie/{init.go => sender.go} (100%) rename senders/opsgenie/{init_test.go => sender_test.go} (100%) rename senders/pagerduty/{init.go => sender.go} (100%) rename senders/pagerduty/{init_test.go => sender_test.go} (100%) rename senders/telegram/{init.go => sender.go} (100%) rename senders/telegram/{init_test.go => sender_test.go} (75%) rename senders/victorops/{init.go => sender.go} (100%) rename senders/victorops/{init_test.go => sender_test.go} (100%) diff --git a/senders/discord/init.go b/senders/discord/sender.go similarity index 100% rename from senders/discord/init.go rename to senders/discord/sender.go diff --git a/senders/discord/init_test.go b/senders/discord/sender_test.go similarity index 100% rename from senders/discord/init_test.go rename to senders/discord/sender_test.go diff --git a/senders/opsgenie/init.go b/senders/opsgenie/sender.go similarity index 100% rename from senders/opsgenie/init.go rename to senders/opsgenie/sender.go diff --git a/senders/opsgenie/init_test.go b/senders/opsgenie/sender_test.go similarity index 100% rename from senders/opsgenie/init_test.go rename to senders/opsgenie/sender_test.go diff --git a/senders/pagerduty/init.go b/senders/pagerduty/sender.go similarity index 100% rename from senders/pagerduty/init.go rename to senders/pagerduty/sender.go diff --git a/senders/pagerduty/init_test.go b/senders/pagerduty/sender_test.go similarity index 100% rename from senders/pagerduty/init_test.go rename to senders/pagerduty/sender_test.go diff --git a/senders/telegram/init.go b/senders/telegram/sender.go similarity index 100% rename from senders/telegram/init.go rename to senders/telegram/sender.go diff --git a/senders/telegram/init_test.go b/senders/telegram/sender_test.go similarity index 75% rename from senders/telegram/init_test.go rename to senders/telegram/sender_test.go index ae645ce62..fb1241edd 100644 --- a/senders/telegram/init_test.go +++ b/senders/telegram/sender_test.go @@ -1,4 +1,4 @@ -package telegram +package telegram_test import ( "fmt" @@ -6,6 +6,7 @@ import ( "time" logging "github.com/moira-alert/moira/logging/zerolog_adapter" + "github.com/moira-alert/moira/senders/telegram" . "github.com/smartystreets/goconvey/convey" ) @@ -14,7 +15,7 @@ func TestNewSender(t *testing.T) { location, _ := time.LoadLocation("UTC") Convey("Init tests", t, func() { Convey("Empty map", func() { - sender, err := NewSender(map[string]string{}, logger, nil, nil) + sender, err := telegram.NewSender(map[string]string{}, logger, nil, nil) So(err, ShouldResemble, fmt.Errorf("can not read telegram api_token from config")) So(sender, ShouldBeNil) }) @@ -24,7 +25,7 @@ func TestNewSender(t *testing.T) { "api_token": "123", "front_uri": "http://moira.uri", } - sender, err := NewSender(senderSettings, logger, location, nil) + sender, err := telegram.NewSender(senderSettings, logger, location, nil) So(sender, ShouldBeNil) So(err, ShouldNotBeNil) }) diff --git a/senders/victorops/init.go b/senders/victorops/sender.go similarity index 100% rename from senders/victorops/init.go rename to senders/victorops/sender.go diff --git a/senders/victorops/init_test.go b/senders/victorops/sender_test.go similarity index 100% rename from senders/victorops/init_test.go rename to senders/victorops/sender_test.go From cce6f5a56d2b32b91f9821ea175bbcb32414e09f Mon Sep 17 00:00:00 2001 From: Dmitry Redkin Date: Fri, 18 Nov 2022 10:00:50 +0500 Subject: [PATCH 03/10] refactor: Move pkg Moved senders directory to notifier directory, because senders have no sense without notifier, senders is a part of notifier. Also resolve some warnings --- notifier/registrator.go | 30 +++++++++---------- .../senders}/calc_message_parts.go | 0 .../senders}/calc_message_parts_test.go | 0 .../senders}/discord/response.go | 0 .../senders}/discord/response_test.go | 0 {senders => notifier/senders}/discord/send.go | 2 +- .../senders}/discord/send_test.go | 0 .../senders}/discord/sender.go | 0 .../senders}/discord/sender_test.go | 0 {senders => notifier/senders}/mail/mail.go | 0 .../senders}/mail/mail_test.go | 0 {senders => notifier/senders}/mail/send.go | 0 .../senders}/mail/send_test.go | 4 +-- .../senders}/mail/template.go | 0 .../senders}/mattermost/client.go | 0 .../senders}/mattermost/doc.go | 0 .../senders}/mattermost/sender.go | 2 +- .../mattermost/sender_internal_test.go | 0 .../senders}/mattermost/sender_manual_test.go | 2 +- .../senders}/mattermost/sender_test.go | 2 +- .../senders}/msteams/datatypes.go | 0 .../senders}/msteams/msteams.go | 0 .../senders}/msteams/msteams_test.go | 0 .../senders}/opsgenie/send.go | 5 ++-- .../senders}/opsgenie/send_test.go | 0 .../senders}/opsgenie/sender.go | 3 +- .../senders}/opsgenie/sender_test.go | 0 .../senders}/pagerduty/send.go | 0 .../senders}/pagerduty/send_test.go | 0 .../senders}/pagerduty/sender.go | 2 +- .../senders}/pagerduty/sender_test.go | 0 .../senders}/pushover/pushover.go | 0 .../senders}/pushover/pushover_test.go | 0 .../senders}/read_image_store_config.go | 0 .../senders}/read_image_store_config_test.go | 0 .../senders}/script/script.go | 0 .../senders}/script/script_test.go | 0 .../senders}/selfstate/selfstate.go | 0 .../senders}/selfstate/selfstate_test.go | 0 {senders => notifier/senders}/slack/slack.go | 6 ++-- .../senders}/slack/slack_test.go | 0 .../senders}/telegram/handle_message.go | 0 .../senders}/telegram/handle_message_test.go | 0 .../senders}/telegram/send.go | 0 .../senders}/telegram/send_test.go | 0 .../senders}/telegram/sender.go | 0 .../senders}/telegram/sender_test.go | 2 +- {senders => notifier/senders}/twilio/sms.go | 2 +- .../senders}/twilio/sms_test.go | 0 .../senders}/twilio/twilio.go | 0 .../senders}/twilio/twilio_test.go | 0 {senders => notifier/senders}/twilio/voice.go | 2 +- .../senders}/twilio/voice_test.go | 0 .../senders}/victorops/api/alert.go | 0 .../senders}/victorops/api/alert_test.go | 0 .../senders}/victorops/api/client.go | 0 .../senders}/victorops/api/client_test.go | 0 .../senders}/victorops/send.go | 2 +- .../senders}/victorops/send_test.go | 6 ++-- .../senders}/victorops/sender.go | 3 +- .../senders}/victorops/sender_test.go | 2 +- .../senders}/webhook/payload.go | 0 .../senders}/webhook/request.go | 0 .../senders}/webhook/request_test.go | 0 .../senders}/webhook/webhook.go | 0 .../senders}/webhook/webhook_test.go | 0 66 files changed, 39 insertions(+), 38 deletions(-) rename {senders => notifier/senders}/calc_message_parts.go (100%) rename {senders => notifier/senders}/calc_message_parts_test.go (100%) rename {senders => notifier/senders}/discord/response.go (100%) rename {senders => notifier/senders}/discord/response_test.go (100%) rename {senders => notifier/senders}/discord/send.go (98%) rename {senders => notifier/senders}/discord/send_test.go (100%) rename {senders => notifier/senders}/discord/sender.go (100%) rename {senders => notifier/senders}/discord/sender_test.go (100%) rename {senders => notifier/senders}/mail/mail.go (100%) rename {senders => notifier/senders}/mail/mail_test.go (100%) rename {senders => notifier/senders}/mail/send.go (100%) rename {senders => notifier/senders}/mail/send_test.go (96%) rename {senders => notifier/senders}/mail/template.go (100%) rename {senders => notifier/senders}/mattermost/client.go (100%) rename {senders => notifier/senders}/mattermost/doc.go (100%) rename {senders => notifier/senders}/mattermost/sender.go (99%) rename {senders => notifier/senders}/mattermost/sender_internal_test.go (100%) rename {senders => notifier/senders}/mattermost/sender_manual_test.go (95%) rename {senders => notifier/senders}/mattermost/sender_test.go (96%) rename {senders => notifier/senders}/msteams/datatypes.go (100%) rename {senders => notifier/senders}/msteams/msteams.go (100%) rename {senders => notifier/senders}/msteams/msteams_test.go (100%) rename {senders => notifier/senders}/opsgenie/send.go (98%) rename {senders => notifier/senders}/opsgenie/send_test.go (100%) rename {senders => notifier/senders}/opsgenie/sender.go (96%) rename {senders => notifier/senders}/opsgenie/sender_test.go (100%) rename {senders => notifier/senders}/pagerduty/send.go (100%) rename {senders => notifier/senders}/pagerduty/send_test.go (100%) rename {senders => notifier/senders}/pagerduty/sender.go (95%) rename {senders => notifier/senders}/pagerduty/sender_test.go (100%) rename {senders => notifier/senders}/pushover/pushover.go (100%) rename {senders => notifier/senders}/pushover/pushover_test.go (100%) rename {senders => notifier/senders}/read_image_store_config.go (100%) rename {senders => notifier/senders}/read_image_store_config_test.go (100%) rename {senders => notifier/senders}/script/script.go (100%) rename {senders => notifier/senders}/script/script_test.go (100%) rename {senders => notifier/senders}/selfstate/selfstate.go (100%) rename {senders => notifier/senders}/selfstate/selfstate_test.go (100%) rename {senders => notifier/senders}/slack/slack.go (98%) rename {senders => notifier/senders}/slack/slack_test.go (100%) rename {senders => notifier/senders}/telegram/handle_message.go (100%) rename {senders => notifier/senders}/telegram/handle_message_test.go (100%) rename {senders => notifier/senders}/telegram/send.go (100%) rename {senders => notifier/senders}/telegram/send_test.go (100%) rename {senders => notifier/senders}/telegram/sender.go (100%) rename {senders => notifier/senders}/telegram/sender_test.go (93%) rename {senders => notifier/senders}/twilio/sms.go (97%) rename {senders => notifier/senders}/twilio/sms_test.go (100%) rename {senders => notifier/senders}/twilio/twilio.go (100%) rename {senders => notifier/senders}/twilio/twilio_test.go (100%) rename {senders => notifier/senders}/twilio/voice.go (87%) rename {senders => notifier/senders}/twilio/voice_test.go (100%) rename {senders => notifier/senders}/victorops/api/alert.go (100%) rename {senders => notifier/senders}/victorops/api/alert_test.go (100%) rename {senders => notifier/senders}/victorops/api/client.go (100%) rename {senders => notifier/senders}/victorops/api/client_test.go (100%) rename {senders => notifier/senders}/victorops/send.go (98%) rename {senders => notifier/senders}/victorops/send_test.go (98%) rename {senders => notifier/senders}/victorops/sender.go (96%) rename {senders => notifier/senders}/victorops/sender_test.go (97%) rename {senders => notifier/senders}/webhook/payload.go (100%) rename {senders => notifier/senders}/webhook/request.go (100%) rename {senders => notifier/senders}/webhook/request_test.go (100%) rename {senders => notifier/senders}/webhook/webhook.go (100%) rename {senders => notifier/senders}/webhook/webhook_test.go (100%) diff --git a/notifier/registrator.go b/notifier/registrator.go index fac5e09a9..84ac64ef1 100644 --- a/notifier/registrator.go +++ b/notifier/registrator.go @@ -5,21 +5,21 @@ import ( "strings" "github.com/moira-alert/moira" - "github.com/moira-alert/moira/senders/discord" - "github.com/moira-alert/moira/senders/mail" - "github.com/moira-alert/moira/senders/mattermost" - "github.com/moira-alert/moira/senders/msteams" - "github.com/moira-alert/moira/senders/opsgenie" - "github.com/moira-alert/moira/senders/pagerduty" - "github.com/moira-alert/moira/senders/pushover" - "github.com/moira-alert/moira/senders/script" - "github.com/moira-alert/moira/senders/selfstate" - "github.com/moira-alert/moira/senders/slack" - "github.com/moira-alert/moira/senders/telegram" - "github.com/moira-alert/moira/senders/twilio" - "github.com/moira-alert/moira/senders/victorops" - "github.com/moira-alert/moira/senders/webhook" - // "github.com/moira-alert/moira/senders/kontur" + "github.com/moira-alert/moira/notifier/senders/discord" + "github.com/moira-alert/moira/notifier/senders/mail" + "github.com/moira-alert/moira/notifier/senders/mattermost" + "github.com/moira-alert/moira/notifier/senders/msteams" + "github.com/moira-alert/moira/notifier/senders/opsgenie" + "github.com/moira-alert/moira/notifier/senders/pagerduty" + "github.com/moira-alert/moira/notifier/senders/pushover" + "github.com/moira-alert/moira/notifier/senders/script" + "github.com/moira-alert/moira/notifier/senders/selfstate" + "github.com/moira-alert/moira/notifier/senders/slack" + "github.com/moira-alert/moira/notifier/senders/telegram" + "github.com/moira-alert/moira/notifier/senders/twilio" + "github.com/moira-alert/moira/notifier/senders/victorops" + "github.com/moira-alert/moira/notifier/senders/webhook" + // "github.com/moira-alert/moira/notifier/senders/kontur" ) const ( diff --git a/senders/calc_message_parts.go b/notifier/senders/calc_message_parts.go similarity index 100% rename from senders/calc_message_parts.go rename to notifier/senders/calc_message_parts.go diff --git a/senders/calc_message_parts_test.go b/notifier/senders/calc_message_parts_test.go similarity index 100% rename from senders/calc_message_parts_test.go rename to notifier/senders/calc_message_parts_test.go diff --git a/senders/discord/response.go b/notifier/senders/discord/response.go similarity index 100% rename from senders/discord/response.go rename to notifier/senders/discord/response.go diff --git a/senders/discord/response_test.go b/notifier/senders/discord/response_test.go similarity index 100% rename from senders/discord/response_test.go rename to notifier/senders/discord/response_test.go diff --git a/senders/discord/send.go b/notifier/senders/discord/send.go similarity index 98% rename from senders/discord/send.go rename to notifier/senders/discord/send.go index f2fb34ebb..68a52f493 100644 --- a/senders/discord/send.go +++ b/notifier/senders/discord/send.go @@ -9,7 +9,7 @@ import ( "github.com/bwmarrin/discordgo" "github.com/moira-alert/moira" - "github.com/moira-alert/moira/senders" + "github.com/moira-alert/moira/notifier/senders" ) const ( diff --git a/senders/discord/send_test.go b/notifier/senders/discord/send_test.go similarity index 100% rename from senders/discord/send_test.go rename to notifier/senders/discord/send_test.go diff --git a/senders/discord/sender.go b/notifier/senders/discord/sender.go similarity index 100% rename from senders/discord/sender.go rename to notifier/senders/discord/sender.go diff --git a/senders/discord/sender_test.go b/notifier/senders/discord/sender_test.go similarity index 100% rename from senders/discord/sender_test.go rename to notifier/senders/discord/sender_test.go diff --git a/senders/mail/mail.go b/notifier/senders/mail/mail.go similarity index 100% rename from senders/mail/mail.go rename to notifier/senders/mail/mail.go diff --git a/senders/mail/mail_test.go b/notifier/senders/mail/mail_test.go similarity index 100% rename from senders/mail/mail_test.go rename to notifier/senders/mail/mail_test.go diff --git a/senders/mail/send.go b/notifier/senders/mail/send.go similarity index 100% rename from senders/mail/send.go rename to notifier/senders/mail/send.go diff --git a/senders/mail/send_test.go b/notifier/senders/mail/send_test.go similarity index 96% rename from senders/mail/send_test.go rename to notifier/senders/mail/send_test.go index d14a43000..2049ad001 100644 --- a/senders/mail/send_test.go +++ b/notifier/senders/mail/send_test.go @@ -48,7 +48,7 @@ some other text _italics text_`, } Convey("Make message", t, func() { - message := sender.makeMessage(generateTestEvents(10, trigger.ID), contact, trigger, [][]byte{[]byte{1, 0, 1}}, true) //nolint + message := sender.makeMessage(generateTestEvents(10, trigger.ID), contact, trigger, [][]byte{{1, 0, 1}}, true) So(message.GetHeader("From")[0], ShouldEqual, sender.From) So(message.GetHeader("To")[0], ShouldEqual, contact.Value) @@ -107,7 +107,7 @@ func TestEmptyTriggerID(t *testing.T) { } Convey("Make message", t, func() { - message := sender.makeMessage(generateTestEvents(10, trigger.ID), contact, trigger, [][]byte{[]byte{1, 0, 1}}, true) //nolint + message := sender.makeMessage(generateTestEvents(10, trigger.ID), contact, trigger, [][]byte{{1, 0, 1}}, true) So(message.GetHeader("From")[0], ShouldEqual, sender.From) So(message.GetHeader("To")[0], ShouldEqual, contact.Value) messageStr := new(bytes.Buffer) diff --git a/senders/mail/template.go b/notifier/senders/mail/template.go similarity index 100% rename from senders/mail/template.go rename to notifier/senders/mail/template.go diff --git a/senders/mattermost/client.go b/notifier/senders/mattermost/client.go similarity index 100% rename from senders/mattermost/client.go rename to notifier/senders/mattermost/client.go diff --git a/senders/mattermost/doc.go b/notifier/senders/mattermost/doc.go similarity index 100% rename from senders/mattermost/doc.go rename to notifier/senders/mattermost/doc.go diff --git a/senders/mattermost/sender.go b/notifier/senders/mattermost/sender.go similarity index 99% rename from senders/mattermost/sender.go rename to notifier/senders/mattermost/sender.go index bc672d6be..c8331a3a1 100644 --- a/senders/mattermost/sender.go +++ b/notifier/senders/mattermost/sender.go @@ -9,7 +9,7 @@ import ( "time" "github.com/moira-alert/moira" - "github.com/moira-alert/moira/senders" + "github.com/moira-alert/moira/notifier/senders" "github.com/mattermost/mattermost-server/v6/model" ) diff --git a/senders/mattermost/sender_internal_test.go b/notifier/senders/mattermost/sender_internal_test.go similarity index 100% rename from senders/mattermost/sender_internal_test.go rename to notifier/senders/mattermost/sender_internal_test.go diff --git a/senders/mattermost/sender_manual_test.go b/notifier/senders/mattermost/sender_manual_test.go similarity index 95% rename from senders/mattermost/sender_manual_test.go rename to notifier/senders/mattermost/sender_manual_test.go index bf59ef657..dd73787a1 100644 --- a/senders/mattermost/sender_manual_test.go +++ b/notifier/senders/mattermost/sender_manual_test.go @@ -7,7 +7,7 @@ import ( "time" "github.com/moira-alert/moira" - "github.com/moira-alert/moira/senders/mattermost" + "github.com/moira-alert/moira/notifier/senders/mattermost" . "github.com/smartystreets/goconvey/convey" ) diff --git a/senders/mattermost/sender_test.go b/notifier/senders/mattermost/sender_test.go similarity index 96% rename from senders/mattermost/sender_test.go rename to notifier/senders/mattermost/sender_test.go index 216efcd4b..4cbf21754 100644 --- a/senders/mattermost/sender_test.go +++ b/notifier/senders/mattermost/sender_test.go @@ -3,7 +3,7 @@ package mattermost_test import ( "testing" - "github.com/moira-alert/moira/senders/mattermost" + "github.com/moira-alert/moira/notifier/senders/mattermost" . "github.com/smartystreets/goconvey/convey" ) diff --git a/senders/msteams/datatypes.go b/notifier/senders/msteams/datatypes.go similarity index 100% rename from senders/msteams/datatypes.go rename to notifier/senders/msteams/datatypes.go diff --git a/senders/msteams/msteams.go b/notifier/senders/msteams/msteams.go similarity index 100% rename from senders/msteams/msteams.go rename to notifier/senders/msteams/msteams.go diff --git a/senders/msteams/msteams_test.go b/notifier/senders/msteams/msteams_test.go similarity index 100% rename from senders/msteams/msteams_test.go rename to notifier/senders/msteams/msteams_test.go diff --git a/senders/opsgenie/send.go b/notifier/senders/opsgenie/send.go similarity index 98% rename from senders/opsgenie/send.go rename to notifier/senders/opsgenie/send.go index d078c9862..4cd1982e0 100644 --- a/senders/opsgenie/send.go +++ b/notifier/senders/opsgenie/send.go @@ -7,9 +7,10 @@ import ( "strings" "github.com/moira-alert/moira" - "github.com/moira-alert/moira/senders" + "github.com/moira-alert/moira/notifier/senders" + "github.com/opsgenie/opsgenie-go-sdk-v2/alert" - blackfriday "github.com/russross/blackfriday/v2" + "github.com/russross/blackfriday/v2" ) const ( diff --git a/senders/opsgenie/send_test.go b/notifier/senders/opsgenie/send_test.go similarity index 100% rename from senders/opsgenie/send_test.go rename to notifier/senders/opsgenie/send_test.go diff --git a/senders/opsgenie/sender.go b/notifier/senders/opsgenie/sender.go similarity index 96% rename from senders/opsgenie/sender.go rename to notifier/senders/opsgenie/sender.go index 3e81a8cd2..5c5908b3a 100644 --- a/senders/opsgenie/sender.go +++ b/notifier/senders/opsgenie/sender.go @@ -5,7 +5,8 @@ import ( "time" "github.com/moira-alert/moira" - "github.com/moira-alert/moira/senders" + "github.com/moira-alert/moira/notifier/senders" + "github.com/opsgenie/opsgenie-go-sdk-v2/alert" "github.com/opsgenie/opsgenie-go-sdk-v2/client" ) diff --git a/senders/opsgenie/sender_test.go b/notifier/senders/opsgenie/sender_test.go similarity index 100% rename from senders/opsgenie/sender_test.go rename to notifier/senders/opsgenie/sender_test.go diff --git a/senders/pagerduty/send.go b/notifier/senders/pagerduty/send.go similarity index 100% rename from senders/pagerduty/send.go rename to notifier/senders/pagerduty/send.go diff --git a/senders/pagerduty/send_test.go b/notifier/senders/pagerduty/send_test.go similarity index 100% rename from senders/pagerduty/send_test.go rename to notifier/senders/pagerduty/send_test.go diff --git a/senders/pagerduty/sender.go b/notifier/senders/pagerduty/sender.go similarity index 95% rename from senders/pagerduty/sender.go rename to notifier/senders/pagerduty/sender.go index 21819fb80..84b483f8f 100644 --- a/senders/pagerduty/sender.go +++ b/notifier/senders/pagerduty/sender.go @@ -4,7 +4,7 @@ import ( "time" "github.com/moira-alert/moira" - "github.com/moira-alert/moira/senders" + "github.com/moira-alert/moira/notifier/senders" ) // Sender implements moira sender interface for PagerDuty. diff --git a/senders/pagerduty/sender_test.go b/notifier/senders/pagerduty/sender_test.go similarity index 100% rename from senders/pagerduty/sender_test.go rename to notifier/senders/pagerduty/sender_test.go diff --git a/senders/pushover/pushover.go b/notifier/senders/pushover/pushover.go similarity index 100% rename from senders/pushover/pushover.go rename to notifier/senders/pushover/pushover.go diff --git a/senders/pushover/pushover_test.go b/notifier/senders/pushover/pushover_test.go similarity index 100% rename from senders/pushover/pushover_test.go rename to notifier/senders/pushover/pushover_test.go diff --git a/senders/read_image_store_config.go b/notifier/senders/read_image_store_config.go similarity index 100% rename from senders/read_image_store_config.go rename to notifier/senders/read_image_store_config.go diff --git a/senders/read_image_store_config_test.go b/notifier/senders/read_image_store_config_test.go similarity index 100% rename from senders/read_image_store_config_test.go rename to notifier/senders/read_image_store_config_test.go diff --git a/senders/script/script.go b/notifier/senders/script/script.go similarity index 100% rename from senders/script/script.go rename to notifier/senders/script/script.go diff --git a/senders/script/script_test.go b/notifier/senders/script/script_test.go similarity index 100% rename from senders/script/script_test.go rename to notifier/senders/script/script_test.go diff --git a/senders/selfstate/selfstate.go b/notifier/senders/selfstate/selfstate.go similarity index 100% rename from senders/selfstate/selfstate.go rename to notifier/senders/selfstate/selfstate.go diff --git a/senders/selfstate/selfstate_test.go b/notifier/senders/selfstate/selfstate_test.go similarity index 100% rename from senders/selfstate/selfstate_test.go rename to notifier/senders/selfstate/selfstate_test.go diff --git a/senders/slack/slack.go b/notifier/senders/slack/slack.go similarity index 98% rename from senders/slack/slack.go rename to notifier/senders/slack/slack.go index d270dca55..44d610e2b 100644 --- a/senders/slack/slack.go +++ b/notifier/senders/slack/slack.go @@ -7,11 +7,11 @@ import ( "strings" "time" - slackdown "github.com/karriereat/blackfriday-slack" "github.com/moira-alert/moira" - "github.com/moira-alert/moira/senders" - blackfriday "github.com/russross/blackfriday/v2" + "github.com/moira-alert/moira/notifier/senders" + slackdown "github.com/karriereat/blackfriday-slack" + "github.com/russross/blackfriday/v2" "github.com/slack-go/slack" ) diff --git a/senders/slack/slack_test.go b/notifier/senders/slack/slack_test.go similarity index 100% rename from senders/slack/slack_test.go rename to notifier/senders/slack/slack_test.go diff --git a/senders/telegram/handle_message.go b/notifier/senders/telegram/handle_message.go similarity index 100% rename from senders/telegram/handle_message.go rename to notifier/senders/telegram/handle_message.go diff --git a/senders/telegram/handle_message_test.go b/notifier/senders/telegram/handle_message_test.go similarity index 100% rename from senders/telegram/handle_message_test.go rename to notifier/senders/telegram/handle_message_test.go diff --git a/senders/telegram/send.go b/notifier/senders/telegram/send.go similarity index 100% rename from senders/telegram/send.go rename to notifier/senders/telegram/send.go diff --git a/senders/telegram/send_test.go b/notifier/senders/telegram/send_test.go similarity index 100% rename from senders/telegram/send_test.go rename to notifier/senders/telegram/send_test.go diff --git a/senders/telegram/sender.go b/notifier/senders/telegram/sender.go similarity index 100% rename from senders/telegram/sender.go rename to notifier/senders/telegram/sender.go diff --git a/senders/telegram/sender_test.go b/notifier/senders/telegram/sender_test.go similarity index 93% rename from senders/telegram/sender_test.go rename to notifier/senders/telegram/sender_test.go index fb1241edd..84a83b47c 100644 --- a/senders/telegram/sender_test.go +++ b/notifier/senders/telegram/sender_test.go @@ -6,7 +6,7 @@ import ( "time" logging "github.com/moira-alert/moira/logging/zerolog_adapter" - "github.com/moira-alert/moira/senders/telegram" + "github.com/moira-alert/moira/notifier/senders/telegram" . "github.com/smartystreets/goconvey/convey" ) diff --git a/senders/twilio/sms.go b/notifier/senders/twilio/sms.go similarity index 97% rename from senders/twilio/sms.go rename to notifier/senders/twilio/sms.go index b25ab0709..8a4b3772e 100644 --- a/senders/twilio/sms.go +++ b/notifier/senders/twilio/sms.go @@ -14,7 +14,7 @@ type twilioSenderSms struct { twilioSender } -func (sender *twilioSenderSms) SendEvents(events moira.NotificationEvents, contact moira.ContactData, trigger moira.TriggerData, plots [][]byte, throttled bool) error { +func (sender *twilioSenderSms) SendEvents(events moira.NotificationEvents, contact moira.ContactData, trigger moira.TriggerData, _ [][]byte, throttled bool) error { message := sender.buildMessage(events, trigger, throttled) sender.logger.Debugf("Calling twilio sms api to phone %s and message body %s", contact.Value, message) twilioMessage, err := twilio.NewMessage(sender.client, sender.APIFromPhone, contact.Value, twilio.Body(message)) diff --git a/senders/twilio/sms_test.go b/notifier/senders/twilio/sms_test.go similarity index 100% rename from senders/twilio/sms_test.go rename to notifier/senders/twilio/sms_test.go diff --git a/senders/twilio/twilio.go b/notifier/senders/twilio/twilio.go similarity index 100% rename from senders/twilio/twilio.go rename to notifier/senders/twilio/twilio.go diff --git a/senders/twilio/twilio_test.go b/notifier/senders/twilio/twilio_test.go similarity index 100% rename from senders/twilio/twilio_test.go rename to notifier/senders/twilio/twilio_test.go diff --git a/senders/twilio/voice.go b/notifier/senders/twilio/voice.go similarity index 87% rename from senders/twilio/voice.go rename to notifier/senders/twilio/voice.go index 116d9d569..9f7483936 100644 --- a/senders/twilio/voice.go +++ b/notifier/senders/twilio/voice.go @@ -17,7 +17,7 @@ type twilioSenderVoice struct { twimletsEcho bool } -func (sender *twilioSenderVoice) SendEvents(events moira.NotificationEvents, contact moira.ContactData, trigger moira.TriggerData, plots [][]byte, throttled bool) error { +func (sender *twilioSenderVoice) SendEvents(_ moira.NotificationEvents, contact moira.ContactData, trigger moira.TriggerData, _ [][]byte, _ bool) error { voiceURL := sender.buildVoiceURL(trigger) twilioCall, err := twilio.NewCall(sender.client, sender.APIFromPhone, contact.Value, twilio.Callback(voiceURL)) if err != nil { diff --git a/senders/twilio/voice_test.go b/notifier/senders/twilio/voice_test.go similarity index 100% rename from senders/twilio/voice_test.go rename to notifier/senders/twilio/voice_test.go diff --git a/senders/victorops/api/alert.go b/notifier/senders/victorops/api/alert.go similarity index 100% rename from senders/victorops/api/alert.go rename to notifier/senders/victorops/api/alert.go diff --git a/senders/victorops/api/alert_test.go b/notifier/senders/victorops/api/alert_test.go similarity index 100% rename from senders/victorops/api/alert_test.go rename to notifier/senders/victorops/api/alert_test.go diff --git a/senders/victorops/api/client.go b/notifier/senders/victorops/api/client.go similarity index 100% rename from senders/victorops/api/client.go rename to notifier/senders/victorops/api/client.go diff --git a/senders/victorops/api/client_test.go b/notifier/senders/victorops/api/client_test.go similarity index 100% rename from senders/victorops/api/client_test.go rename to notifier/senders/victorops/api/client_test.go diff --git a/senders/victorops/send.go b/notifier/senders/victorops/send.go similarity index 98% rename from senders/victorops/send.go rename to notifier/senders/victorops/send.go index 85c9b1cc2..bc420474c 100644 --- a/senders/victorops/send.go +++ b/notifier/senders/victorops/send.go @@ -8,7 +8,7 @@ import ( stripmd "github.com/writeas/go-strip-markdown" "github.com/moira-alert/moira" - "github.com/moira-alert/moira/senders/victorops/api" + "github.com/moira-alert/moira/notifier/senders/victorops/api" ) // SendEvents implements Sender interface Send diff --git a/senders/victorops/send_test.go b/notifier/senders/victorops/send_test.go similarity index 98% rename from senders/victorops/send_test.go rename to notifier/senders/victorops/send_test.go index d2fb0e857..25b3a4570 100644 --- a/senders/victorops/send_test.go +++ b/notifier/senders/victorops/send_test.go @@ -4,11 +4,11 @@ import ( "testing" "time" - "github.com/golang/mock/gomock" - "github.com/moira-alert/moira/senders/victorops/api" - "github.com/moira-alert/moira" mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert" + "github.com/moira-alert/moira/notifier/senders/victorops/api" + + "github.com/golang/mock/gomock" . "github.com/smartystreets/goconvey/convey" ) diff --git a/senders/victorops/sender.go b/notifier/senders/victorops/sender.go similarity index 96% rename from senders/victorops/sender.go rename to notifier/senders/victorops/sender.go index 101856c14..48e65d7a4 100644 --- a/senders/victorops/sender.go +++ b/notifier/senders/victorops/sender.go @@ -4,9 +4,8 @@ import ( "fmt" "time" - "github.com/moira-alert/moira/senders/victorops/api" - "github.com/moira-alert/moira" + "github.com/moira-alert/moira/notifier/senders/victorops/api" ) // Sender implements moira sender interface for VictorOps. diff --git a/senders/victorops/sender_test.go b/notifier/senders/victorops/sender_test.go similarity index 97% rename from senders/victorops/sender_test.go rename to notifier/senders/victorops/sender_test.go index 6a0b7de79..a36f2d5a2 100644 --- a/senders/victorops/sender_test.go +++ b/notifier/senders/victorops/sender_test.go @@ -7,7 +7,7 @@ import ( "github.com/golang/mock/gomock" "github.com/moira-alert/moira" - "github.com/moira-alert/moira/senders/victorops/api" + "github.com/moira-alert/moira/notifier/senders/victorops/api" logging "github.com/moira-alert/moira/logging/zerolog_adapter" mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert" diff --git a/senders/webhook/payload.go b/notifier/senders/webhook/payload.go similarity index 100% rename from senders/webhook/payload.go rename to notifier/senders/webhook/payload.go diff --git a/senders/webhook/request.go b/notifier/senders/webhook/request.go similarity index 100% rename from senders/webhook/request.go rename to notifier/senders/webhook/request.go diff --git a/senders/webhook/request_test.go b/notifier/senders/webhook/request_test.go similarity index 100% rename from senders/webhook/request_test.go rename to notifier/senders/webhook/request_test.go diff --git a/senders/webhook/webhook.go b/notifier/senders/webhook/webhook.go similarity index 100% rename from senders/webhook/webhook.go rename to notifier/senders/webhook/webhook.go diff --git a/senders/webhook/webhook_test.go b/notifier/senders/webhook/webhook_test.go similarity index 100% rename from senders/webhook/webhook_test.go rename to notifier/senders/webhook/webhook_test.go From 045b1968d1469905da789ad4ab8af8a4f5ded0a2 Mon Sep 17 00:00:00 2001 From: Dmitry Redkin Date: Mon, 28 Nov 2022 14:33:19 +0500 Subject: [PATCH 04/10] refactor --- notifier/registrator.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/notifier/registrator.go b/notifier/registrator.go index 84ac64ef1..c8af79bb3 100644 --- a/notifier/registrator.go +++ b/notifier/registrator.go @@ -42,12 +42,14 @@ const ( // RegisterSenders creates all senders and registers them. func (notifier *StandardNotifier) RegisterSenders(connector moira.Database) error { //nolint - var err error + var ( + sender moira.Sender + err error + ) + for _, senderSettings := range notifier.config.Senders { senderSettings["front_uri"] = notifier.config.FrontURL - var sender moira.Sender - switch senderSettings["type"] { case mailSender: sender, err = mail.NewSender(senderSettings, notifier.logger, notifier.config.Location, notifier.config.DateTimeFormat) From 4299b00ae73a9e049a0b2c12695024082b0fec52 Mon Sep 17 00:00:00 2001 From: Dmitry Redkin Date: Mon, 28 Nov 2022 14:42:10 +0500 Subject: [PATCH 05/10] refactor: Discord --- notifier/senders/discord/sender.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/notifier/senders/discord/sender.go b/notifier/senders/discord/sender.go index 82ecdeed1..73657cf43 100644 --- a/notifier/senders/discord/sender.go +++ b/notifier/senders/discord/sender.go @@ -29,27 +29,26 @@ type Sender struct { // NewSender creates Sender instance. func NewSender(senderSettings map[string]string, logger moira.Logger, location *time.Location, db moira.Database) (*Sender, error) { - sender := &Sender{ - DataBase: db, - } - - var err error token := senderSettings["token"] if token == "" { return nil, fmt.Errorf("cannot read the discord token from the config") } - sender.session, err = discordgo.New("Bot " + token) + session, err := discordgo.New("Bot " + token) if err != nil { return nil, fmt.Errorf("error creating discord session: %s", err) } - sender.logger = logger - sender.frontURI = senderSettings["front_uri"] - sender.location = location + sender := &Sender{ + DataBase: db, + session: session, + logger: logger, + frontURI: senderSettings["front_uri"], + location: location, + } handleMsg := func(s *discordgo.Session, m *discordgo.MessageCreate) { channel, err := s.Channel(m.ChannelID) if err != nil { - sender.logger.Errorf("error while getting the channel details: %s", err) + logger.Errorf("error while getting the channel details: %s", err) } msg, err := sender.getResponse(m, channel) From 29b8133bdb8a2dc249d55f4c9bb2b3e998d9bcaa Mon Sep 17 00:00:00 2001 From: Dmitry Redkin Date: Thu, 1 Dec 2022 12:00:57 +0500 Subject: [PATCH 06/10] Remove nolint --- notifier/senders/msteams/msteams.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/notifier/senders/msteams/msteams.go b/notifier/senders/msteams/msteams.go index e57693bb0..a09eb8c01 100644 --- a/notifier/senders/msteams/msteams.go +++ b/notifier/senders/msteams/msteams.go @@ -57,8 +57,9 @@ func NewSender(senderSettings map[string]string, logger moira.Logger, location * return nil, fmt.Errorf("max_events should be an integer: %w", err) } sender.maxEvents = maxEvents + const timeout = time.Duration(30) * time.Second sender.client = &http.Client{ - Timeout: time.Duration(30) * time.Second, //nolint + Timeout: timeout, } return sender, nil From c2474897e3d6e3b7ef0bc80b359d8ea4b38d5a24 Mon Sep 17 00:00:00 2001 From: Dmitry Redkin Date: Thu, 1 Dec 2022 12:11:02 +0500 Subject: [PATCH 07/10] Move it --- notifier/senders/script/script.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/notifier/senders/script/script.go b/notifier/senders/script/script.go index a0bd4eb6b..dcf567e9e 100644 --- a/notifier/senders/script/script.go +++ b/notifier/senders/script/script.go @@ -20,8 +20,6 @@ type Sender struct { // NewSender creates Sender instance. func NewSender(senderSettings map[string]string, logger moira.Logger) (*Sender, error) { - sender := &Sender{} - if senderSettings["name"] == "" { return nil, fmt.Errorf("required name for sender type script") } @@ -29,8 +27,11 @@ func NewSender(senderSettings map[string]string, logger moira.Logger) (*Sender, if err != nil { return nil, err } - sender.exec = senderSettings["exec"] - sender.logger = logger + + sender := &Sender{ + exec: senderSettings["exec"], + logger: logger, + } return sender, nil } From 7e0009df299f5e53360e38f873b6c954bc9d7bf2 Mon Sep 17 00:00:00 2001 From: Dmitry Redkin Date: Thu, 1 Dec 2022 12:13:34 +0500 Subject: [PATCH 08/10] small refactor --- notifier/senders/selfstate/selfstate.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/notifier/senders/selfstate/selfstate.go b/notifier/senders/selfstate/selfstate.go index 495af2e3a..74a183e14 100644 --- a/notifier/senders/selfstate/selfstate.go +++ b/notifier/senders/selfstate/selfstate.go @@ -15,13 +15,10 @@ type Sender struct { // NewSender creates Sender instance. func NewSender(logger moira.Logger, db moira.Database) *Sender { - sender := &Sender{ + return &Sender{ Database: db, + logger: logger, } - - sender.logger = logger - - return sender } // SendEvents implements Sender interface Send From 8ad4ba1a99c3a7ff08bdcee3afbebc3332a04e92 Mon Sep 17 00:00:00 2001 From: Dmitry Redkin Date: Thu, 1 Dec 2022 12:15:04 +0500 Subject: [PATCH 09/10] Remove nolint --- notifier/senders/webhook/webhook_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notifier/senders/webhook/webhook_test.go b/notifier/senders/webhook/webhook_test.go index 3e6c2a7d0..735a18df5 100644 --- a/notifier/senders/webhook/webhook_test.go +++ b/notifier/senders/webhook/webhook_test.go @@ -42,7 +42,7 @@ func TestSender_SendEvents(t *testing.T) { } status, err = testRequestBody(r) if err != nil { - _, err = w.Write([]byte(err.Error())) //nolint + _, err = w.Write([]byte(err.Error())) So(err, ShouldBeNil) w.WriteHeader(status) } From c6e66a55d3e503cb001cca25379dee5f6a46182e Mon Sep 17 00:00:00 2001 From: Dmitry Redkin Date: Wed, 7 Dec 2022 11:10:28 +0500 Subject: [PATCH 10/10] refactor: rename --- integration_tests/notifier/notifier_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/integration_tests/notifier/notifier_test.go b/integration_tests/notifier/notifier_test.go index 79d7e1746..d18fe9ca5 100644 --- a/integration_tests/notifier/notifier_test.go +++ b/integration_tests/notifier/notifier_test.go @@ -14,7 +14,7 @@ import ( logging "github.com/moira-alert/moira/logging/zerolog_adapter" "github.com/moira-alert/moira/metrics" mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert" - "github.com/moira-alert/moira/notifier" + . "github.com/moira-alert/moira/notifier" "github.com/moira-alert/moira/notifier/events" "github.com/moira-alert/moira/notifier/notifications" . "github.com/smartystreets/goconvey/convey" @@ -27,12 +27,12 @@ var senderSettings = map[string]string{ var location, _ = time.LoadLocation("UTC") var dateTimeFormat = "15:04 02.01.2006" -var notifierConfig = notifier.Config{ +var notifierConfig = Config{ SendingTimeout: time.Millisecond * 10, ResendingTimeout: time.Hour * 24, Location: location, DateTimeFormat: dateTimeFormat, - ReadBatchSize: notifier.NotificationsLimitUnlimited, + ReadBatchSize: NotificationsLimitUnlimited, } var shutdown = make(chan struct{}) @@ -92,9 +92,9 @@ func TestNotifier(t *testing.T) { err = database.PushNotificationEvent(&event, true) So(err, ShouldBeNil) - notifier2 := notifier.NewNotifier(database, logger, notifierConfig, notifierMetrics, metricsSourceProvider, map[string]moira.ImageStore{}) + notifier := NewNotifier(database, logger, notifierConfig, notifierMetrics, metricsSourceProvider, map[string]moira.ImageStore{}) sender := mock_moira_alert.NewMockSender(mockCtrl) - err = notifier2.RegisterSender(senderSettings, sender) + err = notifier.RegisterSender(senderSettings, sender) So(err, ShouldBeNil) sender.EXPECT().SendEvents(gomock.Any(), contact, triggerData, gomock.Any(), false).Return(nil).Do(func(arg0, arg1, arg2, arg3, arg4 interface{}) { fmt.Print("SendEvents called. End test") @@ -105,13 +105,13 @@ func TestNotifier(t *testing.T) { Database: database, Logger: logger, Metrics: notifierMetrics, - Scheduler: notifier.NewScheduler(database, logger, notifierMetrics), + Scheduler: NewScheduler(database, logger, notifierMetrics), } fetchNotificationsWorker := notifications.FetchNotificationsWorker{ Database: database, Logger: logger, - Notifier: notifier2, + Notifier: notifier, } fetchEventsWorker.Start()