-
Notifications
You must be signed in to change notification settings - Fork 781
/
api.go
118 lines (103 loc) · 3.24 KB
/
api.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
package ruler
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"github.com/go-kit/kit/log/level"
"github.com/gorilla/mux"
"github.com/cortexproject/cortex/pkg/configs"
"github.com/cortexproject/cortex/pkg/configs/db"
"github.com/cortexproject/cortex/pkg/util"
"github.com/weaveworks/common/user"
)
// API implements the configs api.
type API struct {
db db.DB
http.Handler
}
// NewAPIFromConfig makes a new API from our database config.
func NewAPIFromConfig(cfg db.Config) (*API, error) {
db, err := db.New(cfg)
if err != nil {
return nil, err
}
return NewAPI(db), nil
}
// NewAPI creates a new API.
func NewAPI(db db.DB) *API {
a := &API{db: db}
r := mux.NewRouter()
a.RegisterRoutes(r)
a.Handler = r
return a
}
// RegisterRoutes registers the configs API HTTP routes with the provided Router.
func (a *API) RegisterRoutes(r *mux.Router) {
for _, route := range []struct {
name, method, path string
handler http.HandlerFunc
}{
{"get_rules", "GET", "/api/prom/rules", a.getConfig},
{"cas_rules", "POST", "/api/prom/rules", a.casConfig},
} {
r.Handle(route.path, route.handler).Methods(route.method).Name(route.name)
}
}
// getConfig returns the request configuration.
func (a *API) getConfig(w http.ResponseWriter, r *http.Request) {
userID, _, err := user.ExtractOrgIDFromHTTPRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
logger := util.WithContext(r.Context(), util.Logger)
cfg, err := a.db.GetRulesConfig(r.Context(), userID)
if err == sql.ErrNoRows {
http.Error(w, "No configuration", http.StatusNotFound)
return
} else if err != nil {
level.Error(logger).Log("msg", "error getting config", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(cfg); err != nil {
level.Error(logger).Log("msg", "error encoding config", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
type configUpdateRequest struct {
OldConfig configs.RulesConfig `json:"old_config"`
NewConfig configs.RulesConfig `json:"new_config"`
}
func (a *API) casConfig(w http.ResponseWriter, r *http.Request) {
userID, _, err := user.ExtractOrgIDFromHTTPRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
logger := util.WithContext(r.Context(), util.Logger)
var updateReq configUpdateRequest
if err := json.NewDecoder(r.Body).Decode(&updateReq); err != nil {
level.Error(logger).Log("msg", "error decoding json body", "err", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if _, err = updateReq.NewConfig.Parse(); err != nil {
level.Error(logger).Log("msg", "invalid rules", "err", err)
http.Error(w, fmt.Sprintf("Invalid rules: %v", err), http.StatusBadRequest)
return
}
updated, err := a.db.SetRulesConfig(r.Context(), userID, updateReq.OldConfig, updateReq.NewConfig)
if err != nil {
level.Error(logger).Log("msg", "error storing config", "err", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if !updated {
http.Error(w, "Supplied configuration doesn't match current configuration", http.StatusConflict)
}
w.WriteHeader(http.StatusNoContent)
}