Skip to content

Commit

Permalink
Alerting: Add Option to get all alert rules grafana#144
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit b384168
Merge: bc342a8 3f84a92
Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com>
Date:   Tue May 9 08:44:32 2023 +0200

    Merge branch 'grafana:master' into master

commit bc342a8
Author: Philip Penquitt <30809710+PhilipPenquitt@users.noreply.github.com>
Date:   Thu Apr 6 16:49:11 2023 +0200

    Trigger Build

commit 2a88c53
Author: phpe <30809710+PhilipPenquitt@users.noreply.github.com>
Date:   Thu Apr 6 16:46:06 2023 +0200

    Trigger Build

commit 2471267
Author: Elaine Vuong <elaine.yt.vuong@gmail.com>
Date:   Thu May 4 14:57:04 2023 -0400

    SLO: CRUD API Functionality  (grafana#145)

    * ListSLOs functionality retrives all SLOs

    * CreateSLO and GetSLO Functionality implemented

    * DeleteSLO Functionality implemented

    * UpdateSLO Functionality implemented

    * UpdateSLO Functionality corrected

    * Update Linting Errors

    * Removes Unnecessary Comments

    * Update Variable Naming

    * Lint Checker

    * Update Tests for SLOs

    * Updating Slo Types with a comment to original source files

    * Updated SLO Types and Tests

commit 836a3db
Author: Julien Duchesne <julien.duchesne@grafana.com>
Date:   Thu Apr 27 09:15:11 2023 -0400

    Folder UID: Wrong field type (grafana#148)

    Doh!

commit ca95730
Author: Julien Duchesne <julien.duchesne@grafana.com>
Date:   Thu Apr 27 08:35:50 2023 -0400

    Dashboard: Get folder UID from reads (grafana#147)

    It's easier to manage folders with UIDs than IDs. This will be used to add that functionality to the Terraform provider

commit fcca53a
Author: Julien Duchesne <julien.duchesne@grafana.com>
Date:   Tue Apr 25 13:02:16 2023 -0400

    Folder delete: Fix panic when params are set (grafana#146)

commit 65edf1b
Author: Alex Weaver <weaver.alex.d@gmail.com>
Date:   Tue May 2 15:02:11 2023 -0500

    Add dependabot

commit 3a31f6f
Merge: 0252eaa 6b31ab5
Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com>
Date:   Tue Apr 18 10:58:34 2023 +0200

    Merge branch 'master' of https://github.com/flkhndlr/grafana-api-golang-client

commit 0252eaa
Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com>
Date:   Tue Apr 18 10:57:39 2023 +0200

    fix: change name of variable

commit 6b31ab5
Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com>
Date:   Tue Apr 18 10:44:29 2023 +0200

    adding back whitespaces

commit b1311f0
Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com>
Date:   Tue Apr 18 10:41:25 2023 +0200

    clear whitespaces

commit 17398b1
Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com>
Date:   Tue Apr 18 10:29:34 2023 +0200

    remove whitespace

commit 10b4067
Author: Julien Duchesne <julien.duchesne@grafana.com>
Date:   Mon Apr 10 18:58:06 2023 -0400

    Folder: Add option to force delete folder (grafana#142)

    grafana/terraform-provider-grafana#855

commit e73fa1f
Author: Julien Duchesne <julien.duchesne@grafana.com>
Date:   Mon Apr 10 18:57:49 2023 -0400

    Add missing report fields (grafana#143)

    grafana/terraform-provider-grafana#862

commit 62aa037
Author: Philip Penquitt <30809710+PhilipPenquitt@users.noreply.github.com>
Date:   Thu Apr 6 16:49:11 2023 +0200

    Trigger Build

commit 2cb0ad5
Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com>
Date:   Thu Apr 6 15:01:20 2023 +0200

    adding alert_rule fetch function (grafana#1)

    Co-authored-by: fkhe <Falk.Haendler@t-systems.com>

commit c27c52e
Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com>
Date:   Thu Apr 6 14:59:04 2023 +0200

    correcting testcases

commit 4732c3e
Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com>
Date:   Thu Apr 6 14:49:25 2023 +0200

    correcting testcases, add documentation

commit 1921ba9
Author: flkhndlr <50197097+flkhndlr@users.noreply.github.com>
Date:   Thu Apr 6 13:23:20 2023 +0200

    adding alert_rule fetch function

commit 64ddabb
Author: phpe <30809710+PhilipPenquitt@users.noreply.github.com>
Date:   Thu Apr 6 16:46:06 2023 +0200

    Trigger Build

commit e009637
Author: Philip Penquitt <30809710+PhilipPenquitt@users.noreply.github.com>
Date:   Thu Apr 6 14:37:21 2023 +0200

    fix: formating fixed
  • Loading branch information
JanSF committed Sep 19, 2023
1 parent b3a3c20 commit 5260930
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
29 changes: 29 additions & 0 deletions alerting_alert_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gapi
import (
"encoding/json"
"fmt"
"net/url"
"time"
)

Expand Down Expand Up @@ -62,6 +63,34 @@ type RelativeTimeRange struct {
To time.Duration `json:"to"`
}

// AlertRules fetches and returns Grafana alertRules.
func (c *Client) AlertRules() ([]AlertRule, error) {
const limit = 1000

var (
page = 0
newAlertRules []AlertRule
alertRules []AlertRule
query = make(url.Values)
)
query.Set("limit", fmt.Sprint(limit))

for {
page++
query.Set("page", fmt.Sprint(page))

if err := c.request("GET", "/api/v1/provisioning/alert-rules", query, nil, &newAlertRules); err != nil {
return nil, err
}

alertRules = append(alertRules, newAlertRules...)

if len(newAlertRules) < limit {
return alertRules, nil
}
}
}

// AlertRule fetches a single alert rule, identified by its UID.
func (c *Client) AlertRule(uid string) (AlertRule, error) {
path := fmt.Sprintf("/api/v1/provisioning/alert-rules/%s", uid)
Expand Down
47 changes: 46 additions & 1 deletion alerting_alert_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,47 @@ package gapi

import (
"encoding/json"
"strings"
"testing"
"time"

"github.com/gobs/pretty"
)

func TestAlertRules(t *testing.T) {
mockData := strings.Repeat(getAlertRulesJSON+",", 1000) // make 1000 alertRules.
mockData = "[" + mockData[:len(mockData)-1] + "]" // remove trailing comma; make a json list.

// This creates 1000 + 1000 + 1 (2001, 3 calls) worth of alertRules.

client := gapiTestToolsFromCalls(t, []mockServerCall{
{200, mockData},
{200, mockData},
{200, "[" + getAlertRulesJSON + "]"},
})

const dashCount = 2001

alertRules, err := client.AlertRules()
if err != nil {
t.Fatal(err)
}

t.Log(pretty.PrettyFormat(alertRules))

if len(alertRules) != dashCount {
t.Errorf("Length of returned folders should be %d", dashCount)
}

if alertRules[0].UID != "123abcd" || alertRules[0].Title != "Always in alarm" {
t.Error("Not correctly parsing returned alertRules.")
}
if alertRules[dashCount-1].UID != "123abcd" || alertRules[dashCount-1].Title != "Always in alarm" {
t.Error("Not correctly parsing returned alertRules.")
}
}

func TestAlertRule(t *testing.T) {
t.Run("get alert rule succeeds", func(t *testing.T) {
client := gapiTestTools(t, 200, getAlertRuleJSON)

Expand Down Expand Up @@ -161,7 +195,18 @@ const writeAlertRuleJSON = `
"for": "1m"
}
`

const getAlertRulesJSON = `{
"conditions": "A",
"data": [{"datasourceUid":"-100","model":{"conditions":[{"evaluator":{"params":[0,0],"type":"gt"},"operator":{"type":"and"},"query":{"params":[]},"reducer":{"params":[],"type":"avg"},"type":"query"}],"datasource":{"type":"__expr__","uid":"__expr__"},"expression":"1 == 1","hide":false,"intervalMs":1000,"maxDataPoints":43200,"refId":"A","type":"math"},"queryType":"","refId":"A","relativeTimeRange":{"from":0,"to":0}}],
"execErrState": "OK",
"folderUID": "project_test",
"noDataState": "OK",
"orgId": 1,
"uid": "123abcd",
"ruleGroup": "eval_group_1",
"title": "Always in alarm",
"for": "1m"
}`
const getAlertRuleJSON = `
{
"conditions": "A",
Expand Down

0 comments on commit 5260930

Please sign in to comment.