-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from K-Phoen/alertmanager-discord
Support discord alertmanager contact point type
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package discord | ||
|
||
import ( | ||
"github.com/K-Phoen/grabana/alertmanager" | ||
"github.com/K-Phoen/sdk" | ||
) | ||
|
||
// Option represents an option that can be used to configure a "discord" | ||
// contact point type. | ||
type Option func(contactType *discordType) | ||
|
||
type discordType struct { | ||
builder *sdk.ContactPointType | ||
} | ||
|
||
// With creates an Opsgenie contact point type with the given settings. | ||
func With(webhookURL string, opts ...Option) alertmanager.ContactPointOption { | ||
discord := &discordType{ | ||
builder: &sdk.ContactPointType{ | ||
Type: "discord", | ||
Settings: map[string]interface{}{ | ||
"url": webhookURL, | ||
}, | ||
}, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(discord) | ||
} | ||
|
||
return func(contact *alertmanager.Contact) { | ||
contact.Builder.GrafanaManagedReceivers = append(contact.Builder.GrafanaManagedReceivers, *discord.builder) | ||
} | ||
} | ||
|
||
// UseDiscordUsername uses the username configured in Discord's webhook settings. | ||
// Otherwise, the username will be 'Grafana'. | ||
func UseDiscordUsername() Option { | ||
return func(contactType *discordType) { | ||
contactType.builder.Settings["use_discord_username"] = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package discord | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/K-Phoen/grabana/alertmanager" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWith(t *testing.T) { | ||
req := require.New(t) | ||
|
||
contactPoint := alertmanager.ContactPoint("", With("url")) | ||
|
||
req.Len(contactPoint.Builder.GrafanaManagedReceivers, 1) | ||
|
||
contactType := contactPoint.Builder.GrafanaManagedReceivers[0] | ||
req.Equal("discord", contactType.Type) | ||
req.Equal("url", contactType.Settings["url"].(string)) | ||
} | ||
|
||
func TestUseDiscordUsername(t *testing.T) { | ||
req := require.New(t) | ||
|
||
contactPoint := alertmanager.ContactPoint("", With("", UseDiscordUsername())) | ||
|
||
req.Len(contactPoint.Builder.GrafanaManagedReceivers, 1) | ||
|
||
contactType := contactPoint.Builder.GrafanaManagedReceivers[0] | ||
req.True(contactType.Settings["use_discord_username"].(bool)) | ||
} |