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