|
| 1 | +package gapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// CreateServiceAccountTokenRequest represents the request body for creating a new service account token. |
| 12 | +type CreateServiceAccountTokenRequest struct { |
| 13 | + Name string `json:"name"` |
| 14 | + ServiceAccountID int64 `json:"-"` |
| 15 | + SecondsToLive int64 `json:"secondsToLive,omitempty"` |
| 16 | +} |
| 17 | + |
| 18 | +// CreateServiceAccountRequest is the request body for creating a new service account. |
| 19 | +type CreateServiceAccountRequest struct { |
| 20 | + Name string `json:"name"` |
| 21 | +} |
| 22 | + |
| 23 | +// UpdateServiceAccountRequest is the request body for modifying a service account. |
| 24 | +type UpdateServiceAccountRequest struct { |
| 25 | + Name string `json:"name,omitempty"` |
| 26 | + Role string `json:"role,omitempty"` |
| 27 | + IsDisabled *bool `json:"isDisabled,omitempty"` |
| 28 | +} |
| 29 | + |
| 30 | +// ServiceAccountDTO represents a Grafana service account. |
| 31 | +type ServiceAccountDTO struct { |
| 32 | + ID int64 `json:"id"` |
| 33 | + Name string `json:"name"` |
| 34 | + Login string `json:"login"` |
| 35 | + OrgID int64 `json:"orgId"` |
| 36 | + IsDisabled bool `json:"isDisabled"` |
| 37 | + Role string `json:"role"` |
| 38 | + Tokens int64 `json:"tokens"` |
| 39 | + AvatarURL string `json:"avatarUrl"` |
| 40 | +} |
| 41 | + |
| 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 | + |
| 49 | +// CreateServiceAccountTokenResponse represents the response |
| 50 | +// from the Grafana API when creating a service account token. |
| 51 | +type CreateServiceAccountTokenResponse struct { |
| 52 | + ID int64 `json:"id"` |
| 53 | + Name string `json:"name"` |
| 54 | + Key string `json:"key"` |
| 55 | +} |
| 56 | + |
| 57 | +// GetServiceAccountTokensResponse represents a Grafana service account token. |
| 58 | +type GetServiceAccountTokensResponse struct { |
| 59 | + ID int64 `json:"id"` |
| 60 | + Name string `json:"name"` |
| 61 | + Created time.Time `json:"created,omitempty"` |
| 62 | + Expiration *time.Time `json:"expiration,omitempty"` |
| 63 | + SecondsUntilExpiration *float64 `json:"secondsUntilExpiration,omitempty"` |
| 64 | + HasExpired bool `json:"hasExpired,omitempty"` |
| 65 | +} |
| 66 | + |
| 67 | +// DeleteServiceAccountResponse represents the response from deleting a service account |
| 68 | +// or a service account token. |
| 69 | +type DeleteServiceAccountResponse struct { |
| 70 | + Message string `json:"message"` |
| 71 | +} |
| 72 | + |
| 73 | +// CreateServiceAccount creates a new Grafana service account. |
| 74 | +func (c *Client) CreateServiceAccount(request CreateServiceAccountRequest) (*ServiceAccountDTO, error) { |
| 75 | + response := ServiceAccountDTO{} |
| 76 | + |
| 77 | + data, err := json.Marshal(request) |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + |
| 82 | + err = c.request(http.MethodPost, "/api/serviceaccounts/", nil, bytes.NewBuffer(data), &response) |
| 83 | + return &response, err |
| 84 | +} |
| 85 | + |
| 86 | +// CreateServiceAccountToken creates a new Grafana service account token. |
| 87 | +func (c *Client) CreateServiceAccountToken(request CreateServiceAccountTokenRequest) (*CreateServiceAccountTokenResponse, error) { |
| 88 | + response := CreateServiceAccountTokenResponse{} |
| 89 | + |
| 90 | + data, err := json.Marshal(request) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + err = c.request(http.MethodPost, |
| 96 | + fmt.Sprintf("/api/serviceaccounts/%d/tokens", request.ServiceAccountID), |
| 97 | + nil, bytes.NewBuffer(data), &response) |
| 98 | + return &response, err |
| 99 | +} |
| 100 | + |
| 101 | +// UpdateServiceAccount updates a specific serviceAccountID |
| 102 | +func (c *Client) UpdateServiceAccount(serviceAccountID int64, request UpdateServiceAccountRequest) (*ServiceAccountDTO, error) { |
| 103 | + response := ServiceAccountDTO{} |
| 104 | + |
| 105 | + data, err := json.Marshal(request) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + err = c.request(http.MethodPatch, |
| 111 | + fmt.Sprintf("/api/serviceaccounts/%d", serviceAccountID), |
| 112 | + nil, bytes.NewBuffer(data), &response) |
| 113 | + return &response, err |
| 114 | +} |
| 115 | + |
| 116 | +// GetServiceAccounts retrieves a list of all service accounts for the organization. |
| 117 | +func (c *Client) GetServiceAccounts() ([]ServiceAccountDTO, error) { |
| 118 | + response := RetrieveServiceAccountResponse{} |
| 119 | + |
| 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 |
| 125 | +} |
| 126 | + |
| 127 | +// GetServiceAccountTokens retrieves a list of all service account tokens for a specific service account. |
| 128 | +func (c *Client) GetServiceAccountTokens(serviceAccountID int64) ([]GetServiceAccountTokensResponse, error) { |
| 129 | + response := make([]GetServiceAccountTokensResponse, 0) |
| 130 | + |
| 131 | + err := c.request(http.MethodGet, |
| 132 | + fmt.Sprintf("/api/serviceaccounts/%d/tokens", serviceAccountID), |
| 133 | + nil, nil, &response) |
| 134 | + return response, err |
| 135 | +} |
| 136 | + |
| 137 | +// DeleteServiceAccount deletes the Grafana service account with the specified ID. |
| 138 | +func (c *Client) DeleteServiceAccount(serviceAccountID int64) (*DeleteServiceAccountResponse, error) { |
| 139 | + response := DeleteServiceAccountResponse{} |
| 140 | + |
| 141 | + path := fmt.Sprintf("/api/serviceaccounts/%d", serviceAccountID) |
| 142 | + err := c.request(http.MethodDelete, path, nil, nil, &response) |
| 143 | + return &response, err |
| 144 | +} |
| 145 | + |
| 146 | +// DeleteServiceAccountToken deletes the Grafana service account token with the specified ID. |
| 147 | +func (c *Client) DeleteServiceAccountToken(serviceAccountID, tokenID int64) (*DeleteServiceAccountResponse, error) { |
| 148 | + response := DeleteServiceAccountResponse{} |
| 149 | + |
| 150 | + path := fmt.Sprintf("/api/serviceaccounts/%d/tokens/%d", serviceAccountID, tokenID) |
| 151 | + err := c.request(http.MethodDelete, path, nil, nil, &response) |
| 152 | + return &response, err |
| 153 | +} |
0 commit comments