* Chore: Collect elasticsearch version usage stats
* Fix lint error
* use GetDataSources from sqlstore
* Apply review suggestions
* Return error if datasource type is not specified
* Update pkg/services/sqlstore/datasource.go
* fix undefined var
(cherry picked from commit 0c2045109e)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -18,6 +19,7 @@ import (
|
||||
|
||||
func init() {
|
||||
bus.AddHandler("sql", GetDataSources)
|
||||
bus.AddHandler("sql", GetDataSourcesByType)
|
||||
bus.AddHandler("sql", GetDataSource)
|
||||
bus.AddHandler("sql", AddDataSource)
|
||||
bus.AddHandler("sql", DeleteDataSource)
|
||||
@@ -71,10 +73,21 @@ func GetDataSources(query *models.GetDataSourcesQuery) error {
|
||||
} else {
|
||||
sess = x.Limit(query.DataSourceLimit, 0).Where("org_id=?", query.OrgId).Asc("name")
|
||||
}
|
||||
|
||||
query.Result = make([]*models.DataSource, 0)
|
||||
return sess.Find(&query.Result)
|
||||
}
|
||||
|
||||
// GetDataSourcesByType returns all datasources for a given type or an error if the specified type is an empty string
|
||||
func GetDataSourcesByType(query *models.GetDataSourcesByTypeQuery) error {
|
||||
if query.Type == "" {
|
||||
return fmt.Errorf("datasource type cannot be empty")
|
||||
}
|
||||
|
||||
query.Result = make([]*models.DataSource, 0)
|
||||
return x.Where("type=?", query.Type).Asc("id").Find(&query.Result)
|
||||
}
|
||||
|
||||
// GetDefaultDataSource is used to get the default datasource of organization
|
||||
func GetDefaultDataSource(query *models.GetDefaultDataSourceQuery) error {
|
||||
datasource := models.DataSource{}
|
||||
|
||||
@@ -218,7 +218,7 @@ func TestDataAccess(t *testing.T) {
|
||||
require.Equal(t, 0, len(query.Result))
|
||||
})
|
||||
|
||||
t.Run("GetDataSource", func(t *testing.T) {
|
||||
t.Run("GetDataSources", func(t *testing.T) {
|
||||
t.Run("Number of data sources returned limited to 6 per organization", func(t *testing.T) {
|
||||
InitTestDB(t)
|
||||
datasourceLimit := 6
|
||||
@@ -288,6 +288,49 @@ func TestDataAccess(t *testing.T) {
|
||||
require.Equal(t, numberOfDatasource, len(query.Result))
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("GetDataSourcesByType", func(t *testing.T) {
|
||||
t.Run("Only returns datasources of specified type", func(t *testing.T) {
|
||||
InitTestDB(t)
|
||||
|
||||
err := AddDataSource(&models.AddDataSourceCommand{
|
||||
OrgId: 10,
|
||||
Name: "Elasticsearch",
|
||||
Type: models.DS_ES,
|
||||
Access: models.DS_ACCESS_DIRECT,
|
||||
Url: "http://test",
|
||||
Database: "site",
|
||||
ReadOnly: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = AddDataSource(&models.AddDataSourceCommand{
|
||||
OrgId: 10,
|
||||
Name: "Graphite",
|
||||
Type: models.DS_GRAPHITE,
|
||||
Access: models.DS_ACCESS_DIRECT,
|
||||
Url: "http://test",
|
||||
Database: "site",
|
||||
ReadOnly: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
query := models.GetDataSourcesByTypeQuery{Type: models.DS_ES}
|
||||
|
||||
err = GetDataSourcesByType(&query)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(query.Result))
|
||||
})
|
||||
|
||||
t.Run("Returns an error if no type specified", func(t *testing.T) {
|
||||
query := models.GetDataSourcesByTypeQuery{}
|
||||
|
||||
err := GetDataSourcesByType(&query)
|
||||
|
||||
require.Error(t, err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetDefaultDataSource(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user