-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprometheus_metrics_token.go
143 lines (120 loc) · 4.22 KB
/
prometheus_metrics_token.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
package dbaas
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
// PrometheusMetricTokenCreateOpts represents options for the prometheus metrics token Create request.
type PrometheusMetricTokenCreateOpts struct {
Name string `json:"name"`
}
// PrometheusMetricTokenUpdateOpts represents options for the prometheus metrics token Update request.
type PrometheusMetricTokenUpdateOpts struct {
Name string `json:"name"`
}
// PrometheusMetricToken is the API response for the prometheus metrics tokens.
type PrometheusMetricToken struct {
ID string `json:"id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ProjectID string `json:"project_id"`
Name string `json:"name"`
Value string `json:"value"`
}
const PrometheusMetricsTokensURI = "/prometheus-metrics-tokens"
// PrometheusMetricToken returns a token based on the ID.
func (api *API) PrometheusMetricToken(
ctx context.Context,
prometheusMetricTokenID string,
) (PrometheusMetricToken, error) {
uri := fmt.Sprintf("%s/%s", PrometheusMetricsTokensURI, prometheusMetricTokenID)
resp, err := api.makeRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return PrometheusMetricToken{}, err
}
var result PrometheusMetricToken
err = json.Unmarshal(resp, &result)
if err != nil {
return PrometheusMetricToken{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result, nil
}
// PrometheusMetricTokens returns all tokens.
func (api *API) PrometheusMetricTokens(ctx context.Context) ([]PrometheusMetricToken, error) {
resp, err := api.makeRequest(ctx, http.MethodGet, PrometheusMetricsTokensURI, nil)
if err != nil {
return []PrometheusMetricToken{}, err
}
var result struct {
PrometheusMetricTokens []PrometheusMetricToken `json:"prometheus-metrics-tokens"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return []PrometheusMetricToken{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result.PrometheusMetricTokens, nil
}
// CreatePrometheusMetricToken creates a new token.
func (api *API) CreatePrometheusMetricToken(
ctx context.Context,
opts PrometheusMetricTokenCreateOpts,
) (PrometheusMetricToken, error) {
createPrometheusMetricTokensOpts := struct {
PrometheusMetricToken PrometheusMetricTokenCreateOpts `json:"prometheus-metrics-token"`
}{
PrometheusMetricToken: opts,
}
requestBody, err := json.Marshal(createPrometheusMetricTokensOpts)
if err != nil {
return PrometheusMetricToken{}, fmt.Errorf("Error marshalling params to JSON, %w", err)
}
resp, err := api.makeRequest(ctx, http.MethodPost, PrometheusMetricsTokensURI, requestBody)
if err != nil {
return PrometheusMetricToken{}, err
}
var result struct {
PrometheusMetricToken PrometheusMetricToken `json:"prometheus-metrics-token"`
}
err = json.Unmarshal(resp, &result)
if err != nil {
return PrometheusMetricToken{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result.PrometheusMetricToken, nil
}
// DeletePrometheusMetricToken deletes an existing token.
func (api *API) DeletePrometheusMetricToken(ctx context.Context, prometheusMetricTokenID string) error {
uri := fmt.Sprintf("%s/%s", PrometheusMetricsTokensURI, prometheusMetricTokenID)
_, err := api.makeRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return nil
}
// UpdatePrometheusMetricToken updates an existing token.
func (api *API) UpdatePrometheusMetricToken(
ctx context.Context,
prometheusMetricTokenID string,
opts PrometheusMetricTokenUpdateOpts,
) (PrometheusMetricToken, error) {
uri := fmt.Sprintf("%s/%s", PrometheusMetricsTokensURI, prometheusMetricTokenID)
updatePrometheusMetricTokensOpts := struct {
PrometheusMetricToken PrometheusMetricTokenUpdateOpts `json:"prometheus-metrics-token"`
}{
PrometheusMetricToken: opts,
}
requestBody, err := json.Marshal(updatePrometheusMetricTokensOpts)
if err != nil {
return PrometheusMetricToken{}, fmt.Errorf("Error marshalling params to JSON, %w", err)
}
resp, err := api.makeRequest(ctx, http.MethodPut, uri, requestBody)
if err != nil {
return PrometheusMetricToken{}, err
}
var result PrometheusMetricToken
err = json.Unmarshal(resp, &result)
if err != nil {
return PrometheusMetricToken{}, fmt.Errorf("Error during Unmarshal, %w", err)
}
return result, nil
}