2018-10-02 16:00:29 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
http "net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/influxdata/platform"
|
|
|
|
"github.com/influxdata/platform/chronograf/server"
|
|
|
|
"github.com/influxdata/platform/query"
|
2018-10-05 11:43:56 +00:00
|
|
|
"github.com/influxdata/platform/storage"
|
2018-10-02 16:00:29 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// APIHandler is a collection of all the service handlers.
|
|
|
|
type APIHandler struct {
|
|
|
|
BucketHandler *BucketHandler
|
|
|
|
UserHandler *UserHandler
|
|
|
|
OrgHandler *OrgHandler
|
|
|
|
AuthorizationHandler *AuthorizationHandler
|
|
|
|
DashboardHandler *DashboardHandler
|
|
|
|
AssetHandler *AssetHandler
|
|
|
|
ChronografHandler *ChronografHandler
|
|
|
|
SourceHandler *SourceHandler
|
|
|
|
MacroHandler *MacroHandler
|
|
|
|
TaskHandler *TaskHandler
|
2018-10-24 15:13:30 +00:00
|
|
|
TelegrafHandler *TelegrafHandler
|
2018-10-02 16:00:29 +00:00
|
|
|
QueryHandler *FluxHandler
|
2019-01-07 21:47:16 +00:00
|
|
|
ProtoHandler *ProtoHandler
|
2018-10-02 16:00:29 +00:00
|
|
|
WriteHandler *WriteHandler
|
|
|
|
SetupHandler *SetupHandler
|
|
|
|
SessionHandler *SessionHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
// APIBackend is all services and associated parameters required to construct
|
|
|
|
// an APIHandler.
|
|
|
|
type APIBackend struct {
|
2018-12-21 21:17:18 +00:00
|
|
|
DeveloperMode bool
|
|
|
|
Logger *zap.Logger
|
2018-10-02 16:00:29 +00:00
|
|
|
|
|
|
|
NewBucketService func(*platform.Source) (platform.BucketService, error)
|
|
|
|
NewQueryService func(*platform.Source) (query.ProxyQueryService, error)
|
|
|
|
|
2018-11-02 18:21:14 +00:00
|
|
|
PointsWriter storage.PointsWriter
|
|
|
|
AuthorizationService platform.AuthorizationService
|
|
|
|
BucketService platform.BucketService
|
|
|
|
SessionService platform.SessionService
|
|
|
|
UserService platform.UserService
|
|
|
|
OrganizationService platform.OrganizationService
|
|
|
|
UserResourceMappingService platform.UserResourceMappingService
|
2018-12-03 16:07:08 +00:00
|
|
|
LabelService platform.LabelService
|
2018-11-02 18:21:14 +00:00
|
|
|
DashboardService platform.DashboardService
|
|
|
|
DashboardOperationLogService platform.DashboardOperationLogService
|
|
|
|
BucketOperationLogService platform.BucketOperationLogService
|
|
|
|
UserOperationLogService platform.UserOperationLogService
|
|
|
|
OrganizationOperationLogService platform.OrganizationOperationLogService
|
|
|
|
SourceService platform.SourceService
|
|
|
|
MacroService platform.MacroService
|
|
|
|
BasicAuthService platform.BasicAuthService
|
|
|
|
OnboardingService platform.OnboardingService
|
|
|
|
ProxyQueryService query.ProxyQueryService
|
|
|
|
TaskService platform.TaskService
|
|
|
|
TelegrafService platform.TelegrafConfigStore
|
|
|
|
ScraperTargetStoreService platform.ScraperTargetStoreService
|
2018-12-27 18:51:31 +00:00
|
|
|
SecretService platform.SecretService
|
2018-12-28 23:02:19 +00:00
|
|
|
LookupService platform.LookupService
|
2018-11-02 18:21:14 +00:00
|
|
|
ChronografService *server.Service
|
2019-01-07 21:47:16 +00:00
|
|
|
ProtoService platform.ProtoService
|
2018-10-02 16:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAPIHandler constructs all api handlers beneath it and returns an APIHandler
|
|
|
|
func NewAPIHandler(b *APIBackend) *APIHandler {
|
|
|
|
h := &APIHandler{}
|
2018-12-29 03:41:06 +00:00
|
|
|
sessionBackend := NewSessionBackend(b)
|
|
|
|
h.SessionHandler = NewSessionHandler(sessionBackend)
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2018-12-17 12:43:06 +00:00
|
|
|
h.BucketHandler = NewBucketHandler(b.UserResourceMappingService, b.LabelService, b.UserService)
|
2018-10-02 16:00:29 +00:00
|
|
|
h.BucketHandler.BucketService = b.BucketService
|
2018-11-02 18:21:14 +00:00
|
|
|
h.BucketHandler.BucketOperationLogService = b.BucketOperationLogService
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2018-12-17 12:43:06 +00:00
|
|
|
h.OrgHandler = NewOrgHandler(b.UserResourceMappingService, b.LabelService, b.UserService)
|
2018-10-02 16:00:29 +00:00
|
|
|
h.OrgHandler.OrganizationService = b.OrganizationService
|
|
|
|
h.OrgHandler.BucketService = b.BucketService
|
2018-11-02 18:21:14 +00:00
|
|
|
h.OrgHandler.OrganizationOperationLogService = b.OrganizationOperationLogService
|
2018-12-27 18:51:31 +00:00
|
|
|
h.OrgHandler.SecretService = b.SecretService
|
2018-10-02 16:00:29 +00:00
|
|
|
|
|
|
|
h.UserHandler = NewUserHandler()
|
|
|
|
h.UserHandler.UserService = b.UserService
|
2018-10-18 17:20:26 +00:00
|
|
|
h.UserHandler.BasicAuthService = b.BasicAuthService
|
2018-11-02 18:21:14 +00:00
|
|
|
h.UserHandler.UserOperationLogService = b.UserOperationLogService
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2018-12-17 12:43:06 +00:00
|
|
|
h.DashboardHandler = NewDashboardHandler(b.UserResourceMappingService, b.LabelService, b.UserService)
|
2018-10-02 16:00:29 +00:00
|
|
|
h.DashboardHandler.DashboardService = b.DashboardService
|
2018-11-02 18:21:14 +00:00
|
|
|
h.DashboardHandler.DashboardOperationLogService = b.DashboardOperationLogService
|
2018-10-02 16:00:29 +00:00
|
|
|
|
|
|
|
h.MacroHandler = NewMacroHandler()
|
|
|
|
h.MacroHandler.MacroService = b.MacroService
|
|
|
|
|
2018-12-21 16:14:55 +00:00
|
|
|
h.AuthorizationHandler = NewAuthorizationHandler(b.UserService)
|
2018-12-28 23:02:19 +00:00
|
|
|
h.AuthorizationHandler.OrganizationService = b.OrganizationService
|
2018-10-02 16:00:29 +00:00
|
|
|
h.AuthorizationHandler.AuthorizationService = b.AuthorizationService
|
2018-12-28 23:02:19 +00:00
|
|
|
h.AuthorizationHandler.LookupService = b.LookupService
|
2018-10-02 16:00:29 +00:00
|
|
|
h.AuthorizationHandler.Logger = b.Logger.With(zap.String("handler", "auth"))
|
|
|
|
|
|
|
|
h.SourceHandler = NewSourceHandler()
|
|
|
|
h.SourceHandler.SourceService = b.SourceService
|
|
|
|
h.SourceHandler.NewBucketService = b.NewBucketService
|
|
|
|
h.SourceHandler.NewQueryService = b.NewQueryService
|
|
|
|
|
|
|
|
h.SetupHandler = NewSetupHandler()
|
|
|
|
h.SetupHandler.OnboardingService = b.OnboardingService
|
|
|
|
|
2018-12-17 12:43:06 +00:00
|
|
|
h.TaskHandler = NewTaskHandler(b.UserResourceMappingService, b.LabelService, b.Logger, b.UserService)
|
2018-10-02 16:00:29 +00:00
|
|
|
h.TaskHandler.TaskService = b.TaskService
|
2018-10-23 17:51:13 +00:00
|
|
|
h.TaskHandler.AuthorizationService = b.AuthorizationService
|
|
|
|
h.TaskHandler.UserResourceMappingService = b.UserResourceMappingService
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2018-10-31 18:10:03 +00:00
|
|
|
h.TelegrafHandler = NewTelegrafHandler(
|
|
|
|
b.Logger.With(zap.String("handler", "telegraf")),
|
|
|
|
b.UserResourceMappingService,
|
2018-12-11 18:15:34 +00:00
|
|
|
b.LabelService,
|
2018-10-31 18:10:03 +00:00
|
|
|
b.TelegrafService,
|
2018-12-17 12:43:06 +00:00
|
|
|
b.UserService,
|
2018-10-31 18:10:03 +00:00
|
|
|
)
|
2018-10-24 15:13:30 +00:00
|
|
|
|
2018-10-05 11:43:56 +00:00
|
|
|
h.WriteHandler = NewWriteHandler(b.PointsWriter)
|
2018-10-02 16:00:29 +00:00
|
|
|
h.WriteHandler.OrganizationService = b.OrganizationService
|
|
|
|
h.WriteHandler.BucketService = b.BucketService
|
|
|
|
h.WriteHandler.Logger = b.Logger.With(zap.String("handler", "write"))
|
|
|
|
|
|
|
|
h.QueryHandler = NewFluxHandler()
|
|
|
|
h.QueryHandler.OrganizationService = b.OrganizationService
|
|
|
|
h.QueryHandler.Logger = b.Logger.With(zap.String("handler", "query"))
|
2018-10-03 19:13:27 +00:00
|
|
|
h.QueryHandler.ProxyQueryService = b.ProxyQueryService
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2019-01-07 21:47:16 +00:00
|
|
|
h.ProtoHandler = NewProtoHandler(NewProtoBackend(b))
|
|
|
|
|
2018-10-02 16:00:29 +00:00
|
|
|
h.ChronografHandler = NewChronografHandler(b.ChronografService)
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
var apiLinks = map[string]interface{}{
|
2018-12-20 21:25:53 +00:00
|
|
|
// when adding new links, please take care to keep this list alphabetical
|
|
|
|
// as this makes it easier to verify values against the swagger document.
|
2018-10-24 09:10:26 +00:00
|
|
|
"authorizations": "/api/v2/authorizations",
|
|
|
|
"buckets": "/api/v2/buckets",
|
2018-12-20 21:25:53 +00:00
|
|
|
"dashboards": "/api/v2/dashboards",
|
|
|
|
"external": map[string]string{
|
|
|
|
"statusFeed": "https://www.influxdata.com/feed/json",
|
|
|
|
},
|
|
|
|
"macros": "/api/v2/macros",
|
|
|
|
"me": "/api/v2/me",
|
|
|
|
"orgs": "/api/v2/orgs",
|
2019-01-07 21:47:16 +00:00
|
|
|
"protos": "/api/v2/protos",
|
2018-10-04 18:21:53 +00:00
|
|
|
"query": map[string]string{
|
|
|
|
"self": "/api/v2/query",
|
|
|
|
"ast": "/api/v2/query/ast",
|
2018-12-05 18:13:26 +00:00
|
|
|
"analyze": "/api/v2/query/analyze",
|
2018-10-04 18:21:53 +00:00
|
|
|
"spec": "/api/v2/query/spec",
|
|
|
|
"suggestions": "/api/v2/query/suggestions",
|
2018-10-02 16:00:29 +00:00
|
|
|
},
|
2018-12-20 21:25:53 +00:00
|
|
|
"setup": "/api/v2/setup",
|
|
|
|
"signin": "/api/v2/signin",
|
|
|
|
"signout": "/api/v2/signout",
|
|
|
|
"sources": "/api/v2/sources",
|
2018-10-02 16:00:29 +00:00
|
|
|
"system": map[string]string{
|
|
|
|
"metrics": "/metrics",
|
|
|
|
"debug": "/debug/pprof",
|
2018-10-04 18:21:53 +00:00
|
|
|
"health": "/health",
|
2018-10-02 16:00:29 +00:00
|
|
|
},
|
2018-12-20 21:25:53 +00:00
|
|
|
"tasks": "/api/v2/tasks",
|
|
|
|
"telegrafs": "/api/v2/telegrafs",
|
|
|
|
"users": "/api/v2/users",
|
|
|
|
"write": "/api/v2/write",
|
2018-10-02 16:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *APIHandler) serveLinks(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
if err := encodeResponse(ctx, w, http.StatusOK, apiLinks); err != nil {
|
|
|
|
EncodeError(ctx, err, w)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServeHTTP delegates a request to the appropriate subhandler.
|
|
|
|
func (h *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
setCORSResponseHeaders(w, r)
|
|
|
|
if r.Method == "OPTIONS" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serve the links base links for the API.
|
|
|
|
if r.URL.Path == "/api/v2/" || r.URL.Path == "/api/v2" {
|
|
|
|
h.serveLinks(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Path == "/api/v2/signin" || r.URL.Path == "/api/v2/signout" {
|
|
|
|
h.SessionHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/setup") {
|
|
|
|
h.SetupHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/write") {
|
|
|
|
h.WriteHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/query") {
|
|
|
|
h.QueryHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/buckets") {
|
|
|
|
h.BucketHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/users") {
|
|
|
|
h.UserHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-10 17:06:29 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/me") {
|
|
|
|
h.UserHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-02 16:00:29 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/orgs") {
|
|
|
|
h.OrgHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/authorizations") {
|
|
|
|
h.AuthorizationHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/dashboards") {
|
|
|
|
h.DashboardHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/sources") {
|
|
|
|
h.SourceHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/tasks") {
|
|
|
|
h.TaskHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-24 15:13:30 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/telegrafs") {
|
|
|
|
h.TelegrafHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-02 16:00:29 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/macros") {
|
|
|
|
h.MacroHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-07 21:47:16 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/protos") {
|
|
|
|
h.ProtoHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-02 16:00:29 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/chronograf/") {
|
|
|
|
h.ChronografHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-25 02:32:29 +00:00
|
|
|
notFoundHandler(w, r)
|
2018-10-02 16:00:29 +00:00
|
|
|
}
|