2016-10-25 15:20:06 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-01-05 20:47:44 +00:00
|
|
|
"context"
|
2016-10-25 15:20:06 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/influxdata/chronograf"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sourceLinks struct {
|
|
|
|
Self string `json:"self"` // Self link mapping to this resource
|
|
|
|
Kapacitors string `json:"kapacitors"` // URL for kapacitors endpoint
|
|
|
|
Proxy string `json:"proxy"` // URL for proxy endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
type sourceResponse struct {
|
|
|
|
chronograf.Source
|
|
|
|
Links sourceLinks `json:"links"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newSourceResponse(src chronograf.Source) sourceResponse {
|
2016-11-18 19:13:32 +00:00
|
|
|
// If telegraf is not set, we'll set it to the default value.
|
|
|
|
if src.Telegraf == "" {
|
|
|
|
src.Telegraf = "telegraf"
|
|
|
|
}
|
|
|
|
|
2017-02-12 22:38:05 +00:00
|
|
|
// Omit the password on response
|
|
|
|
src.Password = ""
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
httpAPISrcs := "/chronograf/v1/sources"
|
2016-10-25 15:20:06 +00:00
|
|
|
return sourceResponse{
|
|
|
|
Source: src,
|
|
|
|
Links: sourceLinks{
|
|
|
|
Self: fmt.Sprintf("%s/%d", httpAPISrcs, src.ID),
|
|
|
|
Proxy: fmt.Sprintf("%s/%d/proxy", httpAPISrcs, src.ID),
|
|
|
|
Kapacitors: fmt.Sprintf("%s/%d/kapacitors", httpAPISrcs, src.ID),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// NewSource adds a new valid source to the store
|
|
|
|
func (h *Service) NewSource(w http.ResponseWriter, r *http.Request) {
|
2016-10-25 15:20:06 +00:00
|
|
|
var src chronograf.Source
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&src); err != nil {
|
2016-11-19 17:41:06 +00:00
|
|
|
invalidJSON(w, h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
2016-11-18 19:13:32 +00:00
|
|
|
|
2016-10-25 15:20:06 +00:00
|
|
|
if err := ValidSourceRequest(src); err != nil {
|
2016-11-19 17:41:06 +00:00
|
|
|
invalidData(w, err, h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-18 19:13:32 +00:00
|
|
|
// By default the telegraf database will be telegraf
|
|
|
|
if src.Telegraf == "" {
|
|
|
|
src.Telegraf = "telegraf"
|
|
|
|
}
|
|
|
|
|
2016-10-25 15:20:06 +00:00
|
|
|
var err error
|
|
|
|
if src, err = h.SourcesStore.Add(r.Context(), src); err != nil {
|
|
|
|
msg := fmt.Errorf("Error storing source %v: %v", src, err)
|
2016-11-19 17:41:06 +00:00
|
|
|
unknownErrorWithMessage(w, msg, h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := newSourceResponse(src)
|
|
|
|
w.Header().Add("Location", res.Links.Self)
|
|
|
|
encodeJSON(w, http.StatusCreated, res, h.Logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
type getSourcesResponse struct {
|
|
|
|
Sources []sourceResponse `json:"sources"`
|
|
|
|
}
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// Sources returns all sources from the store.
|
|
|
|
func (h *Service) Sources(w http.ResponseWriter, r *http.Request) {
|
2016-10-25 15:20:06 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
srcs, err := h.SourcesStore.All(ctx)
|
|
|
|
if err != nil {
|
2016-11-19 17:41:06 +00:00
|
|
|
Error(w, http.StatusInternalServerError, "Error loading sources", h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := getSourcesResponse{
|
|
|
|
Sources: make([]sourceResponse, len(srcs)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, src := range srcs {
|
|
|
|
res.Sources[i] = newSourceResponse(src)
|
|
|
|
}
|
|
|
|
|
|
|
|
encodeJSON(w, http.StatusOK, res, h.Logger)
|
|
|
|
}
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// SourcesID retrieves a source from the store
|
|
|
|
func (h *Service) SourcesID(w http.ResponseWriter, r *http.Request) {
|
2016-10-25 15:20:06 +00:00
|
|
|
id, err := paramID("id", r)
|
|
|
|
if err != nil {
|
2016-11-19 17:41:06 +00:00
|
|
|
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
src, err := h.SourcesStore.Get(ctx, id)
|
|
|
|
if err != nil {
|
2016-11-19 17:41:06 +00:00
|
|
|
notFound(w, id, h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := newSourceResponse(src)
|
|
|
|
encodeJSON(w, http.StatusOK, res, h.Logger)
|
|
|
|
}
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// RemoveSource deletes the source from the store
|
|
|
|
func (h *Service) RemoveSource(w http.ResponseWriter, r *http.Request) {
|
2016-10-25 15:20:06 +00:00
|
|
|
id, err := paramID("id", r)
|
|
|
|
if err != nil {
|
2016-11-19 17:41:06 +00:00
|
|
|
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
src := chronograf.Source{ID: id}
|
|
|
|
ctx := r.Context()
|
|
|
|
if err = h.SourcesStore.Delete(ctx, src); err != nil {
|
2016-11-19 17:41:06 +00:00
|
|
|
unknownErrorWithMessage(w, err, h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-01-05 20:47:44 +00:00
|
|
|
// Remove all the associated kapacitors for this source
|
|
|
|
if err = h.removeSrcsKapa(ctx, id); err != nil {
|
|
|
|
unknownErrorWithMessage(w, err, h.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-25 15:20:06 +00:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2017-01-05 20:47:44 +00:00
|
|
|
// removeSrcsKapa will remove all kapacitors and kapacitor rules from the stores.
|
|
|
|
// However, it will not remove the kapacitor tickscript from kapacitor itself.
|
|
|
|
func (h *Service) removeSrcsKapa(ctx context.Context, srcID int) error {
|
|
|
|
kapas, err := h.ServersStore.All(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter the kapacitors to delete by matching the source id
|
|
|
|
deleteKapa := []int{}
|
|
|
|
for _, kapa := range kapas {
|
|
|
|
if kapa.SrcID == srcID {
|
|
|
|
deleteKapa = append(deleteKapa, kapa.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kapaID := range deleteKapa {
|
|
|
|
kapa := chronograf.Server{
|
|
|
|
ID: kapaID,
|
|
|
|
}
|
|
|
|
h.Logger.Debug("Deleting kapacitor resource id ", kapa.ID)
|
|
|
|
|
|
|
|
if err := h.ServersStore.Delete(ctx, kapa); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Now delete all the associated rules
|
|
|
|
rules, err := h.AlertRulesStore.All(ctx, srcID, kapaID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rule := range rules {
|
|
|
|
h.Logger.Debug("Deleting kapacitor rule resource id ", rule.ID)
|
|
|
|
if err := h.AlertRulesStore.Delete(ctx, srcID, kapaID, rule); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// UpdateSource handles incremental updates of a data source
|
|
|
|
func (h *Service) UpdateSource(w http.ResponseWriter, r *http.Request) {
|
2016-10-25 15:20:06 +00:00
|
|
|
id, err := paramID("id", r)
|
|
|
|
if err != nil {
|
2016-11-19 17:41:06 +00:00
|
|
|
Error(w, http.StatusUnprocessableEntity, err.Error(), h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
src, err := h.SourcesStore.Get(ctx, id)
|
|
|
|
if err != nil {
|
2016-11-19 17:41:06 +00:00
|
|
|
notFound(w, id, h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req chronograf.Source
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
2016-11-19 17:41:06 +00:00
|
|
|
invalidJSON(w, h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
src.Default = req.Default
|
2017-01-05 01:35:07 +00:00
|
|
|
src.InsecureSkipVerify = req.InsecureSkipVerify
|
2016-10-25 15:20:06 +00:00
|
|
|
if req.Name != "" {
|
|
|
|
src.Name = req.Name
|
|
|
|
}
|
|
|
|
if req.Password != "" {
|
|
|
|
src.Password = req.Password
|
|
|
|
}
|
|
|
|
if req.Username != "" {
|
|
|
|
src.Username = req.Username
|
|
|
|
}
|
|
|
|
if req.URL != "" {
|
|
|
|
src.URL = req.URL
|
|
|
|
}
|
|
|
|
if req.Type != "" {
|
|
|
|
src.Type = req.Type
|
|
|
|
}
|
2016-11-18 19:13:32 +00:00
|
|
|
if req.Telegraf != "" {
|
|
|
|
src.Telegraf = req.Telegraf
|
|
|
|
}
|
2016-10-25 15:20:06 +00:00
|
|
|
|
|
|
|
if err := ValidSourceRequest(src); err != nil {
|
2016-11-19 17:41:06 +00:00
|
|
|
invalidData(w, err, h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.SourcesStore.Update(ctx, src); err != nil {
|
|
|
|
msg := fmt.Sprintf("Error updating source ID %d", id)
|
2016-11-19 17:41:06 +00:00
|
|
|
Error(w, http.StatusInternalServerError, msg, h.Logger)
|
2016-10-25 15:20:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
encodeJSON(w, http.StatusOK, newSourceResponse(src), h.Logger)
|
|
|
|
}
|
|
|
|
|
2016-10-28 16:27:06 +00:00
|
|
|
// ValidSourceRequest checks if name, url and type are valid
|
2016-10-25 15:20:06 +00:00
|
|
|
func ValidSourceRequest(s chronograf.Source) error {
|
|
|
|
// Name and URL areq required
|
|
|
|
if s.Name == "" || s.URL == "" {
|
|
|
|
return fmt.Errorf("name and url required")
|
|
|
|
}
|
|
|
|
// Type must be influx or influx-enterprise
|
|
|
|
if s.Type != "" {
|
|
|
|
if s.Type != "influx" && s.Type != "influx-enterprise" {
|
|
|
|
return fmt.Errorf("invalid source type %s", s.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
url, err := url.ParseRequestURI(s.URL)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid source URI: %v", err)
|
|
|
|
}
|
|
|
|
if len(url.Scheme) == 0 {
|
|
|
|
return fmt.Errorf("Invalid URL; no URL scheme defined")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|