|
| 1 | +package gapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "regexp" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// Represents a notification routing tree in Grafana Alerting. |
| 11 | +type NotificationPolicy struct { |
| 12 | + Receiver string `json:"receiver,omitempty"` |
| 13 | + GroupBy []string `json:"group_by,omitempty"` |
| 14 | + Routes []SpecificPolicy `json:"routes,omitempty"` |
| 15 | + GroupWait time.Duration `json:"group_wait,omitempty"` |
| 16 | + GroupInterval time.Duration `json:"group_interval,omitempty"` |
| 17 | + RepeatInterval time.Duration `json:"repeat_interval,omitempty"` |
| 18 | + Provenance string `json:"provenance,omitempty"` |
| 19 | +} |
| 20 | + |
| 21 | +// Represents a non-root node in a notification routing tree. |
| 22 | +type SpecificPolicy struct { |
| 23 | + Receiver string `json:"receiver,omitempty"` |
| 24 | + GroupBy []string `json:"group_by,omitempty"` |
| 25 | + ObjectMatchers Matchers `json:"object_matchers,omitempty"` |
| 26 | + MuteTimeIntervals []string `json:"mute_time_intervals,omitempty"` |
| 27 | + Continue bool `json:"continue"` |
| 28 | + Routes []SpecificPolicy `json:"routes,omitempty"` |
| 29 | + GroupWait time.Duration `json:"group_wait,omitempty"` |
| 30 | + GroupInterval time.Duration `json:"group_interval,omitempty"` |
| 31 | + RepeatInterval time.Duration `json:"repeat_interval,omitempty"` |
| 32 | +} |
| 33 | + |
| 34 | +type Matchers []Matcher |
| 35 | + |
| 36 | +type Matcher struct { |
| 37 | + Type MatchType |
| 38 | + Name string |
| 39 | + Value string |
| 40 | + re *regexp.Regexp |
| 41 | +} |
| 42 | + |
| 43 | +type MatchType int |
| 44 | + |
| 45 | +const ( |
| 46 | + MatchEqual MatchType = iota |
| 47 | + MatchNotEqual |
| 48 | + MatchRegexp |
| 49 | + MatchNotRegexp |
| 50 | +) |
| 51 | + |
| 52 | +func (m MatchType) String() string { |
| 53 | + typeToStr := map[MatchType]string{ |
| 54 | + MatchEqual: "=", |
| 55 | + MatchNotEqual: "!=", |
| 56 | + MatchRegexp: "=~", |
| 57 | + MatchNotRegexp: "!~", |
| 58 | + } |
| 59 | + if str, ok := typeToStr[m]; ok { |
| 60 | + return str |
| 61 | + } |
| 62 | + panic("unknown match type") |
| 63 | +} |
| 64 | + |
| 65 | +// UnmarshalJSON implements the json.Unmarshaler interface for Matchers. |
| 66 | +func (m *Matchers) UnmarshalJSON(data []byte) error { |
| 67 | + var rawMatchers [][3]string |
| 68 | + if err := json.Unmarshal(data, &rawMatchers); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + for _, rawMatcher := range rawMatchers { |
| 72 | + var matchType MatchType |
| 73 | + switch rawMatcher[1] { |
| 74 | + case "=": |
| 75 | + matchType = MatchEqual |
| 76 | + case "!=": |
| 77 | + matchType = MatchNotEqual |
| 78 | + case "=~": |
| 79 | + matchType = MatchRegexp |
| 80 | + case "!~": |
| 81 | + matchType = MatchNotRegexp |
| 82 | + default: |
| 83 | + return fmt.Errorf("unsupported match type %q in matcher", rawMatcher[1]) |
| 84 | + } |
| 85 | + |
| 86 | + matcher := Matcher{ |
| 87 | + Type: matchType, |
| 88 | + Name: rawMatcher[0], |
| 89 | + Value: rawMatcher[2], |
| 90 | + } |
| 91 | + if matchType == MatchRegexp || matchType == MatchNotRegexp { |
| 92 | + re, err := regexp.Compile("^(?:" + rawMatcher[2] + ")$") |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + matcher.re = re |
| 97 | + } |
| 98 | + *m = append(*m, matcher) |
| 99 | + } |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +// MarshalJSON implements the json.Marshaler interface for Matchers. |
| 104 | +func (m Matchers) MarshalJSON() ([]byte, error) { |
| 105 | + if len(m) == 0 { |
| 106 | + return nil, nil |
| 107 | + } |
| 108 | + result := make([][3]string, len(m)) |
| 109 | + for i, matcher := range m { |
| 110 | + result[i] = [3]string{matcher.Name, matcher.Type.String(), matcher.Value} |
| 111 | + } |
| 112 | + return json.Marshal(result) |
| 113 | +} |
0 commit comments