2018-10-02 16:00:29 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
http "net/http"
|
|
|
|
"strings"
|
|
|
|
|
2019-01-17 13:36:49 +00:00
|
|
|
influxdb "github.com/influxdata/influxdb"
|
2019-01-16 19:41:57 +00:00
|
|
|
"github.com/influxdata/influxdb/authorizer"
|
2019-01-08 00:37:16 +00:00
|
|
|
"github.com/influxdata/influxdb/chronograf/server"
|
|
|
|
"github.com/influxdata/influxdb/query"
|
|
|
|
"github.com/influxdata/influxdb/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
|
2019-01-18 19:03:36 +00:00
|
|
|
LabelHandler *LabelHandler
|
2018-10-02 16:00:29 +00:00
|
|
|
AssetHandler *AssetHandler
|
|
|
|
ChronografHandler *ChronografHandler
|
2019-01-10 17:39:37 +00:00
|
|
|
ScraperHandler *ScraperHandler
|
2018-10-02 16:00:29 +00:00
|
|
|
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
|
2019-01-22 17:16:27 +00:00
|
|
|
SwaggerHandler http.HandlerFunc
|
2018-10-02 16:00:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2019-01-17 13:36:49 +00:00
|
|
|
NewBucketService func(*influxdb.Source) (influxdb.BucketService, error)
|
|
|
|
NewQueryService func(*influxdb.Source) (query.ProxyQueryService, error)
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2018-11-02 18:21:14 +00:00
|
|
|
PointsWriter storage.PointsWriter
|
2019-01-17 13:36:49 +00:00
|
|
|
AuthorizationService influxdb.AuthorizationService
|
|
|
|
BucketService influxdb.BucketService
|
|
|
|
SessionService influxdb.SessionService
|
|
|
|
UserService influxdb.UserService
|
|
|
|
OrganizationService influxdb.OrganizationService
|
|
|
|
UserResourceMappingService influxdb.UserResourceMappingService
|
|
|
|
LabelService influxdb.LabelService
|
|
|
|
DashboardService influxdb.DashboardService
|
|
|
|
DashboardOperationLogService influxdb.DashboardOperationLogService
|
|
|
|
BucketOperationLogService influxdb.BucketOperationLogService
|
|
|
|
UserOperationLogService influxdb.UserOperationLogService
|
|
|
|
OrganizationOperationLogService influxdb.OrganizationOperationLogService
|
|
|
|
SourceService influxdb.SourceService
|
|
|
|
MacroService influxdb.MacroService
|
|
|
|
BasicAuthService influxdb.BasicAuthService
|
|
|
|
OnboardingService influxdb.OnboardingService
|
2018-11-02 18:21:14 +00:00
|
|
|
ProxyQueryService query.ProxyQueryService
|
2019-01-17 13:36:49 +00:00
|
|
|
TaskService influxdb.TaskService
|
|
|
|
TelegrafService influxdb.TelegrafConfigStore
|
|
|
|
ScraperTargetStoreService influxdb.ScraperTargetStoreService
|
|
|
|
SecretService influxdb.SecretService
|
|
|
|
LookupService influxdb.LookupService
|
2018-11-02 18:21:14 +00:00
|
|
|
ChronografService *server.Service
|
2019-01-17 13:36:49 +00:00
|
|
|
ProtoService influxdb.ProtoService
|
2019-01-18 18:48:39 +00:00
|
|
|
OrgLookupService authorizer.OrganizationService
|
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{}
|
2019-01-16 14:00:34 +00:00
|
|
|
|
2018-12-29 03:41:06 +00:00
|
|
|
sessionBackend := NewSessionBackend(b)
|
|
|
|
h.SessionHandler = NewSessionHandler(sessionBackend)
|
2019-01-18 18:48:39 +00:00
|
|
|
b.UserResourceMappingService = authorizer.NewURMService(b.OrgLookupService, b.UserResourceMappingService)
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2019-01-16 03:11:22 +00:00
|
|
|
bucketBackend := NewBucketBackend(b)
|
2019-01-16 03:34:09 +00:00
|
|
|
bucketBackend.BucketService = authorizer.NewBucketService(b.BucketService)
|
2019-01-16 03:11:22 +00:00
|
|
|
h.BucketHandler = NewBucketHandler(bucketBackend)
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2019-01-16 03:34:09 +00:00
|
|
|
orgBackend := NewOrgBackend(b)
|
|
|
|
orgBackend.OrganizationService = authorizer.NewOrgService(b.OrganizationService)
|
|
|
|
h.OrgHandler = NewOrgHandler(orgBackend)
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2019-01-08 03:33:40 +00:00
|
|
|
userBackend := NewUserBackend(b)
|
|
|
|
userBackend.UserService = authorizer.NewUserService(b.UserService)
|
|
|
|
h.UserHandler = NewUserHandler(userBackend)
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2019-01-16 03:54:47 +00:00
|
|
|
dashboardBackend := NewDashboardBackend(b)
|
|
|
|
dashboardBackend.DashboardService = authorizer.NewDashboardService(b.DashboardService)
|
|
|
|
h.DashboardHandler = NewDashboardHandler(dashboardBackend)
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2019-01-16 04:53:55 +00:00
|
|
|
macroBackend := NewMacroBackend(b)
|
|
|
|
macroBackend.MacroService = authorizer.NewMacroService(b.MacroService)
|
|
|
|
h.MacroHandler = NewMacroHandler(macroBackend)
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2019-01-16 12:26:09 +00:00
|
|
|
authorizationBackend := NewAuthorizationBackend(b)
|
|
|
|
authorizationBackend.AuthorizationService = authorizer.NewAuthorizationService(b.AuthorizationService)
|
|
|
|
h.AuthorizationHandler = NewAuthorizationHandler(authorizationBackend)
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2019-01-16 12:44:17 +00:00
|
|
|
scraperBackend := NewScraperBackend(b)
|
|
|
|
scraperBackend.ScraperStorageService = authorizer.NewScraperTargetStoreService(b.ScraperTargetStoreService, b.UserResourceMappingService)
|
|
|
|
h.ScraperHandler = NewScraperHandler(scraperBackend)
|
2019-01-10 17:39:37 +00:00
|
|
|
|
2019-01-16 12:57:33 +00:00
|
|
|
sourceBackend := NewSourceBackend(b)
|
|
|
|
sourceBackend.SourceService = authorizer.NewSourceService(b.SourceService)
|
|
|
|
sourceBackend.NewBucketService = b.NewBucketService
|
|
|
|
sourceBackend.NewQueryService = b.NewQueryService
|
|
|
|
h.SourceHandler = NewSourceHandler(sourceBackend)
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2019-01-16 14:59:34 +00:00
|
|
|
setupBackend := NewSetupBackend(b)
|
|
|
|
h.SetupHandler = NewSetupHandler(setupBackend)
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2019-01-16 15:33:49 +00:00
|
|
|
taskBackend := NewTaskBackend(b)
|
|
|
|
h.TaskHandler = NewTaskHandler(taskBackend)
|
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,
|
2019-01-16 23:56:00 +00:00
|
|
|
authorizer.NewTelegrafConfigService(b.TelegrafService, b.UserResourceMappingService),
|
2018-12-17 12:43:06 +00:00
|
|
|
b.UserService,
|
2019-01-14 17:07:51 +00:00
|
|
|
b.OrganizationService,
|
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)
|
|
|
|
|
2019-01-22 17:16:27 +00:00
|
|
|
h.SwaggerHandler = SwaggerHandler()
|
|
|
|
|
2018-10-02 16:00:29 +00:00
|
|
|
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",
|
|
|
|
},
|
2019-01-18 19:03:36 +00:00
|
|
|
"labels": "/api/v2/labels",
|
2018-12-20 21:25:53 +00:00
|
|
|
"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
|
|
|
},
|
2019-01-18 15:38:28 +00:00
|
|
|
"setup": "/api/v2/setup",
|
|
|
|
"signin": "/api/v2/signin",
|
|
|
|
"signout": "/api/v2/signout",
|
|
|
|
"sources": "/api/v2/sources",
|
|
|
|
"scrapers": "/api/v2/scrapers",
|
2019-01-22 17:16:27 +00:00
|
|
|
"swagger": "/api/v2/swagger.json",
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-18 19:03:36 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/labels") {
|
|
|
|
h.LabelHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-02 16:00:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-18 15:38:28 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/scrapers") {
|
2019-01-10 17:39:37 +00:00
|
|
|
h.ScraperHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-02 16:00:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:16:27 +00:00
|
|
|
if r.URL.Path == "/api/v2/swagger.json" {
|
|
|
|
h.SwaggerHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-25 02:32:29 +00:00
|
|
|
notFoundHandler(w, r)
|
2018-10-02 16:00:29 +00:00
|
|
|
}
|