-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetric_data_source.go
311 lines (263 loc) · 9.88 KB
/
metric_data_source.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package ilert
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
)
// MetricDataSource definition https://api.ilert.com/api-docs/#tag/Metric-Data-Sources
type MetricDataSource struct {
ID int64 `json:"id,omitempty"`
Name string `json:"name"`
Type string `json:"type"`
Teams []TeamShort `json:"teams,omitempty"`
Metadata *MetricDataSourceMetadata `json:"metadata"`
}
// MetricDataSourceMetadata defines provider metadata for the metric data source
type MetricDataSourceMetadata struct {
Region string `json:"region,omitempty"` // used for Datadog
ApiKey string `json:"apiKey,omitempty"` // used for Datadog
ApplicationKey string `json:"applicationKey,omitempty"` // used for Datadog
AuthType string `json:"authType,omitempty"` // used for Prometheus
BasicUser string `json:"basicUser,omitempty"` // used for Prometheus
BasicPass string `json:"basicPass,omitempty"` // used for Prometheus
HeaderKey string `json:"headerKey,omitempty"` // used for Prometheus
HeaderValue string `json:"headerValue,omitempty"` // used for Prometheus
Url string `json:"url,omitempty"` // used for Prometheus
}
// MetricDataSourceType defines provider type of the metric data source
var MetricDataSourceType = struct {
Datadog string
Prometheus string
}{
Datadog: "DATADOG",
Prometheus: "PROMETHEUS",
}
// MetricDataSourceType defines provider type list
var MetricDataSourceTypeAll = []string{
MetricDataSourceType.Datadog,
MetricDataSourceType.Prometheus,
}
// MetricDataSourceAuthType defines provider authentication type of the metric data source
var MetricDataSourceAuthType = struct {
None string
Basic string
Header string
}{
None: "NONE",
Basic: "BASIC",
Header: "HEADER",
}
// MetricDataSourceAuthType defines provider authentication type list
var MetricDataSourceAuthTypeAll = []string{
MetricDataSourceAuthType.None,
MetricDataSourceAuthType.Basic,
MetricDataSourceAuthType.Header,
}
// CreateMetricDataSourceInput represents the input of a CreateMetricDataSource operation.
type CreateMetricDataSourceInput struct {
_ struct{}
MetricDataSource *MetricDataSource
}
// CreateMetricDataSourceOutput represents the output of a CreateMetricDataSource operation.
type CreateMetricDataSourceOutput struct {
_ struct{}
MetricDataSource *MetricDataSource
}
// CreateMetricDataSource creates a new metric data source. https://api.ilert.com/api-docs/#tag/Metric-Data-Sources/paths/~1metric-data-sources/post
func (c *Client) CreateMetricDataSource(input *CreateMetricDataSourceInput) (*CreateMetricDataSourceOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.MetricDataSource == nil {
return nil, errors.New("metric data source input is required")
}
resp, err := c.httpClient.R().SetBody(input.MetricDataSource).Post(apiRoutes.metricDataSources)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 201); apiErr != nil {
return nil, apiErr
}
metricDataSource := &MetricDataSource{}
err = json.Unmarshal(resp.Body(), metricDataSource)
if err != nil {
return nil, err
}
return &CreateMetricDataSourceOutput{MetricDataSource: metricDataSource}, nil
}
// GetMetricDataSourcesInput represents the input of a GetMetricDataSources operation.
type GetMetricDataSourcesInput struct {
_ struct{}
// an integer specifying the starting point (beginning with 0) when paging through a list of entities
// Default: 0
StartIndex *int
// the maximum number of results when paging through a list of entities.
// Default: 10, Maximum: 100
MaxResults *int
}
// GetMetricDataSourcesOutput represents the output of a GetMetricDataSources operation.
type GetMetricDataSourcesOutput struct {
_ struct{}
MetricDataSources []*MetricDataSource
}
// GetMetricDataSources lists existing metric data sources. https://api.ilert.com/api-docs/#tag/Metric-Data-Sources/paths/~1metric-data-sources/get
func (c *Client) GetMetricDataSources(input *GetMetricDataSourcesInput) (*GetMetricDataSourcesOutput, error) {
if input == nil {
input = &GetMetricDataSourcesInput{}
}
q := url.Values{}
if input.StartIndex != nil {
q.Add("start-index", strconv.Itoa(*input.StartIndex))
} else {
q.Add("start-index", "0")
}
if input.MaxResults != nil {
q.Add("max-results", strconv.Itoa(*input.MaxResults))
} else {
q.Add("max-results", "10")
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s?%s", apiRoutes.metricDataSources, q.Encode()))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
metricDataSource := make([]*MetricDataSource, 0)
err = json.Unmarshal(resp.Body(), &metricDataSource)
if err != nil {
return nil, err
}
return &GetMetricDataSourcesOutput{MetricDataSources: metricDataSource}, nil
}
// GetMetricDataSourceInput represents the input of a GetMetricDataSource operation.
type GetMetricDataSourceInput struct {
_ struct{}
MetricDataSourceID *int64
}
// GetMetricDataSourceOutput represents the output of a GetMetricDataSource operation.
type GetMetricDataSourceOutput struct {
_ struct{}
MetricDataSource *MetricDataSource
}
// GetMetricDataSource gets a metric data source by ID. https://api.ilert.com/api-docs/#tag/Metric-Data-Sources/paths/~1metric-data-sources~1{id}/get
func (c *Client) GetMetricDataSource(input *GetMetricDataSourceInput) (*GetMetricDataSourceOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.MetricDataSourceID == nil {
return nil, errors.New("metric data source id is required")
}
var url = fmt.Sprintf("%s/%d", apiRoutes.metricDataSources, *input.MetricDataSourceID)
resp, err := c.httpClient.R().Get(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
metricDataSource := &MetricDataSource{}
err = json.Unmarshal(resp.Body(), metricDataSource)
if err != nil {
return nil, err
}
return &GetMetricDataSourceOutput{MetricDataSource: metricDataSource}, nil
}
// SearchMetricDataSourceInput represents the input of a SearchMetricDataSource operation.
type SearchMetricDataSourceInput struct {
_ struct{}
MetricDataSourceName *string
}
// SearchMetricDataSourceOutput represents the output of a SearchMetricDataSource operation.
type SearchMetricDataSourceOutput struct {
_ struct{}
MetricDataSource *MetricDataSource
}
// SearchMetricDataSource gets the metric data source with specified name.
func (c *Client) SearchMetricDataSource(input *SearchMetricDataSourceInput) (*SearchMetricDataSourceOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.MetricDataSourceName == nil {
return nil, errors.New("metric data source name is required")
}
resp, err := c.httpClient.R().Get(fmt.Sprintf("%s/name/%s", apiRoutes.metricDataSources, *input.MetricDataSourceName))
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
metricDataSource := &MetricDataSource{}
err = json.Unmarshal(resp.Body(), metricDataSource)
if err != nil {
return nil, err
}
return &SearchMetricDataSourceOutput{MetricDataSource: metricDataSource}, nil
}
// UpdateMetricDataSourceInput represents the input of a UpdateMetricDataSource operation.
type UpdateMetricDataSourceInput struct {
_ struct{}
MetricDataSourceID *int64
MetricDataSource *MetricDataSource
}
// UpdateMetricDataSourceOutput represents the output of a UpdateMetricDataSource operation.
type UpdateMetricDataSourceOutput struct {
_ struct{}
MetricDataSource *MetricDataSource
}
// UpdateMetricDataSource updates the specific metric data source. https://api.ilert.com/api-docs/#tag/Metric-Data-Sources/paths/~1metric-data-sources~1{id}/put
func (c *Client) UpdateMetricDataSource(input *UpdateMetricDataSourceInput) (*UpdateMetricDataSourceOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.MetricDataSourceID == nil {
return nil, errors.New("metric data source id is required")
}
if input.MetricDataSource == nil {
return nil, errors.New("metric data source input is required")
}
url := fmt.Sprintf("%s/%d", apiRoutes.metricDataSources, *input.MetricDataSourceID)
resp, err := c.httpClient.R().SetBody(input.MetricDataSource).Put(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 200); apiErr != nil {
return nil, apiErr
}
metricDataSource := &MetricDataSource{}
err = json.Unmarshal(resp.Body(), metricDataSource)
if err != nil {
return nil, err
}
return &UpdateMetricDataSourceOutput{MetricDataSource: metricDataSource}, nil
}
// DeleteMetricDataSourceInput represents the input of a DeleteMetricDataSource operation.
type DeleteMetricDataSourceInput struct {
_ struct{}
MetricDataSourceID *int64
}
// DeleteMetricDataSourceOutput represents the output of a DeleteMetricDataSource operation.
type DeleteMetricDataSourceOutput struct {
_ struct{}
}
// DeleteMetricDataSource deletes the specified metric data source. https://api.ilert.com/api-docs/#tag/Metric-Data-Sources/paths/~1metric-data-sources~1{id}/delete
func (c *Client) DeleteMetricDataSource(input *DeleteMetricDataSourceInput) (*DeleteMetricDataSourceOutput, error) {
if input == nil {
return nil, errors.New("input is required")
}
if input.MetricDataSourceID == nil {
return nil, errors.New("metric data source id is required")
}
url := fmt.Sprintf("%s/%d", apiRoutes.metricDataSources, *input.MetricDataSourceID)
resp, err := c.httpClient.R().Delete(url)
if err != nil {
return nil, err
}
if apiErr := getGenericAPIError(resp, 204); apiErr != nil {
return nil, apiErr
}
return &DeleteMetricDataSourceOutput{}, nil
}