Correlations: Add organization id (#72258)

* Add org_id to correlations

* Add tests

* Allow org_id to be null in case org_id=0 is used

* Create organization to ensure stable id is generated

* Fix linting

* Ensure backwards compatibility

* Add deprecation information

* Migrate correlations indices

* Default org_id when migrating

* Remove redundant default

* Make PK non-nullable
This commit is contained in:
Piotr Jamróz
2023-08-24 09:39:30 +02:00
committed by GitHub
parent 9b891480d6
commit b30e0aa5aa
11 changed files with 153 additions and 7 deletions

View File

@@ -134,6 +134,18 @@ func (c TestContext) getURL(url string, user User) string {
)
}
func (c TestContext) createOrg(name string) int64 {
c.t.Helper()
store := c.env.SQLStore
store.Cfg.AutoAssignOrg = false
quotaService := quotaimpl.ProvideService(store, store.Cfg)
orgService, err := orgimpl.ProvideService(store, store.Cfg, quotaService)
require.NoError(c.t, err)
orgId, err := orgService.GetOrCreate(context.Background(), name)
require.NoError(c.t, err)
return orgId
}
func (c TestContext) createUser(cmd user.CreateUserCommand) User {
c.t.Helper()
store := c.env.SQLStore

View File

@@ -29,6 +29,14 @@ func TestIntegrationReadCorrelation(t *testing.T) {
Login: "admin",
})
otherOrgId := ctx.createOrg("New organization")
otherOrgUser := ctx.createUser(user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin2",
Login: "admin2",
OrgID: otherOrgId,
})
viewerUser := ctx.createUser(user.CreateUserCommand{
DefaultOrgRole: string(org.RoleViewer),
Password: "viewer",
@@ -86,6 +94,14 @@ func TestIntegrationReadCorrelation(t *testing.T) {
}
dsWithoutCorrelations := ctx.createDs(createDsCommand)
createDsCommand = &datasources.AddDataSourceCommand{
Name: "with-correlations",
UID: dsWithCorrelations.UID, // reuse UID
Type: "loki",
OrgID: otherOrgId,
}
ctx.createDs(createDsCommand)
// This creates 2 records in the correlation table that should never be returned by the API.
// Given all tests in this file work on the assumption that only a single correlation exists,
// this covers the case where bad data exists in the database.
@@ -157,6 +173,25 @@ func TestIntegrationReadCorrelation(t *testing.T) {
require.NoError(t, res.Body.Close())
})
t.Run("Should correctly return correlations for current organization", func(t *testing.T) {
res := ctx.Get(GetParams{
url: "/api/datasources/correlations",
user: otherOrgUser,
})
require.Equal(t, http.StatusOK, res.StatusCode)
responseBody, err := io.ReadAll(res.Body)
require.NoError(t, err)
var response correlations.GetCorrelationsResponseBody
err = json.Unmarshal(responseBody, &response)
require.NoError(t, err)
require.Len(t, response.Correlations, 0)
require.NoError(t, res.Body.Close())
})
})
t.Run("Get all correlations for a given data source", func(t *testing.T) {