Repository navigation
Expand file tree
/
Copy pathhandler_metrics.go
More file actions
346 lines (296 loc) · 10 KB
/
Copy pathhandler_metrics.go
File metadata and controls
346 lines (296 loc) · 10 KB
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package iot
import (
"net/http"
"strings"
"github.com/labstack/echo/v5"
)
func resolveFleetMetricOps(path, method string) string {
switch {
case path == "/fleet-metrics" && method == http.MethodGet:
return opListFleetMetrics
case strings.HasPrefix(path, "/fleet-metric/") && method == http.MethodPut:
return opCreateFleetMetric
case strings.HasPrefix(path, "/fleet-metric/") && method == http.MethodGet:
return opDescribeFleetMetric
case strings.HasPrefix(path, "/fleet-metric/") && method == http.MethodPatch:
return opUpdateFleetMetric
case strings.HasPrefix(path, "/fleet-metric/") && method == http.MethodDelete:
return opDeleteFleetMetric
}
return unknownOperation
}
// resolveCustomMetricOps resolves the custom-metric op family.
//
// ListCustomMetrics' real path is GET /custom-metrics (plural, iot@v1.77.4
// serializers.go) -- every other op in this family is correctly singular
// "/custom-metric/{name}", but List was too, unreachable by a real client.
// Found by gopherstack-n1mb's route table. The singular bare path is kept
// too as a non-canonical route wired for this package's own tests.
func resolveCustomMetricOps(path, method string) string {
switch {
case path == "/custom-metrics" && method == http.MethodGet:
return opListCustomMetrics
case path == "/custom-metric" && method == http.MethodGet:
return opListCustomMetrics
case strings.HasPrefix(path, "/custom-metric/") && method == http.MethodPost:
return opCreateCustomMetric
case strings.HasPrefix(path, "/custom-metric/") && method == http.MethodGet:
return opDescribeCustomMetric
case strings.HasPrefix(path, "/custom-metric/") && method == http.MethodPatch:
return opUpdateCustomMetric
case strings.HasPrefix(path, "/custom-metric/") && method == http.MethodDelete:
return opDeleteCustomMetric
}
return unknownOperation
}
func resolveDimensionOps(path, method string) string {
switch {
case path == "/dimensions" && method == http.MethodGet:
return opListDimensions
case strings.HasPrefix(path, "/dimensions/") && method == http.MethodPost:
return opCreateDimension
case strings.HasPrefix(path, "/dimensions/") && method == http.MethodGet:
return opDescribeDimension
case strings.HasPrefix(path, "/dimensions/") && method == http.MethodPatch:
return opUpdateDimension
case strings.HasPrefix(path, "/dimensions/") && method == http.MethodDelete:
return opDeleteDimension
}
return unknownOperation
}
func (h *Handler) handleCreateFleetMetric(c *echo.Context) error {
name := strings.TrimPrefix(c.Request().URL.Path, "/fleet-metric/")
var input CreateFleetMetricInput
if err := readBody(c, &input); err != nil {
return err
}
input.MetricName = name
fm, err := h.Backend.CreateFleetMetric(&input)
if err != nil {
return respondErr(c, err)
}
return c.JSON(http.StatusOK, map[string]any{
keyMetricName: fm.MetricName,
keyMetricARN: fm.MetricARN,
})
}
func (h *Handler) handleDescribeFleetMetric(c *echo.Context) error {
name := strings.TrimPrefix(c.Request().URL.Path, "/fleet-metric/")
fm, err := h.Backend.DescribeFleetMetric(name)
if err != nil {
return respondErr(c, err)
}
return c.JSON(http.StatusOK, fm)
}
func (h *Handler) handleListFleetMetrics(c *echo.Context) error {
metrics := h.Backend.ListFleetMetrics()
summaries := make([]map[string]any, len(metrics))
for i, fm := range metrics {
summaries[i] = map[string]any{
keyMetricName: fm.MetricName,
keyMetricARN: fm.MetricARN,
}
}
return respondListPage(c, "fleetMetrics", summaries)
}
func (h *Handler) handleUpdateFleetMetric(c *echo.Context) error {
name := strings.TrimPrefix(c.Request().URL.Path, "/fleet-metric/")
var input UpdateFleetMetricInput
if err := readBody(c, &input); err != nil {
return err
}
if err := h.Backend.UpdateFleetMetric(name, &input); err != nil {
return respondErr(c, err)
}
return c.NoContent(http.StatusOK)
}
func (h *Handler) handleDeleteFleetMetric(c *echo.Context) error {
name := strings.TrimPrefix(c.Request().URL.Path, "/fleet-metric/")
expectedVersion := parseExpectedVersionQueryParam(c)
if err := h.Backend.DeleteFleetMetric(name, expectedVersion); err != nil {
// DeleteFleetMetric's own deserializeOpError switch declares no
// ResourceNotFoundException case.
return respondAsInvalidRequest(c, err, ErrResourceNotFound)
}
return c.NoContent(http.StatusOK)
}
func (h *Handler) handleCreateCustomMetric(c *echo.Context) error {
name := strings.TrimPrefix(c.Request().URL.Path, "/custom-metric/")
var input CreateCustomMetricInput
if err := readBody(c, &input); err != nil {
return err
}
input.MetricName = name
cm, err := h.Backend.CreateCustomMetric(&input)
if err != nil {
return respondErr(c, err)
}
return c.JSON(http.StatusOK, map[string]any{
keyMetricName: cm.MetricName,
keyMetricARN: cm.MetricARN,
})
}
func (h *Handler) handleDescribeCustomMetric(c *echo.Context) error {
name := strings.TrimPrefix(c.Request().URL.Path, "/custom-metric/")
cm, err := h.Backend.DescribeCustomMetric(name)
if err != nil {
return respondErr(c, err)
}
return c.JSON(http.StatusOK, cm)
}
func (h *Handler) handleListCustomMetrics(c *echo.Context) error {
metrics := h.Backend.ListCustomMetrics()
names := make([]string, len(metrics))
for i, cm := range metrics {
names[i] = cm.MetricName
}
pageSize, start := parseIoTPagination(c)
page, nextToken := paginateMaps(names, pageSize, start)
resp := map[string]any{"metricNames": page}
if nextToken != "" {
resp["nextToken"] = nextToken
}
return c.JSON(http.StatusOK, resp)
}
func (h *Handler) handleUpdateCustomMetric(c *echo.Context) error {
name := strings.TrimPrefix(c.Request().URL.Path, "/custom-metric/")
var input UpdateCustomMetricInput
if err := readBody(c, &input); err != nil {
return err
}
cm, err := h.Backend.UpdateCustomMetric(name, input.DisplayName)
if err != nil {
return respondErr(c, err)
}
return c.JSON(http.StatusOK, map[string]any{
keyMetricName: cm.MetricName,
keyMetricARN: cm.MetricARN,
})
}
func (h *Handler) handleDeleteCustomMetric(c *echo.Context) error {
name := strings.TrimPrefix(c.Request().URL.Path, "/custom-metric/")
if err := h.Backend.DeleteCustomMetric(name); err != nil {
// DeleteCustomMetric's own deserializeOpError switch declares no
// ResourceNotFoundException case.
return respondAsInvalidRequest(c, err, ErrResourceNotFound)
}
return c.NoContent(http.StatusOK)
}
func (h *Handler) handleCreateDimension(c *echo.Context) error {
name := strings.TrimPrefix(c.Request().URL.Path, "/dimensions/")
var input CreateDimensionInput
if err := readBody(c, &input); err != nil {
return err
}
input.Name = name
d, err := h.Backend.CreateDimension(&input)
if err != nil {
return respondErr(c, err)
}
return c.JSON(http.StatusOK, map[string]any{
keyName: d.Name,
"arn": d.ARN,
})
}
func (h *Handler) handleDescribeDimension(c *echo.Context) error {
name := strings.TrimPrefix(c.Request().URL.Path, "/dimensions/")
d, err := h.Backend.DescribeDimension(name)
if err != nil {
return respondErr(c, err)
}
return c.JSON(http.StatusOK, d)
}
func (h *Handler) handleListDimensions(c *echo.Context) error {
dims := h.Backend.ListDimensions()
names := make([]string, len(dims))
for i, d := range dims {
names[i] = d.Name
}
return respondListPage(c, "dimensionNames", names)
}
func (h *Handler) handleUpdateDimension(c *echo.Context) error {
name := strings.TrimPrefix(c.Request().URL.Path, "/dimensions/")
var input UpdateDimensionInput
if err := readBody(c, &input); err != nil {
return err
}
d, err := h.Backend.UpdateDimension(name, input.StringValues)
if err != nil {
return respondErr(c, err)
}
return c.JSON(http.StatusOK, map[string]any{
keyName: d.Name,
"arn": d.ARN,
})
}
func (h *Handler) handleDeleteDimension(c *echo.Context) error {
name := strings.TrimPrefix(c.Request().URL.Path, "/dimensions/")
if err := h.Backend.DeleteDimension(name); err != nil {
// DeleteDimension's own deserializeOpError switch declares no
// ResourceNotFoundException case.
return respondAsInvalidRequest(c, err, ErrResourceNotFound)
}
return c.NoContent(http.StatusOK)
}
func (h *Handler) handleListMetricValues(c *echo.Context) error {
thingName := c.QueryParam(keyThingName)
metricName := c.QueryParam("metricName")
startTime := parseIoTTimeQueryParam(c, "startTime")
endTime := parseIoTTimeQueryParam(c, "endTime")
maxResults := parseInt32QueryParam(c, "maxResults")
nextToken := c.QueryParam("nextToken")
values, next, err := h.Backend.ListMetricValues(thingName, metricName, startTime, endTime, maxResults, nextToken)
if err != nil {
return h.handleError(c, err)
}
resp := map[string]any{"metricDatumList": values}
if next != "" {
resp["nextToken"] = next
}
return c.JSON(http.StatusOK, resp)
}
func (h *Handler) dispatchFleetMetricOps(c *echo.Context, op string) (bool, error) {
switch op {
case opCreateFleetMetric:
return true, h.handleCreateFleetMetric(c)
case opDescribeFleetMetric:
return true, h.handleDescribeFleetMetric(c)
case opListFleetMetrics:
return true, h.handleListFleetMetrics(c)
case opUpdateFleetMetric:
return true, h.handleUpdateFleetMetric(c)
case opDeleteFleetMetric:
return true, h.handleDeleteFleetMetric(c)
}
return false, nil
}
func (h *Handler) dispatchCustomMetricOps(c *echo.Context, op string) (bool, error) {
switch op {
case opCreateCustomMetric:
return true, h.handleCreateCustomMetric(c)
case opDescribeCustomMetric:
return true, h.handleDescribeCustomMetric(c)
case opListCustomMetrics:
return true, h.handleListCustomMetrics(c)
case opUpdateCustomMetric:
return true, h.handleUpdateCustomMetric(c)
case opDeleteCustomMetric:
return true, h.handleDeleteCustomMetric(c)
}
return false, nil
}
func (h *Handler) dispatchDimensionOps(c *echo.Context, op string) (bool, error) {
switch op {
case opCreateDimension:
return true, h.handleCreateDimension(c)
case opDescribeDimension:
return true, h.handleDescribeDimension(c)
case opListDimensions:
return true, h.handleListDimensions(c)
case opUpdateDimension:
return true, h.handleUpdateDimension(c)
case opDeleteDimension:
return true, h.handleDeleteDimension(c)
}
return false, nil
}