create testintegration method
This commit is contained in:
@@ -813,6 +813,65 @@ func (_c *AlertmanagerMock_StopAndWait_Call) RunAndReturn(run func()) *Alertmana
|
||||
return _c
|
||||
}
|
||||
|
||||
// TestIntegration provides a mock function with given fields: ctx, receiverName, integrationConfig, alert
|
||||
func (_m *AlertmanagerMock) TestIntegration(ctx context.Context, receiverName string, integrationConfig models.Integration, alert alertingmodels.TestReceiversConfigAlertParams) (alertingmodels.IntegrationStatus, error) {
|
||||
ret := _m.Called(ctx, receiverName, integrationConfig, alert)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for TestIntegration")
|
||||
}
|
||||
|
||||
var r0 alertingmodels.IntegrationStatus
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(0).(func(context.Context, string, models.Integration, alertingmodels.TestReceiversConfigAlertParams) (alertingmodels.IntegrationStatus, error)); ok {
|
||||
return rf(ctx, receiverName, integrationConfig, alert)
|
||||
}
|
||||
if rf, ok := ret.Get(0).(func(context.Context, string, models.Integration, alertingmodels.TestReceiversConfigAlertParams) alertingmodels.IntegrationStatus); ok {
|
||||
r0 = rf(ctx, receiverName, integrationConfig, alert)
|
||||
} else {
|
||||
r0 = ret.Get(0).(alertingmodels.IntegrationStatus)
|
||||
}
|
||||
|
||||
if rf, ok := ret.Get(1).(func(context.Context, string, models.Integration, alertingmodels.TestReceiversConfigAlertParams) error); ok {
|
||||
r1 = rf(ctx, receiverName, integrationConfig, alert)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// AlertmanagerMock_TestIntegration_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'TestIntegration'
|
||||
type AlertmanagerMock_TestIntegration_Call struct {
|
||||
*mock.Call
|
||||
}
|
||||
|
||||
// TestIntegration is a helper method to define mock.On call
|
||||
// - ctx context.Context
|
||||
// - receiverName string
|
||||
// - integrationConfig models.Integration
|
||||
// - alert alertingmodels.TestReceiversConfigAlertParams
|
||||
func (_e *AlertmanagerMock_Expecter) TestIntegration(ctx interface{}, receiverName interface{}, integrationConfig interface{}, alert interface{}) *AlertmanagerMock_TestIntegration_Call {
|
||||
return &AlertmanagerMock_TestIntegration_Call{Call: _e.mock.On("TestIntegration", ctx, receiverName, integrationConfig, alert)}
|
||||
}
|
||||
|
||||
func (_c *AlertmanagerMock_TestIntegration_Call) Run(run func(ctx context.Context, receiverName string, integrationConfig models.Integration, alert alertingmodels.TestReceiversConfigAlertParams)) *AlertmanagerMock_TestIntegration_Call {
|
||||
_c.Call.Run(func(args mock.Arguments) {
|
||||
run(args[0].(context.Context), args[1].(string), args[2].(models.Integration), args[3].(alertingmodels.TestReceiversConfigAlertParams))
|
||||
})
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *AlertmanagerMock_TestIntegration_Call) Return(_a0 alertingmodels.IntegrationStatus, _a1 error) *AlertmanagerMock_TestIntegration_Call {
|
||||
_c.Call.Return(_a0, _a1)
|
||||
return _c
|
||||
}
|
||||
|
||||
func (_c *AlertmanagerMock_TestIntegration_Call) RunAndReturn(run func(context.Context, string, models.Integration, alertingmodels.TestReceiversConfigAlertParams) (alertingmodels.IntegrationStatus, error)) *AlertmanagerMock_TestIntegration_Call {
|
||||
_c.Call.Return(run)
|
||||
return _c
|
||||
}
|
||||
|
||||
// TestReceivers provides a mock function with given fields: ctx, c
|
||||
func (_m *AlertmanagerMock) TestReceivers(ctx context.Context, c definitions.TestReceiversConfigBodyParams) (*notify.TestReceiversResult, int, error) {
|
||||
ret := _m.Called(ctx, c)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
alertingModels "github.com/grafana/alerting/models"
|
||||
"github.com/grafana/alerting/notify/nfstatus"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
@@ -71,6 +72,7 @@ type Alertmanager interface {
|
||||
// Receivers
|
||||
GetReceivers(ctx context.Context) ([]apimodels.Receiver, error)
|
||||
TestReceivers(ctx context.Context, c apimodels.TestReceiversConfigBodyParams) (*alertingNotify.TestReceiversResult, int, error)
|
||||
TestIntegration(ctx context.Context, receiverName string, integrationConfig models.Integration, alert alertingModels.TestReceiversConfigAlertParams) (alertingModels.IntegrationStatus, error)
|
||||
TestTemplate(ctx context.Context, c apimodels.TestTemplatesConfigBodyParams) (*TestTemplatesResults, error)
|
||||
|
||||
// Lifecycle
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
v2 "github.com/prometheus/alertmanager/api/v2"
|
||||
|
||||
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
||||
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
)
|
||||
|
||||
func (am *alertmanager) TestReceivers(ctx context.Context, c apimodels.TestReceiversConfigBodyParams) (*alertingNotify.TestReceiversResult, int, error) {
|
||||
@@ -52,6 +53,14 @@ func (am *alertmanager) TestReceivers(ctx context.Context, c apimodels.TestRecei
|
||||
})
|
||||
}
|
||||
|
||||
func (am *alertmanager) TestIntegration(ctx context.Context, receiverName string, integrationConfig ngmodels.Integration, alert models.TestReceiversConfigAlertParams) (models.IntegrationStatus, error) {
|
||||
cfg, err := IntegrationToIntegrationConfig(integrationConfig)
|
||||
if err != nil {
|
||||
return models.IntegrationStatus{}, err
|
||||
}
|
||||
return am.Base.TestIntegration(ctx, receiverName, cfg, alert)
|
||||
}
|
||||
|
||||
func (am *alertmanager) GetReceivers(_ context.Context) ([]apimodels.Receiver, error) {
|
||||
return am.Base.GetReceiversStatus(), nil
|
||||
}
|
||||
|
||||
@@ -30,7 +30,8 @@ import (
|
||||
"github.com/prometheus/alertmanager/pkg/labels"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
common_config "github.com/prometheus/common/config"
|
||||
"go.yaml.in/yaml/v3"
|
||||
"github.com/prometheus/common/model"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/infra/tracing"
|
||||
@@ -40,6 +41,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/notifier"
|
||||
remoteClient "github.com/grafana/grafana/pkg/services/ngalert/remote/client"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/sender"
|
||||
"github.com/grafana/grafana/pkg/services/secrets"
|
||||
"github.com/grafana/grafana/pkg/util/cmputil"
|
||||
)
|
||||
|
||||
@@ -623,6 +625,51 @@ func (am *Alertmanager) TestReceivers(ctx context.Context, c apimodels.TestRecei
|
||||
})
|
||||
}
|
||||
|
||||
func (am *Alertmanager) TestIntegration(ctx context.Context, receiverName string, integrationConfig models.Integration, alert alertingModels.TestReceiversConfigAlertParams) (alertingModels.IntegrationStatus, error) {
|
||||
decrypted := integrationConfig.Clone()
|
||||
err := decrypted.Decrypt(notifier.DecryptIntegrationSettings(ctx, am.crypto))
|
||||
if err != nil {
|
||||
return alertingModels.IntegrationStatus{}, fmt.Errorf("failed to decrypt receivers: %w", err)
|
||||
}
|
||||
cfg, err := notifier.IntegrationToIntegrationConfig(decrypted)
|
||||
if err != nil {
|
||||
return alertingModels.IntegrationStatus{}, fmt.Errorf("failed to convert integration to integration config: %w", err)
|
||||
}
|
||||
|
||||
apiReceivers := []*alertingNotify.APIReceiver{
|
||||
{
|
||||
ConfigReceiver: alertingNotify.ConfigReceiver{
|
||||
Name: receiverName,
|
||||
},
|
||||
ReceiverConfig: alertingModels.ReceiverConfig{
|
||||
Integrations: []*alertingModels.IntegrationConfig{
|
||||
&cfg,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
t := time.Now()
|
||||
result, _, err := am.mimirClient.TestReceivers(ctx, alertingNotify.TestReceiversConfigBodyParams{
|
||||
Alert: &alert,
|
||||
Receivers: apiReceivers,
|
||||
})
|
||||
duration := time.Since(t)
|
||||
if err != nil {
|
||||
return alertingModels.IntegrationStatus{}, fmt.Errorf("failed to test integration: %w", err)
|
||||
}
|
||||
status := alertingModels.IntegrationStatus{
|
||||
LastNotifyAttempt: strfmt.DateTime(result.NotifedAt),
|
||||
LastNotifyAttemptDuration: model.Duration(duration).String(),
|
||||
Name: cfg.Type,
|
||||
SendResolved: false,
|
||||
}
|
||||
if len(result.Receivers) > 0 && len(result.Receivers[0].Configs) > 0 {
|
||||
status.LastNotifyAttemptError = result.Receivers[0].Configs[0].Error
|
||||
}
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func (am *Alertmanager) TestTemplate(ctx context.Context, c apimodels.TestTemplatesConfigBodyParams) (*notifier.TestTemplatesResults, error) {
|
||||
for _, alert := range c.Alerts {
|
||||
notifier.AddDefaultLabelsAndAnnotations(alert)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
alertingModels "github.com/grafana/alerting/models"
|
||||
alertingNotify "github.com/grafana/alerting/notify"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/kvstore"
|
||||
@@ -173,6 +174,10 @@ func (fam *RemotePrimaryForkedAlertmanager) TestReceivers(ctx context.Context, c
|
||||
return fam.remote.TestReceivers(ctx, c)
|
||||
}
|
||||
|
||||
func (fam *RemotePrimaryForkedAlertmanager) TestIntegration(ctx context.Context, receiverName string, integrationConfig models.Integration, alert alertingModels.TestReceiversConfigAlertParams) (alertingModels.IntegrationStatus, error) {
|
||||
return fam.remote.TestIntegration(ctx, receiverName, integrationConfig, alert)
|
||||
}
|
||||
|
||||
func (fam *RemotePrimaryForkedAlertmanager) TestTemplate(ctx context.Context, c apimodels.TestTemplatesConfigBodyParams) (*notifier.TestTemplatesResults, error) {
|
||||
return fam.remote.TestTemplate(ctx, c)
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
alertingModels "github.com/grafana/alerting/models"
|
||||
alertingNotify "github.com/grafana/alerting/notify"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/kvstore"
|
||||
@@ -234,6 +235,10 @@ func (fam *RemoteSecondaryForkedAlertmanager) TestReceivers(ctx context.Context,
|
||||
return fam.internal.TestReceivers(ctx, c)
|
||||
}
|
||||
|
||||
func (fam *RemoteSecondaryForkedAlertmanager) TestIntegration(ctx context.Context, receiverName string, integrationConfig models.Integration, alert alertingModels.TestReceiversConfigAlertParams) (alertingModels.IntegrationStatus, error) {
|
||||
return fam.internal.TestIntegration(ctx, receiverName, integrationConfig, alert)
|
||||
}
|
||||
|
||||
func (fam *RemoteSecondaryForkedAlertmanager) TestTemplate(ctx context.Context, c apimodels.TestTemplatesConfigBodyParams) (*notifier.TestTemplatesResults, error) {
|
||||
return fam.internal.TestTemplate(ctx, c)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user