Alerting: Rule Modify Export APIs (#75322)
* extend RuleStore interface to get namespace by UID * add new export API endpoints * implement request handlers * update authorization and wire handlers to paths * add folder error matchers to errorToResponse * add tests for export methods
This commit is contained in:
@@ -4,8 +4,10 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
|
||||
"github.com/grafana/grafana/pkg/expr"
|
||||
@@ -92,6 +94,7 @@ func WithNotEmptyLabels(count int, prefix string) AlertRuleMutator {
|
||||
rule.Labels = GenerateAlertLabels(count, prefix)
|
||||
}
|
||||
}
|
||||
|
||||
func WithUniqueID() AlertRuleMutator {
|
||||
usedID := make(map[int64]struct{})
|
||||
return func(rule *AlertRule) {
|
||||
@@ -227,6 +230,29 @@ func WithLabel(key, value string) AlertRuleMutator {
|
||||
}
|
||||
}
|
||||
|
||||
func WithUniqueUID(knownUids *sync.Map) AlertRuleMutator {
|
||||
return func(rule *AlertRule) {
|
||||
uid := rule.UID
|
||||
for {
|
||||
_, ok := knownUids.LoadOrStore(uid, struct{}{})
|
||||
if !ok {
|
||||
rule.UID = uid
|
||||
return
|
||||
}
|
||||
uid = uuid.NewString()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func WithQuery(query ...AlertQuery) AlertRuleMutator {
|
||||
return func(rule *AlertRule) {
|
||||
rule.Data = query
|
||||
if len(query) > 1 {
|
||||
rule.Condition = query[0].RefID
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GenerateAlertLabels(count int, prefix string) data.Labels {
|
||||
labels := make(data.Labels, count)
|
||||
for i := 0; i < count; i++ {
|
||||
|
||||
Reference in New Issue
Block a user