Serviceaccounts: create serviceaccount api (#42150)

* WIP

* wip

* wip

* wip

* refactor: new return of the create service accoutn

* refactor: change to have correct role

* refactor: ability to create service accounts

* make public

* refactor: make ints instead

* refactor: remove location sprintf

* refactor: added back named constants
This commit is contained in:
Eric Leijonmarck
2021-12-14 14:39:25 +01:00
committed by GitHub
parent 19374fce39
commit 4a3961400a
6 changed files with 75 additions and 1 deletions
@@ -3,9 +3,11 @@ package database
import (
"context"
"github.com/google/uuid"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/serviceaccounts"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/pkg/errors"
)
type ServiceAccountsStoreImpl struct {
@@ -18,6 +20,21 @@ func NewServiceAccountsStore(store *sqlstore.SQLStore) *ServiceAccountsStoreImpl
}
}
func (s *ServiceAccountsStoreImpl) CreateServiceAccount(ctx context.Context, sa *serviceaccounts.CreateServiceaccountForm) (user *models.User, err error) {
// create a new service account - "user" with empty permissions
cmd := models.CreateUserCommand{
Login: "Service-Account-" + uuid.New().String(),
Name: sa.Name + "-Service-Account-" + uuid.New().String(),
OrgId: sa.OrgID,
IsServiceAccount: true,
}
newuser, err := s.sqlStore.CreateUser(ctx, cmd)
if err != nil {
return nil, errors.Errorf("Failed to create user: %v", err)
}
return newuser, nil
}
func (s *ServiceAccountsStoreImpl) DeleteServiceAccount(ctx context.Context, orgID, serviceaccountID int64) error {
return s.sqlStore.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
return deleteServiceAccountInTransaction(sess, orgID, serviceaccountID)