mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Chore: Add context to org (#40685)
* Add context to org * Rebase * Fix rebase
This commit is contained in:
@@ -16,16 +16,16 @@ import (
|
||||
const MainOrgName = "Main Org."
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", GetOrgById)
|
||||
bus.AddHandler("sql", CreateOrg)
|
||||
bus.AddHandler("sql", UpdateOrg)
|
||||
bus.AddHandler("sql", UpdateOrgAddress)
|
||||
bus.AddHandler("sql", GetOrgByName)
|
||||
bus.AddHandler("sql", SearchOrgs)
|
||||
bus.AddHandler("sql", DeleteOrg)
|
||||
bus.AddHandlerCtx("sql", GetOrgById)
|
||||
bus.AddHandlerCtx("sql", CreateOrg)
|
||||
bus.AddHandlerCtx("sql", UpdateOrg)
|
||||
bus.AddHandlerCtx("sql", UpdateOrgAddress)
|
||||
bus.AddHandlerCtx("sql", GetOrgByName)
|
||||
bus.AddHandlerCtx("sql", SearchOrgs)
|
||||
bus.AddHandlerCtx("sql", DeleteOrg)
|
||||
}
|
||||
|
||||
func SearchOrgs(query *models.SearchOrgsQuery) error {
|
||||
func SearchOrgs(ctx context.Context, query *models.SearchOrgsQuery) error {
|
||||
query.Result = make([]*models.OrgDTO, 0)
|
||||
sess := x.Table("org")
|
||||
if query.Query != "" {
|
||||
@@ -48,7 +48,7 @@ func SearchOrgs(query *models.SearchOrgsQuery) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func GetOrgById(query *models.GetOrgByIdQuery) error {
|
||||
func GetOrgById(ctx context.Context, query *models.GetOrgByIdQuery) error {
|
||||
var org models.Org
|
||||
exists, err := x.Id(query.Id).Get(&org)
|
||||
if err != nil {
|
||||
@@ -63,7 +63,7 @@ func GetOrgById(query *models.GetOrgByIdQuery) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetOrgByName(query *models.GetOrgByNameQuery) error {
|
||||
func GetOrgByName(ctx context.Context, query *models.GetOrgByNameQuery) error {
|
||||
var org models.Org
|
||||
exists, err := x.Where("name=?", query.Name).Get(&org)
|
||||
if err != nil {
|
||||
@@ -154,7 +154,7 @@ func (ss *SQLStore) CreateOrgWithMember(name string, userID int64) (models.Org,
|
||||
return createOrg(name, userID, ss.engine)
|
||||
}
|
||||
|
||||
func CreateOrg(cmd *models.CreateOrgCommand) error {
|
||||
func CreateOrg(ctx context.Context, cmd *models.CreateOrgCommand) error {
|
||||
org, err := createOrg(cmd.Name, cmd.UserId, x)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -164,7 +164,7 @@ func CreateOrg(cmd *models.CreateOrgCommand) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateOrg(cmd *models.UpdateOrgCommand) error {
|
||||
func UpdateOrg(ctx context.Context, cmd *models.UpdateOrgCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
if isNameTaken, err := isOrgNameTaken(cmd.Name, cmd.OrgId, sess); err != nil {
|
||||
return err
|
||||
@@ -197,7 +197,7 @@ func UpdateOrg(cmd *models.UpdateOrgCommand) error {
|
||||
})
|
||||
}
|
||||
|
||||
func UpdateOrgAddress(cmd *models.UpdateOrgAddressCommand) error {
|
||||
func UpdateOrgAddress(ctx context.Context, cmd *models.UpdateOrgAddressCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
org := models.Org{
|
||||
Address1: cmd.Address1,
|
||||
@@ -224,7 +224,7 @@ func UpdateOrgAddress(cmd *models.UpdateOrgAddressCommand) error {
|
||||
})
|
||||
}
|
||||
|
||||
func DeleteOrg(cmd *models.DeleteOrgCommand) error {
|
||||
func DeleteOrg(ctx context.Context, cmd *models.DeleteOrgCommand) error {
|
||||
return inTransaction(func(sess *DBSession) error {
|
||||
if res, err := sess.Query("SELECT 1 from org WHERE id=?", cmd.Id); err != nil {
|
||||
return err
|
||||
|
||||
@@ -25,14 +25,14 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
|
||||
for i := 1; i < 4; i++ {
|
||||
cmd = &models.CreateOrgCommand{Name: fmt.Sprint("Org #", i)}
|
||||
err = CreateOrg(cmd)
|
||||
err = CreateOrg(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
|
||||
ids = append(ids, cmd.Result.Id)
|
||||
}
|
||||
|
||||
query := &models.SearchOrgsQuery{Ids: ids}
|
||||
err = SearchOrgs(query)
|
||||
err = SearchOrgs(context.Background(), query)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(query.Result), 3)
|
||||
@@ -42,13 +42,13 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
sqlStore = InitTestDB(t)
|
||||
for i := 1; i < 4; i++ {
|
||||
cmd := &models.CreateOrgCommand{Name: fmt.Sprint("Org #", i)}
|
||||
err := CreateOrg(cmd)
|
||||
err := CreateOrg(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
t.Run("Should be able to search with defaults", func(t *testing.T) {
|
||||
query := &models.SearchOrgsQuery{}
|
||||
err := SearchOrgs(query)
|
||||
err := SearchOrgs(context.Background(), query)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(query.Result), 3)
|
||||
@@ -56,7 +56,7 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
|
||||
t.Run("Should be able to limit search", func(t *testing.T) {
|
||||
query := &models.SearchOrgsQuery{Limit: 1}
|
||||
err := SearchOrgs(query)
|
||||
err := SearchOrgs(context.Background(), query)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(query.Result), 1)
|
||||
@@ -64,7 +64,7 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
|
||||
t.Run("Should be able to limit and paginate search", func(t *testing.T) {
|
||||
query := &models.SearchOrgsQuery{Limit: 2, Page: 1}
|
||||
err := SearchOrgs(query)
|
||||
err := SearchOrgs(context.Background(), query)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(query.Result), 1)
|
||||
@@ -278,7 +278,7 @@ func TestAccountDataAccess(t *testing.T) {
|
||||
|
||||
t.Run("Removing user from org should delete user completely if in no other org", func(t *testing.T) {
|
||||
// make sure ac2 has no org
|
||||
err := DeleteOrg(&models.DeleteOrgCommand{Id: ac2.OrgId})
|
||||
err := DeleteOrg(context.Background(), &models.DeleteOrgCommand{Id: ac2.OrgId})
|
||||
require.NoError(t, err)
|
||||
|
||||
// remove ac2 user from ac1 org
|
||||
|
||||
@@ -49,7 +49,7 @@ func TestQuotaCommandsAndQueries(t *testing.T) {
|
||||
UserId: 1,
|
||||
}
|
||||
|
||||
err := CreateOrg(&userCmd)
|
||||
err := CreateOrg(context.Background(), &userCmd)
|
||||
require.NoError(t, err)
|
||||
orgId = userCmd.Result.Id
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ func populateDB(t *testing.T, sqlStore *SQLStore) {
|
||||
|
||||
// get 1st user's organisation
|
||||
getOrgByIdQuery := &models.GetOrgByIdQuery{Id: users[0].OrgId}
|
||||
err := GetOrgById(getOrgByIdQuery)
|
||||
err := GetOrgById(context.Background(), getOrgByIdQuery)
|
||||
require.NoError(t, err)
|
||||
org := getOrgByIdQuery.Result
|
||||
|
||||
@@ -102,7 +102,7 @@ func populateDB(t *testing.T, sqlStore *SQLStore) {
|
||||
|
||||
// get 2nd user's organisation
|
||||
getOrgByIdQuery = &models.GetOrgByIdQuery{Id: users[1].OrgId}
|
||||
err = GetOrgById(getOrgByIdQuery)
|
||||
err = GetOrgById(context.Background(), getOrgByIdQuery)
|
||||
require.NoError(t, err)
|
||||
org = getOrgByIdQuery.Result
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ func TestUserDataAccess(t *testing.T) {
|
||||
}()
|
||||
|
||||
orgCmd := &models.CreateOrgCommand{Name: "Some Test Org"}
|
||||
err := CreateOrg(orgCmd)
|
||||
err := CreateOrg(context.Background(), orgCmd)
|
||||
require.Nil(t, err)
|
||||
|
||||
cmd := models.CreateUserCommand{
|
||||
|
||||
Reference in New Issue
Block a user