2021-03-18 13:12:28 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-23 03:31:46 -05:00
|
|
|
"crypto/md5"
|
2021-03-18 13:12:28 -05:00
|
|
|
"fmt"
|
|
|
|
|
2021-09-21 10:01:23 -05:00
|
|
|
"xorm.io/builder"
|
|
|
|
|
2021-03-18 13:12:28 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrNoAlertmanagerConfiguration is an error for when no alertmanager configuration is found.
|
2021-03-24 09:20:44 -05:00
|
|
|
ErrNoAlertmanagerConfiguration = fmt.Errorf("could not find an Alertmanager configuration")
|
2022-03-23 03:31:46 -05:00
|
|
|
// ErrVersionLockedObjectNotFound is returned when an object is not
|
|
|
|
// found using the current hash.
|
|
|
|
ErrVersionLockedObjectNotFound = fmt.Errorf("could not find object using provided id and hash")
|
2021-03-18 13:12:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetLatestAlertmanagerConfiguration returns the lastest version of the alertmanager configuration.
|
|
|
|
// It returns ErrNoAlertmanagerConfiguration if no configuration is found.
|
2022-02-09 03:22:09 -06:00
|
|
|
func (st *DBstore) GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) error {
|
|
|
|
return st.SQLStore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
2021-05-14 15:13:44 -05:00
|
|
|
c := &models.AlertConfiguration{}
|
|
|
|
// The ID is already an auto incremental column, using the ID as an order should guarantee the latest.
|
2021-08-12 08:04:09 -05:00
|
|
|
ok, err := sess.Desc("id").Where("org_id = ?", query.OrgID).Limit(1).Get(c)
|
2021-03-18 13:12:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-14 15:13:44 -05:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return ErrNoAlertmanagerConfiguration
|
|
|
|
}
|
|
|
|
|
2021-03-31 15:00:56 -05:00
|
|
|
query.Result = c
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2021-03-18 13:12:28 -05:00
|
|
|
|
2021-09-21 10:01:23 -05:00
|
|
|
// GetAllLatestAlertmanagerConfiguration returns the latest configuration of every organization
|
|
|
|
func (st *DBstore) GetAllLatestAlertmanagerConfiguration(ctx context.Context) ([]*models.AlertConfiguration, error) {
|
|
|
|
var result []*models.AlertConfiguration
|
|
|
|
err := st.SQLStore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
|
|
|
condition := builder.In("id", builder.Select("MAX(id)").From("alert_configuration").GroupBy("org_id"))
|
|
|
|
if err := sess.Table("alert_configuration").Where(condition).Find(&result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-03-31 15:00:56 -05:00
|
|
|
// SaveAlertmanagerConfiguration creates an alertmanager configuration.
|
2022-02-09 03:22:09 -06:00
|
|
|
func (st DBstore) SaveAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error {
|
|
|
|
return st.SaveAlertmanagerConfigurationWithCallback(ctx, cmd, func() error { return nil })
|
2021-05-14 13:49:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type SaveCallback func() error
|
|
|
|
|
|
|
|
// SaveAlertmanagerConfigurationWithCallback creates an alertmanager configuration version and then executes a callback.
|
2021-09-21 10:01:23 -05:00
|
|
|
// If the callback results in error it rolls back the transaction.
|
2022-02-09 03:22:09 -06:00
|
|
|
func (st DBstore) SaveAlertmanagerConfigurationWithCallback(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd, callback SaveCallback) error {
|
|
|
|
return st.SQLStore.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
2021-03-31 15:00:56 -05:00
|
|
|
config := models.AlertConfiguration{
|
|
|
|
AlertmanagerConfiguration: cmd.AlertmanagerConfiguration,
|
2022-03-23 03:31:46 -05:00
|
|
|
ConfigurationHash: fmt.Sprintf("%x", md5.Sum([]byte(cmd.AlertmanagerConfiguration))),
|
2021-03-31 15:00:56 -05:00
|
|
|
ConfigurationVersion: cmd.ConfigurationVersion,
|
2021-05-14 13:49:54 -05:00
|
|
|
Default: cmd.Default,
|
2021-08-12 08:04:09 -05:00
|
|
|
OrgID: cmd.OrgID,
|
2021-03-31 15:00:56 -05:00
|
|
|
}
|
|
|
|
if _, err := sess.Insert(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-14 13:49:54 -05:00
|
|
|
|
|
|
|
if err := callback(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-31 15:00:56 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2022-03-23 03:31:46 -05:00
|
|
|
|
2022-04-05 16:48:51 -05:00
|
|
|
func (st *DBstore) UpdateAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error {
|
|
|
|
return st.SQLStore.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
2022-03-23 03:31:46 -05:00
|
|
|
config := models.AlertConfiguration{
|
|
|
|
AlertmanagerConfiguration: cmd.AlertmanagerConfiguration,
|
|
|
|
ConfigurationHash: fmt.Sprintf("%x", md5.Sum([]byte(cmd.AlertmanagerConfiguration))),
|
|
|
|
ConfigurationVersion: cmd.ConfigurationVersion,
|
|
|
|
Default: cmd.Default,
|
|
|
|
OrgID: cmd.OrgID,
|
|
|
|
}
|
|
|
|
rows, err := sess.Table("alert_configuration").Where(`
|
|
|
|
EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM alert_configuration
|
|
|
|
WHERE
|
|
|
|
org_id = ?
|
|
|
|
AND
|
|
|
|
id = (SELECT MAX(id) FROM alert_configuration WHERE org_id = ?)
|
|
|
|
AND
|
|
|
|
configuration_hash = ?
|
|
|
|
)`,
|
|
|
|
cmd.OrgID, cmd.OrgID, cmd.FetchedConfigurationHash).Insert(config)
|
|
|
|
if rows == 0 {
|
|
|
|
return ErrVersionLockedObjectNotFound
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|