mirror of
https://github.com/grafana/grafana.git
synced 2025-02-10 23:55:47 -06:00
Chore: Remove CreateOrg from alerting and use orgStore instead (#59440)
This commit is contained in:
parent
933879a347
commit
57fbe264ea
@ -1,86 +1,16 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/events"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
// MainOrgName is the name of the main organization.
|
||||
const MainOrgName = "Main Org."
|
||||
|
||||
func isOrgNameTaken(name string, existingId int64, sess *DBSession) (bool, error) {
|
||||
// check if org name is taken
|
||||
var org models.Org
|
||||
exists, err := sess.Where("name=?", name).Get(&org)
|
||||
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
if exists && existingId != org.Id {
|
||||
return true, nil
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (ss *SQLStore) createOrg(ctx context.Context, name string, userID int64, engine *xorm.Engine) (models.Org, error) {
|
||||
orga := models.Org{
|
||||
Name: name,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
if err := ss.inTransactionWithRetryCtx(ctx, engine, ss.bus, func(sess *DBSession) error {
|
||||
if isNameTaken, err := isOrgNameTaken(name, 0, sess); err != nil {
|
||||
return err
|
||||
} else if isNameTaken {
|
||||
return models.ErrOrgNameTaken
|
||||
}
|
||||
|
||||
if _, err := sess.Insert(&orga); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
user := models.OrgUser{
|
||||
OrgId: orga.Id,
|
||||
UserId: userID,
|
||||
Role: org.RoleAdmin,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
_, err := sess.Insert(&user)
|
||||
|
||||
sess.publishAfterCommit(&events.OrgCreated{
|
||||
Timestamp: orga.Created,
|
||||
Id: orga.Id,
|
||||
Name: orga.Name,
|
||||
})
|
||||
|
||||
return err
|
||||
}, 0); err != nil {
|
||||
return orga, err
|
||||
}
|
||||
|
||||
return orga, nil
|
||||
}
|
||||
|
||||
func (ss *SQLStore) CreateOrg(ctx context.Context, cmd *models.CreateOrgCommand) error {
|
||||
org, err := ss.createOrg(ctx, cmd.Name, cmd.UserId, ss.engine)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd.Result = org
|
||||
return nil
|
||||
}
|
||||
|
||||
func verifyExistingOrg(sess *DBSession, orgId int64) error {
|
||||
var org models.Org
|
||||
has, err := sess.Where("id=?", orgId).Get(&org)
|
||||
|
@ -2,6 +2,7 @@ package alerting
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -18,6 +19,8 @@ import (
|
||||
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/sender"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/org/orgimpl"
|
||||
"github.com/grafana/grafana/pkg/services/quota/quotatest"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/tests/testinfra"
|
||||
)
|
||||
@ -35,6 +38,9 @@ func TestAdminConfiguration_SendingToExternalAlertmanagers(t *testing.T) {
|
||||
|
||||
grafanaListedAddr, s := testinfra.StartGrafana(t, dir, path)
|
||||
|
||||
orgService, err := orgimpl.ProvideService(s, s.Cfg, quotatest.New(false, nil))
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create a user to make authenticated requests
|
||||
userID := createUser(t, s, user.CreateUserCommand{
|
||||
DefaultOrgRole: string(org.RoleAdmin),
|
||||
@ -42,8 +48,12 @@ func TestAdminConfiguration_SendingToExternalAlertmanagers(t *testing.T) {
|
||||
Password: "password",
|
||||
})
|
||||
apiClient := newAlertingApiClient(grafanaListedAddr, "grafana", "password")
|
||||
|
||||
// create another organisation
|
||||
orgID := createOrg(t, s, "another org", userID)
|
||||
newOrg, err := orgService.CreateWithMember(context.Background(), &org.CreateOrgCommand{Name: "another org", UserID: userID})
|
||||
require.NoError(t, err)
|
||||
orgID := newOrg.ID
|
||||
|
||||
// ensure that the orgID is 3 (the disabled org)
|
||||
require.Equal(t, disableOrgID, orgID)
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package alerting
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -11,6 +12,8 @@ import (
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/org/orgimpl"
|
||||
"github.com/grafana/grafana/pkg/services/quota/quotatest"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/tests/testinfra"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -28,6 +31,9 @@ func TestAlertmanagerConfigurationIsTransactional(t *testing.T) {
|
||||
|
||||
grafanaListedAddr, store := testinfra.StartGrafana(t, dir, path)
|
||||
|
||||
orgService, err := orgimpl.ProvideService(store, store.Cfg, quotatest.New(false, nil))
|
||||
require.NoError(t, err)
|
||||
|
||||
// editor from main organisation requests configuration
|
||||
alertConfigURL := fmt.Sprintf("http://editor:editor@%s/api/alertmanager/grafana/config/api/v1/alerts", grafanaListedAddr)
|
||||
|
||||
@ -39,7 +45,9 @@ func TestAlertmanagerConfigurationIsTransactional(t *testing.T) {
|
||||
})
|
||||
|
||||
// create another organisation
|
||||
orgID := createOrg(t, store, "another org", userID)
|
||||
newOrg, err := orgService.CreateWithMember(context.Background(), &org.CreateOrgCommand{Name: "another org", UserID: userID})
|
||||
require.NoError(t, err)
|
||||
orgID := newOrg.ID
|
||||
|
||||
// create user under different organisation
|
||||
createUser(t, store, user.CreateUserCommand{
|
||||
|
@ -16,7 +16,6 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
||||
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
ngstore "github.com/grafana/grafana/pkg/services/ngalert/store"
|
||||
@ -2540,14 +2539,6 @@ func createUser(t *testing.T, store *sqlstore.SQLStore, cmd user.CreateUserComma
|
||||
return u.ID
|
||||
}
|
||||
|
||||
func createOrg(t *testing.T, store *sqlstore.SQLStore, name string, userID int64) int64 {
|
||||
cmd := &models.CreateOrgCommand{Name: name, UserId: userID}
|
||||
err := store.CreateOrg(context.Background(), cmd)
|
||||
require.NoError(t, err)
|
||||
org := cmd.Result
|
||||
return org.Id
|
||||
}
|
||||
|
||||
func getLongString(t *testing.T, n int) string {
|
||||
t.Helper()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user