-
Notifications
You must be signed in to change notification settings - Fork 28
/
list.go
99 lines (82 loc) · 2.54 KB
/
list.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
// SPDX-License-Identifier: Apache-2.0
package worker
import (
"fmt"
"net/http"
"strconv"
"time"
"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/util"
"github.com/sirupsen/logrus"
)
// swagger:operation GET /api/v1/workers workers ListWorkers
//
// Retrieve a list of workers for the configured backend
//
// ---
// produces:
// - application/json
// parameters:
// - in: query
// name: active
// description: Filter workers based on active status
// type: boolean
// - in: query
// name: checked_in_before
// description: filter workers that have checked in before a certain time
// type: integer
// - in: query
// name: checked_in_after
// description: filter workers that have checked in after a certain time
// type: integer
// default: 0
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved the list of workers
// schema:
// type: array
// items:
// "$ref": "#/definitions/Worker"
// '500':
// description: Unable to retrieve the list of workers
// schema:
// "$ref": "#/definitions/Error"
// ListWorkers represents the API handler to capture a
// list of workers from the configured backend.
func ListWorkers(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
ctx := c.Request.Context()
// update engine logger with API metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields
logrus.WithFields(logrus.Fields{
"user": u.GetName(),
}).Info("reading workers")
active := c.Query("active")
// capture before query parameter if present, default to now
before, err := strconv.ParseInt(c.DefaultQuery("checked_in_before", strconv.FormatInt(time.Now().UTC().Unix(), 10)), 10, 64)
if err != nil {
retErr := fmt.Errorf("unable to convert `checked_in_before` query parameter: %w", err)
util.HandleError(c, http.StatusBadRequest, retErr)
return
}
// capture after query parameter if present, default to 0
after, err := strconv.ParseInt(c.DefaultQuery("checked_in_after", "0"), 10, 64)
if err != nil {
retErr := fmt.Errorf("unable to convert `checked_in_after` query parameter: %w", err)
util.HandleError(c, http.StatusBadRequest, retErr)
return
}
w, err := database.FromContext(c).ListWorkers(ctx, active, before, after)
if err != nil {
retErr := fmt.Errorf("unable to get workers: %w", err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
c.JSON(http.StatusOK, w)
}