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