mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Add external Alertmanagers (#39183)
* building ui * saving alertmanager urls * add actions and api call to get external ams * add list to add modal * add validation and edit/delete * work on merging results * merging results * get color for status heart * adding tests * tests added * rename * add pollin and status * fix list sync * fix polling * add info icon with actual tooltip * fix test * Accessibility things * fix strict error * delete public/dist files * Add API tests for invalid URL * start redo admin test * Fix for empty configuration and test * remove admin test * text updates after review * suppress appevent error * fix tests * update description Co-authored-by: gotjosh <josue@grafana.com> * fix text plus go lint * updates after pr review * Adding docs * Update docs/sources/alerting/unified-alerting/fundamentals/alertmanager.md Co-authored-by: gotjosh <josue@grafana.com> * Update docs/sources/alerting/unified-alerting/fundamentals/alertmanager.md Co-authored-by: gotjosh <josue@grafana.com> * Update docs/sources/alerting/unified-alerting/fundamentals/alertmanager.md Co-authored-by: gotjosh <josue@grafana.com> * Update docs/sources/alerting/unified-alerting/fundamentals/alertmanager.md Co-authored-by: gotjosh <josue@grafana.com> * Update docs/sources/alerting/unified-alerting/fundamentals/alertmanager.md Co-authored-by: gotjosh <josue@grafana.com> * Update docs/sources/alerting/unified-alerting/fundamentals/alertmanager.md Co-authored-by: gotjosh <josue@grafana.com> * Update docs/sources/alerting/unified-alerting/fundamentals/alertmanager.md Co-authored-by: gotjosh <josue@grafana.com> * prettier * updates after docs feedback Co-authored-by: gotjosh <josue.abreu@gmail.com> Co-authored-by: gotjosh <josue@grafana.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package models
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// AdminConfiguration represents the ngalert administration configuration settings.
|
||||
@@ -22,3 +23,14 @@ func (ac *AdminConfiguration) AsSHA256() string {
|
||||
_, _ = h.Write([]byte(fmt.Sprintf("%v", ac.Alertmanagers)))
|
||||
return fmt.Sprintf("%x", h.Sum(nil))
|
||||
}
|
||||
|
||||
func (ac *AdminConfiguration) Validate() error {
|
||||
for _, u := range ac.Alertmanagers {
|
||||
_, err := url.Parse(u)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
58
pkg/services/ngalert/models/admin_configuration_test.go
Normal file
58
pkg/services/ngalert/models/admin_configuration_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAdminConfiguration_AsSHA256(t *testing.T) {
|
||||
tc := []struct {
|
||||
name string
|
||||
ac *AdminConfiguration
|
||||
ciphertext string
|
||||
}{
|
||||
{
|
||||
name: "AsSHA256",
|
||||
ac: &AdminConfiguration{Alertmanagers: []string{"http://localhost:9093"}},
|
||||
ciphertext: "3ec9db375a5ba12f7c7b704922cf4b8e21a31e30d85be2386803829f0ee24410",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tc {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
require.Equal(t, tt.ciphertext, tt.ac.AsSHA256())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminConfiguration_Validate(t *testing.T) {
|
||||
tc := []struct {
|
||||
name string
|
||||
ac *AdminConfiguration
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "should return the first error if any of the Alertmanagers URL is invalid",
|
||||
ac: &AdminConfiguration{Alertmanagers: []string{"http://localhost:9093", "http://›∂-)Æÿ ñ"}},
|
||||
err: fmt.Errorf("parse \"http://›∂-)Æÿ ñ\": invalid character \" \" in host name"),
|
||||
},
|
||||
{
|
||||
name: "should not return any errors if all URLs are valid",
|
||||
ac: &AdminConfiguration{Alertmanagers: []string{"http://localhost:9093"}},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tc {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := tt.ac.Validate()
|
||||
if tt.err != nil {
|
||||
require.EqualError(t, err, tt.err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user