Grafana: Replace magic number with a constant variable in response status (#80132)
* Chore: Replace response status with const var * Apply suggestions from code review Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com> * Add net/http import --------- Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com>
This commit is contained in:
co-authored by
Sofia Papagiannaki
parent
a7fbe3c6dc
commit
96dfb385ca
+32
-32
@@ -51,12 +51,12 @@ func (hs *HTTPServer) GetDataSources(c *contextmodel.ReqContext) response.Respon
|
||||
|
||||
dataSources, err := hs.DataSourcesService.GetDataSources(c.Req.Context(), &query)
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to query datasources", err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to query datasources", err)
|
||||
}
|
||||
|
||||
filtered, err := hs.dsGuardian.New(c.SignedInUser.OrgID, c.SignedInUser).FilterDatasourcesByQueryPermissions(dataSources)
|
||||
if err != nil {
|
||||
return response.Error(500, "Failed to query datasources", err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to query datasources", err)
|
||||
}
|
||||
|
||||
result := make(dtos.DataSourceList, 0)
|
||||
@@ -125,12 +125,12 @@ func (hs *HTTPServer) GetDataSourceById(c *contextmodel.ReqContext) response.Res
|
||||
dataSource, err := hs.DataSourcesService.GetDataSource(c.Req.Context(), &query)
|
||||
if err != nil {
|
||||
if errors.Is(err, datasources.ErrDataSourceNotFound) {
|
||||
return response.Error(404, "Data source not found", nil)
|
||||
return response.Error(http.StatusNotFound, "Data source not found", nil)
|
||||
}
|
||||
if errors.Is(err, datasources.ErrDataSourceIdentifierNotSet) {
|
||||
return response.Error(400, "Datasource id is missing", nil)
|
||||
return response.Error(http.StatusBadRequest, "Datasource id is missing", nil)
|
||||
}
|
||||
return response.Error(500, "Failed to query datasources", err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to query datasources", err)
|
||||
}
|
||||
|
||||
dto := hs.convertModelToDtos(c.Req.Context(), dataSource)
|
||||
@@ -165,19 +165,19 @@ func (hs *HTTPServer) DeleteDataSourceById(c *contextmodel.ReqContext) response.
|
||||
}
|
||||
|
||||
if id <= 0 {
|
||||
return response.Error(400, "Missing valid datasource id", nil)
|
||||
return response.Error(http.StatusBadRequest, "Missing valid datasource id", nil)
|
||||
}
|
||||
|
||||
ds, err := hs.getRawDataSourceById(c.Req.Context(), id, c.SignedInUser.GetOrgID())
|
||||
if err != nil {
|
||||
if errors.Is(err, datasources.ErrDataSourceNotFound) {
|
||||
return response.Error(404, "Data source not found", nil)
|
||||
return response.Error(http.StatusNotFound, "Data source not found", nil)
|
||||
}
|
||||
return response.Error(400, "Failed to delete datasource", nil)
|
||||
return response.Error(http.StatusBadRequest, "Failed to delete datasource", nil)
|
||||
}
|
||||
|
||||
if ds.ReadOnly {
|
||||
return response.Error(403, "Cannot delete read-only data source", nil)
|
||||
return response.Error(http.StatusForbidden, "Cannot delete read-only data source", nil)
|
||||
}
|
||||
|
||||
cmd := &datasources.DeleteDataSourceCommand{ID: id, OrgID: c.SignedInUser.GetOrgID(), Name: ds.Name}
|
||||
@@ -185,9 +185,9 @@ func (hs *HTTPServer) DeleteDataSourceById(c *contextmodel.ReqContext) response.
|
||||
err = hs.DataSourcesService.DeleteDataSource(c.Req.Context(), cmd)
|
||||
if err != nil {
|
||||
if errors.As(err, &secretsPluginError) {
|
||||
return response.Error(500, "Failed to delete datasource: "+err.Error(), err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to delete datasource: "+err.Error(), err)
|
||||
}
|
||||
return response.Error(500, "Failed to delete datasource", err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to delete datasource", err)
|
||||
}
|
||||
|
||||
hs.Live.HandleDatasourceDelete(c.SignedInUser.GetOrgID(), ds.UID)
|
||||
@@ -244,19 +244,19 @@ func (hs *HTTPServer) DeleteDataSourceByUID(c *contextmodel.ReqContext) response
|
||||
uid := web.Params(c.Req)[":uid"]
|
||||
|
||||
if uid == "" {
|
||||
return response.Error(400, "Missing datasource uid", nil)
|
||||
return response.Error(http.StatusBadRequest, "Missing datasource uid", nil)
|
||||
}
|
||||
|
||||
ds, err := hs.getRawDataSourceByUID(c.Req.Context(), uid, c.SignedInUser.GetOrgID())
|
||||
if err != nil {
|
||||
if errors.Is(err, datasources.ErrDataSourceNotFound) {
|
||||
return response.Error(404, "Data source not found", nil)
|
||||
return response.Error(http.StatusNotFound, "Data source not found", nil)
|
||||
}
|
||||
return response.Error(400, "Failed to delete datasource", nil)
|
||||
return response.Error(http.StatusBadRequest, "Failed to delete datasource", nil)
|
||||
}
|
||||
|
||||
if ds.ReadOnly {
|
||||
return response.Error(403, "Cannot delete read-only data source", nil)
|
||||
return response.Error(http.StatusForbidden, "Cannot delete read-only data source", nil)
|
||||
}
|
||||
|
||||
cmd := &datasources.DeleteDataSourceCommand{UID: uid, OrgID: c.SignedInUser.GetOrgID(), Name: ds.Name}
|
||||
@@ -264,9 +264,9 @@ func (hs *HTTPServer) DeleteDataSourceByUID(c *contextmodel.ReqContext) response
|
||||
err = hs.DataSourcesService.DeleteDataSource(c.Req.Context(), cmd)
|
||||
if err != nil {
|
||||
if errors.As(err, &secretsPluginError) {
|
||||
return response.Error(500, "Failed to delete datasource: "+err.Error(), err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to delete datasource: "+err.Error(), err)
|
||||
}
|
||||
return response.Error(500, "Failed to delete datasource", err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to delete datasource", err)
|
||||
}
|
||||
|
||||
hs.Live.HandleDatasourceDelete(c.SignedInUser.GetOrgID(), ds.UID)
|
||||
@@ -294,29 +294,29 @@ func (hs *HTTPServer) DeleteDataSourceByName(c *contextmodel.ReqContext) respons
|
||||
name := web.Params(c.Req)[":name"]
|
||||
|
||||
if name == "" {
|
||||
return response.Error(400, "Missing valid datasource name", nil)
|
||||
return response.Error(http.StatusBadRequest, "Missing valid datasource name", nil)
|
||||
}
|
||||
|
||||
getCmd := &datasources.GetDataSourceQuery{Name: name, OrgID: c.SignedInUser.GetOrgID()}
|
||||
dataSource, err := hs.DataSourcesService.GetDataSource(c.Req.Context(), getCmd)
|
||||
if err != nil {
|
||||
if errors.Is(err, datasources.ErrDataSourceNotFound) {
|
||||
return response.Error(404, "Data source not found", nil)
|
||||
return response.Error(http.StatusNotFound, "Data source not found", nil)
|
||||
}
|
||||
return response.Error(500, "Failed to delete datasource", err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to delete datasource", err)
|
||||
}
|
||||
|
||||
if dataSource.ReadOnly {
|
||||
return response.Error(403, "Cannot delete read-only data source", nil)
|
||||
return response.Error(http.StatusForbidden, "Cannot delete read-only data source", nil)
|
||||
}
|
||||
|
||||
cmd := &datasources.DeleteDataSourceCommand{Name: name, OrgID: c.SignedInUser.GetOrgID()}
|
||||
err = hs.DataSourcesService.DeleteDataSource(c.Req.Context(), cmd)
|
||||
if err != nil {
|
||||
if errors.As(err, &secretsPluginError) {
|
||||
return response.Error(500, "Failed to delete datasource: "+err.Error(), err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to delete datasource: "+err.Error(), err)
|
||||
}
|
||||
return response.Error(500, "Failed to delete datasource", err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to delete datasource", err)
|
||||
}
|
||||
|
||||
hs.Live.HandleDatasourceDelete(c.SignedInUser.GetOrgID(), dataSource.UID)
|
||||
@@ -528,9 +528,9 @@ func (hs *HTTPServer) UpdateDataSourceByID(c *contextmodel.ReqContext) response.
|
||||
ds, err := hs.getRawDataSourceById(c.Req.Context(), cmd.ID, cmd.OrgID)
|
||||
if err != nil {
|
||||
if errors.Is(err, datasources.ErrDataSourceNotFound) {
|
||||
return response.Error(404, "Data source not found", nil)
|
||||
return response.Error(http.StatusNotFound, "Data source not found", nil)
|
||||
}
|
||||
return response.Error(500, "Failed to update datasource", err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to update datasource", err)
|
||||
}
|
||||
|
||||
// check if LBAC rules have been modified
|
||||
@@ -613,7 +613,7 @@ func checkTeamHTTPHeaderPermissions(hs *HTTPServer, c *contextmodel.ReqContext,
|
||||
|
||||
func (hs *HTTPServer) updateDataSourceByID(c *contextmodel.ReqContext, ds *datasources.DataSource, cmd datasources.UpdateDataSourceCommand) response.Response {
|
||||
if ds.ReadOnly {
|
||||
return response.Error(403, "Cannot update read-only data source", nil)
|
||||
return response.Error(http.StatusForbidden, "Cannot update read-only data source", nil)
|
||||
}
|
||||
|
||||
_, err := hs.DataSourcesService.UpdateDataSource(c.Req.Context(), &cmd)
|
||||
@@ -641,9 +641,9 @@ func (hs *HTTPServer) updateDataSourceByID(c *contextmodel.ReqContext, ds *datas
|
||||
dataSource, err := hs.DataSourcesService.GetDataSource(c.Req.Context(), &query)
|
||||
if err != nil {
|
||||
if errors.Is(err, datasources.ErrDataSourceNotFound) {
|
||||
return response.Error(404, "Data source not found", nil)
|
||||
return response.Error(http.StatusNotFound, "Data source not found", nil)
|
||||
}
|
||||
return response.Error(500, "Failed to query datasource", err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to query datasource", err)
|
||||
}
|
||||
|
||||
datasourceDTO := hs.convertModelToDtos(c.Req.Context(), dataSource)
|
||||
@@ -704,9 +704,9 @@ func (hs *HTTPServer) GetDataSourceByName(c *contextmodel.ReqContext) response.R
|
||||
dataSource, err := hs.DataSourcesService.GetDataSource(c.Req.Context(), &query)
|
||||
if err != nil {
|
||||
if errors.Is(err, datasources.ErrDataSourceNotFound) {
|
||||
return response.Error(404, "Data source not found", nil)
|
||||
return response.Error(http.StatusNotFound, "Data source not found", nil)
|
||||
}
|
||||
return response.Error(500, "Failed to query datasources", err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to query datasources", err)
|
||||
}
|
||||
|
||||
dto := hs.convertModelToDtos(c.Req.Context(), dataSource)
|
||||
@@ -732,9 +732,9 @@ func (hs *HTTPServer) GetDataSourceIdByName(c *contextmodel.ReqContext) response
|
||||
ds, err := hs.DataSourcesService.GetDataSource(c.Req.Context(), &query)
|
||||
if err != nil {
|
||||
if errors.Is(err, datasources.ErrDataSourceNotFound) {
|
||||
return response.Error(404, "Data source not found", nil)
|
||||
return response.Error(http.StatusNotFound, "Data source not found", nil)
|
||||
}
|
||||
return response.Error(500, "Failed to query datasources", err)
|
||||
return response.Error(http.StatusInternalServerError, "Failed to query datasources", err)
|
||||
}
|
||||
|
||||
dtos := dtos.AnyId{
|
||||
|
||||
Reference in New Issue
Block a user