mirror of
https://github.com/grafana/grafana.git
synced 2025-02-20 11:48:34 -06:00
* feat: add serviceaccountDTO * WIP * feat: listing number of tokens for a given service account * nit: removed fmt * Update pkg/services/serviceaccounts/database/database.go * Update public/app/features/serviceaccounts/ServiceAccountsListPage.tsx * fixes * align DTOProfile data to the frontend * reviewed myself fixes * fix: tests fix
67 lines
2.1 KiB
Go
67 lines
2.1 KiB
Go
package manager
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
|
"github.com/grafana/grafana/pkg/services/serviceaccounts"
|
|
"github.com/grafana/grafana/pkg/services/serviceaccounts/api"
|
|
"github.com/grafana/grafana/pkg/services/serviceaccounts/database"
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
)
|
|
|
|
var (
|
|
ServiceAccountFeatureToggleNotFound = "FeatureToggle service-accounts not found, try adding it to your custom.ini"
|
|
)
|
|
|
|
type ServiceAccountsService struct {
|
|
store serviceaccounts.Store
|
|
features featuremgmt.FeatureToggles
|
|
log log.Logger
|
|
}
|
|
|
|
func ProvideServiceAccountsService(
|
|
cfg *setting.Cfg,
|
|
features featuremgmt.FeatureToggles,
|
|
store *sqlstore.SQLStore,
|
|
ac accesscontrol.AccessControl,
|
|
routeRegister routing.RouteRegister,
|
|
) (*ServiceAccountsService, error) {
|
|
s := &ServiceAccountsService{
|
|
features: features,
|
|
store: database.NewServiceAccountsStore(store),
|
|
log: log.New("serviceaccounts"),
|
|
}
|
|
|
|
if features.IsEnabled(featuremgmt.FlagServiceAccounts) {
|
|
if err := RegisterRoles(ac); err != nil {
|
|
s.log.Error("Failed to register roles", "error", err)
|
|
}
|
|
}
|
|
|
|
serviceaccountsAPI := api.NewServiceAccountsAPI(cfg, s, ac, routeRegister, s.store, store)
|
|
serviceaccountsAPI.RegisterAPIEndpoints(features)
|
|
|
|
return s, nil
|
|
}
|
|
|
|
func (sa *ServiceAccountsService) CreateServiceAccount(ctx context.Context, saForm *serviceaccounts.CreateServiceaccountForm) (*serviceaccounts.ServiceAccountDTO, error) {
|
|
if !sa.features.IsEnabled(featuremgmt.FlagServiceAccounts) {
|
|
sa.log.Debug(ServiceAccountFeatureToggleNotFound)
|
|
return nil, nil
|
|
}
|
|
return sa.store.CreateServiceAccount(ctx, saForm)
|
|
}
|
|
|
|
func (sa *ServiceAccountsService) DeleteServiceAccount(ctx context.Context, orgID, serviceAccountID int64) error {
|
|
if !sa.features.IsEnabled(featuremgmt.FlagServiceAccounts) {
|
|
sa.log.Debug(ServiceAccountFeatureToggleNotFound)
|
|
return nil
|
|
}
|
|
return sa.store.DeleteServiceAccount(ctx, orgID, serviceAccountID)
|
|
}
|