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

Commit 6ee7f30

Browse files
authored
ServiceAccounts: Fix retrieve using search (#92)
1 parent 50d2d63 commit 6ee7f30

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

service_account.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ type ServiceAccountDTO struct {
3939
AvatarURL string `json:"avatarUrl"`
4040
}
4141

42+
type RetrieveServiceAccountResponse struct {
43+
TotalCount int64 `json:"totalCount"`
44+
ServiceAccounts []ServiceAccountDTO `json:"serviceAccounts"`
45+
Page int64 `json:"page"`
46+
PerPage int64 `json:"perPage"`
47+
}
48+
4249
// CreateServiceAccountTokenResponse represents the response
4350
// from the Grafana API when creating a service account token.
4451
type CreateServiceAccountTokenResponse struct {
@@ -108,10 +115,13 @@ func (c *Client) UpdateServiceAccount(serviceAccountID int64, request UpdateServ
108115

109116
// GetServiceAccounts retrieves a list of all service accounts for the organization.
110117
func (c *Client) GetServiceAccounts() ([]ServiceAccountDTO, error) {
111-
response := make([]ServiceAccountDTO, 0)
118+
response := RetrieveServiceAccountResponse{}
112119

113-
err := c.request(http.MethodGet, "/api/serviceaccounts/search", nil, nil, &response)
114-
return response, err
120+
if err := c.request(http.MethodGet, "/api/serviceaccounts/search", nil, nil, &response); err != nil {
121+
return nil, err
122+
}
123+
124+
return response.ServiceAccounts, nil
115125
}
116126

117127
// GetServiceAccountTokens retrieves a list of all service account tokens for a specific service account.

service_account_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ const (
1818
"tokens": 0,
1919
"avatarUrl": ""
2020
}`
21-
searchServiceAccountsJSON = `[
21+
searchServiceAccountsJSON = `{
22+
"totalCount": 2,
23+
"serviceAccounts": [
2224
{
2325
"id": 8,
2426
"name": "newSA",
@@ -39,7 +41,10 @@ const (
3941
"tokens": 2,
4042
"avatarUrl": "/avatar/0e29f33c929824a5163d953582e83abe"
4143
}
42-
]`
44+
],
45+
"page": 1,
46+
"perPage": 1000
47+
}`
4348
createServiceAccountTokenJSON = `{"name":"key-name", "key":"mock-api-key"}` //#nosec
4449
deleteServiceAccountTokenJSON = `{"message":"Service account token deleted"}` //#nosec
4550
deleteServiceAccountJSON = `{"message":"service account deleted"}`

0 commit comments

Comments
 (0)