datasources.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package api
  2. import (
  3. "sort"
  4. "github.com/grafana/grafana/pkg/api/dtos"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/middleware"
  7. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/plugins"
  9. "github.com/grafana/grafana/pkg/util"
  10. )
  11. func GetDataSources(c *middleware.Context) Response {
  12. query := m.GetDataSourcesQuery{OrgId: c.OrgId}
  13. if err := bus.Dispatch(&query); err != nil {
  14. return ApiError(500, "Failed to query datasources", err)
  15. }
  16. result := make(dtos.DataSourceList, 0)
  17. for _, ds := range query.Result {
  18. dsItem := dtos.DataSourceListItemDTO{
  19. Id: ds.Id,
  20. OrgId: ds.OrgId,
  21. Name: ds.Name,
  22. Url: ds.Url,
  23. Type: ds.Type,
  24. Access: ds.Access,
  25. Password: ds.Password,
  26. Database: ds.Database,
  27. User: ds.User,
  28. BasicAuth: ds.BasicAuth,
  29. IsDefault: ds.IsDefault,
  30. JsonData: ds.JsonData,
  31. ReadOnly: ds.ReadOnly,
  32. }
  33. if plugin, exists := plugins.DataSources[ds.Type]; exists {
  34. dsItem.TypeLogoUrl = plugin.Info.Logos.Small
  35. } else {
  36. dsItem.TypeLogoUrl = "public/img/icn-datasource.svg"
  37. }
  38. result = append(result, dsItem)
  39. }
  40. sort.Sort(result)
  41. return Json(200, &result)
  42. }
  43. func GetDataSourceById(c *middleware.Context) Response {
  44. query := m.GetDataSourceByIdQuery{
  45. Id: c.ParamsInt64(":id"),
  46. OrgId: c.OrgId,
  47. }
  48. if err := bus.Dispatch(&query); err != nil {
  49. if err == m.ErrDataSourceNotFound {
  50. return ApiError(404, "Data source not found", nil)
  51. }
  52. return ApiError(500, "Failed to query datasources", err)
  53. }
  54. ds := query.Result
  55. dtos := convertModelToDtos(ds)
  56. return Json(200, &dtos)
  57. }
  58. func DeleteDataSourceById(c *middleware.Context) {
  59. id := c.ParamsInt64(":id")
  60. if id <= 0 {
  61. c.JsonApiErr(400, "Missing valid datasource id", nil)
  62. return
  63. }
  64. ds, err := getRawDataSourceById(id, c.OrgId)
  65. if err != nil {
  66. c.JsonApiErr(400, "Failed to delete datasource", nil)
  67. return
  68. }
  69. if ds.ReadOnly {
  70. c.JsonApiErr(403, "Cannot delete read-only data source", nil)
  71. return
  72. }
  73. cmd := &m.DeleteDataSourceByIdCommand{Id: id, OrgId: c.OrgId}
  74. err = bus.Dispatch(cmd)
  75. if err != nil {
  76. c.JsonApiErr(500, "Failed to delete datasource", err)
  77. return
  78. }
  79. c.JsonOK("Data source deleted")
  80. }
  81. func DeleteDataSourceByName(c *middleware.Context) {
  82. name := c.Params(":name")
  83. if name == "" {
  84. c.JsonApiErr(400, "Missing valid datasource name", nil)
  85. return
  86. }
  87. getCmd := &m.GetDataSourceByNameQuery{Name: name, OrgId: c.OrgId}
  88. if err := bus.Dispatch(getCmd); err != nil {
  89. c.JsonApiErr(500, "Failed to delete datasource", err)
  90. return
  91. }
  92. if getCmd.Result.ReadOnly {
  93. c.JsonApiErr(403, "Cannot delete read-only data source", nil)
  94. return
  95. }
  96. cmd := &m.DeleteDataSourceByNameCommand{Name: name, OrgId: c.OrgId}
  97. err := bus.Dispatch(cmd)
  98. if err != nil {
  99. c.JsonApiErr(500, "Failed to delete datasource", err)
  100. return
  101. }
  102. c.JsonOK("Data source deleted")
  103. }
  104. func AddDataSource(c *middleware.Context, cmd m.AddDataSourceCommand) {
  105. cmd.OrgId = c.OrgId
  106. if err := bus.Dispatch(&cmd); err != nil {
  107. if err == m.ErrDataSourceNameExists {
  108. c.JsonApiErr(409, err.Error(), err)
  109. return
  110. }
  111. c.JsonApiErr(500, "Failed to add datasource", err)
  112. return
  113. }
  114. ds := convertModelToDtos(cmd.Result)
  115. c.JSON(200, util.DynMap{
  116. "message": "Datasource added",
  117. "id": cmd.Result.Id,
  118. "name": cmd.Result.Name,
  119. "datasource": ds,
  120. })
  121. }
  122. func UpdateDataSource(c *middleware.Context, cmd m.UpdateDataSourceCommand) Response {
  123. cmd.OrgId = c.OrgId
  124. cmd.Id = c.ParamsInt64(":id")
  125. err := fillWithSecureJsonData(&cmd)
  126. if err != nil {
  127. return ApiError(500, "Failed to update datasource", err)
  128. }
  129. err = bus.Dispatch(&cmd)
  130. if err != nil {
  131. if err == m.ErrDataSourceUpdatingOldVersion {
  132. return ApiError(500, "Failed to update datasource. Reload new version and try again", err)
  133. } else {
  134. return ApiError(500, "Failed to update datasource", err)
  135. }
  136. }
  137. ds := convertModelToDtos(cmd.Result)
  138. return Json(200, util.DynMap{
  139. "message": "Datasource updated",
  140. "id": cmd.Id,
  141. "name": cmd.Name,
  142. "datasource": ds,
  143. })
  144. }
  145. func fillWithSecureJsonData(cmd *m.UpdateDataSourceCommand) error {
  146. if len(cmd.SecureJsonData) == 0 {
  147. return nil
  148. }
  149. ds, err := getRawDataSourceById(cmd.Id, cmd.OrgId)
  150. if err != nil {
  151. return err
  152. }
  153. if ds.ReadOnly {
  154. return m.ErrDatasourceIsReadOnly
  155. }
  156. secureJsonData := ds.SecureJsonData.Decrypt()
  157. for k, v := range secureJsonData {
  158. if _, ok := cmd.SecureJsonData[k]; !ok {
  159. cmd.SecureJsonData[k] = v
  160. }
  161. }
  162. return nil
  163. }
  164. func getRawDataSourceById(id int64, orgId int64) (*m.DataSource, error) {
  165. query := m.GetDataSourceByIdQuery{
  166. Id: id,
  167. OrgId: orgId,
  168. }
  169. if err := bus.Dispatch(&query); err != nil {
  170. return nil, err
  171. }
  172. return query.Result, nil
  173. }
  174. // Get /api/datasources/name/:name
  175. func GetDataSourceByName(c *middleware.Context) Response {
  176. query := m.GetDataSourceByNameQuery{Name: c.Params(":name"), OrgId: c.OrgId}
  177. if err := bus.Dispatch(&query); err != nil {
  178. if err == m.ErrDataSourceNotFound {
  179. return ApiError(404, "Data source not found", nil)
  180. }
  181. return ApiError(500, "Failed to query datasources", err)
  182. }
  183. dtos := convertModelToDtos(query.Result)
  184. dtos.ReadOnly = true
  185. return Json(200, &dtos)
  186. }
  187. // Get /api/datasources/id/:name
  188. func GetDataSourceIdByName(c *middleware.Context) Response {
  189. query := m.GetDataSourceByNameQuery{Name: c.Params(":name"), OrgId: c.OrgId}
  190. if err := bus.Dispatch(&query); err != nil {
  191. if err == m.ErrDataSourceNotFound {
  192. return ApiError(404, "Data source not found", nil)
  193. }
  194. return ApiError(500, "Failed to query datasources", err)
  195. }
  196. ds := query.Result
  197. dtos := dtos.AnyId{
  198. Id: ds.Id,
  199. }
  200. return Json(200, &dtos)
  201. }
  202. func convertModelToDtos(ds *m.DataSource) dtos.DataSource {
  203. dto := dtos.DataSource{
  204. Id: ds.Id,
  205. OrgId: ds.OrgId,
  206. Name: ds.Name,
  207. Url: ds.Url,
  208. Type: ds.Type,
  209. Access: ds.Access,
  210. Password: ds.Password,
  211. Database: ds.Database,
  212. User: ds.User,
  213. BasicAuth: ds.BasicAuth,
  214. BasicAuthUser: ds.BasicAuthUser,
  215. BasicAuthPassword: ds.BasicAuthPassword,
  216. WithCredentials: ds.WithCredentials,
  217. IsDefault: ds.IsDefault,
  218. JsonData: ds.JsonData,
  219. SecureJsonFields: map[string]bool{},
  220. Version: ds.Version,
  221. ReadOnly: ds.ReadOnly,
  222. }
  223. for k, v := range ds.SecureJsonData {
  224. if len(v) > 0 {
  225. dto.SecureJsonFields[k] = true
  226. }
  227. }
  228. return dto
  229. }