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

Commit e56f7ae

Browse files
Merge pull request #39 from baloo/baloo/api-keys
Add support for calling API Key endpoints. Take 2
2 parents bcbbe36 + 2066976 commit e56f7ae

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

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+
}

0 commit comments

Comments
 (0)