SetRulesConfig sets the current alertmanager config for a user.
(ctx context.Context, userID string, oldConfig, newConfig userconfig.RulesConfig)
| 205 | |
| 206 | // SetRulesConfig sets the current alertmanager config for a user. |
| 207 | func (d DB) SetRulesConfig(ctx context.Context, userID string, oldConfig, newConfig userconfig.RulesConfig) (bool, error) { |
| 208 | updated := false |
| 209 | err := d.Transaction(func(tx DB) error { |
| 210 | current, err := d.GetConfig(ctx, userID) |
| 211 | if err != nil && err != sql.ErrNoRows { |
| 212 | return err |
| 213 | } |
| 214 | // The supplied oldConfig must match the current config. If no config |
| 215 | // exists, then oldConfig must be nil. Otherwise, it must exactly |
| 216 | // equal the existing config. |
| 217 | if !((err == sql.ErrNoRows && oldConfig.Files == nil) || oldConfig.Equal(current.Config.RulesConfig)) { //nolint:staticcheck |
| 218 | return nil |
| 219 | } |
| 220 | new := userconfig.Config{ |
| 221 | AlertmanagerConfig: current.Config.AlertmanagerConfig, |
| 222 | RulesConfig: newConfig, |
| 223 | } |
| 224 | updated = true |
| 225 | return d.SetConfig(ctx, userID, new) |
| 226 | }) |
| 227 | return updated, err |
| 228 | } |
| 229 | |
| 230 | // findRulesConfigs helps GetAllRulesConfigs and GetRulesConfigs retrieve the |
| 231 | // set of all active rules configurations across all our users. |