Chore: Return correct error for name taken and validation error on add/update datasource (#70465)
This commit is contained in:
+11
-6
@@ -390,14 +390,14 @@ func (hs *HTTPServer) AddDataSource(c *contextmodel.ReqContext) response.Respons
|
||||
dataSource, err := hs.DataSourcesService.AddDataSource(c.Req.Context(), &cmd)
|
||||
if err != nil {
|
||||
if errors.Is(err, datasources.ErrDataSourceNameExists) || errors.Is(err, datasources.ErrDataSourceUidExists) {
|
||||
return response.Error(409, err.Error(), err)
|
||||
return response.Error(http.StatusConflict, err.Error(), err)
|
||||
}
|
||||
|
||||
if errors.As(err, &secretsPluginError) {
|
||||
return response.Error(500, "Failed to add datasource: "+err.Error(), err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to add datasource: "+err.Error(), err)
|
||||
}
|
||||
|
||||
return response.Error(500, "Failed to add datasource", err)
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "Failed to add datasource", err)
|
||||
}
|
||||
|
||||
// Clear permission cache for the user who's created the data source, so that new permissions are fetched for their next call
|
||||
@@ -512,14 +512,19 @@ func (hs *HTTPServer) updateDataSourceByID(c *contextmodel.ReqContext, ds *datas
|
||||
|
||||
_, err := hs.DataSourcesService.UpdateDataSource(c.Req.Context(), &cmd)
|
||||
if err != nil {
|
||||
if errors.Is(err, datasources.ErrDataSourceNameExists) {
|
||||
return response.Error(http.StatusConflict, "Failed to update datasource: "+err.Error(), err)
|
||||
}
|
||||
|
||||
if errors.Is(err, datasources.ErrDataSourceUpdatingOldVersion) {
|
||||
return response.Error(409, "Datasource has already been updated by someone else. Please reload and try again", err)
|
||||
return response.Error(http.StatusConflict, "Datasource has already been updated by someone else. Please reload and try again", err)
|
||||
}
|
||||
|
||||
if errors.As(err, &secretsPluginError) {
|
||||
return response.Error(500, "Failed to update datasource: "+err.Error(), err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to update datasource: "+err.Error(), err)
|
||||
}
|
||||
return response.Error(500, "Failed to update datasource", err)
|
||||
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "Failed to update datasource", err)
|
||||
}
|
||||
|
||||
query := datasources.GetDataSourceQuery{
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/datasources/permissions"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/web"
|
||||
"github.com/grafana/grafana/pkg/web/webtest"
|
||||
)
|
||||
|
||||
@@ -248,6 +249,38 @@ func TestUpdateDataSource_URLWithoutProtocol(t *testing.T) {
|
||||
assert.Equal(t, 200, sc.resp.Code)
|
||||
}
|
||||
|
||||
// Updating data source name where data source with same name exists.
|
||||
func TestUpdateDataSourceByID_DataSourceNameExists(t *testing.T) {
|
||||
hs := &HTTPServer{
|
||||
DataSourcesService: &dataSourcesServiceMock{
|
||||
expectedDatasource: &datasources.DataSource{},
|
||||
mockUpdateDataSource: func(ctx context.Context, cmd *datasources.UpdateDataSourceCommand) (*datasources.DataSource, error) {
|
||||
return nil, datasources.ErrDataSourceNameExists
|
||||
},
|
||||
},
|
||||
Cfg: setting.NewCfg(),
|
||||
AccessControl: acimpl.ProvideAccessControl(setting.NewCfg()),
|
||||
accesscontrolService: actest.FakeService{},
|
||||
Live: newTestLive(t, nil),
|
||||
}
|
||||
|
||||
sc := setupScenarioContext(t, "/api/datasources/1")
|
||||
|
||||
sc.m.Put(sc.url, routing.Wrap(func(c *contextmodel.ReqContext) response.Response {
|
||||
c.Req = web.SetURLParams(c.Req, map[string]string{":id": "1"})
|
||||
c.Req.Body = mockRequestBody(datasources.UpdateDataSourceCommand{
|
||||
Access: "direct",
|
||||
Type: "test",
|
||||
Name: "test",
|
||||
})
|
||||
return hs.UpdateDataSourceByID(c)
|
||||
}))
|
||||
|
||||
sc.fakeReqWithParams("PUT", sc.url, map[string]string{}).exec()
|
||||
|
||||
require.Equal(t, http.StatusConflict, sc.resp.Code)
|
||||
}
|
||||
|
||||
func TestAPI_datasources_AccessControl(t *testing.T) {
|
||||
type testCase struct {
|
||||
desc string
|
||||
@@ -359,6 +392,8 @@ type dataSourcesServiceMock struct {
|
||||
expectedDatasources []*datasources.DataSource
|
||||
expectedDatasource *datasources.DataSource
|
||||
expectedError error
|
||||
|
||||
mockUpdateDataSource func(ctx context.Context, cmd *datasources.UpdateDataSourceCommand) (*datasources.DataSource, error)
|
||||
}
|
||||
|
||||
func (m *dataSourcesServiceMock) GetDataSource(ctx context.Context, query *datasources.GetDataSourceQuery) (*datasources.DataSource, error) {
|
||||
@@ -386,6 +421,10 @@ func (m *dataSourcesServiceMock) AddDataSource(ctx context.Context, cmd *datasou
|
||||
}
|
||||
|
||||
func (m *dataSourcesServiceMock) UpdateDataSource(ctx context.Context, cmd *datasources.UpdateDataSourceCommand) (*datasources.DataSource, error) {
|
||||
if m.mockUpdateDataSource != nil {
|
||||
return m.mockUpdateDataSource(ctx, cmd)
|
||||
}
|
||||
|
||||
return m.expectedDatasource, m.expectedError
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user