2017-10-31 20:41:17 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/influxdata/chronograf"
|
|
|
|
"github.com/influxdata/chronograf/organizations"
|
|
|
|
)
|
|
|
|
|
2017-10-31 21:40:58 +00:00
|
|
|
const organizationKey = "organizationID"
|
|
|
|
|
|
|
|
func hasOrganizationContext(ctx context.Context) (string, bool) {
|
|
|
|
// prevents panic in case of nil context
|
|
|
|
if ctx == nil {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
orgID, ok := ctx.Value(organizationKey).(string)
|
|
|
|
// should never happen
|
|
|
|
if !ok {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
if orgID == "" {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
return orgID, true
|
|
|
|
}
|
|
|
|
|
2017-10-31 20:41:17 +00:00
|
|
|
// TODO: Comment
|
|
|
|
// DataSource is ...
|
|
|
|
// Having this as an interface is useful for testing
|
|
|
|
type DataStore interface {
|
|
|
|
Sources(ctx context.Context) chronograf.SourcesStore
|
|
|
|
Servers(ctx context.Context) chronograf.ServersStore
|
|
|
|
Layouts(ctx context.Context) chronograf.LayoutsStore
|
|
|
|
Users(ctx context.Context) chronograf.UsersStore
|
|
|
|
// TODO: remove
|
|
|
|
RawUsers(ctx context.Context) chronograf.UsersStore
|
|
|
|
Organizations(ctx context.Context) chronograf.OrganizationsStore
|
|
|
|
Dashboards(ctx context.Context) chronograf.DashboardsStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that Store implements a DataStore
|
|
|
|
var _ DataStore = &Store{}
|
|
|
|
|
|
|
|
// Store is a DataStore
|
|
|
|
type Store struct {
|
|
|
|
SourcesStore chronograf.SourcesStore
|
|
|
|
ServersStore chronograf.ServersStore
|
|
|
|
LayoutsStore chronograf.LayoutsStore
|
|
|
|
UsersStore chronograf.UsersStore
|
|
|
|
DashboardsStore chronograf.DashboardsStore
|
|
|
|
OrganizationsStore chronograf.OrganizationsStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) Sources(ctx context.Context) chronograf.SourcesStore {
|
2017-10-31 21:40:58 +00:00
|
|
|
if org, ok := hasOrganizationContext(ctx); ok {
|
|
|
|
return organizations.NewSourcesStore(s.SourcesStore, org)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.SourcesStore
|
2017-10-31 20:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) Servers(ctx context.Context) chronograf.ServersStore {
|
2017-10-31 21:40:58 +00:00
|
|
|
if org, ok := hasOrganizationContext(ctx); ok {
|
|
|
|
return organizations.NewServersStore(s.ServersStore, org)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.ServersStore
|
2017-10-31 20:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) Layouts(ctx context.Context) chronograf.LayoutsStore {
|
2017-10-31 21:40:58 +00:00
|
|
|
if org, ok := hasOrganizationContext(ctx); ok {
|
|
|
|
return organizations.NewLayoutsStore(s.LayoutsStore, org)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.LayoutsStore
|
2017-10-31 20:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) Users(ctx context.Context) chronograf.UsersStore {
|
2017-10-31 21:40:58 +00:00
|
|
|
if org, ok := hasOrganizationContext(ctx); ok {
|
|
|
|
return organizations.NewUsersStore(s.UsersStore, org)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.UsersStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) Dashboards(ctx context.Context) chronograf.DashboardsStore {
|
|
|
|
if org, ok := hasOrganizationContext(ctx); ok {
|
|
|
|
return organizations.NewDashboardsStore(s.DashboardsStore, org)
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.DashboardsStore
|
2017-10-31 20:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove me and put logic into Users Call
|
|
|
|
func (s *Store) RawUsers(ctx context.Context) chronograf.UsersStore {
|
|
|
|
return s.UsersStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) Organizations(ctx context.Context) chronograf.OrganizationsStore {
|
|
|
|
return s.OrganizationsStore
|
|
|
|
}
|