From 7398fe3fcbdd7d25ae17b7575314796b6c0d664e Mon Sep 17 00:00:00 2001 From: Matthew Jacobson Date: Fri, 20 Sep 2024 15:25:14 -0400 Subject: [PATCH] Alerting: Proxy RouteDeleteAlertingConfig through MultiOrgAlertmanager (#93549) Proxy RouteDeleteAlertingConfig through MultiOrgAlertmanager --- pkg/services/ngalert/api/api_alertmanager.go | 10 +++------- .../ngalert/notifier/alertmanager_config.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pkg/services/ngalert/api/api_alertmanager.go b/pkg/services/ngalert/api/api_alertmanager.go index ff2cd001821..417beb74285 100644 --- a/pkg/services/ngalert/api/api_alertmanager.go +++ b/pkg/services/ngalert/api/api_alertmanager.go @@ -65,14 +65,10 @@ func (srv AlertmanagerSrv) RouteGetAMStatus(c *contextmodel.ReqContext) response } func (srv AlertmanagerSrv) RouteDeleteAlertingConfig(c *contextmodel.ReqContext) response.Response { - am, errResp := srv.AlertmanagerFor(c.SignedInUser.GetOrgID()) - if errResp != nil { - return errResp - } - - if err := am.SaveAndApplyDefaultConfig(c.Req.Context()); err != nil { + err := srv.mam.SaveAndApplyDefaultConfig(c.Req.Context(), c.SignedInUser.GetOrgID()) + if err != nil { srv.log.Error("Unable to save and apply default alertmanager configuration", "error", err) - return ErrResp(http.StatusInternalServerError, err, "failed to save and apply default Alertmanager configuration") + return response.ErrOrFallback(http.StatusInternalServerError, "failed to save and apply default Alertmanager configuration", err) } return response.JSON(http.StatusAccepted, util.DynMap{"message": "configuration deleted; the default is applied"}) diff --git a/pkg/services/ngalert/notifier/alertmanager_config.go b/pkg/services/ngalert/notifier/alertmanager_config.go index ec6472723e2..7d71de2c343 100644 --- a/pkg/services/ngalert/notifier/alertmanager_config.go +++ b/pkg/services/ngalert/notifier/alertmanager_config.go @@ -49,6 +49,22 @@ type configurationStore interface { GetLatestAlertmanagerConfiguration(ctx context.Context, orgID int64) (*models.AlertConfiguration, error) } +func (moa *MultiOrgAlertmanager) SaveAndApplyDefaultConfig(ctx context.Context, orgId int64) error { + moa.alertmanagersMtx.RLock() + defer moa.alertmanagersMtx.RUnlock() + + orgAM, err := moa.alertmanagerForOrg(orgId) + if err != nil { + return err + } + + err = orgAM.SaveAndApplyDefaultConfig(ctx) + if err != nil { + return err + } + return nil +} + // ApplyConfig will apply the given alertmanager configuration for a given org. // Can be used to force regeneration of autogenerated routes. func (moa *MultiOrgAlertmanager) ApplyConfig(ctx context.Context, orgId int64, dbConfig *models.AlertConfiguration) error {