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

Commit b55ac47

Browse files
committed
Merge remote-tracking branch 'upstream/master' into annotations
2 parents 507c8eb + c4e06a3 commit b55ac47

30 files changed

+2337
-102
lines changed

.drone.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
kind: pipeline
3+
type: docker
4+
name: default
5+
6+
steps:
7+
- name: test
8+
image: golang
9+
commands:
10+
- go mod download
11+
- go test -cover -race -vet all -mod readonly ./...
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
5+
---
6+
7+
**Describe the bug**
8+
A clear and concise description of what the bug is.
9+
10+
**To reproduce**
11+
Steps to reproduce the behavior:
12+
1. Go to '...'
13+
2. Click on '....'
14+
3. Scroll down to '....'
15+
4. See error
16+
17+
**Expected behavior**
18+
A clear and concise description of what you expected to happen.
19+
20+
**Screenshots**
21+
If applicable, add screenshots to help explain your problem.
22+
23+
**Additional context**
24+
Add any other context about the problem here.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
5+
---
6+
7+
**Is your feature request related to a problem?**
8+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
9+
10+
**Describe the solution you'd like**
11+
A clear and concise description of what you want to happen.
12+
13+
**Describe alternatives you've considered**
14+
A clear and concise description of any alternative solutions or features you've considered.
15+
16+
**Additional context**
17+
Add any other context or screenshots about the feature request here.

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ _testmain.go
2222
*.exe
2323
*.test
2424
*.prof
25+
26+
# vim swap files
27+
.*.sw?
28+
.idea

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# grafana-api-golang-client
22

3+
[![Build Status](https://cloud.drone.io/api/badges/nytm/go-grafana-api/status.svg)](https://cloud.drone.io/nytm/go-grafana-api)
4+
35
Grafana HTTP API Client for Go
46

57
## Tests

admin.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,42 @@ import (
66
"errors"
77
"fmt"
88
"io/ioutil"
9-
10-
"github.com/grafana/grafana/pkg/api/dtos"
119
)
1210

13-
func (c *Client) CreateUserForm(settings dtos.AdminCreateUserForm) error {
14-
data, err := json.Marshal(settings)
15-
req, err := c.newRequest("POST", "/api/admin/users", bytes.NewBuffer(data))
11+
func (c *Client) CreateUser(user User) (int64, error) {
12+
id := int64(0)
13+
data, err := json.Marshal(user)
1614
if err != nil {
17-
return err
15+
return id, err
16+
}
17+
18+
req, err := c.newRequest("POST", "/api/admin/users", nil, bytes.NewBuffer(data))
19+
if err != nil {
20+
return id, err
1821
}
1922
resp, err := c.Do(req)
2023
if err != nil {
21-
return err
24+
return id, err
25+
}
26+
if resp.StatusCode != 200 {
27+
return id, errors.New(resp.Status)
2228
}
2329
data, err = ioutil.ReadAll(resp.Body)
2430
if err != nil {
25-
return err
31+
return id, err
2632
}
27-
if resp.StatusCode != 200 {
28-
return errors.New(resp.Status)
33+
created := struct {
34+
Id int64 `json:"id"`
35+
}{}
36+
err = json.Unmarshal(data, &created)
37+
if err != nil {
38+
return id, err
2939
}
30-
return err
40+
return created.Id, err
3141
}
3242

3343
func (c *Client) DeleteUser(id int64) error {
34-
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/admin/users/%d", id), nil)
44+
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/admin/users/%d", id), nil, nil)
3545
if err != nil {
3646
return err
3747
}

admin_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package gapi
2+
3+
import (
4+
"testing"
5+
)
6+
7+
const (
8+
createUserJSON = `{"id":1,"message":"User created"}`
9+
deleteUserJSON = `{"message":"User deleted"}`
10+
)
11+
12+
func TestCreateUser(t *testing.T) {
13+
server, client := gapiTestTools(200, createUserJSON)
14+
defer server.Close()
15+
user := User{
16+
Email: "admin@localhost",
17+
Login: "admin",
18+
Name: "Administrator",
19+
Password: "password",
20+
}
21+
resp, err := client.CreateUser(user)
22+
if err != nil {
23+
t.Error(err)
24+
}
25+
26+
if resp != 1 {
27+
t.Error("Not correctly parsing returned user message.")
28+
}
29+
}
30+
31+
func TestDeleteUser(t *testing.T) {
32+
server, client := gapiTestTools(200, deleteUserJSON)
33+
defer server.Close()
34+
35+
err := client.DeleteUser(int64(1))
36+
if err != nil {
37+
t.Error(err)
38+
}
39+
}

alertnotification.go

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,45 @@ import (
99
)
1010

1111
type AlertNotification struct {
12-
Id int64 `json:"id,omitempty"`
13-
Name string `json:"name"`
14-
Type string `json:"type"`
15-
IsDefault bool `json:"isDefault"`
16-
Settings interface{} `json:"settings"`
12+
Id int64 `json:"id,omitempty"`
13+
Uid string `json:"uid"`
14+
Name string `json:"name"`
15+
Type string `json:"type"`
16+
IsDefault bool `json:"isDefault"`
17+
DisableResolveMessage bool `json:"disableResolveMessage"`
18+
SendReminder bool `json:"sendReminder"`
19+
Frequency string `json:"frequency"`
20+
Settings interface{} `json:"settings"`
21+
}
22+
23+
func (c *Client) AlertNotifications() ([]AlertNotification, error) {
24+
alertnotifications := make([]AlertNotification, 0)
25+
26+
req, err := c.newRequest("GET", "/api/alert-notifications/", nil, nil)
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
resp, err := c.Do(req)
32+
if err != nil {
33+
return nil, err
34+
}
35+
if resp.StatusCode != 200 {
36+
return nil, errors.New(resp.Status)
37+
}
38+
39+
data, err := ioutil.ReadAll(resp.Body)
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
err = json.Unmarshal(data, &alertnotifications)
45+
return alertnotifications, err
1746
}
1847

1948
func (c *Client) AlertNotification(id int64) (*AlertNotification, error) {
2049
path := fmt.Sprintf("/api/alert-notifications/%d", id)
21-
req, err := c.newRequest("GET", path, nil)
50+
req, err := c.newRequest("GET", path, nil, nil)
2251
if err != nil {
2352
return nil, err
2453
}
@@ -46,7 +75,7 @@ func (c *Client) NewAlertNotification(a *AlertNotification) (int64, error) {
4675
if err != nil {
4776
return 0, err
4877
}
49-
req, err := c.newRequest("POST", "/api/alert-notifications", bytes.NewBuffer(data))
78+
req, err := c.newRequest("POST", "/api/alert-notifications", nil, bytes.NewBuffer(data))
5079
if err != nil {
5180
return 0, err
5281
}
@@ -77,7 +106,7 @@ func (c *Client) UpdateAlertNotification(a *AlertNotification) error {
77106
if err != nil {
78107
return err
79108
}
80-
req, err := c.newRequest("PUT", path, bytes.NewBuffer(data))
109+
req, err := c.newRequest("PUT", path, nil, bytes.NewBuffer(data))
81110
if err != nil {
82111
return err
83112
}
@@ -95,7 +124,7 @@ func (c *Client) UpdateAlertNotification(a *AlertNotification) error {
95124

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

0 commit comments

Comments
 (0)