Refactor data stores into a common interface

multitenancy_test_integration_0
Michael Desa 2017-10-31 16:41:17 -04:00
parent d7d69730d7
commit 5f63e2738a
31 changed files with 477 additions and 291 deletions

46
mocks/store.go Normal file
View File

@ -0,0 +1,46 @@
package mocks
import (
"context"
"github.com/influxdata/chronograf"
)
// Store is a server.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 {
return s.SourcesStore
}
func (s *Store) Servers(ctx context.Context) chronograf.ServersStore {
return s.ServersStore
}
func (s *Store) Layouts(ctx context.Context) chronograf.LayoutsStore {
return s.LayoutsStore
}
func (s *Store) Users(ctx context.Context) chronograf.UsersStore {
return s.UsersStore
}
// 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
}
func (s *Store) Dashboards(ctx context.Context) chronograf.DashboardsStore {
return s.DashboardsStore
}

View File

@ -52,8 +52,7 @@ func AuthorizedToken(auth oauth2.Authenticator, logger chronograf.Logger, next h
// name and provider. If the user is found, we verify that the user has at at // name and provider. If the user is found, we verify that the user has at at
// least the role supplied. // least the role supplied.
func AuthorizedUser( func AuthorizedUser(
usersStore chronograf.UsersStore, store DataStore,
organizationsStore chronograf.OrganizationsStore,
useAuth bool, useAuth bool,
role string, role string,
logger chronograf.Logger, logger chronograf.Logger,
@ -98,7 +97,7 @@ func AuthorizedUser(
Error(w, http.StatusUnauthorized, "User is not authorized", logger) Error(w, http.StatusUnauthorized, "User is not authorized", logger)
return return
} }
_, err = organizationsStore.Get(ctx, chronograf.OrganizationQuery{ID: &orgID}) _, err = store.Organizations(ctx).Get(ctx, chronograf.OrganizationQuery{ID: &orgID})
if err != nil { if err != nil {
log.Error("Failed to retrieve organization from organizations store") log.Error("Failed to retrieve organization from organizations store")
Error(w, http.StatusUnauthorized, "User is not authorized", logger) Error(w, http.StatusUnauthorized, "User is not authorized", logger)
@ -106,7 +105,7 @@ func AuthorizedUser(
} }
ctx = context.WithValue(ctx, "organizationID", p.Organization) ctx = context.WithValue(ctx, "organizationID", p.Organization)
u, err := usersStore.Get(ctx, chronograf.UserQuery{ u, err := store.Users(ctx).Get(ctx, chronograf.UserQuery{
Name: &p.Subject, Name: &p.Subject,
Provider: &p.Issuer, Provider: &p.Issuer,
Scheme: &scheme, Scheme: &scheme,

View File

@ -108,7 +108,10 @@ func TestAuthorizedUser(t *testing.T) {
Provider: "google", Provider: "google",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{ Roles: []chronograf.Role{
server.ViewerRole, {
Name: server.ViewerRoleName,
Organization: "1337",
},
}, },
}, nil }, nil
}, },
@ -152,7 +155,10 @@ func TestAuthorizedUser(t *testing.T) {
Provider: "google", Provider: "google",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{ Roles: []chronograf.Role{
server.EditorRole, {
Name: server.EditorRoleName,
Organization: "1337",
},
}, },
}, nil }, nil
}, },
@ -196,7 +202,10 @@ func TestAuthorizedUser(t *testing.T) {
Provider: "google", Provider: "google",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{ Roles: []chronograf.Role{
server.AdminRole, {
Name: server.AdminRoleName,
Organization: "1337",
},
}, },
}, nil }, nil
}, },
@ -240,7 +249,10 @@ func TestAuthorizedUser(t *testing.T) {
Provider: "google", Provider: "google",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{ Roles: []chronograf.Role{
server.ViewerRole, {
Name: server.ViewerRoleName,
Organization: "1337",
},
}, },
}, nil }, nil
}, },
@ -284,7 +296,10 @@ func TestAuthorizedUser(t *testing.T) {
Provider: "google", Provider: "google",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{ Roles: []chronograf.Role{
server.EditorRole, {
Name: server.EditorRoleName,
Organization: "1337",
},
}, },
}, nil }, nil
}, },
@ -328,7 +343,10 @@ func TestAuthorizedUser(t *testing.T) {
Provider: "google", Provider: "google",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{ Roles: []chronograf.Role{
server.AdminRole, {
Name: server.AdminRoleName,
Organization: "1337",
},
}, },
}, nil }, nil
}, },
@ -372,7 +390,10 @@ func TestAuthorizedUser(t *testing.T) {
Provider: "google", Provider: "google",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{ Roles: []chronograf.Role{
server.ViewerRole, {
Name: server.ViewerRoleName,
Organization: "1337",
},
}, },
}, nil }, nil
}, },
@ -416,7 +437,10 @@ func TestAuthorizedUser(t *testing.T) {
Provider: "google", Provider: "google",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{ Roles: []chronograf.Role{
server.EditorRole, {
Name: server.EditorRoleName,
Organization: "1337",
},
}, },
}, nil }, nil
}, },
@ -460,7 +484,10 @@ func TestAuthorizedUser(t *testing.T) {
Provider: "google", Provider: "google",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{ Roles: []chronograf.Role{
server.AdminRole, {
Name: server.AdminRoleName,
Organization: "1337",
},
}, },
}, nil }, nil
}, },
@ -768,7 +795,10 @@ func TestAuthorizedUser(t *testing.T) {
Provider: "google", Provider: "google",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{ Roles: []chronograf.Role{
server.AdminRole, {
Name: server.AdminRoleName,
Organization: "1337",
},
}, },
}, nil }, nil
}, },
@ -808,7 +838,10 @@ func TestAuthorizedUser(t *testing.T) {
Provider: "google", Provider: "google",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{ Roles: []chronograf.Role{
server.AdminRole, {
Name: server.AdminRoleName,
Organization: "1337",
},
}, },
}, nil }, nil
}, },
@ -851,7 +884,10 @@ func TestAuthorizedUser(t *testing.T) {
Provider: "google", Provider: "google",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{ Roles: []chronograf.Role{
server.AdminRole, {
Name: server.AdminRoleName,
Organization: "1337",
},
}, },
}, nil }, nil
}, },
@ -895,7 +931,10 @@ func TestAuthorizedUser(t *testing.T) {
Provider: "google", Provider: "google",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{ Roles: []chronograf.Role{
server.AdminRole, {
Name: server.AdminRoleName,
Organization: "1337",
},
}, },
}, nil }, nil
}, },
@ -946,7 +985,10 @@ func TestAuthorizedUser(t *testing.T) {
Provider: "google", Provider: "google",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{ Roles: []chronograf.Role{
server.AdminRole, {
Name: server.AdminRoleName,
Organization: "1337",
},
}, },
}, nil }, nil
default: default:
@ -988,8 +1030,10 @@ func TestAuthorizedUser(t *testing.T) {
authorized = true authorized = true
} }
fn := server.AuthorizedUser( fn := server.AuthorizedUser(
tt.fields.UsersStore, &server.Store{
tt.fields.OrganizationsStore, UsersStore: tt.fields.UsersStore,
OrganizationsStore: tt.fields.OrganizationsStore,
},
tt.args.useAuth, tt.args.useAuth,
tt.args.role, tt.args.role,
tt.fields.Logger, tt.fields.Logger,

View File

@ -135,7 +135,7 @@ func (s *Service) DashboardCells(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
e, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id)) e, err := s.Store.Dashboards(ctx).Get(ctx, chronograf.DashboardID(id))
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -155,7 +155,7 @@ func (s *Service) NewDashboardCell(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
dash, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id)) dash, err := s.Store.Dashboards(ctx).Get(ctx, chronograf.DashboardID(id))
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -181,7 +181,7 @@ func (s *Service) NewDashboardCell(w http.ResponseWriter, r *http.Request) {
cell.ID = cid cell.ID = cid
dash.Cells = append(dash.Cells, cell) dash.Cells = append(dash.Cells, cell)
if err := s.DashboardsStore.Update(ctx, dash); err != nil { if err := s.Store.Dashboards(ctx).Update(ctx, dash); err != nil {
msg := fmt.Sprintf("Error adding cell %s to dashboard %d: %v", cid, id, err) msg := fmt.Sprintf("Error adding cell %s to dashboard %d: %v", cid, id, err)
Error(w, http.StatusInternalServerError, msg, s.Logger) Error(w, http.StatusInternalServerError, msg, s.Logger)
return return
@ -205,7 +205,7 @@ func (s *Service) DashboardCellID(w http.ResponseWriter, r *http.Request) {
return return
} }
dash, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id)) dash, err := s.Store.Dashboards(ctx).Get(ctx, chronograf.DashboardID(id))
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -231,7 +231,7 @@ func (s *Service) RemoveDashboardCell(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
dash, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id)) dash, err := s.Store.Dashboards(ctx).Get(ctx, chronograf.DashboardID(id))
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -251,7 +251,7 @@ func (s *Service) RemoveDashboardCell(w http.ResponseWriter, r *http.Request) {
} }
dash.Cells = append(dash.Cells[:cellid], dash.Cells[cellid+1:]...) dash.Cells = append(dash.Cells[:cellid], dash.Cells[cellid+1:]...)
if err := s.DashboardsStore.Update(ctx, dash); err != nil { if err := s.Store.Dashboards(ctx).Update(ctx, dash); err != nil {
msg := fmt.Sprintf("Error removing cell %s from dashboard %d: %v", cid, id, err) msg := fmt.Sprintf("Error removing cell %s from dashboard %d: %v", cid, id, err)
Error(w, http.StatusInternalServerError, msg, s.Logger) Error(w, http.StatusInternalServerError, msg, s.Logger)
return return
@ -268,7 +268,7 @@ func (s *Service) ReplaceDashboardCell(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
dash, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id)) dash, err := s.Store.Dashboards(ctx).Get(ctx, chronograf.DashboardID(id))
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -300,7 +300,7 @@ func (s *Service) ReplaceDashboardCell(w http.ResponseWriter, r *http.Request) {
cell.ID = cid cell.ID = cid
dash.Cells[cellid] = cell dash.Cells[cellid] = cell
if err := s.DashboardsStore.Update(ctx, dash); err != nil { if err := s.Store.Dashboards(ctx).Update(ctx, dash); err != nil {
msg := fmt.Sprintf("Error updating cell %s in dashboard %d: %v", cid, id, err) msg := fmt.Sprintf("Error updating cell %s in dashboard %d: %v", cid, id, err)
Error(w, http.StatusInternalServerError, msg, s.Logger) Error(w, http.StatusInternalServerError, msg, s.Logger)
return return

View File

@ -228,6 +228,7 @@ func Test_Service_DashboardCells(t *testing.T) {
// setup mock DashboardCells store and logger // setup mock DashboardCells store and logger
tlog := &mocks.TestLogger{} tlog := &mocks.TestLogger{}
svc := &server.Service{ svc := &server.Service{
Store: &mocks.Store{
DashboardsStore: &mocks.DashboardsStore{ DashboardsStore: &mocks.DashboardsStore{
GetF: func(ctx context.Context, id chronograf.DashboardID) (chronograf.Dashboard, error) { GetF: func(ctx context.Context, id chronograf.DashboardID) (chronograf.Dashboard, error) {
return chronograf.Dashboard{ return chronograf.Dashboard{
@ -238,6 +239,7 @@ func Test_Service_DashboardCells(t *testing.T) {
}, nil }, nil
}, },
}, },
},
Logger: tlog, Logger: tlog,
} }

View File

@ -49,7 +49,7 @@ func newDashboardResponse(d chronograf.Dashboard) *dashboardResponse {
// Dashboards returns all dashboards within the store // Dashboards returns all dashboards within the store
func (s *Service) Dashboards(w http.ResponseWriter, r *http.Request) { func (s *Service) Dashboards(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
dashboards, err := s.DashboardsStore.All(ctx) dashboards, err := s.Store.Dashboards(ctx).All(ctx)
if err != nil { if err != nil {
Error(w, http.StatusInternalServerError, "Error loading dashboards", s.Logger) Error(w, http.StatusInternalServerError, "Error loading dashboards", s.Logger)
return return
@ -74,7 +74,7 @@ func (s *Service) DashboardID(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
e, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id)) e, err := s.Store.Dashboards(ctx).Get(ctx, chronograf.DashboardID(id))
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -98,7 +98,8 @@ func (s *Service) NewDashboard(w http.ResponseWriter, r *http.Request) {
} }
var err error var err error
if dashboard, err = s.DashboardsStore.Add(r.Context(), dashboard); err != nil { ctx := r.Context()
if dashboard, err = s.Store.Dashboards(ctx).Add(r.Context(), dashboard); err != nil {
msg := fmt.Errorf("Error storing dashboard %v: %v", dashboard, err) msg := fmt.Errorf("Error storing dashboard %v: %v", dashboard, err)
unknownErrorWithMessage(w, msg, s.Logger) unknownErrorWithMessage(w, msg, s.Logger)
return return
@ -118,13 +119,13 @@ func (s *Service) RemoveDashboard(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
e, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id)) e, err := s.Store.Dashboards(ctx).Get(ctx, chronograf.DashboardID(id))
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
} }
if err := s.DashboardsStore.Delete(ctx, e); err != nil { if err := s.Store.Dashboards(ctx).Delete(ctx, e); err != nil {
unknownErrorWithMessage(w, err, s.Logger) unknownErrorWithMessage(w, err, s.Logger)
return return
} }
@ -141,7 +142,7 @@ func (s *Service) ReplaceDashboard(w http.ResponseWriter, r *http.Request) {
} }
id := chronograf.DashboardID(idParam) id := chronograf.DashboardID(idParam)
_, err = s.DashboardsStore.Get(ctx, id) _, err = s.Store.Dashboards(ctx).Get(ctx, id)
if err != nil { if err != nil {
Error(w, http.StatusNotFound, fmt.Sprintf("ID %d not found", id), s.Logger) Error(w, http.StatusNotFound, fmt.Sprintf("ID %d not found", id), s.Logger)
return return
@ -159,7 +160,7 @@ func (s *Service) ReplaceDashboard(w http.ResponseWriter, r *http.Request) {
return return
} }
if err := s.DashboardsStore.Update(ctx, req); err != nil { if err := s.Store.Dashboards(ctx).Update(ctx, req); err != nil {
msg := fmt.Sprintf("Error updating dashboard ID %d: %v", id, err) msg := fmt.Sprintf("Error updating dashboard ID %d: %v", id, err)
Error(w, http.StatusInternalServerError, msg, s.Logger) Error(w, http.StatusInternalServerError, msg, s.Logger)
return return
@ -180,7 +181,7 @@ func (s *Service) UpdateDashboard(w http.ResponseWriter, r *http.Request) {
} }
id := chronograf.DashboardID(idParam) id := chronograf.DashboardID(idParam)
orig, err := s.DashboardsStore.Get(ctx, id) orig, err := s.Store.Dashboards(ctx).Get(ctx, id)
if err != nil { if err != nil {
Error(w, http.StatusNotFound, fmt.Sprintf("ID %d not found", id), s.Logger) Error(w, http.StatusNotFound, fmt.Sprintf("ID %d not found", id), s.Logger)
return return
@ -206,7 +207,7 @@ func (s *Service) UpdateDashboard(w http.ResponseWriter, r *http.Request) {
return return
} }
if err := s.DashboardsStore.Update(ctx, orig); err != nil { if err := s.Store.Dashboards(ctx).Update(ctx, orig); err != nil {
msg := fmt.Sprintf("Error updating dashboard ID %d: %v", id, err) msg := fmt.Sprintf("Error updating dashboard ID %d: %v", id, err)
Error(w, http.StatusInternalServerError, msg, s.Logger) Error(w, http.StatusInternalServerError, msg, s.Logger)
return return

View File

@ -76,7 +76,7 @@ func (h *Service) GetDatabases(w http.ResponseWriter, r *http.Request) {
return return
} }
src, err := h.SourcesStore.Get(ctx, srcID) src, err := h.Store.Sources(ctx).Get(ctx, srcID)
if err != nil { if err != nil {
notFound(w, srcID, h.Logger) notFound(w, srcID, h.Logger)
return return
@ -122,7 +122,7 @@ func (h *Service) NewDatabase(w http.ResponseWriter, r *http.Request) {
return return
} }
src, err := h.SourcesStore.Get(ctx, srcID) src, err := h.Store.Sources(ctx).Get(ctx, srcID)
if err != nil { if err != nil {
notFound(w, srcID, h.Logger) notFound(w, srcID, h.Logger)
return return
@ -172,7 +172,7 @@ func (h *Service) DropDatabase(w http.ResponseWriter, r *http.Request) {
return return
} }
src, err := h.SourcesStore.Get(ctx, srcID) src, err := h.Store.Sources(ctx).Get(ctx, srcID)
if err != nil { if err != nil {
notFound(w, srcID, h.Logger) notFound(w, srcID, h.Logger)
return return
@ -207,7 +207,7 @@ func (h *Service) RetentionPolicies(w http.ResponseWriter, r *http.Request) {
return return
} }
src, err := h.SourcesStore.Get(ctx, srcID) src, err := h.Store.Sources(ctx).Get(ctx, srcID)
if err != nil { if err != nil {
notFound(w, srcID, h.Logger) notFound(w, srcID, h.Logger)
return return
@ -261,7 +261,7 @@ func (h *Service) NewRetentionPolicy(w http.ResponseWriter, r *http.Request) {
return return
} }
src, err := h.SourcesStore.Get(ctx, srcID) src, err := h.Store.Sources(ctx).Get(ctx, srcID)
if err != nil { if err != nil {
notFound(w, srcID, h.Logger) notFound(w, srcID, h.Logger)
return return
@ -311,7 +311,7 @@ func (h *Service) UpdateRetentionPolicy(w http.ResponseWriter, r *http.Request)
return return
} }
src, err := h.SourcesStore.Get(ctx, srcID) src, err := h.Store.Sources(ctx).Get(ctx, srcID)
if err != nil { if err != nil {
notFound(w, srcID, h.Logger) notFound(w, srcID, h.Logger)
return return
@ -364,7 +364,7 @@ func (s *Service) DropRetentionPolicy(w http.ResponseWriter, r *http.Request) {
return return
} }
src, err := s.SourcesStore.Get(ctx, srcID) src, err := s.Store.Sources(ctx).Get(ctx, srcID)
if err != nil { if err != nil {
notFound(w, srcID, s.Logger) notFound(w, srcID, s.Logger)
return return

View File

@ -33,11 +33,13 @@ func TestService_GetDatabases(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
h := &Service{ h := &Service{
Store: &Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
ServersStore: tt.fields.ServersStore, ServersStore: tt.fields.ServersStore,
LayoutsStore: tt.fields.LayoutsStore, LayoutsStore: tt.fields.LayoutsStore,
UsersStore: tt.fields.UsersStore, UsersStore: tt.fields.UsersStore,
DashboardsStore: tt.fields.DashboardsStore, DashboardsStore: tt.fields.DashboardsStore,
},
TimeSeriesClient: tt.fields.TimeSeriesClient, TimeSeriesClient: tt.fields.TimeSeriesClient,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,
@ -74,11 +76,13 @@ func TestService_NewDatabase(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
h := &Service{ h := &Service{
Store: &Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
ServersStore: tt.fields.ServersStore, ServersStore: tt.fields.ServersStore,
LayoutsStore: tt.fields.LayoutsStore, LayoutsStore: tt.fields.LayoutsStore,
UsersStore: tt.fields.UsersStore, UsersStore: tt.fields.UsersStore,
DashboardsStore: tt.fields.DashboardsStore, DashboardsStore: tt.fields.DashboardsStore,
},
TimeSeriesClient: tt.fields.TimeSeriesClient, TimeSeriesClient: tt.fields.TimeSeriesClient,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,
@ -115,11 +119,13 @@ func TestService_DropDatabase(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
h := &Service{ h := &Service{
Store: &Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
ServersStore: tt.fields.ServersStore, ServersStore: tt.fields.ServersStore,
LayoutsStore: tt.fields.LayoutsStore, LayoutsStore: tt.fields.LayoutsStore,
UsersStore: tt.fields.UsersStore, UsersStore: tt.fields.UsersStore,
DashboardsStore: tt.fields.DashboardsStore, DashboardsStore: tt.fields.DashboardsStore,
},
TimeSeriesClient: tt.fields.TimeSeriesClient, TimeSeriesClient: tt.fields.TimeSeriesClient,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,
@ -156,11 +162,13 @@ func TestService_RetentionPolicies(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
h := &Service{ h := &Service{
Store: &Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
ServersStore: tt.fields.ServersStore, ServersStore: tt.fields.ServersStore,
LayoutsStore: tt.fields.LayoutsStore, LayoutsStore: tt.fields.LayoutsStore,
UsersStore: tt.fields.UsersStore, UsersStore: tt.fields.UsersStore,
DashboardsStore: tt.fields.DashboardsStore, DashboardsStore: tt.fields.DashboardsStore,
},
TimeSeriesClient: tt.fields.TimeSeriesClient, TimeSeriesClient: tt.fields.TimeSeriesClient,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,
@ -197,11 +205,13 @@ func TestService_NewRetentionPolicy(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
h := &Service{ h := &Service{
Store: &Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
ServersStore: tt.fields.ServersStore, ServersStore: tt.fields.ServersStore,
LayoutsStore: tt.fields.LayoutsStore, LayoutsStore: tt.fields.LayoutsStore,
UsersStore: tt.fields.UsersStore, UsersStore: tt.fields.UsersStore,
DashboardsStore: tt.fields.DashboardsStore, DashboardsStore: tt.fields.DashboardsStore,
},
TimeSeriesClient: tt.fields.TimeSeriesClient, TimeSeriesClient: tt.fields.TimeSeriesClient,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,
@ -238,11 +248,13 @@ func TestService_UpdateRetentionPolicy(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
h := &Service{ h := &Service{
Store: &Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
ServersStore: tt.fields.ServersStore, ServersStore: tt.fields.ServersStore,
LayoutsStore: tt.fields.LayoutsStore, LayoutsStore: tt.fields.LayoutsStore,
UsersStore: tt.fields.UsersStore, UsersStore: tt.fields.UsersStore,
DashboardsStore: tt.fields.DashboardsStore, DashboardsStore: tt.fields.DashboardsStore,
},
TimeSeriesClient: tt.fields.TimeSeriesClient, TimeSeriesClient: tt.fields.TimeSeriesClient,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,
@ -279,11 +291,13 @@ func TestService_DropRetentionPolicy(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
h := &Service{ h := &Service{
Store: &Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
ServersStore: tt.fields.ServersStore, ServersStore: tt.fields.ServersStore,
LayoutsStore: tt.fields.LayoutsStore, LayoutsStore: tt.fields.LayoutsStore,
UsersStore: tt.fields.UsersStore, UsersStore: tt.fields.UsersStore,
DashboardsStore: tt.fields.DashboardsStore, DashboardsStore: tt.fields.DashboardsStore,
},
TimeSeriesClient: tt.fields.TimeSeriesClient, TimeSeriesClient: tt.fields.TimeSeriesClient,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,

View File

@ -42,7 +42,7 @@ func (s *Service) Influx(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
src, err := s.SourcesStore.Get(ctx, id) src, err := s.Store.Sources(ctx).Get(ctx, id)
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -87,7 +87,7 @@ func (s *Service) Write(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
src, err := s.SourcesStore.Get(ctx, id) src, err := s.Store.Sources(ctx).Get(ctx, id)
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return

View File

@ -67,7 +67,7 @@ func (s *Service) NewKapacitor(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
_, err = s.SourcesStore.Get(ctx, srcID) _, err = s.Store.Sources(ctx).Get(ctx, srcID)
if err != nil { if err != nil {
notFound(w, srcID, s.Logger) notFound(w, srcID, s.Logger)
return return
@ -93,7 +93,7 @@ func (s *Service) NewKapacitor(w http.ResponseWriter, r *http.Request) {
Organization: req.Organization, Organization: req.Organization,
} }
if srv, err = s.ServersStore.Add(ctx, srv); err != nil { if srv, err = s.Store.Servers(ctx).Add(ctx, srv); err != nil {
msg := fmt.Errorf("Error storing kapacitor %v: %v", req, err) msg := fmt.Errorf("Error storing kapacitor %v: %v", req, err)
unknownErrorWithMessage(w, msg, s.Logger) unknownErrorWithMessage(w, msg, s.Logger)
return return
@ -135,7 +135,7 @@ func (s *Service) Kapacitors(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
mrSrvs, err := s.ServersStore.All(ctx) mrSrvs, err := s.Store.Servers(ctx).All(ctx)
if err != nil { if err != nil {
Error(w, http.StatusInternalServerError, "Error loading kapacitors", s.Logger) Error(w, http.StatusInternalServerError, "Error loading kapacitors", s.Logger)
return return
@ -170,7 +170,7 @@ func (s *Service) KapacitorsID(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
srv, err := s.ServersStore.Get(ctx, id) srv, err := s.Store.Servers(ctx).Get(ctx, id)
if err != nil || srv.SrcID != srcID { if err != nil || srv.SrcID != srcID {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -195,13 +195,13 @@ func (s *Service) RemoveKapacitor(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
srv, err := s.ServersStore.Get(ctx, id) srv, err := s.Store.Servers(ctx).Get(ctx, id)
if err != nil || srv.SrcID != srcID { if err != nil || srv.SrcID != srcID {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
} }
if err = s.ServersStore.Delete(ctx, srv); err != nil { if err = s.Store.Servers(ctx).Delete(ctx, srv); err != nil {
unknownErrorWithMessage(w, err, s.Logger) unknownErrorWithMessage(w, err, s.Logger)
return return
} }
@ -245,7 +245,7 @@ func (s *Service) UpdateKapacitor(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
srv, err := s.ServersStore.Get(ctx, id) srv, err := s.Store.Servers(ctx).Get(ctx, id)
if err != nil || srv.SrcID != srcID { if err != nil || srv.SrcID != srcID {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -278,7 +278,7 @@ func (s *Service) UpdateKapacitor(w http.ResponseWriter, r *http.Request) {
srv.Active = *req.Active srv.Active = *req.Active
} }
if err := s.ServersStore.Update(ctx, srv); err != nil { if err := s.Store.Servers(ctx).Update(ctx, srv); err != nil {
msg := fmt.Sprintf("Error updating kapacitor ID %d", id) msg := fmt.Sprintf("Error updating kapacitor ID %d", id)
Error(w, http.StatusInternalServerError, msg, s.Logger) Error(w, http.StatusInternalServerError, msg, s.Logger)
return return
@ -303,7 +303,7 @@ func (s *Service) KapacitorRulesPost(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
srv, err := s.ServersStore.Get(ctx, id) srv, err := s.Store.Servers(ctx).Get(ctx, id)
if err != nil || srv.SrcID != srcID { if err != nil || srv.SrcID != srcID {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -432,7 +432,7 @@ func (s *Service) KapacitorRulesPut(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
srv, err := s.ServersStore.Get(ctx, id) srv, err := s.Store.Servers(ctx).Get(ctx, id)
if err != nil || srv.SrcID != srcID { if err != nil || srv.SrcID != srcID {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -502,7 +502,7 @@ func (s *Service) KapacitorRulesStatus(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
srv, err := s.ServersStore.Get(ctx, id) srv, err := s.Store.Servers(ctx).Get(ctx, id)
if err != nil || srv.SrcID != srcID { if err != nil || srv.SrcID != srcID {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -563,7 +563,7 @@ func (s *Service) KapacitorRulesGet(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
srv, err := s.ServersStore.Get(ctx, id) srv, err := s.Store.Servers(ctx).Get(ctx, id)
if err != nil || srv.SrcID != srcID { if err != nil || srv.SrcID != srcID {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -605,7 +605,7 @@ func (s *Service) KapacitorRulesID(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
srv, err := s.ServersStore.Get(ctx, id) srv, err := s.Store.Servers(ctx).Get(ctx, id)
if err != nil || srv.SrcID != srcID { if err != nil || srv.SrcID != srcID {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -644,7 +644,7 @@ func (s *Service) KapacitorRulesDelete(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
srv, err := s.ServersStore.Get(ctx, id) srv, err := s.Store.Servers(ctx).Get(ctx, id)
if err != nil || srv.SrcID != srcID { if err != nil || srv.SrcID != srcID {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return

View File

@ -186,6 +186,7 @@ func Test_KapacitorRulesGet(t *testing.T) {
// setup mock service and test logger // setup mock service and test logger
testLogger := mocks.TestLogger{} testLogger := mocks.TestLogger{}
svc := &server.Service{ svc := &server.Service{
Store: &mocks.Store{
ServersStore: &mocks.ServersStore{ ServersStore: &mocks.ServersStore{
GetF: func(ctx context.Context, ID int) (chronograf.Server, error) { GetF: func(ctx context.Context, ID int) (chronograf.Server, error) {
return chronograf.Server{ return chronograf.Server{
@ -194,6 +195,7 @@ func Test_KapacitorRulesGet(t *testing.T) {
}, nil }, nil
}, },
}, },
},
Logger: &testLogger, Logger: &testLogger,
} }

View File

@ -63,7 +63,8 @@ func (s *Service) NewLayout(w http.ResponseWriter, r *http.Request) {
} }
var err error var err error
if layout, err = s.LayoutsStore.Add(r.Context(), layout); err != nil { ctx := r.Context()
if layout, err = s.Store.Layouts(ctx).Add(r.Context(), layout); err != nil {
msg := fmt.Errorf("Error storing layout %v: %v", layout, err) msg := fmt.Errorf("Error storing layout %v: %v", layout, err)
unknownErrorWithMessage(w, msg, s.Logger) unknownErrorWithMessage(w, msg, s.Logger)
return return
@ -91,7 +92,7 @@ func (s *Service) Layouts(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
layouts, err := s.LayoutsStore.All(ctx) layouts, err := s.Store.Layouts(ctx).All(ctx)
if err != nil { if err != nil {
Error(w, http.StatusInternalServerError, "Error loading layouts", s.Logger) Error(w, http.StatusInternalServerError, "Error loading layouts", s.Logger)
return return
@ -123,7 +124,7 @@ func (s *Service) LayoutsID(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
id := httprouter.GetParamFromContext(ctx, "id") id := httprouter.GetParamFromContext(ctx, "id")
layout, err := s.LayoutsStore.Get(ctx, id) layout, err := s.Store.Layouts(ctx).Get(ctx, id)
if err != nil { if err != nil {
Error(w, http.StatusNotFound, fmt.Sprintf("ID %s not found", id), s.Logger) Error(w, http.StatusNotFound, fmt.Sprintf("ID %s not found", id), s.Logger)
return return
@ -142,7 +143,7 @@ func (s *Service) RemoveLayout(w http.ResponseWriter, r *http.Request) {
ID: id, ID: id,
} }
if err := s.LayoutsStore.Delete(ctx, layout); err != nil { if err := s.Store.Layouts(ctx).Delete(ctx, layout); err != nil {
unknownErrorWithMessage(w, err, s.Logger) unknownErrorWithMessage(w, err, s.Logger)
return return
} }
@ -155,7 +156,7 @@ func (s *Service) UpdateLayout(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
id := httprouter.GetParamFromContext(ctx, "id") id := httprouter.GetParamFromContext(ctx, "id")
_, err := s.LayoutsStore.Get(ctx, id) _, err := s.Store.Layouts(ctx).Get(ctx, id)
if err != nil { if err != nil {
Error(w, http.StatusNotFound, fmt.Sprintf("ID %s not found", id), s.Logger) Error(w, http.StatusNotFound, fmt.Sprintf("ID %s not found", id), s.Logger)
return return
@ -173,7 +174,7 @@ func (s *Service) UpdateLayout(w http.ResponseWriter, r *http.Request) {
return return
} }
if err := s.LayoutsStore.Update(ctx, req); err != nil { if err := s.Store.Layouts(ctx).Update(ctx, req); err != nil {
msg := fmt.Sprintf("Error updating layout ID %s: %v", id, err) msg := fmt.Sprintf("Error updating layout ID %s: %v", id, err)
Error(w, http.StatusInternalServerError, msg, s.Logger) Error(w, http.StatusInternalServerError, msg, s.Logger)
return return

View File

@ -126,7 +126,7 @@ func Test_Layouts(t *testing.T) {
// setup mock chronograf.Service and mock logger // setup mock chronograf.Service and mock logger
lg := &mocks.TestLogger{} lg := &mocks.TestLogger{}
svc := server.Service{ svc := server.Service{
LayoutsStore: &mocks.LayoutsStore{ Store: &mocks.Store{LayoutsStore: &mocks.LayoutsStore{
AllF: func(ctx context.Context) ([]chronograf.Layout, error) { AllF: func(ctx context.Context) ([]chronograf.Layout, error) {
if len(test.allLayouts) == 0 { if len(test.allLayouts) == 0 {
return []chronograf.Layout{ return []chronograf.Layout{
@ -137,6 +137,7 @@ func Test_Layouts(t *testing.T) {
} }
}, },
}, },
},
Logger: lg, Logger: lg,
} }

View File

@ -14,7 +14,7 @@ type mapping struct {
// GetMappings returns the known mappings of measurements to applications // GetMappings returns the known mappings of measurements to applications
func (s *Service) GetMappings(w http.ResponseWriter, r *http.Request) { func (s *Service) GetMappings(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
layouts, err := s.LayoutsStore.All(ctx) layouts, err := s.Store.Layouts(ctx).All(ctx)
if err != nil { if err != nil {
Error(w, http.StatusInternalServerError, "Error loading layouts", s.Logger) Error(w, http.StatusInternalServerError, "Error loading layouts", s.Logger)
return return

View File

@ -119,7 +119,7 @@ func (s *Service) MeOrganization(auth oauth2.Authenticator) func(http.ResponseWr
Error(w, http.StatusInternalServerError, err.Error(), s.Logger) Error(w, http.StatusInternalServerError, err.Error(), s.Logger)
return return
} }
_, err = s.OrganizationsStore.Get(ctx, chronograf.OrganizationQuery{ID: &orgID}) _, err = s.Store.Organizations(ctx).Get(ctx, chronograf.OrganizationQuery{ID: &orgID})
if err != nil { if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger) Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return return
@ -137,7 +137,7 @@ func (s *Service) MeOrganization(auth oauth2.Authenticator) func(http.ResponseWr
} }
// validate that user belongs to organization // validate that user belongs to organization
ctx = context.WithValue(ctx, "organizationID", req.OrganizationID) ctx = context.WithValue(ctx, "organizationID", req.OrganizationID)
_, err = s.OrganizationUsersStore.Get(ctx, chronograf.UserQuery{ _, err = s.Store.Users(ctx).Get(ctx, chronograf.UserQuery{
Name: &p.Subject, Name: &p.Subject,
Provider: &p.Issuer, Provider: &p.Issuer,
Scheme: &scheme, Scheme: &scheme,
@ -188,7 +188,8 @@ func (s *Service) Me(w http.ResponseWriter, r *http.Request) {
// TODO: add real implementation // TODO: add real implementation
ctx = context.WithValue(ctx, "organizationID", p.Organization) ctx = context.WithValue(ctx, "organizationID", p.Organization)
usr, err := s.UsersStore.Get(ctx, chronograf.UserQuery{ // TODO: Change RawUsers to Users
usr, err := s.Store.RawUsers(ctx).Get(ctx, chronograf.UserQuery{
Name: &p.Subject, Name: &p.Subject,
Provider: &p.Issuer, Provider: &p.Issuer,
Scheme: &scheme, Scheme: &scheme,
@ -215,7 +216,7 @@ func (s *Service) Me(w http.ResponseWriter, r *http.Request) {
Scheme: scheme, Scheme: scheme,
} }
newUser, err := s.UsersStore.Add(ctx, user) newUser, err := s.Store.RawUsers(ctx).Add(ctx, user)
if err != nil { if err != nil {
msg := fmt.Errorf("error storing user %s: %v", user.Name, err) msg := fmt.Errorf("error storing user %s: %v", user.Name, err)
unknownErrorWithMessage(w, msg, s.Logger) unknownErrorWithMessage(w, msg, s.Logger)

View File

@ -159,7 +159,9 @@ func TestService_Me(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
tt.args.r = tt.args.r.WithContext(context.WithValue(context.Background(), oauth2.PrincipalKey, tt.principal)) tt.args.r = tt.args.r.WithContext(context.WithValue(context.Background(), oauth2.PrincipalKey, tt.principal))
s := &Service{ s := &Service{
Store: &mocks.Store{
UsersStore: tt.fields.UsersStore, UsersStore: tt.fields.UsersStore,
},
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,
} }
@ -186,7 +188,6 @@ func TestService_MeOrganizations(t *testing.T) {
type fields struct { type fields struct {
UsersStore chronograf.UsersStore UsersStore chronograf.UsersStore
OrganizationsStore chronograf.OrganizationsStore OrganizationsStore chronograf.OrganizationsStore
OrganizationUsersStore chronograf.UsersStore
Logger chronograf.Logger Logger chronograf.Logger
UseAuth bool UseAuth bool
} }
@ -227,6 +228,12 @@ func TestService_MeOrganizations(t *testing.T) {
Name: "me", Name: "me",
Provider: "github", Provider: "github",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{
{
Name: AdminRoleName,
Organization: "1337",
},
},
}, nil }, nil
}, },
}, },
@ -241,25 +248,6 @@ func TestService_MeOrganizations(t *testing.T) {
}, nil }, nil
}, },
}, },
OrganizationUsersStore: &mocks.UsersStore{
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
orgID, ok := ctx.Value("organizationID").(string)
if !ok {
return nil, fmt.Errorf("expected organization key to be a string")
}
if orgID == "" {
return nil, fmt.Errorf("expected organization key to be set")
}
if q.Name == nil || q.Provider == nil || q.Scheme == nil {
return nil, fmt.Errorf("Invalid user query: missing Name, Provider, and/or Scheme")
}
return &chronograf.User{
Name: "me",
Provider: "github",
Scheme: "oauth2",
}, nil
},
},
}, },
principal: oauth2.Principal{ principal: oauth2.Principal{
Subject: "me", Subject: "me",
@ -267,7 +255,7 @@ func TestService_MeOrganizations(t *testing.T) {
}, },
wantStatus: http.StatusOK, wantStatus: http.StatusOK,
wantContentType: "application/json", wantContentType: "application/json",
wantBody: `{"name":"me","provider":"github","scheme":"oauth2","currentOrganization":"1337","links":{"self":"/chronograf/v1/users/me"}}`, wantBody: `{"name":"me","roles":[{"name":"admin","organization":"\"1337\""}],"provider":"github","scheme":"oauth2","currentOrganization":"1337","links":{"self":"/chronograf/v1/users/me"}}`,
}, },
{ {
name: "Change the current User's organization", name: "Change the current User's organization",
@ -291,6 +279,12 @@ func TestService_MeOrganizations(t *testing.T) {
Name: "me", Name: "me",
Provider: "github", Provider: "github",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{
{
Name: AdminRoleName,
Organization: "1337",
},
},
}, nil }, nil
}, },
}, },
@ -305,25 +299,6 @@ func TestService_MeOrganizations(t *testing.T) {
}, nil }, nil
}, },
}, },
OrganizationUsersStore: &mocks.UsersStore{
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
orgID, ok := ctx.Value("organizationID").(string)
if !ok {
return nil, fmt.Errorf("expected organization key to be a string")
}
if orgID == "" {
return nil, fmt.Errorf("expected organization key to be set")
}
if q.Name == nil || q.Provider == nil || q.Scheme == nil {
return nil, fmt.Errorf("Invalid user query: missing Name, Provider, and/or Scheme")
}
return &chronograf.User{
Name: "me",
Provider: "github",
Scheme: "oauth2",
}, nil
},
},
}, },
principal: oauth2.Principal{ principal: oauth2.Principal{
Subject: "me", Subject: "me",
@ -332,7 +307,7 @@ func TestService_MeOrganizations(t *testing.T) {
}, },
wantStatus: http.StatusOK, wantStatus: http.StatusOK,
wantContentType: "application/json", wantContentType: "application/json",
wantBody: `{"name":"me","provider":"github","scheme":"oauth2","currentOrganization":"1337","links":{"self":"/chronograf/v1/users/me"}}`, wantBody: `{"name":"me","roles":[{"name":"admin","organization":"\"1337\""}],"provider":"github","scheme":"oauth2","currentOrganization":"1337","links":{"self":"/chronograf/v1/users/me"}}`,
}, },
{ {
name: "Unable to find requested user in valid organization", name: "Unable to find requested user in valid organization",
@ -352,11 +327,7 @@ func TestService_MeOrganizations(t *testing.T) {
if q.Name == nil || q.Provider == nil || q.Scheme == nil { if q.Name == nil || q.Provider == nil || q.Scheme == nil {
return nil, fmt.Errorf("Invalid user query: missing Name, Provider, and/or Scheme") return nil, fmt.Errorf("Invalid user query: missing Name, Provider, and/or Scheme")
} }
return &chronograf.User{ return nil, chronograf.ErrUserNotFound
Name: "me",
Provider: "github",
Scheme: "oauth2",
}, nil
}, },
}, },
OrganizationsStore: &mocks.OrganizationsStore{ OrganizationsStore: &mocks.OrganizationsStore{
@ -370,11 +341,6 @@ func TestService_MeOrganizations(t *testing.T) {
}, nil }, nil
}, },
}, },
OrganizationUsersStore: &mocks.UsersStore{
GetF: func(ctx context.Context, q chronograf.UserQuery) (*chronograf.User, error) {
return nil, chronograf.ErrUserNotFound
},
},
}, },
principal: oauth2.Principal{ principal: oauth2.Principal{
Subject: "me", Subject: "me",
@ -407,6 +373,12 @@ func TestService_MeOrganizations(t *testing.T) {
Name: "me", Name: "me",
Provider: "github", Provider: "github",
Scheme: "oauth2", Scheme: "oauth2",
Roles: []chronograf.Role{
{
Name: AdminRoleName,
Organization: "1337",
},
},
}, nil }, nil
}, },
}, },
@ -429,9 +401,12 @@ func TestService_MeOrganizations(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
tt.args.r = tt.args.r.WithContext(context.WithValue(context.Background(), oauth2.PrincipalKey, tt.principal)) tt.args.r = tt.args.r.WithContext(context.WithValue(context.Background(), oauth2.PrincipalKey, tt.principal))
s := &Service{ s := &Service{
Store: &Store{
UsersStore: tt.fields.UsersStore, UsersStore: tt.fields.UsersStore,
OrganizationsStore: tt.fields.OrganizationsStore, OrganizationsStore: tt.fields.OrganizationsStore,
OrganizationUsersStore: tt.fields.OrganizationUsersStore, },
//UsersStore: tt.fields.UsersStore,
//OrganizationUsersStore: tt.fields.OrganizationUsersStore,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,
} }

View File

@ -69,8 +69,7 @@ func NewMux(opts MuxOpts, service Service) http.Handler {
EnsureViewer := func(next http.HandlerFunc) http.HandlerFunc { EnsureViewer := func(next http.HandlerFunc) http.HandlerFunc {
return AuthorizedUser( return AuthorizedUser(
service.OrganizationUsersStore, service.Store,
service.OrganizationsStore,
opts.UseAuth, opts.UseAuth,
ViewerRoleName, ViewerRoleName,
opts.Logger, opts.Logger,
@ -79,8 +78,7 @@ func NewMux(opts MuxOpts, service Service) http.Handler {
} }
EnsureEditor := func(next http.HandlerFunc) http.HandlerFunc { EnsureEditor := func(next http.HandlerFunc) http.HandlerFunc {
return AuthorizedUser( return AuthorizedUser(
service.OrganizationUsersStore, service.Store,
service.OrganizationsStore,
opts.UseAuth, opts.UseAuth,
EditorRoleName, EditorRoleName,
opts.Logger, opts.Logger,
@ -89,8 +87,7 @@ func NewMux(opts MuxOpts, service Service) http.Handler {
} }
EnsureAdmin := func(next http.HandlerFunc) http.HandlerFunc { EnsureAdmin := func(next http.HandlerFunc) http.HandlerFunc {
return AuthorizedUser( return AuthorizedUser(
service.OrganizationUsersStore, service.Store,
service.OrganizationsStore,
opts.UseAuth, opts.UseAuth,
AdminRoleName, AdminRoleName,
opts.Logger, opts.Logger,

View File

@ -66,7 +66,7 @@ func newOrganizationsResponse(orgs []chronograf.Organization) *organizationsResp
func (s *Service) Organizations(w http.ResponseWriter, r *http.Request) { func (s *Service) Organizations(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
orgs, err := s.OrganizationsStore.All(ctx) orgs, err := s.Store.Organizations(ctx).All(ctx)
if err != nil { if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger) Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return return
@ -94,7 +94,7 @@ func (s *Service) NewOrganization(w http.ResponseWriter, r *http.Request) {
Name: req.Name, Name: req.Name,
} }
res, err := s.OrganizationsStore.Add(ctx, org) res, err := s.Store.Organizations(ctx).Add(ctx, org)
if err != nil { if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger) Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return return
@ -116,7 +116,7 @@ func (s *Service) OrganizationID(w http.ResponseWriter, r *http.Request) {
return return
} }
org, err := s.OrganizationsStore.Get(ctx, chronograf.OrganizationQuery{ID: &id}) org, err := s.Store.Organizations(ctx).Get(ctx, chronograf.OrganizationQuery{ID: &id})
if err != nil { if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger) Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return return
@ -146,7 +146,7 @@ func (s *Service) UpdateOrganization(w http.ResponseWriter, r *http.Request) {
return return
} }
org, err := s.OrganizationsStore.Get(ctx, chronograf.OrganizationQuery{ID: &id}) org, err := s.Store.Organizations(ctx).Get(ctx, chronograf.OrganizationQuery{ID: &id})
if err != nil { if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger) Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return return
@ -156,7 +156,7 @@ func (s *Service) UpdateOrganization(w http.ResponseWriter, r *http.Request) {
org.Name = req.Name org.Name = req.Name
} }
err = s.OrganizationsStore.Update(ctx, org) err = s.Store.Organizations(ctx).Update(ctx, org)
if err != nil { if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger) Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return return
@ -176,12 +176,12 @@ func (s *Service) RemoveOrganization(w http.ResponseWriter, r *http.Request) {
return return
} }
org, err := s.OrganizationsStore.Get(ctx, chronograf.OrganizationQuery{ID: &id}) org, err := s.Store.Organizations(ctx).Get(ctx, chronograf.OrganizationQuery{ID: &id})
if err != nil { if err != nil {
Error(w, http.StatusNotFound, err.Error(), s.Logger) Error(w, http.StatusNotFound, err.Error(), s.Logger)
return return
} }
if err := s.OrganizationsStore.Delete(ctx, org); err != nil { if err := s.Store.Organizations(ctx).Delete(ctx, org); err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger) Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return return
} }

View File

@ -70,7 +70,9 @@ func TestService_OrganizationID(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := &Service{ s := &Service{
Store: &Store{
OrganizationsStore: tt.fields.OrganizationsStore, OrganizationsStore: tt.fields.OrganizationsStore,
},
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }
@ -155,7 +157,9 @@ func TestService_Organizations(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := &Service{ s := &Service{
Store: &Store{
OrganizationsStore: tt.fields.OrganizationsStore, OrganizationsStore: tt.fields.OrganizationsStore,
},
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }
@ -234,7 +238,9 @@ func TestService_UpdateOrganization(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := &Service{ s := &Service{
Store: &Store{
OrganizationsStore: tt.fields.OrganizationsStore, OrganizationsStore: tt.fields.OrganizationsStore,
},
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }
@ -319,7 +325,9 @@ func TestService_RemoveOrganization(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := &Service{ s := &Service{
Store: &Store{
OrganizationsStore: tt.fields.OrganizationsStore, OrganizationsStore: tt.fields.OrganizationsStore,
},
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }
@ -393,7 +401,9 @@ func TestService_NewOrganization(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := &Service{ s := &Service{
Store: &Store{
OrganizationsStore: tt.fields.OrganizationsStore, OrganizationsStore: tt.fields.OrganizationsStore,
},
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }

View File

@ -16,7 +16,7 @@ func (s *Service) Permissions(w http.ResponseWriter, r *http.Request) {
return return
} }
src, err := s.SourcesStore.Get(ctx, srcID) src, err := s.Store.Sources(ctx).Get(ctx, srcID)
if err != nil { if err != nil {
notFound(w, srcID, s.Logger) notFound(w, srcID, s.Logger)
return return

View File

@ -89,7 +89,9 @@ func TestService_Permissions(t *testing.T) {
}, },
})) }))
h := &Service{ h := &Service{
Store: &mocks.Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
},
TimeSeriesClient: tt.fields.TimeSeries, TimeSeriesClient: tt.fields.TimeSeries,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,

View File

@ -28,7 +28,7 @@ func (s *Service) KapacitorProxy(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
srv, err := s.ServersStore.Get(ctx, id) srv, err := s.Store.Servers(ctx).Get(ctx, id)
if err != nil || srv.SrcID != srcID { if err != nil || srv.SrcID != srcID {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return

View File

@ -44,7 +44,7 @@ func (s *Service) Queries(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
src, err := s.SourcesStore.Get(ctx, srcID) src, err := s.Store.Sources(ctx).Get(ctx, srcID)
if err != nil { if err != nil {
notFound(w, srcID, s.Logger) notFound(w, srcID, s.Logger)
return return

View File

@ -19,7 +19,6 @@ import (
"github.com/influxdata/chronograf/influx" "github.com/influxdata/chronograf/influx"
clog "github.com/influxdata/chronograf/log" clog "github.com/influxdata/chronograf/log"
"github.com/influxdata/chronograf/oauth2" "github.com/influxdata/chronograf/oauth2"
"github.com/influxdata/chronograf/organizations"
"github.com/influxdata/chronograf/uuid" "github.com/influxdata/chronograf/uuid"
client "github.com/influxdata/usage-client/v1" client "github.com/influxdata/usage-client/v1"
flags "github.com/jessevdk/go-flags" flags "github.com/jessevdk/go-flags"
@ -431,13 +430,15 @@ func openService(ctx context.Context, boltPath string, lBuilder LayoutBuilder, s
return Service{ return Service{
TimeSeriesClient: &InfluxClient{}, TimeSeriesClient: &InfluxClient{},
SourcesStore: organizations.NewSourcesStore(sources), Store: &Store{
ServersStore: organizations.NewServersStore(kapacitors), SourcesStore: sources,
ServersStore: kapacitors,
UsersStore: db.UsersStore, UsersStore: db.UsersStore,
OrganizationUsersStore: organizations.NewUsersStore(db.UsersStore),
OrganizationsStore: db.OrganizationsStore, OrganizationsStore: db.OrganizationsStore,
LayoutsStore: organizations.NewLayoutsStore(layouts), LayoutsStore: layouts,
DashboardsStore: organizations.NewDashboardsStore(db.DashboardsStore), DashboardsStore: db.DashboardsStore,
//OrganizationUsersStore: organizations.NewUsersStore(db.UsersStore),
},
Logger: logger, Logger: logger,
UseAuth: useAuth, UseAuth: useAuth,
Databases: &influx.Client{Logger: logger}, Databases: &influx.Client{Logger: logger},

View File

@ -11,13 +11,7 @@ import (
// Service handles REST calls to the persistence // Service handles REST calls to the persistence
type Service struct { type Service struct {
SourcesStore chronograf.SourcesStore Store DataStore
ServersStore chronograf.ServersStore
LayoutsStore chronograf.LayoutsStore
UsersStore chronograf.UsersStore
OrganizationUsersStore chronograf.UsersStore
DashboardsStore chronograf.DashboardsStore
OrganizationsStore chronograf.OrganizationsStore
TimeSeriesClient TimeSeriesClient TimeSeriesClient TimeSeriesClient
Logger chronograf.Logger Logger chronograf.Logger
UseAuth bool UseAuth bool

View File

@ -86,7 +86,7 @@ func (s *Service) NewSource(w http.ResponseWriter, r *http.Request) {
} }
src.Type = dbType src.Type = dbType
if src, err = s.SourcesStore.Add(ctx, src); err != nil { if src, err = s.Store.Sources(ctx).Add(ctx, src); err != nil {
msg := fmt.Errorf("Error storing source %v: %v", src, err) msg := fmt.Errorf("Error storing source %v: %v", src, err)
unknownErrorWithMessage(w, msg, s.Logger) unknownErrorWithMessage(w, msg, s.Logger)
return return
@ -115,7 +115,7 @@ type getSourcesResponse struct {
// Sources returns all sources from the store. // Sources returns all sources from the store.
func (s *Service) Sources(w http.ResponseWriter, r *http.Request) { func (s *Service) Sources(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
srcs, err := s.SourcesStore.All(ctx) srcs, err := s.Store.Sources(ctx).All(ctx)
if err != nil { if err != nil {
Error(w, http.StatusInternalServerError, "Error loading sources", s.Logger) Error(w, http.StatusInternalServerError, "Error loading sources", s.Logger)
return return
@ -141,7 +141,7 @@ func (s *Service) SourcesID(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
src, err := s.SourcesStore.Get(ctx, id) src, err := s.Store.Sources(ctx).Get(ctx, id)
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -161,7 +161,7 @@ func (s *Service) RemoveSource(w http.ResponseWriter, r *http.Request) {
src := chronograf.Source{ID: id} src := chronograf.Source{ID: id}
ctx := r.Context() ctx := r.Context()
if err = s.SourcesStore.Delete(ctx, src); err != nil { if err = s.Store.Sources(ctx).Delete(ctx, src); err != nil {
if err == chronograf.ErrSourceNotFound { if err == chronograf.ErrSourceNotFound {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
} else { } else {
@ -182,7 +182,7 @@ func (s *Service) RemoveSource(w http.ResponseWriter, r *http.Request) {
// removeSrcsKapa will remove all kapacitors and kapacitor rules from the stores. // removeSrcsKapa will remove all kapacitors and kapacitor rules from the stores.
// However, it will not remove the kapacitor tickscript from kapacitor itself. // However, it will not remove the kapacitor tickscript from kapacitor itself.
func (s *Service) removeSrcsKapa(ctx context.Context, srcID int) error { func (s *Service) removeSrcsKapa(ctx context.Context, srcID int) error {
kapas, err := s.ServersStore.All(ctx) kapas, err := s.Store.Servers(ctx).All(ctx)
if err != nil { if err != nil {
return err return err
} }
@ -201,7 +201,7 @@ func (s *Service) removeSrcsKapa(ctx context.Context, srcID int) error {
} }
s.Logger.Debug("Deleting kapacitor resource id ", kapa.ID) s.Logger.Debug("Deleting kapacitor resource id ", kapa.ID)
if err := s.ServersStore.Delete(ctx, kapa); err != nil { if err := s.Store.Servers(ctx).Delete(ctx, kapa); err != nil {
return err return err
} }
} }
@ -218,7 +218,7 @@ func (s *Service) UpdateSource(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
src, err := s.SourcesStore.Get(ctx, id) src, err := s.Store.Sources(ctx).Get(ctx, id)
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -266,7 +266,7 @@ func (s *Service) UpdateSource(w http.ResponseWriter, r *http.Request) {
} }
src.Type = dbType src.Type = dbType
if err := s.SourcesStore.Update(ctx, src); err != nil { if err := s.Store.Sources(ctx).Update(ctx, src); err != nil {
msg := fmt.Sprintf("Error updating source ID %d", id) msg := fmt.Sprintf("Error updating source ID %d", id)
Error(w, http.StatusInternalServerError, msg, s.Logger) Error(w, http.StatusInternalServerError, msg, s.Logger)
return return
@ -338,7 +338,7 @@ func (s *Service) HandleNewSources(ctx context.Context, input string) error {
// newSourceKapacitor adds sources to BoltDB idempotently by name, as well as respective kapacitors // newSourceKapacitor adds sources to BoltDB idempotently by name, as well as respective kapacitors
func (s *Service) newSourceKapacitor(ctx context.Context, src chronograf.Source, kapa chronograf.Server) error { func (s *Service) newSourceKapacitor(ctx context.Context, src chronograf.Source, kapa chronograf.Server) error {
srcs, err := s.SourcesStore.All(ctx) srcs, err := s.Store.Sources(ctx).All(ctx)
if err != nil { if err != nil {
return err return err
} }
@ -354,13 +354,13 @@ func (s *Service) newSourceKapacitor(ctx context.Context, src chronograf.Source,
} }
} }
src, err = s.SourcesStore.Add(ctx, src) src, err = s.Store.Sources(ctx).Add(ctx, src)
if err != nil { if err != nil {
return err return err
} }
kapa.SrcID = src.ID kapa.SrcID = src.ID
if _, err := s.ServersStore.Add(ctx, kapa); err != nil { if _, err := s.Store.Servers(ctx).Add(ctx, kapa); err != nil {
return err return err
} }
@ -540,7 +540,7 @@ func (s *Service) sourcesSeries(ctx context.Context, w http.ResponseWriter, r *h
return 0, nil, err return 0, nil, err
} }
src, err := s.SourcesStore.Get(ctx, srcID) src, err := s.Store.Sources(ctx).Get(ctx, srcID)
if err != nil { if err != nil {
notFound(w, srcID, s.Logger) notFound(w, srcID, s.Logger)
return 0, nil, err return 0, nil, err

View File

@ -239,8 +239,10 @@ func TestService_newSourceKapacitor(t *testing.T) {
srcCount = 0 srcCount = 0
srvCount = 0 srvCount = 0
h := &Service{ h := &Service{
Store: &mocks.Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
ServersStore: tt.fields.ServersStore, ServersStore: tt.fields.ServersStore,
},
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }
if err := h.newSourceKapacitor(tt.args.ctx, tt.args.src, tt.args.kapa); (err != nil) != tt.wantErr { if err := h.newSourceKapacitor(tt.args.ctx, tt.args.src, tt.args.kapa); (err != nil) != tt.wantErr {
@ -535,7 +537,9 @@ func TestService_NewSourceUser(t *testing.T) {
})) }))
h := &Service{ h := &Service{
Store: &mocks.Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
},
TimeSeriesClient: tt.fields.TimeSeries, TimeSeriesClient: tt.fields.TimeSeries,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,
@ -702,7 +706,9 @@ func TestService_SourceUsers(t *testing.T) {
}, },
})) }))
h := &Service{ h := &Service{
Store: &mocks.Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
},
TimeSeriesClient: tt.fields.TimeSeries, TimeSeriesClient: tt.fields.TimeSeries,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,
@ -867,7 +873,9 @@ func TestService_SourceUserID(t *testing.T) {
}, },
})) }))
h := &Service{ h := &Service{
Store: &mocks.Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
},
TimeSeriesClient: tt.fields.TimeSeries, TimeSeriesClient: tt.fields.TimeSeries,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,
@ -962,7 +970,9 @@ func TestService_RemoveSourceUser(t *testing.T) {
}, },
})) }))
h := &Service{ h := &Service{
Store: &mocks.Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
},
TimeSeriesClient: tt.fields.TimeSeries, TimeSeriesClient: tt.fields.TimeSeries,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,
@ -1144,7 +1154,9 @@ func TestService_UpdateSourceUser(t *testing.T) {
}, },
})) }))
h := &Service{ h := &Service{
Store: &mocks.Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
},
TimeSeriesClient: tt.fields.TimeSeries, TimeSeriesClient: tt.fields.TimeSeries,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
UseAuth: tt.fields.UseAuth, UseAuth: tt.fields.UseAuth,
@ -1367,7 +1379,9 @@ func TestService_NewSourceRole(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
h := &Service{ h := &Service{
Store: &mocks.Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
},
TimeSeriesClient: tt.fields.TimeSeries, TimeSeriesClient: tt.fields.TimeSeries,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }
@ -1480,7 +1494,9 @@ func TestService_UpdateSourceRole(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
h := &Service{ h := &Service{
Store: &mocks.Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
},
TimeSeriesClient: tt.fields.TimeSeries, TimeSeriesClient: tt.fields.TimeSeries,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }
@ -1603,7 +1619,9 @@ func TestService_SourceRoleID(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
h := &Service{ h := &Service{
Store: &mocks.Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
},
TimeSeriesClient: tt.fields.TimeSeries, TimeSeriesClient: tt.fields.TimeSeries,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }
@ -1699,7 +1717,9 @@ func TestService_RemoveSourceRole(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
h := &Service{ h := &Service{
Store: &mocks.Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
},
TimeSeriesClient: tt.fields.TimeSeries, TimeSeriesClient: tt.fields.TimeSeries,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }
@ -1811,7 +1831,9 @@ func TestService_SourceRoles(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
h := &Service{ h := &Service{
Store: &mocks.Store{
SourcesStore: tt.fields.SourcesStore, SourcesStore: tt.fields.SourcesStore,
},
TimeSeriesClient: tt.fields.TimeSeries, TimeSeriesClient: tt.fields.TimeSeries,
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }

64
server/stores.go Normal file
View File

@ -0,0 +1,64 @@
package server
import (
"context"
"github.com/influxdata/chronograf"
"github.com/influxdata/chronograf/organizations"
)
// 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 {
return organizations.NewSourcesStore(s.SourcesStore)
}
func (s *Store) Servers(ctx context.Context) chronograf.ServersStore {
return organizations.NewServersStore(s.ServersStore)
}
func (s *Store) Layouts(ctx context.Context) chronograf.LayoutsStore {
return organizations.NewLayoutsStore(s.LayoutsStore)
}
func (s *Store) Users(ctx context.Context) chronograf.UsersStore {
return organizations.NewUsersStore(s.UsersStore)
}
// 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
}
func (s *Store) Dashboards(ctx context.Context) chronograf.DashboardsStore {
return organizations.NewDashboardsStore(s.DashboardsStore)
}

View File

@ -73,7 +73,7 @@ func (s *Service) Templates(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
d, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id)) d, err := s.Store.Dashboards(ctx).Get(ctx, chronograf.DashboardID(id))
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -94,7 +94,7 @@ func (s *Service) NewTemplate(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
dash, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id)) dash, err := s.Store.Dashboards(ctx).Get(ctx, chronograf.DashboardID(id))
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -121,7 +121,7 @@ func (s *Service) NewTemplate(w http.ResponseWriter, r *http.Request) {
template.ID = chronograf.TemplateID(tid) template.ID = chronograf.TemplateID(tid)
dash.Templates = append(dash.Templates, template) dash.Templates = append(dash.Templates, template)
if err := s.DashboardsStore.Update(ctx, dash); err != nil { if err := s.Store.Dashboards(ctx).Update(ctx, dash); err != nil {
msg := fmt.Sprintf("Error adding template %s to dashboard %d: %v", tid, id, err) msg := fmt.Sprintf("Error adding template %s to dashboard %d: %v", tid, id, err)
Error(w, http.StatusInternalServerError, msg, s.Logger) Error(w, http.StatusInternalServerError, msg, s.Logger)
return return
@ -140,7 +140,7 @@ func (s *Service) TemplateID(w http.ResponseWriter, r *http.Request) {
return return
} }
dash, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id)) dash, err := s.Store.Dashboards(ctx).Get(ctx, chronograf.DashboardID(id))
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -167,7 +167,7 @@ func (s *Service) RemoveTemplate(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
dash, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id)) dash, err := s.Store.Dashboards(ctx).Get(ctx, chronograf.DashboardID(id))
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -187,7 +187,7 @@ func (s *Service) RemoveTemplate(w http.ResponseWriter, r *http.Request) {
} }
dash.Templates = append(dash.Templates[:pos], dash.Templates[pos+1:]...) dash.Templates = append(dash.Templates[:pos], dash.Templates[pos+1:]...)
if err := s.DashboardsStore.Update(ctx, dash); err != nil { if err := s.Store.Dashboards(ctx).Update(ctx, dash); err != nil {
msg := fmt.Sprintf("Error removing template %s from dashboard %d: %v", tid, id, err) msg := fmt.Sprintf("Error removing template %s from dashboard %d: %v", tid, id, err)
Error(w, http.StatusInternalServerError, msg, s.Logger) Error(w, http.StatusInternalServerError, msg, s.Logger)
return return
@ -205,7 +205,7 @@ func (s *Service) ReplaceTemplate(w http.ResponseWriter, r *http.Request) {
} }
ctx := r.Context() ctx := r.Context()
dash, err := s.DashboardsStore.Get(ctx, chronograf.DashboardID(id)) dash, err := s.Store.Dashboards(ctx).Get(ctx, chronograf.DashboardID(id))
if err != nil { if err != nil {
notFound(w, id, s.Logger) notFound(w, id, s.Logger)
return return
@ -237,7 +237,7 @@ func (s *Service) ReplaceTemplate(w http.ResponseWriter, r *http.Request) {
template.ID = chronograf.TemplateID(tid) template.ID = chronograf.TemplateID(tid)
dash.Templates[pos] = template dash.Templates[pos] = template
if err := s.DashboardsStore.Update(ctx, dash); err != nil { if err := s.Store.Dashboards(ctx).Update(ctx, dash); err != nil {
msg := fmt.Sprintf("Error updating template %s in dashboard %d: %v", tid, id, err) msg := fmt.Sprintf("Error updating template %s in dashboard %d: %v", tid, id, err)
Error(w, http.StatusInternalServerError, msg, s.Logger) Error(w, http.StatusInternalServerError, msg, s.Logger)
return return

View File

@ -151,7 +151,7 @@ func (s *Service) UserID(w http.ResponseWriter, r *http.Request) {
Error(w, http.StatusBadRequest, fmt.Sprintf("invalid user id: %s", err.Error()), s.Logger) Error(w, http.StatusBadRequest, fmt.Sprintf("invalid user id: %s", err.Error()), s.Logger)
return return
} }
user, err := s.OrganizationUsersStore.Get(ctx, chronograf.UserQuery{ID: &id}) user, err := s.Store.Users(ctx).Get(ctx, chronograf.UserQuery{ID: &id})
if err != nil { if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger) Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return return
@ -182,7 +182,7 @@ func (s *Service) NewUser(w http.ResponseWriter, r *http.Request) {
Roles: req.Roles, Roles: req.Roles,
} }
res, err := s.OrganizationUsersStore.Add(ctx, user) res, err := s.Store.Users(ctx).Add(ctx, user)
if err != nil { if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger) Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return return
@ -203,12 +203,12 @@ func (s *Service) RemoveUser(w http.ResponseWriter, r *http.Request) {
return return
} }
u, err := s.OrganizationUsersStore.Get(ctx, chronograf.UserQuery{ID: &id}) u, err := s.Store.Users(ctx).Get(ctx, chronograf.UserQuery{ID: &id})
if err != nil { if err != nil {
Error(w, http.StatusNotFound, err.Error(), s.Logger) Error(w, http.StatusNotFound, err.Error(), s.Logger)
return return
} }
if err := s.OrganizationUsersStore.Delete(ctx, u); err != nil { if err := s.Store.Users(ctx).Delete(ctx, u); err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger) Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return return
} }
@ -237,7 +237,7 @@ func (s *Service) UpdateUser(w http.ResponseWriter, r *http.Request) {
return return
} }
u, err := s.OrganizationUsersStore.Get(ctx, chronograf.UserQuery{ID: &id}) u, err := s.Store.Users(ctx).Get(ctx, chronograf.UserQuery{ID: &id})
if err != nil { if err != nil {
Error(w, http.StatusNotFound, err.Error(), s.Logger) Error(w, http.StatusNotFound, err.Error(), s.Logger)
return return
@ -246,7 +246,7 @@ func (s *Service) UpdateUser(w http.ResponseWriter, r *http.Request) {
// ValidUpdate should ensure that req.Roles is not nil // ValidUpdate should ensure that req.Roles is not nil
u.Roles = req.Roles u.Roles = req.Roles
err = s.OrganizationUsersStore.Update(ctx, u) err = s.Store.Users(ctx).Update(ctx, u)
if err != nil { if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger) Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return return
@ -261,7 +261,7 @@ func (s *Service) UpdateUser(w http.ResponseWriter, r *http.Request) {
func (s *Service) Users(w http.ResponseWriter, r *http.Request) { func (s *Service) Users(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
users, err := s.OrganizationUsersStore.All(ctx) users, err := s.Store.Users(ctx).All(ctx)
if err != nil { if err != nil {
Error(w, http.StatusBadRequest, err.Error(), s.Logger) Error(w, http.StatusBadRequest, err.Error(), s.Logger)
return return

View File

@ -75,7 +75,9 @@ func TestService_UserID(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := &Service{ s := &Service{
OrganizationUsersStore: tt.fields.UsersStore, Store: &mocks.Store{
UsersStore: tt.fields.UsersStore,
},
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }
@ -163,7 +165,9 @@ func TestService_NewUser(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := &Service{ s := &Service{
OrganizationUsersStore: tt.fields.UsersStore, Store: &mocks.Store{
UsersStore: tt.fields.UsersStore,
},
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }
@ -251,7 +255,9 @@ func TestService_RemoveUser(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := &Service{ s := &Service{
OrganizationUsersStore: tt.fields.UsersStore, Store: &mocks.Store{
UsersStore: tt.fields.UsersStore,
},
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }
@ -344,7 +350,9 @@ func TestService_UpdateUser(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := &Service{ s := &Service{
OrganizationUsersStore: tt.fields.UsersStore, Store: &mocks.Store{
UsersStore: tt.fields.UsersStore,
},
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }
@ -475,7 +483,9 @@ func TestService_Users(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := &Service{ s := &Service{
OrganizationUsersStore: tt.fields.UsersStore, Store: &mocks.Store{
UsersStore: tt.fields.UsersStore,
},
Logger: tt.fields.Logger, Logger: tt.fields.Logger,
} }