This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 97
/
service_account.go
154 lines (127 loc) · 5.26 KB
/
service_account.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package gapi
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
// CreateServiceAccountTokenRequest represents the request body for creating a new service account token.
type CreateServiceAccountTokenRequest struct {
Name string `json:"name"`
ServiceAccountID int64 `json:"-"`
SecondsToLive int64 `json:"secondsToLive,omitempty"`
}
// CreateServiceAccountRequest is the request body for creating a new service account.
type CreateServiceAccountRequest struct {
Name string `json:"name"`
Role string `json:"role,omitempty"`
IsDisabled *bool `json:"isDisabled,omitempty"`
}
// UpdateServiceAccountRequest is the request body for modifying a service account.
type UpdateServiceAccountRequest struct {
Name string `json:"name,omitempty"`
Role string `json:"role,omitempty"`
IsDisabled *bool `json:"isDisabled,omitempty"`
}
// ServiceAccountDTO represents a Grafana service account.
type ServiceAccountDTO struct {
ID int64 `json:"id"`
Name string `json:"name"`
Login string `json:"login"`
OrgID int64 `json:"orgId"`
IsDisabled bool `json:"isDisabled"`
Role string `json:"role"`
Tokens int64 `json:"tokens"`
AvatarURL string `json:"avatarUrl"`
}
type RetrieveServiceAccountResponse struct {
TotalCount int64 `json:"totalCount"`
ServiceAccounts []ServiceAccountDTO `json:"serviceAccounts"`
Page int64 `json:"page"`
PerPage int64 `json:"perPage"`
}
// CreateServiceAccountTokenResponse represents the response
// from the Grafana API when creating a service account token.
type CreateServiceAccountTokenResponse struct {
ID int64 `json:"id"`
Name string `json:"name"`
Key string `json:"key"`
}
// GetServiceAccountTokensResponse represents a Grafana service account token.
type GetServiceAccountTokensResponse struct {
ID int64 `json:"id"`
Name string `json:"name"`
Created time.Time `json:"created,omitempty"`
Expiration *time.Time `json:"expiration,omitempty"`
SecondsUntilExpiration *float64 `json:"secondsUntilExpiration,omitempty"`
HasExpired bool `json:"hasExpired,omitempty"`
}
// DeleteServiceAccountResponse represents the response from deleting a service account
// or a service account token.
type DeleteServiceAccountResponse struct {
Message string `json:"message"`
}
// CreateServiceAccount creates a new Grafana service account.
func (c *Client) CreateServiceAccount(request CreateServiceAccountRequest) (*ServiceAccountDTO, error) {
response := ServiceAccountDTO{}
data, err := json.Marshal(request)
if err != nil {
return nil, err
}
err = c.request(http.MethodPost, "/api/serviceaccounts/", nil, data, &response)
return &response, err
}
// CreateServiceAccountToken creates a new Grafana service account token.
func (c *Client) CreateServiceAccountToken(request CreateServiceAccountTokenRequest) (*CreateServiceAccountTokenResponse, error) {
response := CreateServiceAccountTokenResponse{}
data, err := json.Marshal(request)
if err != nil {
return nil, err
}
err = c.request(http.MethodPost,
fmt.Sprintf("/api/serviceaccounts/%d/tokens", request.ServiceAccountID),
nil, data, &response)
return &response, err
}
// UpdateServiceAccount updates a specific serviceAccountID
func (c *Client) UpdateServiceAccount(serviceAccountID int64, request UpdateServiceAccountRequest) (*ServiceAccountDTO, error) {
response := ServiceAccountDTO{}
data, err := json.Marshal(request)
if err != nil {
return nil, err
}
err = c.request(http.MethodPatch,
fmt.Sprintf("/api/serviceaccounts/%d", serviceAccountID),
nil, data, &response)
return &response, err
}
// GetServiceAccounts retrieves a list of all service accounts for the organization.
func (c *Client) GetServiceAccounts() ([]ServiceAccountDTO, error) {
response := RetrieveServiceAccountResponse{}
if err := c.request(http.MethodGet, "/api/serviceaccounts/search", nil, nil, &response); err != nil {
return nil, err
}
return response.ServiceAccounts, nil
}
// GetServiceAccountTokens retrieves a list of all service account tokens for a specific service account.
func (c *Client) GetServiceAccountTokens(serviceAccountID int64) ([]GetServiceAccountTokensResponse, error) {
response := make([]GetServiceAccountTokensResponse, 0)
err := c.request(http.MethodGet,
fmt.Sprintf("/api/serviceaccounts/%d/tokens", serviceAccountID),
nil, nil, &response)
return response, err
}
// DeleteServiceAccount deletes the Grafana service account with the specified ID.
func (c *Client) DeleteServiceAccount(serviceAccountID int64) (*DeleteServiceAccountResponse, error) {
response := DeleteServiceAccountResponse{}
path := fmt.Sprintf("/api/serviceaccounts/%d", serviceAccountID)
err := c.request(http.MethodDelete, path, nil, nil, &response)
return &response, err
}
// DeleteServiceAccountToken deletes the Grafana service account token with the specified ID.
func (c *Client) DeleteServiceAccountToken(serviceAccountID, tokenID int64) (*DeleteServiceAccountResponse, error) {
response := DeleteServiceAccountResponse{}
path := fmt.Sprintf("/api/serviceaccounts/%d/tokens/%d", serviceAccountID, tokenID)
err := c.request(http.MethodDelete, path, nil, nil, &response)
return &response, err
}