-
Notifications
You must be signed in to change notification settings - Fork 0
/
hatchery.go
66 lines (54 loc) · 1.6 KB
/
hatchery.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
package main
import (
"database/sql"
"encoding/json"
"io/ioutil"
"net/http"
"github.com/gorilla/mux"
"github.com/ovh/cds/engine/api/context"
"github.com/ovh/cds/engine/api/hatchery"
"github.com/ovh/cds/engine/api/worker"
"github.com/ovh/cds/engine/log"
"github.com/ovh/cds/sdk"
)
func registerHatchery(w http.ResponseWriter, r *http.Request, db *sql.DB, c *context.Context) {
data, err := ioutil.ReadAll(r.Body)
if err != nil {
WriteError(w, r, sdk.ErrUnknownError)
return
}
// Unmarshal body
hatch := sdk.Hatchery{}
if err = json.Unmarshal(data, &hatch); err != nil {
log.Warning("registerHatchery: Cannot unmarshal data: %s\n", err)
WriteError(w, r, sdk.ErrUnknownError)
return
}
// Load token
tk, err := worker.LoadToken(db, hatch.UID)
if err != nil {
log.Warning("registerHatchery: Invalid token> %s\n", err)
WriteError(w, r, sdk.ErrUnauthorized)
return
}
hatch.GroupID = tk.GroupID
if err = hatchery.InsertHatchery(db, &hatch); err != nil {
if err != sdk.ErrModelNameExist {
log.Warning("registerHatchery> Cannot insert new hatchery: %s\n", err)
}
log.Warning("registerHatchery> Error %s", err)
WriteError(w, r, err)
return
}
log.Info("registerHatchery> Welcome %d", hatch.ID)
WriteJSON(w, r, hatch, http.StatusOK)
}
func refreshHatcheryHandler(w http.ResponseWriter, r *http.Request, db *sql.DB, c *context.Context) {
vars := mux.Vars(r)
hatcheryID := vars["id"]
if err := hatchery.RefreshHatchery(db, hatcheryID); err != nil {
log.Warning("refreshHatcheryHandler> cannot refresh last beat of %s: %s\n", hatcheryID, err)
WriteError(w, r, err)
return
}
}