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
|
2019-02-14 20:32:54 +00:00
|
|
|
VariableHandler *VariableHandler
|
2018-10-02 16:00:29 +00:00
|
|
|
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
|
2019-03-04 17:41:24 +00:00
|
|
|
DocumentHandler *DocumentHandler
|
2018-10-02 16:00:29 +00:00
|
|
|
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 {
|
2019-02-19 23:47:19 +00:00
|
|
|
AssetsPath string // if empty then assets are served from bindata.
|
|
|
|
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
|
2019-02-14 20:32:54 +00:00
|
|
|
VariableService influxdb.VariableService
|
2019-02-19 23:47:19 +00:00
|
|
|
PasswordsService influxdb.PasswordsService
|
2019-01-17 13:36:49 +00:00
|
|
|
OnboardingService influxdb.OnboardingService
|
2019-02-22 17:11:41 +00:00
|
|
|
InfluxQLService query.ProxyQueryService
|
|
|
|
FluxService 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
|
2019-02-05 21:52:15 +00:00
|
|
|
ViewService influxdb.ViewService
|
2019-03-04 17:41:24 +00:00
|
|
|
DocumentService influxdb.DocumentService
|
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
|
|
|
|
2019-02-06 18:39:38 +00:00
|
|
|
internalURM := b.UserResourceMappingService
|
|
|
|
b.UserResourceMappingService = authorizer.NewURMService(b.OrgLookupService, b.UserResourceMappingService)
|
|
|
|
|
2019-03-04 17:41:24 +00:00
|
|
|
documentBackend := NewDocumentBackend(b)
|
|
|
|
h.DocumentHandler = NewDocumentHandler(documentBackend)
|
|
|
|
|
2018-12-29 03:41:06 +00:00
|
|
|
sessionBackend := NewSessionBackend(b)
|
|
|
|
h.SessionHandler = NewSessionHandler(sessionBackend)
|
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-02-14 20:32:54 +00:00
|
|
|
variableBackend := NewVariableBackend(b)
|
|
|
|
variableBackend.VariableService = authorizer.NewVariableService(b.VariableService)
|
|
|
|
h.VariableHandler = NewVariableHandler(variableBackend)
|
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)
|
2019-02-06 18:39:38 +00:00
|
|
|
h.TaskHandler.UserResourceMappingService = internalURM
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2019-01-16 15:48:17 +00:00
|
|
|
telegrafBackend := NewTelegrafBackend(b)
|
|
|
|
telegrafBackend.TelegrafService = authorizer.NewTelegrafConfigService(b.TelegrafService, b.UserResourceMappingService)
|
|
|
|
h.TelegrafHandler = NewTelegrafHandler(telegrafBackend)
|
2018-10-24 15:13:30 +00:00
|
|
|
|
2019-01-16 16:03:38 +00:00
|
|
|
writeBackend := NewWriteBackend(b)
|
|
|
|
h.WriteHandler = NewWriteHandler(writeBackend)
|
2018-10-02 16:00:29 +00:00
|
|
|
|
2019-01-16 16:12:57 +00:00
|
|
|
fluxBackend := NewFluxBackend(b)
|
|
|
|
h.QueryHandler = NewFluxHandler(fluxBackend)
|
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()
|
2019-02-07 22:03:42 +00:00
|
|
|
h.LabelHandler = NewLabelHandler(b.LabelService)
|
|
|
|
|
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-02-14 20:32:54 +00:00
|
|
|
"labels": "/api/v2/labels",
|
|
|
|
"variables": "/api/v2/variables",
|
|
|
|
"me": "/api/v2/me",
|
|
|
|
"orgs": "/api/v2/orgs",
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2019-02-14 20:32:54 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/variables") {
|
|
|
|
h.VariableHandler.ServeHTTP(w, r)
|
2018-10-02 16:00:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-07 21:47:16 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/protos") {
|
|
|
|
h.ProtoHandler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-04 17:41:24 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, "/api/v2/documents") {
|
|
|
|
h.DocumentHandler.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
|
|
|
}
|