Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit dbae38f

Browse files
authored
Merge branch 'master' into alexweav/policies
2 parents 2c14d00 + e9dd216 commit dbae38f

File tree

4 files changed

+256
-2
lines changed

4 files changed

+256
-2
lines changed

alerting_mute_timing.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package gapi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
)
8+
9+
// MuteTiming represents a Grafana Alerting mute timing.
10+
type MuteTiming struct {
11+
Name string `json:"name"`
12+
TimeIntervals []TimeInterval `json:"time_intervals"`
13+
Provenance string `json:"provenance,omitempty"`
14+
}
15+
16+
// TimeInterval describes intervals of time using a Prometheus-defined standard.
17+
type TimeInterval struct {
18+
Times []TimeRange `json:"times,omitempty"`
19+
Weekdays []WeekdayRange `json:"weekdays,omitempty"`
20+
DaysOfMonth []DayOfMonthRange `json:"days_of_month,omitempty"`
21+
Months []MonthRange `json:"months,omitempty"`
22+
Years []YearRange `json:"years,omitempty"`
23+
}
24+
25+
// TimeRange represents a range of minutes within a 1440 minute day, exclusive of the End minute.
26+
type TimeRange struct {
27+
StartMinute int
28+
EndMinute int
29+
}
30+
31+
// A WeekdayRange is an inclusive range of weekdays, e.g. "monday" or "tuesday:thursday".
32+
type WeekdayRange string
33+
34+
// A DayOfMonthRange is an inclusive range of days, 1-31, within a month, e.g. "1" or "14:16". Negative values can be used to represent days counting from the end of a month, e.g. "-1".
35+
type DayOfMonthRange string
36+
37+
// A MonthRange is an inclusive range of months, either numerical or full calendar month, e.g "1:3", "december", or "may:august".
38+
type MonthRange string
39+
40+
// A YearRange is a positive inclusive range of years, e.g. "2030" or "2021:2022".
41+
type YearRange string
42+
43+
// MuteTimings fetches all mute timings.
44+
func (c *Client) MuteTimings() ([]MuteTiming, error) {
45+
mts := make([]MuteTiming, 0)
46+
err := c.request("GET", "/api/v1/provisioning/mute-timings", nil, nil, &mts)
47+
if err != nil {
48+
return nil, err
49+
}
50+
return mts, nil
51+
}
52+
53+
// MuteTiming fetches a single mute timing, identified by its name.
54+
func (c *Client) MuteTiming(name string) (MuteTiming, error) {
55+
mt := MuteTiming{}
56+
uri := fmt.Sprintf("/api/v1/provisioning/mute-timings/%s", name)
57+
err := c.request("GET", uri, nil, nil, &mt)
58+
return mt, err
59+
}
60+
61+
// NewMuteTiming creates a new mute timing.
62+
func (c *Client) NewMuteTiming(mt *MuteTiming) error {
63+
req, err := json.Marshal(mt)
64+
if err != nil {
65+
return err
66+
}
67+
68+
return c.request("POST", "/api/v1/provisioning/mute-timings", nil, bytes.NewBuffer(req), nil)
69+
}
70+
71+
// UpdateMuteTiming updates a mute timing.
72+
func (c *Client) UpdateMuteTiming(mt *MuteTiming) error {
73+
uri := fmt.Sprintf("/api/v1/provisioning/mute-timings/%s", mt.Name)
74+
req, err := json.Marshal(mt)
75+
if err != nil {
76+
return err
77+
}
78+
79+
return c.request("PUT", uri, nil, bytes.NewBuffer(req), nil)
80+
}
81+
82+
// DeleteMutetiming deletes a mute timing.
83+
func (c *Client) DeleteMuteTiming(name string) error {
84+
uri := fmt.Sprintf("/api/v1/provisioning/mute-timings/%s", name)
85+
return c.request("DELETE", uri, nil, nil, nil)
86+
}

