Skip to content

Commit

Permalink
Merge pull request #23 from multiversx/more-notifiers-instances
Browse files Browse the repository at this point in the history
Added the possibility to define more than one Pushover & Telegram notifiers
  • Loading branch information
iulianpascalau authored May 31, 2024
2 parents 68ad905 + fd47da0 commit 034273a
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 24 deletions.
10 changes: 10 additions & 0 deletions cmd/monitor/config/example/credentials.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
[Pushover]
Token=""
UserKey=""
# to add more pushover notifiers, uncomment and use the example below
# Additional = [
# { Token = "token P2", UserKey = "userKey P2"},
# { Token = "token P3", UserKey = "userKey P3"},
# ]

[Smtp]
Email="[email protected]"
Expand All @@ -9,3 +14,8 @@
[Telegram]
Token=""
ChatID=""
# to add more telegram notifiers, uncomment and use the example below
# Additional = [
# { Token = "token T2", ChatID = "chatID T2"},
# { Token = "token T3", ChatID = "chatID T3"},
# ]
20 changes: 16 additions & 4 deletions config/credentialsConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@ type CredentialsConfig struct {
Telegram TelegramCredentialsConfig
}

// PushoverCredentialsConfig defines the Pushover service credentials
type PushoverCredentialsConfig struct {
// TokenUserKeyConfig defines a struct that contains one token and one user key
type TokenUserKeyConfig struct {
Token string
UserKey string
}

// TokenChatIDConfig defines a struct that contains one token and one chat ID
type TokenChatIDConfig struct {
Token string
ChatID string
}

// PushoverCredentialsConfig defines the Pushover service credentials
type PushoverCredentialsConfig struct {
TokenUserKeyConfig
Additional []TokenUserKeyConfig
}

// EmailPasswordConfig defines an email-password credential
type EmailPasswordConfig struct {
Email string
Expand All @@ -21,6 +33,6 @@ type EmailPasswordConfig struct {

// TelegramCredentialsConfig defines the Telegram service credentials
type TelegramCredentialsConfig struct {
Token string
ChatID string
TokenChatIDConfig
Additional []TokenChatIDConfig
}
48 changes: 40 additions & 8 deletions config/tomlConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,30 +134,62 @@ func TestLoadCredentialsConfig(t *testing.T) {

testString := `
[Pushover]
Token="token1"
UserKey="userKey1"
Token="token P1"
UserKey="userKey P1"
Additional = [
{ Token = "token P2", UserKey = "userKey P2"},
{ Token = "token P3", UserKey = "userKey P3"},
]
[Smtp]
Email="[email protected]"
Password="password"
[Telegram]
Token="token2"
ChatID="chat_id2"
Token="token T1"
ChatID="chatID T1"
Additional = [
{ Token = "token T2", ChatID = "chatID T2"},
{ Token = "token T3", ChatID = "chatID T3"},
]
`

expectedCfg := CredentialsConfig{
Pushover: PushoverCredentialsConfig{
Token: "token1",
UserKey: "userKey1",
TokenUserKeyConfig: TokenUserKeyConfig{
Token: "token P1",
UserKey: "userKey P1",
},
Additional: []TokenUserKeyConfig{
{
Token: "token P2",
UserKey: "userKey P2",
},
{
Token: "token P3",
UserKey: "userKey P3",
},
},
},
Smtp: EmailPasswordConfig{
Email: "[email protected]",
Password: "password",
},
Telegram: TelegramCredentialsConfig{
Token: "token2",
ChatID: "chat_id2",
TokenChatIDConfig: TokenChatIDConfig{
Token: "token T1",
ChatID: "chatID T1",
},
Additional: []TokenChatIDConfig{
{
Token: "token T2",
ChatID: "chatID T2",
},
{
Token: "token T3",
ChatID: "chatID T3",
},
},
},
}

Expand Down
62 changes: 50 additions & 12 deletions factory/notifiersFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ func CreateOutputNotifiers(allConfig config.AllConfigs) ([]executors.OutputNotif
outputNotifiers = append(outputNotifiers, logNotifier)

if allConfig.Config.OutputNotifiers.Pushover.Enabled {
outputNotifiers = append(outputNotifiers, notifiers.NewPushoverNotifier(
allConfig.Config.OutputNotifiers.Pushover.URL,
allConfig.Credentials.Pushover.Token,
allConfig.Credentials.Pushover.UserKey,
))
log.Debug("created pushover notifier")
pushoverNotifiers := createPushoverNotifiers(allConfig)
outputNotifiers = append(outputNotifiers, pushoverNotifiers...)

log.Debug("created pushover notifier(s)", "num pushover notifiers", len(pushoverNotifiers))
}
if allConfig.Config.OutputNotifiers.Smtp.Enabled {
args := notifiers.ArgsSmtpNotifier{
Expand All @@ -44,13 +42,53 @@ func CreateOutputNotifiers(allConfig config.AllConfigs) ([]executors.OutputNotif
log.Debug("created smtp notifier")
}
if allConfig.Config.OutputNotifiers.Telegram.Enabled {
outputNotifiers = append(outputNotifiers, notifiers.NewTelegramNotifier(
allConfig.Config.OutputNotifiers.Telegram.URL,
allConfig.Credentials.Telegram.Token,
allConfig.Credentials.Telegram.ChatID,
))
log.Debug("created telegram notifier")
telegramNotifiers := createTelegramNotifiers(allConfig)
outputNotifiers = append(outputNotifiers, telegramNotifiers...)

log.Debug("created telegram notifier(s)", "num telegram notifiers", len(telegramNotifiers))
}

return outputNotifiers, nil
}

func createPushoverNotifiers(allConfig config.AllConfigs) []executors.OutputNotifier {
defaultNotifier := notifiers.NewPushoverNotifier(
allConfig.Config.OutputNotifiers.Pushover.URL,
allConfig.Credentials.Pushover.Token,
allConfig.Credentials.Pushover.UserKey,
)

notifierInstances := []executors.OutputNotifier{defaultNotifier}
for _, credentials := range allConfig.Credentials.Pushover.Additional {
notifierInstance := notifiers.NewPushoverNotifier(
allConfig.Config.OutputNotifiers.Pushover.URL,
credentials.Token,
credentials.UserKey,
)

notifierInstances = append(notifierInstances, notifierInstance)
}

return notifierInstances
}

func createTelegramNotifiers(allConfig config.AllConfigs) []executors.OutputNotifier {
defaultNotifier := notifiers.NewTelegramNotifier(
allConfig.Config.OutputNotifiers.Telegram.URL,
allConfig.Credentials.Telegram.Token,
allConfig.Credentials.Telegram.ChatID,
)

notifierInstances := []executors.OutputNotifier{defaultNotifier}
for _, credentials := range allConfig.Credentials.Telegram.Additional {
notifierInstance := notifiers.NewTelegramNotifier(
allConfig.Config.OutputNotifiers.Telegram.URL,
credentials.Token,
credentials.ChatID,
)

notifierInstances = append(notifierInstances, notifierInstance)
}

return notifierInstances
}
72 changes: 72 additions & 0 deletions factory/notifiersFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,42 @@ func TestCreateOutputNotifiers(t *testing.T) {
assert.Equal(t, "*notifiers.logNotifier", fmt.Sprintf("%T", notifiers[0]))
assert.Equal(t, "*notifiers.pushoverNotifier", fmt.Sprintf("%T", notifiers[1]))
})
t.Run("should create 3 pushover notifiers and a log notifier", func(t *testing.T) {
notifiers, err := CreateOutputNotifiers(config.AllConfigs{
Config: config.MainConfig{
OutputNotifiers: config.OutputNotifiersConfig{
Pushover: config.PushoverNotifierConfig{
Enabled: true,
},
},
},
Credentials: config.CredentialsConfig{
Pushover: config.PushoverCredentialsConfig{
TokenUserKeyConfig: config.TokenUserKeyConfig{
Token: "token1",
UserKey: "userKey1",
},
Additional: []config.TokenUserKeyConfig{
{
Token: "token2",
UserKey: "userKey2",
},
{
Token: "token3",
UserKey: "userKey3",
},
},
},
},
})

assert.Nil(t, err)
assert.Equal(t, 4, len(notifiers)) // 1 log + 3 pushover
assert.Equal(t, "*notifiers.logNotifier", fmt.Sprintf("%T", notifiers[0]))
assert.Equal(t, "*notifiers.pushoverNotifier", fmt.Sprintf("%T", notifiers[1]))
assert.Equal(t, "*notifiers.pushoverNotifier", fmt.Sprintf("%T", notifiers[2]))
assert.Equal(t, "*notifiers.pushoverNotifier", fmt.Sprintf("%T", notifiers[3]))
})
t.Run("should create a smtp and a log notifier", func(t *testing.T) {
notifiers, err := CreateOutputNotifiers(config.AllConfigs{
Config: config.MainConfig{
Expand Down Expand Up @@ -66,4 +102,40 @@ func TestCreateOutputNotifiers(t *testing.T) {
assert.Equal(t, "*notifiers.logNotifier", fmt.Sprintf("%T", notifiers[0]))
assert.Equal(t, "*notifiers.telegramNotifier", fmt.Sprintf("%T", notifiers[1]))
})
t.Run("should create 3 telegram notifiers and a log notifier", func(t *testing.T) {
notifiers, err := CreateOutputNotifiers(config.AllConfigs{
Config: config.MainConfig{
OutputNotifiers: config.OutputNotifiersConfig{
Telegram: config.TelegramNotifierConfig{
Enabled: true,
},
},
},
Credentials: config.CredentialsConfig{
Telegram: config.TelegramCredentialsConfig{
TokenChatIDConfig: config.TokenChatIDConfig{
Token: "token1",
ChatID: "chatID1",
},
Additional: []config.TokenChatIDConfig{
{
Token: "token2",
ChatID: "chatID2",
},
{
Token: "token3",
ChatID: "chatID3",
},
},
},
},
})

assert.Nil(t, err)
assert.Equal(t, 4, len(notifiers)) // 1 log + 3 telegram
assert.Equal(t, "*notifiers.logNotifier", fmt.Sprintf("%T", notifiers[0]))
assert.Equal(t, "*notifiers.telegramNotifier", fmt.Sprintf("%T", notifiers[1]))
assert.Equal(t, "*notifiers.telegramNotifier", fmt.Sprintf("%T", notifiers[2]))
assert.Equal(t, "*notifiers.telegramNotifier", fmt.Sprintf("%T", notifiers[3]))
})
}

0 comments on commit 034273a

Please sign in to comment.