API: only overwrite specific worker fields when worker already exists (#236)
* API: only overwrite specific worker fields when worker already exists * Don't forget to return when creating new worker * Return updated worker when updating the worker
This commit is contained in:
parent
08769e00b4
commit
1fce915d67
|
|
@ -71,26 +71,39 @@ func (controller *Controller) createWorker(ctx *gin.Context) responder.Responder
|
|||
|
||||
return controller.storeUpdate(func(txn storepkg.Transaction) responder.Responder {
|
||||
// In case there already exist a worker with the same name,
|
||||
// allow overwriting it if the request comes from a worker
|
||||
// with the same machine ID
|
||||
// update it (to avoid overwriting things like SchedulingPaused)
|
||||
// if the request comes from a worker with the same machine ID
|
||||
dbWorker, err := txn.GetWorker(worker.Name)
|
||||
if err != nil && !errors.Is(err, storepkg.ErrNotFound) {
|
||||
controller.logger.Errorf("failed to check if the worker "+
|
||||
"with name %q exists in the DB: %v", worker.Name, err)
|
||||
|
||||
return responder.Code(http.StatusInternalServerError)
|
||||
}
|
||||
if err == nil && worker.MachineID != dbWorker.MachineID {
|
||||
return responder.JSON(http.StatusConflict,
|
||||
NewErrorResponse("this worker is managed from a different machine ID, "+
|
||||
"delete this worker first to be able to re-create it"))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, storepkg.ErrNotFound) {
|
||||
// Create a new worker
|
||||
if err := txn.SetWorker(worker); err != nil {
|
||||
return responder.Error(err)
|
||||
}
|
||||
|
||||
return responder.JSON(200, worker)
|
||||
}
|
||||
|
||||
controller.logger.Errorf("failed to check if the worker "+
|
||||
"with name %q exists in the DB: %v", worker.Name, err)
|
||||
|
||||
return responder.Code(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
// Update an already existing worker
|
||||
if worker.MachineID != dbWorker.MachineID {
|
||||
return responder.JSON(http.StatusConflict, NewErrorResponse("this worker is managed "+
|
||||
"from a different machine ID, delete this worker first to be able to re-create it"))
|
||||
}
|
||||
|
||||
dbWorker.LastSeen = worker.LastSeen
|
||||
dbWorker.Resources = worker.Resources
|
||||
|
||||
if err := txn.SetWorker(*dbWorker); err != nil {
|
||||
return responder.Error(err)
|
||||
}
|
||||
|
||||
return responder.JSON(200, dbWorker)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue