Support bundles: Refactor registry into separate service (#62945)
* add bundle registry service to avoid dependency cycles * move user support bundle collector to user service * move usage stat bundle implementation to usage stats * add info for background service * fix remaining imports * whitespace
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/org/orgimpl"
|
||||
"github.com/grafana/grafana/pkg/services/quota/quotaimpl"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||||
"github.com/grafana/grafana/pkg/services/supportbundles/supportbundlestest"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
@@ -30,7 +31,7 @@ func TestIntegrationUserDataAccess(t *testing.T) {
|
||||
orgService, err := orgimpl.ProvideService(ss, ss.Cfg, quotaService)
|
||||
require.NoError(t, err)
|
||||
userStore := ProvideStore(ss, setting.NewCfg())
|
||||
usrSvc, err := ProvideService(ss, orgService, ss.Cfg, nil, nil, quotaService)
|
||||
usrSvc, err := ProvideService(ss, orgService, ss.Cfg, nil, nil, quotaService, supportbundlestest.NewFakeBundleService())
|
||||
require.NoError(t, err)
|
||||
usr := &user.SignedInUser{
|
||||
OrgID: 1,
|
||||
@@ -497,7 +498,7 @@ func TestIntegrationUserDataAccess(t *testing.T) {
|
||||
ss := db.InitTestDB(t)
|
||||
orgService, err := orgimpl.ProvideService(ss, ss.Cfg, quotaService)
|
||||
require.NoError(t, err)
|
||||
usrSvc, err := ProvideService(ss, orgService, ss.Cfg, nil, nil, quotaService)
|
||||
usrSvc, err := ProvideService(ss, orgService, ss.Cfg, nil, nil, quotaService, supportbundlestest.NewFakeBundleService())
|
||||
require.NoError(t, err)
|
||||
|
||||
createFiveTestUsers(t, usrSvc, func(i int) *user.CreateUserCommand {
|
||||
@@ -942,7 +943,7 @@ func createOrgAndUserSvc(t *testing.T, store db.DB, cfg *setting.Cfg) (org.Servi
|
||||
quotaService := quotaimpl.ProvideService(store, cfg)
|
||||
orgService, err := orgimpl.ProvideService(store, cfg, quotaService)
|
||||
require.NoError(t, err)
|
||||
usrSvc, err := ProvideService(store, orgService, cfg, nil, nil, quotaService)
|
||||
usrSvc, err := ProvideService(store, orgService, cfg, nil, nil, quotaService, supportbundlestest.NewFakeBundleService())
|
||||
require.NoError(t, err)
|
||||
|
||||
return orgService, usrSvc
|
||||
|
||||
@@ -2,6 +2,7 @@ package userimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -13,6 +14,7 @@ import (
|
||||
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/quota"
|
||||
"github.com/grafana/grafana/pkg/services/supportbundles"
|
||||
"github.com/grafana/grafana/pkg/services/team"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
@@ -34,6 +36,7 @@ func ProvideService(
|
||||
teamService team.Service,
|
||||
cacheService *localcache.CacheService,
|
||||
quotaService quota.Service,
|
||||
bundleRegistry supportbundles.Service,
|
||||
) (user.Service, error) {
|
||||
store := ProvideStore(db, cfg)
|
||||
s := &Service{
|
||||
@@ -56,6 +59,8 @@ func ProvideService(
|
||||
}); err != nil {
|
||||
return s, err
|
||||
}
|
||||
|
||||
bundleRegistry.RegisterSupportItemCollector(s.supportBundleCollector())
|
||||
return s, nil
|
||||
}
|
||||
|
||||
@@ -547,3 +552,45 @@ func (s *Service) CreateServiceAccount(ctx context.Context, cmd *user.CreateUser
|
||||
}
|
||||
return usr, nil
|
||||
}
|
||||
|
||||
func (s *Service) supportBundleCollector() supportbundles.Collector {
|
||||
collectorFn := func(ctx context.Context) (*supportbundles.SupportItem, error) {
|
||||
query := &user.SearchUsersQuery{
|
||||
SignedInUser: &user.SignedInUser{
|
||||
Login: "sa-supportbundle",
|
||||
OrgRole: "Admin",
|
||||
IsGrafanaAdmin: true,
|
||||
IsServiceAccount: true},
|
||||
OrgID: 0,
|
||||
Query: "",
|
||||
Page: 0,
|
||||
Limit: 0,
|
||||
AuthModule: "",
|
||||
Filters: []user.Filter{},
|
||||
IsDisabled: new(bool),
|
||||
}
|
||||
res, err := s.Search(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
userBytes, err := json.Marshal(res.Users)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &supportbundles.SupportItem{
|
||||
Filename: "users.json",
|
||||
FileBytes: userBytes,
|
||||
}, nil
|
||||
}
|
||||
|
||||
return supportbundles.Collector{
|
||||
UID: "users",
|
||||
DisplayName: "User information",
|
||||
Description: "List users belonging to the Grafana instance",
|
||||
IncludedByDefault: false,
|
||||
Default: false,
|
||||
Fn: collectorFn,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user