mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
api: fix so that datasources functions returns Response
This commit is contained in:
parent
89b27b35ba
commit
909601d6ab
@ -199,10 +199,10 @@ func (hs *HttpServer) registerRoutes() {
|
|||||||
// Data sources
|
// Data sources
|
||||||
apiRoute.Group("/datasources", func(datasourceRoute RouteRegister) {
|
apiRoute.Group("/datasources", func(datasourceRoute RouteRegister) {
|
||||||
datasourceRoute.Get("/", wrap(GetDataSources))
|
datasourceRoute.Get("/", wrap(GetDataSources))
|
||||||
datasourceRoute.Post("/", quota("data_source"), bind(m.AddDataSourceCommand{}), AddDataSource)
|
datasourceRoute.Post("/", quota("data_source"), bind(m.AddDataSourceCommand{}), wrap(AddDataSource))
|
||||||
datasourceRoute.Put("/:id", bind(m.UpdateDataSourceCommand{}), wrap(UpdateDataSource))
|
datasourceRoute.Put("/:id", bind(m.UpdateDataSourceCommand{}), wrap(UpdateDataSource))
|
||||||
datasourceRoute.Delete("/:id", DeleteDataSourceById)
|
datasourceRoute.Delete("/:id", wrap(DeleteDataSourceById))
|
||||||
datasourceRoute.Delete("/name/:name", DeleteDataSourceByName)
|
datasourceRoute.Delete("/name/:name", wrap(DeleteDataSourceByName))
|
||||||
datasourceRoute.Get("/:id", wrap(GetDataSourceById))
|
datasourceRoute.Get("/:id", wrap(GetDataSourceById))
|
||||||
datasourceRoute.Get("/name/:name", wrap(GetDataSourceByName))
|
datasourceRoute.Get("/name/:name", wrap(GetDataSourceByName))
|
||||||
}, reqOrgAdmin)
|
}, reqOrgAdmin)
|
||||||
|
@ -69,80 +69,70 @@ func GetDataSourceById(c *middleware.Context) Response {
|
|||||||
return Json(200, &dtos)
|
return Json(200, &dtos)
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteDataSourceById(c *middleware.Context) {
|
func DeleteDataSourceById(c *middleware.Context) Response {
|
||||||
id := c.ParamsInt64(":id")
|
id := c.ParamsInt64(":id")
|
||||||
|
|
||||||
if id <= 0 {
|
if id <= 0 {
|
||||||
c.JsonApiErr(400, "Missing valid datasource id", nil)
|
return ApiError(400, "Missing valid datasource id", nil)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ds, err := getRawDataSourceById(id, c.OrgId)
|
ds, err := getRawDataSourceById(id, c.OrgId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JsonApiErr(400, "Failed to delete datasource", nil)
|
return ApiError(400, "Failed to delete datasource", nil)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ds.ReadOnly {
|
if ds.ReadOnly {
|
||||||
c.JsonApiErr(403, "Cannot delete read-only data source", nil)
|
return ApiError(403, "Cannot delete read-only data source", nil)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := &m.DeleteDataSourceByIdCommand{Id: id, OrgId: c.OrgId}
|
cmd := &m.DeleteDataSourceByIdCommand{Id: id, OrgId: c.OrgId}
|
||||||
|
|
||||||
err = bus.Dispatch(cmd)
|
err = bus.Dispatch(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JsonApiErr(500, "Failed to delete datasource", err)
|
return ApiError(500, "Failed to delete datasource", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JsonOK("Data source deleted")
|
return ApiSuccess("Data source deleted")
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteDataSourceByName(c *middleware.Context) {
|
func DeleteDataSourceByName(c *middleware.Context) Response {
|
||||||
name := c.Params(":name")
|
name := c.Params(":name")
|
||||||
|
|
||||||
if name == "" {
|
if name == "" {
|
||||||
c.JsonApiErr(400, "Missing valid datasource name", nil)
|
return ApiError(400, "Missing valid datasource name", nil)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getCmd := &m.GetDataSourceByNameQuery{Name: name, OrgId: c.OrgId}
|
getCmd := &m.GetDataSourceByNameQuery{Name: name, OrgId: c.OrgId}
|
||||||
if err := bus.Dispatch(getCmd); err != nil {
|
if err := bus.Dispatch(getCmd); err != nil {
|
||||||
c.JsonApiErr(500, "Failed to delete datasource", err)
|
return ApiError(500, "Failed to delete datasource", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if getCmd.Result.ReadOnly {
|
if getCmd.Result.ReadOnly {
|
||||||
c.JsonApiErr(403, "Cannot delete read-only data source", nil)
|
return ApiError(403, "Cannot delete read-only data source", nil)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := &m.DeleteDataSourceByNameCommand{Name: name, OrgId: c.OrgId}
|
cmd := &m.DeleteDataSourceByNameCommand{Name: name, OrgId: c.OrgId}
|
||||||
err := bus.Dispatch(cmd)
|
err := bus.Dispatch(cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JsonApiErr(500, "Failed to delete datasource", err)
|
return ApiError(500, "Failed to delete datasource", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JsonOK("Data source deleted")
|
return ApiSuccess("Data source deleted")
|
||||||
}
|
}
|
||||||
|
|
||||||
func AddDataSource(c *middleware.Context, cmd m.AddDataSourceCommand) {
|
func AddDataSource(c *middleware.Context, cmd m.AddDataSourceCommand) Response {
|
||||||
cmd.OrgId = c.OrgId
|
cmd.OrgId = c.OrgId
|
||||||
|
|
||||||
if err := bus.Dispatch(&cmd); err != nil {
|
if err := bus.Dispatch(&cmd); err != nil {
|
||||||
if err == m.ErrDataSourceNameExists {
|
if err == m.ErrDataSourceNameExists {
|
||||||
c.JsonApiErr(409, err.Error(), err)
|
return ApiError(409, err.Error(), err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JsonApiErr(500, "Failed to add datasource", err)
|
return ApiError(500, "Failed to add datasource", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ds := convertModelToDtos(cmd.Result)
|
ds := convertModelToDtos(cmd.Result)
|
||||||
c.JSON(200, util.DynMap{
|
return Json(200, util.DynMap{
|
||||||
"message": "Datasource added",
|
"message": "Datasource added",
|
||||||
"id": cmd.Result.Id,
|
"id": cmd.Result.Id,
|
||||||
"name": cmd.Result.Name,
|
"name": cmd.Result.Name,
|
||||||
|
Loading…
Reference in New Issue
Block a user