(w http.ResponseWriter, req *http.Request)
| 554 | } |
| 555 | |
| 556 | func (a *API) CreateRuleGroup(w http.ResponseWriter, req *http.Request) { |
| 557 | logger := util_log.WithContext(req.Context(), a.logger) |
| 558 | userID, namespace, _, err := parseRequest(req, true, false) |
| 559 | if err != nil { |
| 560 | util_api.RespondError(logger, w, v1.ErrBadData, err.Error(), http.StatusBadRequest) |
| 561 | return |
| 562 | } |
| 563 | |
| 564 | payload, err := io.ReadAll(req.Body) |
| 565 | if err != nil { |
| 566 | level.Error(logger).Log("msg", "unable to read rule group payload", "err", err.Error()) |
| 567 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 568 | return |
| 569 | } |
| 570 | |
| 571 | level.Debug(logger).Log("msg", "attempting to unmarshal rulegroup", "userID", userID, "group", string(payload)) |
| 572 | |
| 573 | rg := rulefmt.RuleGroup{} |
| 574 | err = yaml.Unmarshal(payload, &rg) |
| 575 | if err != nil { |
| 576 | level.Error(logger).Log("msg", "unable to unmarshal rule group payload", "err", err.Error()) |
| 577 | http.Error(w, ErrBadRuleGroup.Error(), http.StatusBadRequest) |
| 578 | return |
| 579 | } |
| 580 | |
| 581 | errs := a.ruler.manager.ValidateRuleGroup(rg) |
| 582 | if len(errs) > 0 { |
| 583 | e := []string{} |
| 584 | for _, err := range errs { |
| 585 | level.Error(logger).Log("msg", "unable to validate rule group payload", "err", err.Error()) |
| 586 | e = append(e, err.Error()) |
| 587 | } |
| 588 | |
| 589 | http.Error(w, strings.Join(e, ", "), http.StatusBadRequest) |
| 590 | return |
| 591 | } |
| 592 | |
| 593 | if err := a.ruler.AssertMaxRulesPerRuleGroup(userID, len(rg.Rules)); err != nil { |
| 594 | level.Error(logger).Log("msg", "limit validation failure", "err", err.Error(), "user", userID) |
| 595 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 596 | return |
| 597 | } |
| 598 | |
| 599 | if a.ruler.HasMaxRuleGroupsLimit(userID) { |
| 600 | rgs, err := a.store.ListRuleGroupsForUserAndNamespace(req.Context(), userID, "") |
| 601 | if err != nil { |
| 602 | level.Error(logger).Log("msg", "unable to fetch current rule groups for validation", "err", err.Error(), "user", userID) |
| 603 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 604 | return |
| 605 | } |
| 606 | |
| 607 | if err := a.ruler.AssertMaxRuleGroups(userID, len(rgs)+1); err != nil { |
| 608 | level.Error(logger).Log("msg", "limit validation failure", "err", err.Error(), "user", userID) |
| 609 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 610 | return |
| 611 | } |
| 612 | } |
| 613 |
nothing calls this directly
no test coverage detected