-
Notifications
You must be signed in to change notification settings - Fork 88
/
appstatus.go
66 lines (56 loc) · 1.65 KB
/
appstatus.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 handlers
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/replicatedhq/kots/pkg/api/appstatus/types"
"github.com/replicatedhq/kots/pkg/appstatus"
"github.com/replicatedhq/kots/pkg/logger"
"github.com/replicatedhq/kots/pkg/reporting"
"github.com/replicatedhq/kots/pkg/store"
)
// NOTE: this uses special cluster authorization
func (h *Handler) SetAppStatus(w http.ResponseWriter, r *http.Request) {
auth, err := parseClusterAuthorization(r.Header.Get("Authorization"))
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusForbidden)
return
}
_, err = store.GetStore().GetClusterIDFromDeployToken(auth.Password)
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusForbidden)
return
}
body, err := ioutil.ReadAll(r.Body)
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
newAppStatus := types.AppStatus{}
err = json.Unmarshal(body, &newAppStatus)
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
currentAppStatus, err := store.GetStore().GetAppStatus(newAppStatus.AppID)
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
err = store.GetStore().SetAppStatus(newAppStatus.AppID, newAppStatus.ResourceStates, newAppStatus.UpdatedAt, newAppStatus.Sequence)
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
newAppState := appstatus.GetState(newAppStatus.ResourceStates)
if currentAppStatus != nil && newAppState != currentAppStatus.State {
go reporting.SendAppInfo(newAppStatus.AppID)
}
w.WriteHeader(http.StatusNoContent)
}