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

Commit 544e218

Browse files
Add management of Cloud API Keys (#71)
Inspired from https://github.com/form3tech-oss/terraform-provider-grafanacloud
1 parent 44fb93c commit 544e218

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

cloud_api_key.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package gapi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
)
8+
9+
type CreateCloudAPIKeyInput struct {
10+
Name string `json:"name"`
11+
Role string `json:"role"`
12+
}
13+
14+
type ListCloudAPIKeysOutput struct {
15+
Items []*CloudAPIKey
16+
}
17+
18+
type CloudAPIKey struct {
19+
ID int
20+
Name string
21+
Role string
22+
Token string
23+
Expiration string
24+
}
25+
26+
func (c *Client) CreateCloudAPIKey(org string, input *CreateCloudAPIKeyInput) (*CloudAPIKey, error) {
27+
resp := CloudAPIKey{}
28+
data, err := json.Marshal(input)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
err = c.request("POST", fmt.Sprintf("/api/orgs/%s/api-keys", org), nil, bytes.NewBuffer(data), &resp)
34+
return &resp, err
35+
}
36+
37+
func (c *Client) ListCloudAPIKeys(org string) (*ListCloudAPIKeysOutput, error) {
38+
resp := &ListCloudAPIKeysOutput{}
39+
err := c.request("GET", fmt.Sprintf("/api/orgs/%s/api-keys", org), nil, nil, &resp)
40+
return resp, err
41+
}
42+
43+
func (c *Client) DeleteCloudAPIKey(org string, keyName string) error {
44+
return c.request("DELETE", fmt.Sprintf("/api/orgs/%s/api-keys/%s", org, keyName), nil, nil, nil)
45+
}

0 commit comments

Comments
 (0)