2017-12-13 01:06:57 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-12-13 17:28:39 +00:00
|
|
|
"encoding/json"
|
2017-12-13 17:04:44 +00:00
|
|
|
"fmt"
|
2017-12-13 01:06:57 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/influxdata/chronograf"
|
|
|
|
)
|
|
|
|
|
|
|
|
type configResponse struct {
|
|
|
|
Links selfLinks `json:"links"`
|
|
|
|
chronograf.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConfigResponse(config chronograf.Config) *configResponse {
|
|
|
|
return &configResponse{
|
|
|
|
Links: selfLinks{
|
|
|
|
Self: "/chronograf/v1/config",
|
|
|
|
},
|
|
|
|
Config: config,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 17:04:44 +00:00
|
|
|
type authConfigResponse struct {
|
|
|
|
Links selfLinks `json:"links"`
|
|
|
|
chronograf.AuthConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAuthConfigResponse(config chronograf.Config) *authConfigResponse {
|
|
|
|
return &authConfigResponse{
|
|
|
|
Links: selfLinks{
|
|
|
|
Self: "/chronograf/v1/config/auth",
|
|
|
|
},
|
|
|
|
AuthConfig: config.Auth,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-30 01:44:57 +00:00
|
|
|
type logViewerUIResponse struct {
|
|
|
|
Links selfLinks `json:"links"`
|
|
|
|
chronograf.LogViewerUIConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func newLogViewerUIConfigResponse(config chronograf.Config) *logViewerUIResponse {
|
|
|
|
return &logViewerUIResponse{
|
|
|
|
Links: selfLinks{
|
2018-07-03 00:33:59 +00:00
|
|
|
Self: "/chronograf/v1/config/logviewer",
|
2018-06-30 01:44:57 +00:00
|
|
|
},
|
2018-07-03 00:54:14 +00:00
|
|
|
LogViewerUIConfig: config.LogViewer,
|
2018-06-30 01:44:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-13 01:06:57 +00:00
|
|
|
// Config retrieves the global application configuration
|
|
|
|
func (s *Service) Config(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
config, err := s.Store.Config(ctx).Get(ctx)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
Error(w, http.StatusBadRequest, "Configuration object was nil", s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res := newConfigResponse(*config)
|
|
|
|
encodeJSON(w, http.StatusOK, res, s.Logger)
|
|
|
|
}
|
2017-12-13 17:04:44 +00:00
|
|
|
|
2018-07-02 23:17:23 +00:00
|
|
|
// LogViewerUIConfig retrieves the log viewer UI section of the global application configuration
|
|
|
|
func (s *Service) LogViewerUIConfig(w http.ResponseWriter, r *http.Request) {
|
2017-12-13 17:04:44 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
config, err := s.Store.Config(ctx).Get(ctx)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
Error(w, http.StatusBadRequest, "Configuration object was nil", s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-02 23:17:23 +00:00
|
|
|
var res interface{}
|
|
|
|
res = newLogViewerUIConfigResponse(*config)
|
|
|
|
|
|
|
|
encodeJSON(w, http.StatusOK, res, s.Logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReplaceLogViewerUIConfig replaces the log viewer UI section of the global application configuration
|
|
|
|
func (s *Service) ReplaceLogViewerUIConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
config, err := s.Store.Config(ctx).Get(ctx)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
Error(w, http.StatusBadRequest, "Configuration object was nil", s.Logger)
|
|
|
|
return
|
|
|
|
}
|
2018-06-30 01:44:57 +00:00
|
|
|
|
2017-12-13 17:04:44 +00:00
|
|
|
var res interface{}
|
2018-07-02 23:17:23 +00:00
|
|
|
|
|
|
|
var logViewerUIConfig chronograf.LogViewerUIConfig
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&logViewerUIConfig); err != nil {
|
|
|
|
invalidJSON(w, s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := validLogViewerUIConfig(logViewerUIConfig); err != nil {
|
|
|
|
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-03 00:54:14 +00:00
|
|
|
config.LogViewer = logViewerUIConfig
|
2018-07-02 23:17:23 +00:00
|
|
|
res = newLogViewerUIConfigResponse(*config)
|
|
|
|
|
|
|
|
if err := s.Store.Config(ctx).Update(ctx, config); err != nil {
|
|
|
|
unknownErrorWithMessage(w, err, s.Logger)
|
2017-12-13 17:04:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
encodeJSON(w, http.StatusOK, res, s.Logger)
|
|
|
|
}
|
2017-12-13 17:28:39 +00:00
|
|
|
|
2018-07-02 23:17:23 +00:00
|
|
|
// AuthConfig retrieves the auth section of the global application configuration
|
|
|
|
func (s *Service) AuthConfig(w http.ResponseWriter, r *http.Request) {
|
2017-12-13 17:28:39 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
config, err := s.Store.Config(ctx).Get(ctx)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
Error(w, http.StatusBadRequest, "Configuration object was nil", s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var res interface{}
|
2018-07-02 23:17:23 +00:00
|
|
|
res = newAuthConfigResponse(*config)
|
|
|
|
|
|
|
|
encodeJSON(w, http.StatusOK, res, s.Logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReplaceAuthConfig replaces the auth section of the global application configuration
|
|
|
|
func (s *Service) ReplaceAuthConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
config, err := s.Store.Config(ctx).Get(ctx)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, http.StatusBadRequest, err.Error(), s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if config == nil {
|
|
|
|
Error(w, http.StatusBadRequest, "Configuration object was nil", s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var res interface{}
|
|
|
|
|
|
|
|
var authConfig chronograf.AuthConfig
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&authConfig); err != nil {
|
|
|
|
invalidJSON(w, s.Logger)
|
2017-12-13 17:28:39 +00:00
|
|
|
return
|
|
|
|
}
|
2018-07-02 23:17:23 +00:00
|
|
|
config.Auth = authConfig
|
|
|
|
res = newAuthConfigResponse(*config)
|
2017-12-13 17:28:39 +00:00
|
|
|
|
|
|
|
if err := s.Store.Config(ctx).Update(ctx, config); err != nil {
|
|
|
|
unknownErrorWithMessage(w, err, s.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
encodeJSON(w, http.StatusOK, res, s.Logger)
|
|
|
|
}
|
2018-07-02 23:17:23 +00:00
|
|
|
|
|
|
|
func validLogViewerUIConfig(cfg chronograf.LogViewerUIConfig) error {
|
|
|
|
if len(cfg.Columns) == 0 {
|
|
|
|
return fmt.Errorf("Invalid log viewer UI config: must have at least 1 column")
|
|
|
|
}
|
|
|
|
|
|
|
|
nameMatcher := map[string]bool{}
|
|
|
|
positionMatcher := map[int32]bool{}
|
|
|
|
|
|
|
|
for _, clm := range cfg.Columns {
|
|
|
|
iconCount := 0
|
|
|
|
textCount := 0
|
|
|
|
visibility := 0
|
|
|
|
|
|
|
|
if nameMatcher[clm.Name] == true {
|
|
|
|
return fmt.Errorf("Invalid log viewer UI config: Duplicate column name %s", clm.Name)
|
|
|
|
}
|
|
|
|
nameMatcher[clm.Name] = true
|
|
|
|
|
|
|
|
if positionMatcher[clm.Position] == true {
|
|
|
|
return fmt.Errorf("Invalid log viewer UI config: Multiple columns with same position value")
|
|
|
|
}
|
|
|
|
positionMatcher[clm.Position] = true
|
|
|
|
|
2018-07-02 23:35:06 +00:00
|
|
|
for _, e := range clm.Encodings {
|
2018-07-02 23:17:23 +00:00
|
|
|
if e.Type == "visibility" {
|
|
|
|
visibility++
|
|
|
|
if !(e.Value == "visible" || e.Value == "hidden") {
|
|
|
|
return fmt.Errorf("Invalid log viewer UI config: invalid visibility in column %s", clm.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if clm.Name == "severity" {
|
|
|
|
if e.Value == "icon" {
|
|
|
|
iconCount++
|
|
|
|
} else if e.Value == "text" {
|
|
|
|
textCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if visibility != 1 {
|
|
|
|
return fmt.Errorf("Invalid log viewer UI config: missing visibility encoding in column %s", clm.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if clm.Name == "severity" {
|
|
|
|
if iconCount+textCount == 0 || iconCount > 1 || textCount > 1 {
|
|
|
|
return fmt.Errorf("Invalid log viewer UI config: invalid number of severity format encodings in column %s", clm.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|