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

Commit 4e60762

Browse files
Merge branch 'master' into gw0/add-http-headers
2 parents e3586dd + 7fcf1d4 commit 4e60762

30 files changed

+1506
-75
lines changed

.drone.jsonnet renamed to .drone/drone.jsonnet

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ local pipeline(name, trigger) = {
1414
name: 'test',
1515
image: image,
1616
commands: [
17-
'go version',
18-
'golangci-lint --version',
19-
'golangci-lint run ./...',
20-
'go test -cover -race -vet all -mod readonly ./...',
17+
'make test',
2118
],
2219
},
2320
],
@@ -29,6 +26,7 @@ local pipeline(name, trigger) = {
2926
'pull_request',
3027
],
3128
}),
29+
3230
pipeline('test-master', {
3331
branch: 'master',
3432
event: [

.drone/drone.yml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
kind: pipeline
3+
type: docker
4+
name: test-pr
5+
6+
platform:
7+
os: linux
8+
arch: amd64
9+
10+
steps:
11+
- name: test
12+
image: grafana/build-container:1.2.27
13+
commands:
14+
- make test
15+
16+
trigger:
17+
event:
18+
- pull_request
19+
20+
---
21+
kind: pipeline
22+
type: docker
23+
name: test-master
24+
25+
platform:
26+
os: linux
27+
arch: amd64
28+
29+
steps:
30+
- name: test
31+
image: grafana/build-container:1.2.27
32+
commands:
33+
- make test
34+
35+
trigger:
36+
branch:
37+
- master
38+
event:
39+
- push
40+
41+
---
42+
kind: signature
43+
hmac: 4b9e92aa0df11523a1df89ec418113152fa135a012967f3c9e11a9fbc27a7dea
44+
45+
...

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.PHONY: drone, test
2+
3+
drone:
4+
drone jsonnet --stream --format --source .drone/drone.jsonnet --target .drone/drone.yml
5+
drone lint .drone/drone.yml
6+
drone sign --save grafana/grafana-api-golang-client .drone/drone.yml
7+
8+
test:
9+
go version
10+
golangci-lint --version
11+
golangci-lint run ./...
12+
go test -cover -race -vet all -mod readonly ./...
13+

README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
# DEPRECATED
1+
# Grafana HTTP API Client for Go
22

3-
:warning: :warning: :warning:
3+
This library provides a low-level client to access Grafana [HTTP API](https://grafana.com/docs/grafana/latest/http_api/).
44

5-
**This repository is no longer being maintained.**
6-
7-
We're in the process of creating our next generation API clients.
8-
9-
In the interim, further changes to this repository should only be to support
10-
[grafana/terraform-provider-grafana](https://github.com/grafana/terraform-provider-grafana).
11-
12-
---
13-
14-
Grafana HTTP API Client for Go
5+
:warning: This repository is still active but not under heavy development.
6+
Contributions to this library offering support for the [Terraform provider for Grafana](https://github.com/grafana/terraform-provider-grafana) will be prioritized over generic ones.
157

168
## Tests
179

alertnotification.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ type AlertNotification struct {
1717
SendReminder bool `json:"sendReminder"`
1818
Frequency string `json:"frequency"`
1919
Settings interface{} `json:"settings"`
20+
SecureFields interface{} `json:"secureFields,omitempty"`
21+
SecureSettings interface{} `json:"secureSettings,omitempty"`
2022
}
2123

2224
// AlertNotifications fetches and returns Grafana alert notifications.

annotation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type GraphiteAnnotation struct {
3838
// Annotations fetches the annotations queried with the params it's passed
3939
func (c *Client) Annotations(params url.Values) ([]Annotation, error) {
4040
result := []Annotation{}
41-
err := c.request("GET", "/api/annotation", params, nil, &result)
41+
err := c.request("GET", "/api/annotations", params, nil, &result)
4242
if err != nil {
4343
return nil, err
4444
}

api_key.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package gapi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"net/url"
8+
"strconv"
9+
"time"
10+
)
11+
12+
type CreateAPIKeyRequest struct {
13+
Name string `json:"name"`
14+
Role string `json:"role"`
15+
SecondsToLive int64 `json:"secondsToLive,omitempty"`
16+
}
17+
18+
type CreateAPIKeyResponse struct {
19+
// ID field only returned after Grafana v7.
20+
ID int64 `json:"id,omitempty"`
21+
Name string `json:"name"`
22+
Key string `json:"key"`
23+
}
24+
25+
type GetAPIKeysResponse struct {
26+
ID int64 `json:"id"`
27+
Name string `json:"name"`
28+
Role string `json:"role"`
29+
Expiration time.Time `json:"expiration,omitempty"`
30+
}
31+
32+
type DeleteAPIKeyResponse struct {
33+
Message string `json:"message"`
34+
}
35+
36+
// CreateAPIKey creates a new Grafana API key.
37+
func (c *Client) CreateAPIKey(request CreateAPIKeyRequest) (CreateAPIKeyResponse, error) {
38+
response := CreateAPIKeyResponse{}
39+
40+
data, err := json.Marshal(request)
41+
if err != nil {
42+
return response, err
43+
}
44+
45+
err = c.request("POST", "/api/auth/keys", nil, bytes.NewBuffer(data), &response)
46+
return response, err
47+
}
48+
49+
// GetAPIKeys retrieves a list of all API keys.
50+
func (c *Client) GetAPIKeys(includeExpired bool) ([]*GetAPIKeysResponse, error) {
51+
response := make([]*GetAPIKeysResponse, 0)
52+
53+
query := url.Values{}
54+
query.Add("includeExpired", strconv.FormatBool(includeExpired))
55+
56+
err := c.request("GET", "/api/auth/keys", query, nil, &response)
57+
return response, err
58+
}
59+
60+
// DeleteAPIKey deletes the Grafana API key with the specified ID.
61+
func (c *Client) DeleteAPIKey(id int64) (DeleteAPIKeyResponse, error) {
62+
response := DeleteAPIKeyResponse{}
63+
64+
path := fmt.Sprintf("/api/auth/keys/%d", id)
65+
err := c.request("DELETE", path, nil, nil, &response)
66+
return response, err
67+
}

api_key_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package gapi
2+
3+
import (
4+
"testing"
5+
6+
"github.com/gobs/pretty"
7+
)
8+
9+
const (
10+
createAPIKeyJSON = `{"name":"key-name", "key":"mock-api-key"}`
11+
deleteAPIKeyJSON = `{"message":"API key deleted"}`
12+
13+
getAPIKeysJSON = `[
14+
{
15+
"id": 1,
16+
"name": "key-name-2",
17+
"role": "Viewer"
18+
},
19+
{
20+
"id": 2,
21+
"name": "key-name-2",
22+
"role": "Admin",
23+
"expiration": "2021-10-30T10:52:03+03:00"
24+
}
25+
]`
26+
)
27+
28+
func TestCreateAPIKey(t *testing.T) {
29+
server, client := gapiTestTools(t, 200, createAPIKeyJSON)
30+
defer server.Close()
31+
32+
req := CreateAPIKeyRequest{
33+
Name: "key-name",
34+
Role: "Viewer",
35+
SecondsToLive: 0,
36+
}
37+
38+
res, err := client.CreateAPIKey(req)
39+
if err != nil {
40+
t.Error(err)
41+
}
42+
43+
t.Log(pretty.PrettyFormat(res))
44+
}
45+
46+
func TestDeleteAPIKey(t *testing.T) {
47+
server, client := gapiTestTools(t, 200, deleteAPIKeyJSON)
48+
defer server.Close()
49+
50+
res, err := client.DeleteAPIKey(int64(1))
51+
if err != nil {
52+
t.Error(err)
53+
}
54+
55+
t.Log(pretty.PrettyFormat(res))
56+
}
57+
58+
func TestGetAPIKeys(t *testing.T) {
59+
server, client := gapiTestTools(t, 200, getAPIKeysJSON)
60+
defer server.Close()
61+
62+
res, err := client.GetAPIKeys(true)
63+
if err != nil {
64+
t.Error(err)
65+
}
66+
67+
t.Log(pretty.PrettyFormat(res))
68+
}

builtin_role_assignments.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package gapi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
)
8+
9+
const baseURL = "/api/access-control/builtin-roles"
10+
11+
type BuiltInRoleAssignment struct {
12+
BuiltinRole string `json:"builtInRole"`
13+
RoleUID string `json:"roleUid"`
14+
Global bool `json:"global"`
15+
}
16+
17+
// GetBuiltInRoleAssignments gets all built-in role assignments. Available only in Grafana Enterprise 8.+.
18+
func (c *Client) GetBuiltInRoleAssignments() (map[string][]*Role, error) {
19+
br := make(map[string][]*Role)
20+
err := c.request("GET", baseURL, nil, nil, &br)
21+
if err != nil {
22+
return nil, err
23+
}
24+
return br, nil
25+
}
26+
27+
// NewBuiltInRoleAssignment creates a new built-in role assignment. Available only in Grafana Enterprise 8.+.
28+
func (c *Client) NewBuiltInRoleAssignment(builtInRoleAssignment BuiltInRoleAssignment) (*BuiltInRoleAssignment, error) {
29+
body, err := json.Marshal(builtInRoleAssignment)
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
br := &BuiltInRoleAssignment{}
35+
36+
err = c.request("POST", baseURL, nil, bytes.NewBuffer(body), &br)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
return br, err
42+
}
43+
44+
// DeleteBuiltInRoleAssignment remove the built-in role assignments. Available only in Grafana Enterprise 8.+.
45+
func (c *Client) DeleteBuiltInRoleAssignment(builtInRole BuiltInRoleAssignment) error {
46+
data, err := json.Marshal(builtInRole)
47+
if err != nil {
48+
return err
49+
}
50+
51+
qp := map[string][]string{
52+
"global": {fmt.Sprint(builtInRole.Global)},
53+
}
54+
url := fmt.Sprintf("%s/%s/roles/%s", baseURL, builtInRole.BuiltinRole, builtInRole.RoleUID)
55+
err = c.request("DELETE", url, qp, bytes.NewBuffer(data), nil)
56+
57+
return err
58+
}

0 commit comments

Comments
 (0)