-
Notifications
You must be signed in to change notification settings - Fork 28
/
list.go
119 lines (101 loc) · 2.92 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// SPDX-License-Identifier: Apache-2.0
package user
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/go-vela/server/api"
"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/users users ListUsers
//
// Retrieve a list of users for the configured backend
//
// ---
// produces:
// - application/json
// security:
// - ApiKeyAuth: []
// parameters:
// - in: query
// name: page
// description: The page of results to retrieve
// type: integer
// default: 1
// - in: query
// name: per_page
// description: How many results per page to return
// type: integer
// maximum: 100
// default: 10
// responses:
// '200':
// description: Successfully retrieved the list of users
// schema:
// type: array
// items:
// "$ref": "#/definitions/User"
// headers:
// X-Total-Count:
// description: Total number of results
// type: integer
// Link:
// description: see https://tools.ietf.org/html/rfc5988
// type: string
// '400':
// description: Unable to retrieve the list of users
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Unable to retrieve the list of users
// schema:
// "$ref": "#/definitions/Error"
// ListUsers represents the API handler to capture a list
// of users from the configured backend.
func ListUsers(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 lite users")
// capture page query parameter if present
page, err := strconv.Atoi(c.DefaultQuery("page", "1"))
if err != nil {
retErr := fmt.Errorf("unable to convert page query parameter for users: %w", err)
util.HandleError(c, http.StatusBadRequest, retErr)
return
}
// capture per_page query parameter if present
perPage, err := strconv.Atoi(c.DefaultQuery("per_page", "10"))
if err != nil {
retErr := fmt.Errorf("unable to convert per_page query parameter for users: %w", err)
util.HandleError(c, http.StatusBadRequest, retErr)
return
}
// ensure per_page isn't above or below allowed values
perPage = util.MaxInt(1, util.MinInt(100, perPage))
// send API call to capture the list of users
users, t, err := database.FromContext(c).ListLiteUsers(ctx, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to get users: %w", err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
// create pagination object
pagination := api.Pagination{
Page: page,
PerPage: perPage,
Total: t,
}
// set pagination headers
pagination.SetHeaderLink(c)
c.JSON(http.StatusOK, users)
}