2022-04-05 16:48:51 -05:00
|
|
|
package provisioning
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-09 03:05:52 -05:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-04-05 16:48:51 -05:00
|
|
|
|
2022-09-09 03:05:52 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
2022-04-05 16:48:51 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
2022-07-13 17:36:17 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/quota"
|
2022-04-05 16:48:51 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// AMStore is a store of Alertmanager configurations.
|
2022-09-12 05:03:49 -05:00
|
|
|
//
|
2022-04-28 13:51:57 -05:00
|
|
|
//go:generate mockery --name AMConfigStore --structname MockAMConfigStore --inpackage --filename persist_mock.go --with-expecter
|
2022-04-05 16:48:51 -05:00
|
|
|
type AMConfigStore interface {
|
2023-03-28 03:34:35 -05:00
|
|
|
GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) (*models.AlertConfiguration, error)
|
2022-04-05 16:48:51 -05:00
|
|
|
UpdateAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProvisioningStore is a store of provisioning data for arbitrary objects.
|
2022-09-12 05:03:49 -05:00
|
|
|
//
|
2022-05-05 15:21:42 -05:00
|
|
|
//go:generate mockery --name ProvisioningStore --structname MockProvisioningStore --inpackage --filename provisioning_store_mock.go --with-expecter
|
2022-04-05 16:48:51 -05:00
|
|
|
type ProvisioningStore interface {
|
2022-04-26 10:30:57 -05:00
|
|
|
GetProvenance(ctx context.Context, o models.Provisionable, org int64) (models.Provenance, error)
|
|
|
|
GetProvenances(ctx context.Context, org int64, resourceType string) (map[string]models.Provenance, error)
|
|
|
|
SetProvenance(ctx context.Context, o models.Provisionable, org int64, p models.Provenance) error
|
|
|
|
DeleteProvenance(ctx context.Context, o models.Provisionable, org int64) error
|
2022-04-05 16:48:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TransactionManager represents the ability to issue and close transactions through contexts.
|
|
|
|
type TransactionManager interface {
|
|
|
|
InTransaction(ctx context.Context, work func(ctx context.Context) error) error
|
|
|
|
}
|
2022-06-09 03:38:46 -05:00
|
|
|
|
|
|
|
// RuleStore represents the ability to persist and query alert rules.
|
|
|
|
type RuleStore interface {
|
2023-03-28 03:34:35 -05:00
|
|
|
GetAlertRuleByUID(ctx context.Context, query *models.GetAlertRuleByUIDQuery) (*models.AlertRule, error)
|
|
|
|
ListAlertRules(ctx context.Context, query *models.ListAlertRulesQuery) (models.RulesGroup, error)
|
2022-06-09 03:38:46 -05:00
|
|
|
GetRuleGroupInterval(ctx context.Context, orgID int64, namespaceUID string, ruleGroup string) (int64, error)
|
|
|
|
InsertAlertRules(ctx context.Context, rule []models.AlertRule) (map[string]int64, error)
|
2022-09-29 15:47:56 -05:00
|
|
|
UpdateAlertRules(ctx context.Context, rule []models.UpdateRule) error
|
2022-06-09 03:38:46 -05:00
|
|
|
DeleteAlertRulesByUID(ctx context.Context, orgID int64, ruleUID ...string) error
|
2023-03-28 03:34:35 -05:00
|
|
|
GetAlertRulesGroupByRuleUID(ctx context.Context, query *models.GetAlertRulesGroupByRuleUIDQuery) ([]*models.AlertRule, error)
|
2022-06-09 03:38:46 -05:00
|
|
|
}
|
2022-07-13 17:36:17 -05:00
|
|
|
|
|
|
|
// QuotaChecker represents the ability to evaluate whether quotas are met.
|
2022-09-12 05:03:49 -05:00
|
|
|
//
|
2022-07-13 17:36:17 -05:00
|
|
|
//go:generate mockery --name QuotaChecker --structname MockQuotaChecker --inpackage --filename quota_checker_mock.go --with-expecter
|
|
|
|
type QuotaChecker interface {
|
2022-11-14 13:08:10 -06:00
|
|
|
CheckQuotaReached(ctx context.Context, target quota.TargetSrv, scopeParams *quota.ScopeParameters) (bool, error)
|
2022-07-13 17:36:17 -05:00
|
|
|
}
|
2022-09-09 03:05:52 -05:00
|
|
|
|
|
|
|
// PersistConfig validates to config before eventually persisting it if no error occurs
|
|
|
|
func PersistConfig(ctx context.Context, store AMConfigStore, cmd *models.SaveAlertmanagerConfigurationCmd) error {
|
|
|
|
cfg := &definitions.PostableUserConfig{}
|
|
|
|
if err := json.Unmarshal([]byte(cmd.AlertmanagerConfiguration), cfg); err != nil {
|
|
|
|
return fmt.Errorf("change would result in an invalid configuration state: %w", err)
|
|
|
|
}
|
|
|
|
return store.UpdateAlertmanagerConfiguration(ctx, cmd)
|
|
|
|
}
|