alerting_mute_timing_test.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package gapi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gobs/pretty"
7+
)
8+
9+
func TestMuteTimings(t *testing.T) {
10+
t.Run("get mute timings succeeds", func(t *testing.T) {
11+
server, client := gapiTestTools(t, 200, getMuteTimingsJSON)
12+
defer server.Close()
13+
14+
mts, err := client.MuteTimings()
15+
16+
if err != nil {
17+
t.Error(err)
18+
}
19+
t.Log(pretty.PrettyFormat(mts))
20+
if len(mts) != 2 {
21+
t.Errorf("wrong number of mute timings returned, got %#v", mts)
22+
}
23+
if mts[0].Name != "timing one" {
24+
t.Errorf("incorrect name - expected %s on element %d, got %#v", "timing one", 0, mts)
25+
}
26+
if mts[1].Name != "another timing" {
27+
t.Errorf("incorrect name - expected %s on element %d, got %#v", "another timing", 1, mts)
28+
}
29+
})
30+
31+
t.Run("get mute timing succeeds", func(t *testing.T) {
32+
server, client := gapiTestTools(t, 200, muteTimingJSON)
33+
defer server.Close()
34+
35+
mt, err := client.MuteTiming("timing one")
36+
37+
if err != nil {
38+
t.Error(err)
39+
}
40+
t.Log(pretty.PrettyFormat(mt))
41+
if mt.Name != "timing one" {
42+
t.Errorf("incorrect name - expected %s, got %#v", "timing one", mt)
43+
}
44+
})
45+
46+
t.Run("get non-existent mute timing fails", func(t *testing.T) {
47+
server, client := gapiTestTools(t, 404, muteTimingJSON)
48+
defer server.Close()
49+
50+
mt, err := client.MuteTiming("does not exist")
51+
52+
if err == nil {
53+
t.Errorf("expected error but got nil")
54+
t.Log(pretty.PrettyFormat(mt))
55+
}
56+
})
57+
58+
t.Run("create mute timing succeeds", func(t *testing.T) {
59+
server, client := gapiTestTools(t, 201, muteTimingJSON)
60+
defer server.Close()
61+
mt := createMuteTiming()
62+
63+
err := client.NewMuteTiming(&mt)
64+
65+
if err != nil {
66+
t.Error(err)
67+
}
68+
})
69+
70+
t.Run("update mute timing succeeds", func(t *testing.T) {
71+
server, client := gapiTestTools(t, 200, muteTimingJSON)
72+
defer server.Close()
73+
mt := createMuteTiming()
74+
mt.TimeIntervals[0].Weekdays = []WeekdayRange{"tuesday", "thursday"}
75+
76+
err := client.UpdateMuteTiming(&mt)
77+
78+
if err != nil {
79+
t.Error(err)
80+
}
81+
})
82+
83+
t.Run("delete mute timing succeeds", func(t *testing.T) {
84+
server, client := gapiTestTools(t, 204, muteTimingJSON)
85+
defer server.Close()
86+
87+
err := client.DeleteMuteTiming("timing two")
88+
89+
if err != nil {
90+
t.Error(err)
91+
}
92+
})
93+
}
94+
95+
func createMuteTiming() MuteTiming {
96+
return MuteTiming{
97+
Name: "timing two",
98+
TimeIntervals: []TimeInterval{
99+
{
100+
Weekdays: []WeekdayRange{"monday", "wednesday"},
101+
Months: []MonthRange{"1:3", "4"},
102+
Years: []YearRange{"2022", "2023"},
103+
},
104+
},
105+
}
106+
}
107+
108+
const getMuteTimingsJSON = `
109+
[
110+
{
111+
"name": "timing one",
112+
"time_intervals": [
113+
{
114+
"times": [
115+
{
116+
"start_time": "13:13",
117+
"end_time": "15:15"
118+
}
119+
],
120+
"weekdays": [
121+
"monday:wednesday"
122+
],
123+
"months": [
124+
"1"
125+
]
126+
}
127+
]
128+
},
129+
{
130+
"name": "another timing",
131+
"time_intervals": [
132+
{
133+
"days_of_month": [
134+
"1"
135+
],
136+
"years": [
137+
"2030"
138+
]
139+
}
140+
]
141+
}
142+
]`
143+
144+
const muteTimingJSON = `
145+
{
146+
"name": "timing one",
147+
"time_intervals": [
148+
{
149+
"times": [
150+
{
151+
"start_time": "13:13",
152+
"end_time": "15:15"
153+
}
154+
],
155+
"weekdays": [
156+
"monday:wednesday"
157+
],
158+
"months": [
159+
"1"
160+
]
161+
}
162+
]
163+
}`

service_account.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ type CreateServiceAccountTokenRequest struct {
1717

1818
// CreateServiceAccountRequest is the request body for creating a new service account.
1919
type CreateServiceAccountRequest struct {
20-
Name string `json:"name"`
20+
Name string `json:"name"`
21+
Role string `json:"role,omitempty"`
22+
IsDisabled *bool `json:"isDisabled,omitempty"`
2123
}
2224

2325
// UpdateServiceAccountRequest is the request body for modifying a service account.

service_account_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,11 @@ func TestCreateServiceAccount(t *testing.T) {
9898
server, client := gapiTestTools(t, http.StatusOK, serviceAccountJSON)
9999
defer server.Close()
100100

101+
isDisabled := true
101102
req := CreateServiceAccountRequest{
102-
Name: "newSA",
103+
Name: "newSA",
104+
Role: "Admin",
105+
IsDisabled: &isDisabled,
103106
}
104107

105108
res, err := client.CreateServiceAccount(req)

0 commit comments

Comments
 (0)