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

Update alert notification and test has been added #1

Closed
wants to merge 60 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
54c6b7d
CRUD methods for data sources.
apparentlymart Oct 12, 2015
d49f95c
CRUD methods for Dashboards.
apparentlymart Oct 12, 2015
086eb9b
Implemented alert notification management
mvisonneau Aug 15, 2017
912be29
add JSONData and SecureJSONData to DataSource
mdb Sep 21, 2017
ed820f6
add basic NewDataSource test
mdb Sep 21, 2017
caa7705
add test-running documentation
mdb Sep 21, 2017
3bdf366
log request details if env var `GF_LOG` is present
mdb Sep 21, 2017
35245a0
use `log.Println`
mdb Sep 27, 2017
8003b0c
If the base URL includes a path component, don’t overwrite this with the
sl1pm4t Nov 17, 2017
b18120d
Add assumeRoleArn into data structure
spirius Dec 15, 2017
2967b3a
Add customMetricsNamespaces into data structure
Feb 22, 2018
7380665
Add customMetricsNamespaces into data structure (#1)
teemupo Feb 22, 2018
a2ec15b
Add method for fetching org membership
mlclmj Jun 18, 2018
1ad379d
Add methods to add/update/delete org users
mlclmj Jun 18, 2018
7fbea2b
Moved OrgUser methods to separate file
mlclmj Jun 18, 2018
04e16fb
Fix spacing issue
mlclmj Jun 18, 2018
4743232
Add method to retrieve org by name
mlclmj Jun 18, 2018
443facd
Better naming for JSON data
mlclmj Jun 20, 2018
0b75ce3
Add endpoint for updating orgs
mlclmj Jun 20, 2018
a33f220
Add endpoint for getting org by id
mlclmj Jun 20, 2018
9968a4a
Fix arg type
mlclmj Jun 20, 2018
09e688d
Add request method for supporting GET parameters
mlclmj Jun 26, 2018
e3d5e5a
Add method for getting a user by login or email
mlclmj Jun 26, 2018
c8a6813
Add logging of query parameters
mlclmj Jun 26, 2018
4e1736b
Fix variable name
mlclmj Jun 26, 2018
9286081
Add logging
mlclmj Jun 27, 2018
7a4d445
Add generic method to create a new user
mlclmj Jun 27, 2018
cf649e0
Merge pull request #7 from teemupo/custom_metrics_namespaces
mlclmj Jun 27, 2018
646a34a
Return the orgId for newly created orgs
mlclmj Jun 27, 2018
a55e687
Add tests for orgs
mlclmj Jun 27, 2018
bc8a142
Add org_users tests
mlclmj Jun 28, 2018
266ab81
Fix JSON decoding
mlclmj Jun 28, 2018
4a01512
Add tests for user.go
mlclmj Jun 28, 2018
5857fb3
Better handling of create org response
mlclmj Jun 28, 2018
d0e8099
Add JSON definitions for struct fields
mlclmj Jun 28, 2018
f5ae1b6
Return id of newly created user
mlclmj Jun 28, 2018
2af2767
Add testing output and tests for admin.go
mlclmj Jun 28, 2018
1168099
Move fixtures into respective test files
mlclmj Jun 28, 2018
f63ea13
Remove unused imports
mlclmj Jun 28, 2018
f0238e7
Add JSON tags for field names
mlclmj Jun 28, 2018
3f9aece
Change variable name for consistency
mlclmj Jun 28, 2018
d437479
Rework request method
mlclmj Jul 6, 2018
2f8875a
Better request parameter handling
mlclmj Jul 9, 2018
a7599ef
Formatting fix
mlclmj Jul 9, 2018
9ac7c4a
Updated method for handling query parameters
mlclmj Jul 10, 2018
d1dd660
Remove dtos data struct
mlclmj Jul 10, 2018
0a62311
Go formatting
mlclmj Jul 12, 2018
d769eac
Merge pull request #8 from nytm/org-users
mlclmj Jul 13, 2018
0ee96e2
Update issue templates
tonglil Sep 5, 2018
9b58bab
Merge pull request #10 from nytm/tonglil-patch-1
tonglil Sep 6, 2018
7fd34a3
add swap files to vim ignore
Oct 9, 2018
aaaf590
add folder support
Oct 9, 2018
8063858
Implement skipping TLS certificate verification that is available thr…
Oct 10, 2018
8f0b4f8
expose uid and id on folder, add NewDashboard method to accept folderId
Oct 10, 2018
42c903f
use folder id api endpoint for finding folder by id
Oct 11, 2018
8ef34fa
copy back folder id from meta
Oct 11, 2018
cebdee9
include method in debug output
Oct 11, 2018
bc20f1a
Merge pull request #13 from maxramqvist/master
tonglil Oct 22, 2018
9cc848d
Merge pull request #12 from goraxe/master
tonglil Oct 22, 2018
984accf
Update alert notification and test has been added
quinont Sep 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
name: Bug report
about: Create a report to help us improve

---

**Describe the bug**
A clear and concise description of what the bug is.

**To reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

**Expected behavior**
A clear and concise description of what you expected to happen.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Additional context**
Add any other context about the problem here.
17 changes: 17 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: Feature request
about: Suggest an idea for this project

---

**Is your feature request related to a problem?**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

**Describe the solution you'd like**
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
Add any other context or screenshots about the feature request here.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ _testmain.go
*.exe
*.test
*.prof

# vim swap files
.*.sw?
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# grafana-api-golang-client

Grafana HTTP API Client for Go

## Tests

To run the tests:

```
go test
```
30 changes: 18 additions & 12 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,38 @@ import (
"errors"
"fmt"
"io/ioutil"

"github.com/grafana/grafana/pkg/api/dtos"
)

func (c *Client) CreateUserForm(settings dtos.AdminCreateUserForm) error {
data, err := json.Marshal(settings)
req, err := c.newRequest("POST", "/api/admin/users", bytes.NewBuffer(data))
func (c *Client) CreateUser(user User) (int64, error) {
id := int64(0)
data, err := json.Marshal(user)
req, err := c.newRequest("POST", "/api/admin/users", nil, bytes.NewBuffer(data))
if err != nil {
return err
return id, err
}
resp, err := c.Do(req)
if err != nil {
return err
return id, err
}
if resp.StatusCode != 200 {
return id, errors.New(resp.Status)
}
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
return id, err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
created := struct {
Id int64 `json:"id"`
}{}
err = json.Unmarshal(data, &created)
if err != nil {
return id, err
}
return err
return created.Id, err
}

func (c *Client) DeleteUser(id int64) error {
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/admin/users/%d", id), nil)
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/admin/users/%d", id), nil, nil)
if err != nil {
return err
}
Expand Down
39 changes: 39 additions & 0 deletions admin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gapi

import (
"testing"
)

const (
createUserJSON = `{"id":1,"message":"User created"}`
deleteUserJSON = `{"message":"User deleted"}`
)

func TestCreateUser(t *testing.T) {
server, client := gapiTestTools(200, createUserJSON)
defer server.Close()
user := User{
Email: "admin@localhost",
Login: "admin",
Name: "Administrator",
Password: "password",
}
resp, err := client.CreateUser(user)
if err != nil {
t.Error(err)
}

if resp != 1 {
t.Error("Not correctly parsing returned user message.")
}
}

func TestDeleteUser(t *testing.T) {
server, client := gapiTestTools(200, deleteUserJSON)
defer server.Close()

err := client.DeleteUser(int64(1))
if err != nil {
t.Error(err)
}
}
140 changes: 140 additions & 0 deletions alertnotification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package gapi

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
)

type AlertNotification struct {
Id int64 `json:"id,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
IsDefault bool `json:"isDefault"`
DisableResolveMessage bool `json:"disableResolveMessage"`
SendReminder bool `json:"sendReminder"`
Frequency string `json:"frequency"`
Settings interface{} `json:"settings"`
}

func (c *Client) AlertNotifications() ([]AlertNotification, error) {
alertnotifications := make([]AlertNotification, 0)

req, err := c.newRequest("GET", "/api/alert-notifications/", nil, nil)
if err != nil {
return nil, err
}

resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

err = json.Unmarshal(data, &alertnotifications)
return alertnotifications, err
}

func (c *Client) AlertNotification(id int64) (*AlertNotification, error) {
path := fmt.Sprintf("/api/alert-notifications/%d", id)
req, err := c.newRequest("GET", path, nil, nil)
if err != nil {
return nil, err
}

resp, err := c.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

result := &AlertNotification{}
err = json.Unmarshal(data, &result)
return result, err
}

func (c *Client) NewAlertNotification(a *AlertNotification) (int64, error) {
data, err := json.Marshal(a)
if err != nil {
return 0, err
}
req, err := c.newRequest("POST", "/api/alert-notifications", nil, bytes.NewBuffer(data))
if err != nil {
return 0, err
}

resp, err := c.Do(req)
if err != nil {
return 0, err
}
if resp.StatusCode != 200 {
return 0, errors.New(resp.Status)
}

data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return 0, err
}

result := struct {
Id int64 `json:"id"`
}{}
err = json.Unmarshal(data, &result)
return result.Id, err
}

func (c *Client) UpdateAlertNotification(a *AlertNotification) error {
path := fmt.Sprintf("/api/alert-notifications/%d", a.Id)
data, err := json.Marshal(a)
if err != nil {
return err
}
req, err := c.newRequest("PUT", path, nil, bytes.NewBuffer(data))
if err != nil {
return err
}

resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
}

return nil
}

func (c *Client) DeleteAlertNotification(id int64) error {
path := fmt.Sprintf("/api/alert-notifications/%d", id)
req, err := c.newRequest("DELETE", path, nil, nil)
if err != nil {
return err
}

resp, err := c.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(resp.Status)
}

return nil
}
Loading