-
Notifications
You must be signed in to change notification settings - Fork 28
/
update.go
136 lines (114 loc) · 3.22 KB
/
update.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
// SPDX-License-Identifier: Apache-2.0
package worker
import (
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/router/middleware/worker"
"github.com/go-vela/server/util"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)
// swagger:operation PUT /api/v1/workers/{worker} workers UpdateWorker
//
// Update a worker for the configured backend
//
// ---
// produces:
// - application/json
// parameters:
// - in: body
// name: body
// description: Payload containing the worker to update
// required: true
// schema:
// "$ref": "#/definitions/Worker"
// - in: path
// name: worker
// description: Name of the worker
// required: true
// type: string
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully updated the worker
// schema:
// "$ref": "#/definitions/Worker"
// '400':
// description: Unable to update the worker
// schema:
// "$ref": "#/definitions/Error"
// '404':
// description: Unable to update the worker
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Unable to update the worker
// schema:
// "$ref": "#/definitions/Error"
// UpdateWorker represents the API handler to
// update a worker in the configured backend.
func UpdateWorker(c *gin.Context) {
// capture middleware values
u := user.Retrieve(c)
w := worker.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(),
"worker": w.GetHostname(),
}).Infof("updating worker %s", w.GetHostname())
// capture body from API request
input := new(library.Worker)
err := c.Bind(input)
if err != nil {
retErr := fmt.Errorf("unable to decode JSON for worker %s: %w", w.GetHostname(), err)
util.HandleError(c, http.StatusBadRequest, retErr)
return
}
if len(input.GetAddress()) > 0 {
// update address if set
w.SetAddress(input.GetAddress())
}
if len(input.GetRoutes()) > 0 {
// update routes if set
w.SetRoutes(input.GetRoutes())
}
if input.Active != nil {
// update active if set
w.SetActive(input.GetActive())
}
if input.RunningBuildIDs != nil {
// update runningBuildIDs if set
w.SetRunningBuildIDs(input.GetRunningBuildIDs())
}
if len(input.GetStatus()) > 0 {
// update status if set
w.SetStatus(input.GetStatus())
}
if input.GetLastStatusUpdateAt() > 0 {
// update lastStatusUpdateAt if set
w.SetLastStatusUpdateAt(input.GetLastStatusUpdateAt())
}
if input.GetLastBuildStartedAt() > 0 {
// update lastBuildStartedAt if set
w.SetLastBuildStartedAt(input.GetLastBuildStartedAt())
}
if input.GetLastBuildFinishedAt() > 0 {
// update lastBuildFinishedAt if set
w.SetLastBuildFinishedAt(input.GetLastBuildFinishedAt())
}
// send API call to update the worker
w, err = database.FromContext(c).UpdateWorker(ctx, w)
if err != nil {
retErr := fmt.Errorf("unable to update worker %s: %w", w.GetHostname(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
c.JSON(http.StatusOK, w)
}