Chore: Simplify function signature for GetLatestAlertmanagerConfiguration (#79392)

This commit is contained in:
Santiago
2023-12-12 13:49:54 +01:00
committed by GitHub
parent 6989db1ad3
commit 57e0d6bcb5
17 changed files with 113 additions and 117 deletions

View File

@ -36,7 +36,7 @@ type ExternalAlertmanagerProvider interface {
}
type AlertingStore interface {
GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) (*models.AlertConfiguration, error)
GetLatestAlertmanagerConfiguration(ctx context.Context, orgID int64) (*models.AlertConfiguration, error)
}
type RuleAccessControlService interface {

View File

@ -24,11 +24,6 @@ type HistoricAlertConfiguration struct {
LastApplied int64 `xorm:"last_applied"`
}
// GetLatestAlertmanagerConfigurationQuery is the query to get the latest alertmanager configuration.
type GetLatestAlertmanagerConfigurationQuery struct {
OrgID int64
}
// SaveAlertmanagerConfigurationCmd is the command to save an alertmanager configuration.
type SaveAlertmanagerConfigurationCmd struct {
AlertmanagerConfiguration string

View File

@ -31,12 +31,11 @@ func (e AlertmanagerConfigRejectedError) Error() string {
}
type configurationStore interface {
GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) (*models.AlertConfiguration, error)
GetLatestAlertmanagerConfiguration(ctx context.Context, orgID int64) (*models.AlertConfiguration, error)
}
func (moa *MultiOrgAlertmanager) GetAlertmanagerConfiguration(ctx context.Context, org int64) (definitions.GettableUserConfig, error) {
query := models.GetLatestAlertmanagerConfigurationQuery{OrgID: org}
amConfig, err := moa.configStore.GetLatestAlertmanagerConfiguration(ctx, &query)
amConfig, err := moa.configStore.GetLatestAlertmanagerConfiguration(ctx, org)
if err != nil {
return definitions.GettableUserConfig{}, fmt.Errorf("failed to get latest configuration: %w", err)
}
@ -158,8 +157,7 @@ func (moa *MultiOrgAlertmanager) gettableUserConfigFromAMConfigString(ctx contex
func (moa *MultiOrgAlertmanager) ApplyAlertmanagerConfiguration(ctx context.Context, org int64, config definitions.PostableUserConfig) error {
// Get the last known working configuration
query := models.GetLatestAlertmanagerConfigurationQuery{OrgID: org}
_, err := moa.configStore.GetLatestAlertmanagerConfiguration(ctx, &query)
_, err := moa.configStore.GetLatestAlertmanagerConfiguration(ctx, org)
if err != nil {
// If we don't have a configuration there's nothing for us to know and we should just continue saving the new one
if !errors.Is(err, store.ErrNoAlertmanagerConfiguration) {

View File

@ -8,7 +8,6 @@ import (
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/ngalert/store"
"github.com/grafana/grafana/pkg/services/secrets"
)
@ -80,8 +79,7 @@ func EncryptReceiverConfigs(c []*definitions.PostableApiReceiver, encrypt defini
// LoadSecureSettings adds the corresponding unencrypted secrets stored to the list of input receivers.
func (c *alertmanagerCrypto) LoadSecureSettings(ctx context.Context, orgId int64, receivers []*definitions.PostableApiReceiver) error {
// Get the last known working configuration.
query := models.GetLatestAlertmanagerConfigurationQuery{OrgID: orgId}
amConfig, err := c.configs.GetLatestAlertmanagerConfiguration(ctx, &query)
amConfig, err := c.configs.GetLatestAlertmanagerConfiguration(ctx, orgId)
if err != nil {
// If we don't have a configuration there's nothing for us to know and we should just continue saving the new one.
if !errors.Is(err, store.ErrNoAlertmanagerConfiguration) {

View File

@ -65,8 +65,8 @@ func (f *fakeConfigStore) GetAllLatestAlertmanagerConfiguration(context.Context)
return result, nil
}
func (f *fakeConfigStore) GetLatestAlertmanagerConfiguration(_ context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) (*models.AlertConfiguration, error) {
config, ok := f.configs[query.OrgID]
func (f *fakeConfigStore) GetLatestAlertmanagerConfiguration(_ context.Context, orgID int64) (*models.AlertConfiguration, error) {
config, ok := f.configs[orgID]
if !ok {
return nil, store.ErrNoAlertmanagerConfiguration
}

View File

@ -6,7 +6,6 @@ import (
"fmt"
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"github.com/grafana/grafana/pkg/services/ngalert/models"
)
func deserializeAlertmanagerConfig(config []byte) (*definitions.PostableUserConfig, error) {
@ -28,10 +27,7 @@ type cfgRevision struct {
}
func getLastConfiguration(ctx context.Context, orgID int64, store AMConfigStore) (*cfgRevision, error) {
q := models.GetLatestAlertmanagerConfigurationQuery{
OrgID: orgID,
}
alertManagerConfig, err := store.GetLatestAlertmanagerConfiguration(ctx, &q)
alertManagerConfig, err := store.GetLatestAlertmanagerConfiguration(ctx, orgID)
if err != nil {
return nil, err
}

View File

@ -239,10 +239,7 @@ func TestContactPointService(t *testing.T) {
t.Run("service respects concurrency token when updating", func(t *testing.T) {
sut := createContactPointServiceSut(t, secretsService)
newCp := createTestContactPoint()
q := models.GetLatestAlertmanagerConfigurationQuery{
OrgID: 1,
}
config, err := sut.amStore.GetLatestAlertmanagerConfiguration(context.Background(), &q)
config, err := sut.amStore.GetLatestAlertmanagerConfiguration(context.Background(), 1)
require.NoError(t, err)
expectedConcurrencyToken := config.ConfigurationHash

View File

@ -34,10 +34,7 @@ func (nps *NotificationPolicyService) GetAMConfigStore() AMConfigStore {
}
func (nps *NotificationPolicyService) GetPolicyTree(ctx context.Context, orgID int64) (definitions.Route, error) {
q := models.GetLatestAlertmanagerConfigurationQuery{
OrgID: orgID,
}
alertManagerConfig, err := nps.amStore.GetLatestAlertmanagerConfiguration(ctx, &q)
alertManagerConfig, err := nps.amStore.GetLatestAlertmanagerConfiguration(ctx, orgID)
if err != nil {
return definitions.Route{}, err
}

View File

@ -146,10 +146,7 @@ func TestNotificationPolicyService(t *testing.T) {
t.Run("service respects concurrency token when updating", func(t *testing.T) {
sut := createNotificationPolicyServiceSut()
newRoute := createTestRoutingTree()
q := models.GetLatestAlertmanagerConfigurationQuery{
OrgID: 1,
}
config, err := sut.GetAMConfigStore().GetLatestAlertmanagerConfiguration(context.Background(), &q)
config, err := sut.GetAMConfigStore().GetLatestAlertmanagerConfiguration(context.Background(), 1)
require.NoError(t, err)
expectedConcurrencyToken := config.ConfigurationHash

View File

@ -14,7 +14,7 @@ import (
//
//go:generate mockery --name AMConfigStore --structname MockAMConfigStore --inpackage --filename persist_mock.go --with-expecter
type AMConfigStore interface {
GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) (*models.AlertConfiguration, error)
GetLatestAlertmanagerConfiguration(ctx context.Context, orgID int64) (*models.AlertConfiguration, error)
UpdateAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error
}

View File

@ -1,4 +1,4 @@
// Code generated by mockery v2.16.0. DO NOT EDIT.
// Code generated by mockery v2.34.2. DO NOT EDIT.
package provisioning
@ -22,22 +22,25 @@ func (_m *MockAMConfigStore) EXPECT() *MockAMConfigStore_Expecter {
return &MockAMConfigStore_Expecter{mock: &_m.Mock}
}
// GetLatestAlertmanagerConfiguration provides a mock function with given fields: ctx, query
func (_m *MockAMConfigStore) GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) (*models.AlertConfiguration, error) {
ret := _m.Called(ctx, query)
// GetLatestAlertmanagerConfiguration provides a mock function with given fields: ctx, orgID
func (_m *MockAMConfigStore) GetLatestAlertmanagerConfiguration(ctx context.Context, orgID int64) (*models.AlertConfiguration, error) {
ret := _m.Called(ctx, orgID)
var r0 *models.AlertConfiguration
if rf, ok := ret.Get(0).(func(context.Context, *models.GetLatestAlertmanagerConfigurationQuery) *models.AlertConfiguration); ok {
r0 = rf(ctx, query)
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, int64) (*models.AlertConfiguration, error)); ok {
return rf(ctx, orgID)
}
if rf, ok := ret.Get(0).(func(context.Context, int64) *models.AlertConfiguration); ok {
r0 = rf(ctx, orgID)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*models.AlertConfiguration)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, *models.GetLatestAlertmanagerConfigurationQuery) error); ok {
r1 = rf(ctx, query)
if rf, ok := ret.Get(1).(func(context.Context, int64) error); ok {
r1 = rf(ctx, orgID)
} else {
r1 = ret.Error(1)
}
@ -52,14 +55,14 @@ type MockAMConfigStore_GetLatestAlertmanagerConfiguration_Call struct {
// GetLatestAlertmanagerConfiguration is a helper method to define mock.On call
// - ctx context.Context
// - query *models.GetLatestAlertmanagerConfigurationQuery
func (_e *MockAMConfigStore_Expecter) GetLatestAlertmanagerConfiguration(ctx any, query any) *MockAMConfigStore_GetLatestAlertmanagerConfiguration_Call {
return &MockAMConfigStore_GetLatestAlertmanagerConfiguration_Call{Call: _e.mock.On("GetLatestAlertmanagerConfiguration", ctx, query)}
// - orgID int64
func (_e *MockAMConfigStore_Expecter) GetLatestAlertmanagerConfiguration(ctx interface{}, orgID interface{}) *MockAMConfigStore_GetLatestAlertmanagerConfiguration_Call {
return &MockAMConfigStore_GetLatestAlertmanagerConfiguration_Call{Call: _e.mock.On("GetLatestAlertmanagerConfiguration", ctx, orgID)}
}
func (_c *MockAMConfigStore_GetLatestAlertmanagerConfiguration_Call) Run(run func(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery)) *MockAMConfigStore_GetLatestAlertmanagerConfiguration_Call {
func (_c *MockAMConfigStore_GetLatestAlertmanagerConfiguration_Call) Run(run func(ctx context.Context, orgID int64)) *MockAMConfigStore_GetLatestAlertmanagerConfiguration_Call {
_c.Call.Run(func(args mock.Arguments) {
run(args[0].(context.Context), args[1].(*models.GetLatestAlertmanagerConfigurationQuery))
run(args[0].(context.Context), args[1].(int64))
})
return _c
}
@ -69,6 +72,11 @@ func (_c *MockAMConfigStore_GetLatestAlertmanagerConfiguration_Call) Return(_a0
return _c
}
func (_c *MockAMConfigStore_GetLatestAlertmanagerConfiguration_Call) RunAndReturn(run func(context.Context, int64) (*models.AlertConfiguration, error)) *MockAMConfigStore_GetLatestAlertmanagerConfiguration_Call {
_c.Call.Return(run)
return _c
}
// UpdateAlertmanagerConfiguration provides a mock function with given fields: ctx, cmd
func (_m *MockAMConfigStore) UpdateAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error {
ret := _m.Called(ctx, cmd)
@ -91,7 +99,7 @@ type MockAMConfigStore_UpdateAlertmanagerConfiguration_Call struct {
// UpdateAlertmanagerConfiguration is a helper method to define mock.On call
// - ctx context.Context
// - cmd *models.SaveAlertmanagerConfigurationCmd
func (_e *MockAMConfigStore_Expecter) UpdateAlertmanagerConfiguration(ctx any, cmd any) *MockAMConfigStore_UpdateAlertmanagerConfiguration_Call {
func (_e *MockAMConfigStore_Expecter) UpdateAlertmanagerConfiguration(ctx interface{}, cmd interface{}) *MockAMConfigStore_UpdateAlertmanagerConfiguration_Call {
return &MockAMConfigStore_UpdateAlertmanagerConfiguration_Call{Call: _e.mock.On("UpdateAlertmanagerConfiguration", ctx, cmd)}
}
@ -107,13 +115,17 @@ func (_c *MockAMConfigStore_UpdateAlertmanagerConfiguration_Call) Return(_a0 err
return _c
}
type mockConstructorTestingTNewMockAMConfigStore interface {
mock.TestingT
Cleanup(func())
func (_c *MockAMConfigStore_UpdateAlertmanagerConfiguration_Call) RunAndReturn(run func(context.Context, *models.SaveAlertmanagerConfigurationCmd) error) *MockAMConfigStore_UpdateAlertmanagerConfiguration_Call {
_c.Call.Return(run)
return _c
}
// NewMockAMConfigStore creates a new instance of MockAMConfigStore. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
func NewMockAMConfigStore(t mockConstructorTestingTNewMockAMConfigStore) *MockAMConfigStore {
// The first argument is typically a *testing.T value.
func NewMockAMConfigStore(t interface {
mock.TestingT
Cleanup(func())
}) *MockAMConfigStore {
mock := &MockAMConfigStore{}
mock.Mock.Test(t)

View File

@ -1,14 +1,12 @@
// Code generated by mockery v2.12.0. DO NOT EDIT.
// Code generated by mockery v2.34.2. DO NOT EDIT.
package provisioning
import (
context "context"
testing "testing"
mock "github.com/stretchr/testify/mock"
models "github.com/grafana/grafana/pkg/services/ngalert/models"
mock "github.com/stretchr/testify/mock"
)
// MockProvisioningStore is an autogenerated mock type for the ProvisioningStore type
@ -47,7 +45,7 @@ type MockProvisioningStore_DeleteProvenance_Call struct {
// - ctx context.Context
// - o models.Provisionable
// - org int64
func (_e *MockProvisioningStore_Expecter) DeleteProvenance(ctx any, o any, org any) *MockProvisioningStore_DeleteProvenance_Call {
func (_e *MockProvisioningStore_Expecter) DeleteProvenance(ctx interface{}, o interface{}, org interface{}) *MockProvisioningStore_DeleteProvenance_Call {
return &MockProvisioningStore_DeleteProvenance_Call{Call: _e.mock.On("DeleteProvenance", ctx, o, org)}
}
@ -63,18 +61,26 @@ func (_c *MockProvisioningStore_DeleteProvenance_Call) Return(_a0 error) *MockPr
return _c
}
func (_c *MockProvisioningStore_DeleteProvenance_Call) RunAndReturn(run func(context.Context, models.Provisionable, int64) error) *MockProvisioningStore_DeleteProvenance_Call {
_c.Call.Return(run)
return _c
}
// GetProvenance provides a mock function with given fields: ctx, o, org
func (_m *MockProvisioningStore) GetProvenance(ctx context.Context, o models.Provisionable, org int64) (models.Provenance, error) {
ret := _m.Called(ctx, o, org)
var r0 models.Provenance
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, models.Provisionable, int64) (models.Provenance, error)); ok {
return rf(ctx, o, org)
}
if rf, ok := ret.Get(0).(func(context.Context, models.Provisionable, int64) models.Provenance); ok {
r0 = rf(ctx, o, org)
} else {
r0 = ret.Get(0).(models.Provenance)
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, models.Provisionable, int64) error); ok {
r1 = rf(ctx, o, org)
} else {
@ -93,7 +99,7 @@ type MockProvisioningStore_GetProvenance_Call struct {
// - ctx context.Context
// - o models.Provisionable
// - org int64
func (_e *MockProvisioningStore_Expecter) GetProvenance(ctx any, o any, org any) *MockProvisioningStore_GetProvenance_Call {
func (_e *MockProvisioningStore_Expecter) GetProvenance(ctx interface{}, o interface{}, org interface{}) *MockProvisioningStore_GetProvenance_Call {
return &MockProvisioningStore_GetProvenance_Call{Call: _e.mock.On("GetProvenance", ctx, o, org)}
}
@ -109,11 +115,20 @@ func (_c *MockProvisioningStore_GetProvenance_Call) Return(_a0 models.Provenance
return _c
}
func (_c *MockProvisioningStore_GetProvenance_Call) RunAndReturn(run func(context.Context, models.Provisionable, int64) (models.Provenance, error)) *MockProvisioningStore_GetProvenance_Call {
_c.Call.Return(run)
return _c
}
// GetProvenances provides a mock function with given fields: ctx, org, resourceType
func (_m *MockProvisioningStore) GetProvenances(ctx context.Context, org int64, resourceType string) (map[string]models.Provenance, error) {
ret := _m.Called(ctx, org, resourceType)
var r0 map[string]models.Provenance
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, int64, string) (map[string]models.Provenance, error)); ok {
return rf(ctx, org, resourceType)
}
if rf, ok := ret.Get(0).(func(context.Context, int64, string) map[string]models.Provenance); ok {
r0 = rf(ctx, org, resourceType)
} else {
@ -122,7 +137,6 @@ func (_m *MockProvisioningStore) GetProvenances(ctx context.Context, org int64,
}
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, int64, string) error); ok {
r1 = rf(ctx, org, resourceType)
} else {
@ -141,7 +155,7 @@ type MockProvisioningStore_GetProvenances_Call struct {
// - ctx context.Context
// - org int64
// - resourceType string
func (_e *MockProvisioningStore_Expecter) GetProvenances(ctx any, org any, resourceType any) *MockProvisioningStore_GetProvenances_Call {
func (_e *MockProvisioningStore_Expecter) GetProvenances(ctx interface{}, org interface{}, resourceType interface{}) *MockProvisioningStore_GetProvenances_Call {
return &MockProvisioningStore_GetProvenances_Call{Call: _e.mock.On("GetProvenances", ctx, org, resourceType)}
}
@ -157,6 +171,11 @@ func (_c *MockProvisioningStore_GetProvenances_Call) Return(_a0 map[string]model
return _c
}
func (_c *MockProvisioningStore_GetProvenances_Call) RunAndReturn(run func(context.Context, int64, string) (map[string]models.Provenance, error)) *MockProvisioningStore_GetProvenances_Call {
_c.Call.Return(run)
return _c
}
// SetProvenance provides a mock function with given fields: ctx, o, org, p
func (_m *MockProvisioningStore) SetProvenance(ctx context.Context, o models.Provisionable, org int64, p models.Provenance) error {
ret := _m.Called(ctx, o, org, p)
@ -181,7 +200,7 @@ type MockProvisioningStore_SetProvenance_Call struct {
// - o models.Provisionable
// - org int64
// - p models.Provenance
func (_e *MockProvisioningStore_Expecter) SetProvenance(ctx any, o any, org any, p any) *MockProvisioningStore_SetProvenance_Call {
func (_e *MockProvisioningStore_Expecter) SetProvenance(ctx interface{}, o interface{}, org interface{}, p interface{}) *MockProvisioningStore_SetProvenance_Call {
return &MockProvisioningStore_SetProvenance_Call{Call: _e.mock.On("SetProvenance", ctx, o, org, p)}
}
@ -197,8 +216,17 @@ func (_c *MockProvisioningStore_SetProvenance_Call) Return(_a0 error) *MockProvi
return _c
}
// NewMockProvisioningStore creates a new instance of MockProvisioningStore. It also registers the testing.TB interface on the mock and a cleanup function to assert the mocks expectations.
func NewMockProvisioningStore(t testing.TB) *MockProvisioningStore {
func (_c *MockProvisioningStore_SetProvenance_Call) RunAndReturn(run func(context.Context, models.Provisionable, int64, models.Provenance) error) *MockProvisioningStore_SetProvenance_Call {
_c.Call.Return(run)
return _c
}
// NewMockProvisioningStore creates a new instance of MockProvisioningStore. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
// The first argument is typically a *testing.T value.
func NewMockProvisioningStore(t interface {
mock.TestingT
Cleanup(func())
}) *MockProvisioningStore {
mock := &MockProvisioningStore{}
mock.Mock.Test(t)

View File

@ -1,13 +1,12 @@
// Code generated by mockery v2.14.0. DO NOT EDIT.
// Code generated by mockery v2.34.2. DO NOT EDIT.
package provisioning
import (
context "context"
mock "github.com/stretchr/testify/mock"
quota "github.com/grafana/grafana/pkg/services/quota"
mock "github.com/stretchr/testify/mock"
)
// MockQuotaChecker is an autogenerated mock type for the QuotaChecker type
@ -28,13 +27,16 @@ func (_m *MockQuotaChecker) CheckQuotaReached(ctx context.Context, target quota.
ret := _m.Called(ctx, target, scopeParams)
var r0 bool
var r1 error
if rf, ok := ret.Get(0).(func(context.Context, quota.TargetSrv, *quota.ScopeParameters) (bool, error)); ok {
return rf(ctx, target, scopeParams)
}
if rf, ok := ret.Get(0).(func(context.Context, quota.TargetSrv, *quota.ScopeParameters) bool); ok {
r0 = rf(ctx, target, scopeParams)
} else {
r0 = ret.Get(0).(bool)
}
var r1 error
if rf, ok := ret.Get(1).(func(context.Context, quota.TargetSrv, *quota.ScopeParameters) error); ok {
r1 = rf(ctx, target, scopeParams)
} else {
@ -53,7 +55,7 @@ type MockQuotaChecker_CheckQuotaReached_Call struct {
// - ctx context.Context
// - target quota.TargetSrv
// - scopeParams *quota.ScopeParameters
func (_e *MockQuotaChecker_Expecter) CheckQuotaReached(ctx any, target any, scopeParams any) *MockQuotaChecker_CheckQuotaReached_Call {
func (_e *MockQuotaChecker_Expecter) CheckQuotaReached(ctx interface{}, target interface{}, scopeParams interface{}) *MockQuotaChecker_CheckQuotaReached_Call {
return &MockQuotaChecker_CheckQuotaReached_Call{Call: _e.mock.On("CheckQuotaReached", ctx, target, scopeParams)}
}
@ -69,13 +71,17 @@ func (_c *MockQuotaChecker_CheckQuotaReached_Call) Return(_a0 bool, _a1 error) *
return _c
}
type mockConstructorTestingTNewMockQuotaChecker interface {
mock.TestingT
Cleanup(func())
func (_c *MockQuotaChecker_CheckQuotaReached_Call) RunAndReturn(run func(context.Context, quota.TargetSrv, *quota.ScopeParameters) (bool, error)) *MockQuotaChecker_CheckQuotaReached_Call {
_c.Call.Return(run)
return _c
}
// NewMockQuotaChecker creates a new instance of MockQuotaChecker. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations.
func NewMockQuotaChecker(t mockConstructorTestingTNewMockQuotaChecker) *MockQuotaChecker {
// The first argument is typically a *testing.T value.
func NewMockQuotaChecker(t interface {
mock.TestingT
Cleanup(func())
}) *MockQuotaChecker {
mock := &MockQuotaChecker{}
mock.Mock.Test(t)

View File

@ -70,9 +70,9 @@ func newFakeAMConfigStore(config string) *fakeAMConfigStore {
}
}
func (f *fakeAMConfigStore) GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) (*models.AlertConfiguration, error) {
func (f *fakeAMConfigStore) GetLatestAlertmanagerConfiguration(ctx context.Context, orgID int64) (*models.AlertConfiguration, error) {
result := &f.config
result.OrgID = query.OrgID
result.OrgID = orgID
result.ConfigurationHash = fmt.Sprintf("%x", md5.Sum([]byte(f.config.AlertmanagerConfiguration)))
return result, nil
}

View File

@ -24,11 +24,11 @@ var (
// GetLatestAlertmanagerConfiguration returns the lastest version of the alertmanager configuration.
// It returns ErrNoAlertmanagerConfiguration if no configuration is found.
func (st *DBstore) GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) (result *models.AlertConfiguration, err error) {
func (st *DBstore) GetLatestAlertmanagerConfiguration(ctx context.Context, orgID int64) (result *models.AlertConfiguration, err error) {
err = st.SQLStore.WithDbSession(ctx, func(sess *db.Session) error {
c := &models.AlertConfiguration{}
// The ID is already an auto incremental column, using the ID as an order should guarantee the latest.
ok, err := sess.Table("alert_configuration").Where("org_id = ?", query.OrgID).Get(c)
ok, err := sess.Table("alert_configuration").Where("org_id = ?", orgID).Get(c)
if err != nil {
return err
}

View File

@ -27,11 +27,7 @@ func TestIntegrationAlertmanagerStore(t *testing.T) {
t.Run("GetLatestAlertmanagerConfiguration for org that doesn't exist returns error", func(t *testing.T) {
_, _ = setupConfig(t, "my-config", store)
req := &models.GetLatestAlertmanagerConfigurationQuery{
OrgID: 1234,
}
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), 1234)
require.ErrorIs(t, err, ErrNoAlertmanagerConfiguration)
require.Nil(t, config)
@ -39,11 +35,7 @@ func TestIntegrationAlertmanagerStore(t *testing.T) {
t.Run("GetLatestAlertmanagerConfiguration return the right config", func(t *testing.T) {
_, configMD5 := setupConfig(t, "my-config", store)
req := &models.GetLatestAlertmanagerConfigurationQuery{
OrgID: 1,
}
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), 1)
require.NoError(t, err)
require.NotNil(t, config)
@ -55,11 +47,7 @@ func TestIntegrationAlertmanagerStore(t *testing.T) {
_, _ = setupConfig(t, "my-config1", store)
_, _ = setupConfig(t, "my-config2", store)
_, configMD5 := setupConfig(t, "my-config3", store)
req := &models.GetLatestAlertmanagerConfigurationQuery{
OrgID: 1,
}
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), 1)
require.NoError(t, err)
require.NotNil(t, config)
@ -109,8 +97,7 @@ func TestIntegrationAlertmanagerStore(t *testing.T) {
require.ErrorContains(t, err, "callback failed")
// Assert that we rolled back the transaction.
get := &models.GetLatestAlertmanagerConfigurationQuery{OrgID: 1}
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), get)
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), 1)
require.NoError(t, err)
require.Equal(t, config.AlertmanagerConfiguration, "my-config")
})
@ -136,10 +123,7 @@ func TestIntegrationAlertmanagerHash(t *testing.T) {
t.Run("When passing the right hash the config should be updated", func(t *testing.T) {
_, configMD5 := setupConfig(t, "my-config", store)
req := &models.GetLatestAlertmanagerConfigurationQuery{
OrgID: 1,
}
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
config, err := store.GetLatestAlertmanagerConfiguration(context.Background(), 1)
require.NoError(t, err)
require.Equal(t, configMD5, config.ConfigurationHash)
newConfig, newConfigMD5 := "my-config-new", fmt.Sprintf("%x", md5.Sum([]byte("my-config-new")))
@ -151,7 +135,7 @@ func TestIntegrationAlertmanagerHash(t *testing.T) {
OrgID: 1,
})
require.NoError(t, err)
config, err = store.GetLatestAlertmanagerConfiguration(context.Background(), req)
config, err = store.GetLatestAlertmanagerConfiguration(context.Background(), 1)
require.NoError(t, err)
require.Equal(t, newConfig, config.AlertmanagerConfiguration)
require.Equal(t, newConfigMD5, config.ConfigurationHash)
@ -159,10 +143,7 @@ func TestIntegrationAlertmanagerHash(t *testing.T) {
t.Run("When passing the wrong hash the update should error", func(t *testing.T) {
config, configMD5 := setupConfig(t, "my-config", store)
req := &models.GetLatestAlertmanagerConfigurationQuery{
OrgID: 1,
}
amConfig, err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
amConfig, err := store.GetLatestAlertmanagerConfiguration(context.Background(), 1)
require.NoError(t, err)
require.Equal(t, configMD5, amConfig.ConfigurationHash)
err = store.UpdateAlertmanagerConfiguration(context.Background(), &models.SaveAlertmanagerConfigurationCmd{
@ -219,10 +200,7 @@ func TestIntegrationAlertmanagerConfigCleanup(t *testing.T) {
require.Equal(t, int64(0), rowsAffected)
require.NoError(t, err)
req := &models.GetLatestAlertmanagerConfigurationQuery{
OrgID: orgID,
}
amConfig, err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
amConfig, err := store.GetLatestAlertmanagerConfiguration(context.Background(), orgID)
require.NoError(t, err)
require.Equal(t, "newest-record", amConfig.AlertmanagerConfiguration)
})
@ -259,10 +237,7 @@ func TestIntegrationAlertmanagerConfigCleanup(t *testing.T) {
require.Equal(t, int64(2), rowsAffacted)
require.NoError(t, err)
req := &models.GetLatestAlertmanagerConfigurationQuery{
OrgID: orgID,
}
amConfig, err := store.GetLatestAlertmanagerConfiguration(context.Background(), req)
amConfig, err := store.GetLatestAlertmanagerConfiguration(context.Background(), orgID)
require.NoError(t, err)
require.Equal(t, "newest-record", amConfig.AlertmanagerConfiguration)
})
@ -314,10 +289,7 @@ func TestIntegrationMarkConfigurationAsApplied(t *testing.T) {
require.NoError(tt, err)
require.Len(tt, configs, 0)
query := models.GetLatestAlertmanagerConfigurationQuery{
OrgID: orgID,
}
amConfig, err := store.GetLatestAlertmanagerConfiguration(ctx, &query)
amConfig, err := store.GetLatestAlertmanagerConfiguration(ctx, orgID)
require.NoError(tt, err)
cmd := models.MarkConfigurationAsAppliedCmd{

View File

@ -22,7 +22,7 @@ const AlertDefinitionMaxTitleLength = 190
// AlertingStore is the database interface used by the Alertmanager service.
type AlertingStore interface {
GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) (*models.AlertConfiguration, error)
GetLatestAlertmanagerConfiguration(ctx context.Context, orgID int64) (*models.AlertConfiguration, error)
GetAllLatestAlertmanagerConfiguration(ctx context.Context) ([]*models.AlertConfiguration, error)
SaveAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error
SaveAlertmanagerConfigurationWithCallback(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd, callback SaveCallback) error