mirror of
https://github.com/grafana/grafana.git
synced 2024-11-27 03:11:01 -06:00
Chore: Delete org model duplicates (#60940)
* Delete org model duplicates * Fix lint * Move OrgDetailsDTO to org pkg
This commit is contained in:
parent
39b8d3a182
commit
bb35f37b66
@ -15,6 +15,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
||||
"github.com/grafana/grafana/pkg/services/auth"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
@ -67,7 +68,7 @@ func (hs *HTTPServer) AdminCreateUser(c *models.ReqContext) response.Response {
|
||||
|
||||
usr, err := hs.userService.Create(c.Req.Context(), &cmd)
|
||||
if err != nil {
|
||||
if errors.Is(err, models.ErrOrgNotFound) {
|
||||
if errors.Is(err, org.ErrOrgNotFound) {
|
||||
return response.Error(400, err.Error(), nil)
|
||||
}
|
||||
|
||||
|
@ -203,7 +203,7 @@ func TestAdminAPIEndpoint(t *testing.T) {
|
||||
Password: testPassword,
|
||||
OrgId: nonExistingOrgID,
|
||||
}
|
||||
usrSvc := &usertest.FakeUserService{ExpectedError: models.ErrOrgNotFound}
|
||||
usrSvc := &usertest.FakeUserService{ExpectedError: org.ErrOrgNotFound}
|
||||
adminCreateUserScenario(t, "Should create the user", "/api/admin/users", "/api/admin/users", createCmd, usrSvc, func(sc *scenarioContext) {
|
||||
sc.fakeReqWithParams("POST", sc.url, map[string]string{}).exec()
|
||||
assert.Equal(t, 400, sc.resp.Code)
|
||||
|
@ -64,16 +64,16 @@ func (hs *HTTPServer) GetOrgByID(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) GetOrgByName(c *models.ReqContext) response.Response {
|
||||
orga, err := hs.orgService.GetByName(c.Req.Context(), &org.GetOrgByNameQuery{Name: web.Params(c.Req)[":name"]})
|
||||
if err != nil {
|
||||
if errors.Is(err, models.ErrOrgNotFound) {
|
||||
if errors.Is(err, org.ErrOrgNotFound) {
|
||||
return response.Error(http.StatusNotFound, "Organization not found", err)
|
||||
}
|
||||
|
||||
return response.Error(http.StatusInternalServerError, "Failed to get organization", err)
|
||||
}
|
||||
result := models.OrgDetailsDTO{
|
||||
Id: orga.ID,
|
||||
result := org.OrgDetailsDTO{
|
||||
ID: orga.ID,
|
||||
Name: orga.Name,
|
||||
Address: models.Address{
|
||||
Address: org.Address{
|
||||
Address1: orga.Address1,
|
||||
Address2: orga.Address2,
|
||||
City: orga.City,
|
||||
@ -87,21 +87,21 @@ func (hs *HTTPServer) GetOrgByName(c *models.ReqContext) response.Response {
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) getOrgHelper(ctx context.Context, orgID int64) response.Response {
|
||||
query := org.GetOrgByIdQuery{ID: orgID}
|
||||
query := org.GetOrgByIDQuery{ID: orgID}
|
||||
|
||||
res, err := hs.orgService.GetByID(ctx, &query)
|
||||
if err != nil {
|
||||
if errors.Is(err, models.ErrOrgNotFound) {
|
||||
if errors.Is(err, org.ErrOrgNotFound) {
|
||||
return response.Error(http.StatusNotFound, "Organization not found", err)
|
||||
}
|
||||
return response.Error(http.StatusInternalServerError, "Failed to get organization", err)
|
||||
}
|
||||
|
||||
orga := res
|
||||
result := models.OrgDetailsDTO{
|
||||
Id: orga.ID,
|
||||
result := org.OrgDetailsDTO{
|
||||
ID: orga.ID,
|
||||
Name: orga.Name,
|
||||
Address: models.Address{
|
||||
Address: org.Address{
|
||||
Address1: orga.Address1,
|
||||
Address2: orga.Address2,
|
||||
City: orga.City,
|
||||
@ -139,7 +139,7 @@ func (hs *HTTPServer) CreateOrg(c *models.ReqContext) response.Response {
|
||||
cmd.UserID = c.UserID
|
||||
result, err := hs.orgService.CreateWithMember(c.Req.Context(), &cmd)
|
||||
if err != nil {
|
||||
if errors.Is(err, models.ErrOrgNameTaken) {
|
||||
if errors.Is(err, org.ErrOrgNameTaken) {
|
||||
return response.Error(http.StatusConflict, "Organization name taken", err)
|
||||
}
|
||||
return response.Error(http.StatusInternalServerError, "Failed to create organization", err)
|
||||
@ -199,7 +199,7 @@ func (hs *HTTPServer) UpdateOrg(c *models.ReqContext) response.Response {
|
||||
func (hs *HTTPServer) updateOrgHelper(ctx context.Context, form dtos.UpdateOrgForm, orgID int64) response.Response {
|
||||
cmd := org.UpdateOrgCommand{Name: form.Name, OrgId: orgID}
|
||||
if err := hs.orgService.UpdateOrg(ctx, &cmd); err != nil {
|
||||
if errors.Is(err, models.ErrOrgNameTaken) {
|
||||
if errors.Is(err, org.ErrOrgNameTaken) {
|
||||
return response.Error(http.StatusBadRequest, "Organization name taken", err)
|
||||
}
|
||||
return response.Error(http.StatusInternalServerError, "Failed to update organization", err)
|
||||
@ -293,7 +293,7 @@ func (hs *HTTPServer) DeleteOrgByID(c *models.ReqContext) response.Response {
|
||||
}
|
||||
|
||||
if err := hs.orgService.Delete(c.Req.Context(), &org.DeleteOrgCommand{ID: orgID}); err != nil {
|
||||
if errors.Is(err, models.ErrOrgNotFound) {
|
||||
if errors.Is(err, org.ErrOrgNotFound) {
|
||||
return response.Error(http.StatusNotFound, "Failed to delete organization. ID not found", nil)
|
||||
}
|
||||
return response.Error(http.StatusInternalServerError, "Failed to update organization", err)
|
||||
@ -406,7 +406,7 @@ type GetOrgByNameParams struct {
|
||||
type CreateOrgParams struct {
|
||||
// in:body
|
||||
// required:true
|
||||
Body models.CreateOrgCommand `json:"body"`
|
||||
Body org.CreateOrgCommand `json:"body"`
|
||||
}
|
||||
|
||||
// swagger:parameters searchOrgs
|
||||
@ -448,26 +448,26 @@ type CreateOrgResponse struct {
|
||||
type SearchOrgsResponse struct {
|
||||
// The response message
|
||||
// in: body
|
||||
Body []*models.OrgDTO `json:"body"`
|
||||
Body []*org.OrgDTO `json:"body"`
|
||||
}
|
||||
|
||||
// swagger:response getCurrentOrgResponse
|
||||
type GetCurrentOrgResponse struct {
|
||||
// The response message
|
||||
// in: body
|
||||
Body models.OrgDetailsDTO `json:"body"`
|
||||
Body org.OrgDetailsDTO `json:"body"`
|
||||
}
|
||||
|
||||
// swagger:response getOrgByIDResponse
|
||||
type GetOrgByIDResponse struct {
|
||||
// The response message
|
||||
// in: body
|
||||
Body models.OrgDetailsDTO `json:"body"`
|
||||
Body org.OrgDetailsDTO `json:"body"`
|
||||
}
|
||||
|
||||
// swagger:response getOrgByNameResponse
|
||||
type GetOrgByNameResponse struct {
|
||||
// The response message
|
||||
// in: body
|
||||
Body models.OrgDetailsDTO `json:"body"`
|
||||
Body org.OrgDetailsDTO `json:"body"`
|
||||
}
|
||||
|
@ -643,14 +643,14 @@ type UserResponse struct {
|
||||
type GetUserOrgListResponse struct {
|
||||
// The response message
|
||||
// in: body
|
||||
Body []*models.UserOrgDTO `json:"body"`
|
||||
Body []*org.UserOrgDTO `json:"body"`
|
||||
}
|
||||
|
||||
// swagger:response getSignedInUserOrgListResponse
|
||||
type GetSignedInUserOrgListResponse struct {
|
||||
// The response message
|
||||
// in: body
|
||||
Body []*models.UserOrgDTO `json:"body"`
|
||||
Body []*org.UserOrgDTO `json:"body"`
|
||||
}
|
||||
|
||||
// swagger:response getUserTeamsResponse
|
||||
|
@ -1,87 +0,0 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
)
|
||||
|
||||
// Typed errors
|
||||
var (
|
||||
ErrOrgNotFound = errors.New("organization not found")
|
||||
ErrOrgNameTaken = errors.New("organization name is taken")
|
||||
)
|
||||
|
||||
type Org struct {
|
||||
Id int64
|
||||
Version int
|
||||
Name string
|
||||
|
||||
Address1 string
|
||||
Address2 string
|
||||
City string
|
||||
ZipCode string
|
||||
State string
|
||||
Country string
|
||||
|
||||
Created time.Time
|
||||
Updated time.Time
|
||||
}
|
||||
|
||||
// ---------------------
|
||||
// COMMANDS
|
||||
|
||||
type CreateOrgCommand struct {
|
||||
Name string `json:"name" binding:"Required"`
|
||||
|
||||
// initial admin user for account
|
||||
UserId int64 `json:"-"`
|
||||
Result Org `json:"-"`
|
||||
}
|
||||
|
||||
type DeleteOrgCommand struct {
|
||||
Id int64
|
||||
}
|
||||
|
||||
type UpdateOrgAddressCommand struct {
|
||||
OrgId int64
|
||||
Address
|
||||
}
|
||||
|
||||
type GetOrgByIdQuery struct {
|
||||
Id int64
|
||||
Result *Org
|
||||
}
|
||||
|
||||
type GetOrgByNameQuery struct {
|
||||
Name string
|
||||
Result *Org
|
||||
}
|
||||
|
||||
type SearchOrgsQuery struct {
|
||||
Query string
|
||||
Name string
|
||||
Limit int
|
||||
Page int
|
||||
Ids []int64
|
||||
|
||||
Result []*OrgDTO
|
||||
}
|
||||
|
||||
type OrgDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type OrgDetailsDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Address Address `json:"address"`
|
||||
}
|
||||
|
||||
type UserOrgDTO struct {
|
||||
OrgId int64 `json:"orgId"`
|
||||
Name string `json:"name"`
|
||||
Role org.RoleType `json:"role"`
|
||||
}
|
@ -16,6 +16,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/middleware/cookies"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/models/usertoken"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/util"
|
||||
@ -215,7 +216,7 @@ func UseOrgFromContextParams(c *models.ReqContext) (int64, error) {
|
||||
|
||||
// Special case of macaron handling invalid params
|
||||
if orgID == 0 || err != nil {
|
||||
return 0, models.ErrOrgNotFound
|
||||
return 0, org.ErrOrgNotFound
|
||||
}
|
||||
|
||||
return orgID, nil
|
||||
|
@ -82,7 +82,7 @@ func (s *OrgSync) SyncOrgUser(ctx context.Context,
|
||||
// add role
|
||||
cmd := &org.AddOrgUserCommand{UserID: userID, Role: orgRole, OrgID: orgId}
|
||||
err := s.orgService.AddOrgUser(ctx, cmd)
|
||||
if err != nil && !errors.Is(err, models.ErrOrgNotFound) {
|
||||
if err != nil && !errors.Is(err, org.ErrOrgNotFound) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -295,7 +295,7 @@ func (ls *Implementation) syncOrgRoles(ctx context.Context, usr *user.User, extU
|
||||
// add role
|
||||
cmd := &org.AddOrgUserCommand{UserID: usr.ID, Role: orgRole, OrgID: orgId}
|
||||
err := ls.orgService.AddOrgUser(ctx, cmd)
|
||||
if err != nil && !errors.Is(err, models.ErrOrgNotFound) {
|
||||
if err != nil && !errors.Is(err, org.ErrOrgNotFound) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
@ -90,7 +90,7 @@ type OrgDTO struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type GetOrgByIdQuery struct {
|
||||
type GetOrgByIDQuery struct {
|
||||
ID int64
|
||||
}
|
||||
|
||||
@ -187,6 +187,12 @@ type SearchOrgUsersQueryResult struct {
|
||||
|
||||
type ByOrgName []*UserOrgDTO
|
||||
|
||||
type OrgDetailsDTO struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Address Address `json:"address"`
|
||||
}
|
||||
|
||||
// Len returns the length of an array of organisations.
|
||||
func (o ByOrgName) Len() int {
|
||||
return len(o)
|
||||
|
@ -11,7 +11,7 @@ type Service interface {
|
||||
GetUserOrgList(context.Context, *GetUserOrgListQuery) ([]*UserOrgDTO, error)
|
||||
UpdateOrg(context.Context, *UpdateOrgCommand) error
|
||||
Search(context.Context, *SearchOrgsQuery) ([]*OrgDTO, error)
|
||||
GetByID(context.Context, *GetOrgByIdQuery) (*Org, error)
|
||||
GetByID(context.Context, *GetOrgByIDQuery) (*Org, error)
|
||||
GetByName(context.Context, *GetOrgByNameQuery) (*Org, error)
|
||||
CreateWithMember(context.Context, *CreateOrgCommand) (*Org, error)
|
||||
UpdateAddress(context.Context, *UpdateOrgAddressCommand) error
|
||||
|
@ -121,7 +121,7 @@ func (s *Service) Search(ctx context.Context, query *org.SearchOrgsQuery) ([]*or
|
||||
return s.store.Search(ctx, query)
|
||||
}
|
||||
|
||||
func (s *Service) GetByID(ctx context.Context, query *org.GetOrgByIdQuery) (*org.Org, error) {
|
||||
func (s *Service) GetByID(ctx context.Context, query *org.GetOrgByIDQuery) (*org.Org, error) {
|
||||
return s.store.GetByID(ctx, query)
|
||||
}
|
||||
|
||||
|
@ -121,7 +121,7 @@ func (f *FakeOrgStore) GetOrgUsers(ctx context.Context, query *org.GetOrgUsersQu
|
||||
return f.ExpectedOrgUsers, f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakeOrgStore) GetByID(ctx context.Context, query *org.GetOrgByIdQuery) (*org.Org, error) {
|
||||
func (f *FakeOrgStore) GetByID(ctx context.Context, query *org.GetOrgByIDQuery) (*org.Org, error) {
|
||||
return f.ExpectedOrg, f.ExpectedError
|
||||
}
|
||||
|
||||
|
@ -40,7 +40,7 @@ type store interface {
|
||||
AddOrgUser(context.Context, *org.AddOrgUserCommand) error
|
||||
UpdateOrgUser(context.Context, *org.UpdateOrgUserCommand) error
|
||||
GetOrgUsers(context.Context, *org.GetOrgUsersQuery) ([]*org.OrgUserDTO, error)
|
||||
GetByID(context.Context, *org.GetOrgByIdQuery) (*org.Org, error)
|
||||
GetByID(context.Context, *org.GetOrgByIDQuery) (*org.Org, error)
|
||||
GetByName(context.Context, *org.GetOrgByNameQuery) (*org.Org, error)
|
||||
SearchOrgUsers(context.Context, *org.SearchOrgUsersQuery) (*org.SearchOrgUsersQueryResult, error)
|
||||
RemoveOrgUser(context.Context, *org.RemoveOrgUserCommand) error
|
||||
@ -129,28 +129,28 @@ func (ss *sqlStore) Update(ctx context.Context, cmd *org.UpdateOrgCommand) error
|
||||
if isNameTaken, err := isOrgNameTaken(cmd.Name, cmd.OrgId, sess); err != nil {
|
||||
return err
|
||||
} else if isNameTaken {
|
||||
return models.ErrOrgNameTaken
|
||||
return org.ErrOrgNameTaken
|
||||
}
|
||||
|
||||
org := org.Org{
|
||||
orga := org.Org{
|
||||
Name: cmd.Name,
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
affectedRows, err := sess.ID(cmd.OrgId).Update(&org)
|
||||
affectedRows, err := sess.ID(cmd.OrgId).Update(&orga)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if affectedRows == 0 {
|
||||
return models.ErrOrgNotFound
|
||||
return org.ErrOrgNotFound
|
||||
}
|
||||
|
||||
sess.PublishAfterCommit(&events.OrgUpdated{
|
||||
Timestamp: org.Updated,
|
||||
Id: org.ID,
|
||||
Name: org.Name,
|
||||
Timestamp: orga.Updated,
|
||||
Id: orga.ID,
|
||||
Name: orga.Name,
|
||||
})
|
||||
|
||||
return nil
|
||||
@ -207,7 +207,7 @@ func (ss *sqlStore) Delete(ctx context.Context, cmd *org.DeleteOrgCommand) error
|
||||
if res, err := sess.Query("SELECT 1 from org WHERE id=?", cmd.ID); err != nil {
|
||||
return err
|
||||
} else if len(res) != 1 {
|
||||
return models.ErrOrgNotFound
|
||||
return org.ErrOrgNotFound
|
||||
}
|
||||
|
||||
deletes := []string{
|
||||
@ -310,7 +310,7 @@ func (ss *sqlStore) CreateWithMember(ctx context.Context, cmd *org.CreateOrgComm
|
||||
if isNameTaken, err := isOrgNameTaken(cmd.Name, 0, sess); err != nil {
|
||||
return err
|
||||
} else if isNameTaken {
|
||||
return models.ErrOrgNameTaken
|
||||
return org.ErrOrgNameTaken
|
||||
}
|
||||
|
||||
if _, err := sess.Insert(&orga); err != nil {
|
||||
@ -364,7 +364,7 @@ func (ss *sqlStore) AddOrgUser(ctx context.Context, cmd *org.AddOrgUserCommand)
|
||||
if res, err := sess.Query("SELECT 1 from org WHERE id=?", cmd.OrgID); err != nil {
|
||||
return err
|
||||
} else if len(res) != 1 {
|
||||
return models.ErrOrgNotFound
|
||||
return org.ErrOrgNotFound
|
||||
}
|
||||
|
||||
entity := org.OrgUser{
|
||||
@ -589,7 +589,7 @@ func (ss *sqlStore) GetOrgUsers(ctx context.Context, query *org.GetOrgUsersQuery
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (ss *sqlStore) GetByID(ctx context.Context, query *org.GetOrgByIdQuery) (*org.Org, error) {
|
||||
func (ss *sqlStore) GetByID(ctx context.Context, query *org.GetOrgByIDQuery) (*org.Org, error) {
|
||||
var orga org.Org
|
||||
err := ss.db.WithDbSession(ctx, func(dbSession *db.Session) error {
|
||||
exists, err := dbSession.ID(query.ID).Get(&orga)
|
||||
@ -598,7 +598,7 @@ func (ss *sqlStore) GetByID(ctx context.Context, query *org.GetOrgByIdQuery) (*o
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return models.ErrOrgNotFound
|
||||
return org.ErrOrgNotFound
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@ -699,7 +699,7 @@ func (ss *sqlStore) GetByName(ctx context.Context, query *org.GetOrgByNameQuery)
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return models.ErrOrgNotFound
|
||||
return org.ErrOrgNotFound
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@ -739,7 +739,7 @@ func (ss *sqlStore) RemoveOrgUser(ctx context.Context, cmd *org.RemoveOrgUserCom
|
||||
}
|
||||
|
||||
// check user other orgs and update user current org
|
||||
var userOrgs []*models.UserOrgDTO
|
||||
var userOrgs []*org.UserOrgDTO
|
||||
sess.Table("org_user")
|
||||
sess.Join("INNER", "org", "org_user.org_id=org.id")
|
||||
sess.Where("org_user.user_id=?", usr.ID)
|
||||
@ -753,14 +753,14 @@ func (ss *sqlStore) RemoveOrgUser(ctx context.Context, cmd *org.RemoveOrgUserCom
|
||||
if len(userOrgs) > 0 {
|
||||
hasCurrentOrgSet := false
|
||||
for _, userOrg := range userOrgs {
|
||||
if usr.OrgID == userOrg.OrgId {
|
||||
if usr.OrgID == userOrg.OrgID {
|
||||
hasCurrentOrgSet = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !hasCurrentOrgSet {
|
||||
err = setUsingOrgInTransaction(sess, usr.ID, userOrgs[0].OrgId)
|
||||
err = setUsingOrgInTransaction(sess, usr.ID, userOrgs[0].OrgID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -158,7 +158,7 @@ func TestIntegrationOrgDataAccess(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("Get org by ID", func(t *testing.T) {
|
||||
query := &org.GetOrgByIdQuery{ID: 1}
|
||||
query := &org.GetOrgByIDQuery{ID: 1}
|
||||
result, err := orgStore.GetByID(context.Background(), query)
|
||||
|
||||
require.NoError(t, err)
|
||||
|
@ -54,7 +54,7 @@ func (f *FakeOrgService) Search(ctx context.Context, query *org.SearchOrgsQuery)
|
||||
return f.ExpectedOrgs, f.ExpectedError
|
||||
}
|
||||
|
||||
func (f *FakeOrgService) GetByID(ctx context.Context, query *org.GetOrgByIdQuery) (*org.Org, error) {
|
||||
func (f *FakeOrgService) GetByID(ctx context.Context, query *org.GetOrgByIDQuery) (*org.Org, error) {
|
||||
return f.ExpectedOrg, f.ExpectedError
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/org/orgtest"
|
||||
)
|
||||
|
||||
@ -28,11 +28,11 @@ func TestDashboardsAsConfig(t *testing.T) {
|
||||
orgFake := orgtest.NewOrgServiceFake()
|
||||
|
||||
t.Run("Should fail if orgs don't exist in the database", func(t *testing.T) {
|
||||
orgFake.ExpectedError = models.ErrOrgNotFound
|
||||
orgFake.ExpectedError = org.ErrOrgNotFound
|
||||
cfgProvider := configReader{path: appliedDefaults, log: logger, orgService: orgFake}
|
||||
_, err := cfgProvider.readConfig(context.Background())
|
||||
require.Error(t, err)
|
||||
assert.True(t, errors.Is(err, models.ErrOrgNotFound))
|
||||
assert.True(t, errors.Is(err, org.ErrOrgNotFound))
|
||||
orgFake.ExpectedError = nil
|
||||
})
|
||||
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
|
||||
models "github.com/grafana/grafana/pkg/models"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
)
|
||||
|
||||
// Store is an autogenerated mock type for the Store type
|
||||
@ -15,11 +16,11 @@ type Store struct {
|
||||
}
|
||||
|
||||
// GetOrgByNameHandler provides a mock function with given fields: ctx, query
|
||||
func (_m *Store) GetOrgByNameHandler(ctx context.Context, query *models.GetOrgByNameQuery) error {
|
||||
func (_m *Store) GetOrgByNameHandler(ctx context.Context, query * org.GetOrgByNameQuery) error {
|
||||
ret := _m.Called(ctx, query)
|
||||
|
||||
var r0 error
|
||||
if rf, ok := ret.Get(0).(func(context.Context, *models.GetOrgByNameQuery) error); ok {
|
||||
if rf, ok := ret.Get(0).(func(context.Context, *org.GetOrgByNameQuery) error); ok {
|
||||
r0 = rf(ctx, query)
|
||||
} else {
|
||||
r0 = ret.Error(0)
|
||||
|
@ -84,13 +84,6 @@ type mockStore struct {
|
||||
updateRequests []*pluginsettings.UpdateArgs
|
||||
}
|
||||
|
||||
func (m *mockStore) GetOrgByNameHandler(_ context.Context, query *models.GetOrgByNameQuery) error {
|
||||
if query.Name == "Org 4" {
|
||||
query.Result = &models.Org{Id: 4}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockStore) GetPluginSettingByPluginID(_ context.Context, args *pluginsettings.GetByPluginIDArgs) (*pluginsettings.DTO, error) {
|
||||
if args.PluginID == "test-plugin" && args.OrgID == 2 {
|
||||
return &pluginsettings.DTO{
|
||||
|
@ -14,10 +14,10 @@ type DashboardStore interface {
|
||||
}
|
||||
|
||||
func CheckOrgExists(ctx context.Context, orgService org.Service, orgID int64) error {
|
||||
query := org.GetOrgByIdQuery{ID: orgID}
|
||||
query := org.GetOrgByIDQuery{ID: orgID}
|
||||
_, err := orgService.GetByID(ctx, &query)
|
||||
if err != nil {
|
||||
if errors.Is(err, models.ErrOrgNotFound) {
|
||||
if errors.Is(err, org.ErrOrgNotFound) {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("failed to check whether org. with the given ID exists: %w", err)
|
||||
|
@ -17,6 +17,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
ngModels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/migrations"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/migrations/ualert"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
||||
@ -659,10 +660,10 @@ func createDatasource(t *testing.T, id int64, orgId int64, uid string) *datasour
|
||||
}
|
||||
}
|
||||
|
||||
func createOrg(t *testing.T, id int64) *models.Org {
|
||||
func createOrg(t *testing.T, id int64) *org.Org {
|
||||
t.Helper()
|
||||
return &models.Org{
|
||||
Id: id,
|
||||
return &org.Org{
|
||||
ID: id,
|
||||
Version: 1,
|
||||
Name: fmt.Sprintf("org_%d", id),
|
||||
Created: time.Now(),
|
||||
@ -701,7 +702,7 @@ func runDashAlertMigrationTestRun(t *testing.T, x *xorm.Engine) {
|
||||
func setupLegacyAlertsTables(t *testing.T, x *xorm.Engine, legacyChannels []*models.AlertNotification, alerts []*models.Alert) {
|
||||
t.Helper()
|
||||
|
||||
orgs := []models.Org{
|
||||
orgs := []org.Org{
|
||||
*createOrg(t, 1),
|
||||
*createOrg(t, 2),
|
||||
}
|
||||
|
@ -134,26 +134,26 @@ func (ss *SQLStore) createUser(ctx context.Context, sess *DBSession, args user.C
|
||||
}
|
||||
|
||||
func verifyExistingOrg(sess *DBSession, orgId int64) error {
|
||||
var org models.Org
|
||||
has, err := sess.Where("id=?", orgId).Get(&org)
|
||||
var orga org.Org
|
||||
has, err := sess.Where("id=?", orgId).Get(&orga)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !has {
|
||||
return models.ErrOrgNotFound
|
||||
return org.ErrOrgNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ss *SQLStore) getOrCreateOrg(sess *DBSession, orgName string) (int64, error) {
|
||||
var org models.Org
|
||||
var org org.Org
|
||||
if ss.Cfg.AutoAssignOrg {
|
||||
has, err := sess.Where("id=?", ss.Cfg.AutoAssignOrgId).Get(&org)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if has {
|
||||
return org.Id, nil
|
||||
return org.ID, nil
|
||||
}
|
||||
|
||||
if ss.Cfg.AutoAssignOrgId != 1 {
|
||||
@ -164,7 +164,7 @@ func (ss *SQLStore) getOrCreateOrg(sess *DBSession, orgName string) (int64, erro
|
||||
}
|
||||
|
||||
org.Name = mainOrgName
|
||||
org.Id = int64(ss.Cfg.AutoAssignOrgId)
|
||||
org.ID = int64(ss.Cfg.AutoAssignOrgId)
|
||||
} else {
|
||||
org.Name = orgName
|
||||
}
|
||||
@ -172,7 +172,7 @@ func (ss *SQLStore) getOrCreateOrg(sess *DBSession, orgName string) (int64, erro
|
||||
org.Created = time.Now()
|
||||
org.Updated = time.Now()
|
||||
|
||||
if org.Id != 0 {
|
||||
if org.ID != 0 {
|
||||
if _, err := sess.InsertId(&org, ss.Dialect); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@ -184,9 +184,9 @@ func (ss *SQLStore) getOrCreateOrg(sess *DBSession, orgName string) (int64, erro
|
||||
|
||||
sess.publishAfterCommit(&events.OrgCreated{
|
||||
Timestamp: org.Created,
|
||||
Id: org.Id,
|
||||
Id: org.ID,
|
||||
Name: org.Name,
|
||||
})
|
||||
|
||||
return org.Id, nil
|
||||
return org.ID, nil
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/db"
|
||||
@ -191,6 +192,10 @@ func (s *Service) GetByEmail(ctx context.Context, query *user.GetUserByEmailQuer
|
||||
}
|
||||
|
||||
func (s *Service) Update(ctx context.Context, cmd *user.UpdateUserCommand) error {
|
||||
if s.cfg.CaseInsensitiveLogin {
|
||||
cmd.Login = strings.ToLower(cmd.Login)
|
||||
cmd.Email = strings.ToLower(cmd.Email)
|
||||
}
|
||||
return s.store.Update(ctx, cmd)
|
||||
}
|
||||
|
||||
@ -440,7 +445,7 @@ func (s *Service) CreateUserForTests(ctx context.Context, cmd *user.CreateUserCo
|
||||
|
||||
func (s *Service) getOrgIDForNewUser(ctx context.Context, cmd *user.CreateUserCommand) (int64, error) {
|
||||
if s.cfg.AutoAssignOrg && cmd.OrgID != 0 {
|
||||
if _, err := s.orgService.GetByID(ctx, &org.GetOrgByIdQuery{ID: cmd.OrgID}); err != nil {
|
||||
if _, err := s.orgService.GetByID(ctx, &org.GetOrgByIDQuery{ID: cmd.OrgID}); err != nil {
|
||||
return -1, err
|
||||
}
|
||||
return cmd.OrgID, nil
|
||||
|
Loading…
Reference in New Issue
Block a user