datasources.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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) {
  12. query := m.GetDataSourcesQuery{OrgId: c.OrgId}
  13. if err := bus.Dispatch(&query); err != nil {
  14. c.JsonApiErr(500, "Failed to query datasources", err)
  15. return
  16. }
  17. result := make(dtos.DataSourceList, 0)
  18. for _, ds := range query.Result {
  19. dsItem := dtos.DataSource{
  20. Id: ds.Id,
  21. OrgId: ds.OrgId,
  22. Name: ds.Name,
  23. Url: ds.Url,
  24. Type: ds.Type,
  25. Access: ds.Access,
  26. Password: ds.Password,
  27. Database: ds.Database,
  28. User: ds.User,
  29. BasicAuth: ds.BasicAuth,
  30. IsDefault: ds.IsDefault,
  31. JsonData: ds.JsonData,
  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. c.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 DeleteDataSource(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. cmd := &m.DeleteDataSourceCommand{Id: id, OrgId: c.OrgId}
  65. err := bus.Dispatch(cmd)
  66. if err != nil {
  67. c.JsonApiErr(500, "Failed to delete datasource", err)
  68. return
  69. }
  70. c.JsonOK("Data source deleted")
  71. }
  72. func AddDataSource(c *middleware.Context, cmd m.AddDataSourceCommand) {
  73. cmd.OrgId = c.OrgId
  74. if err := bus.Dispatch(&cmd); err != nil {
  75. if err == m.ErrDataSourceNameExists {
  76. c.JsonApiErr(409, err.Error(), err)
  77. return
  78. }
  79. c.JsonApiErr(500, "Failed to add datasource", err)
  80. return
  81. }
  82. c.JSON(200, util.DynMap{"message": "Datasource added", "id": cmd.Result.Id})
  83. }
  84. func UpdateDataSource(c *middleware.Context, cmd m.UpdateDataSourceCommand) Response {
  85. cmd.OrgId = c.OrgId
  86. cmd.Id = c.ParamsInt64(":id")
  87. err := fillWithSecureJsonData(&cmd)
  88. if err != nil {
  89. return ApiError(500, "Failed to update datasource", err)
  90. }
  91. err = bus.Dispatch(&cmd)
  92. if err != nil {
  93. return ApiError(500, "Failed to update datasource", err)
  94. }
  95. return Json(200, "Datasource updated")
  96. }
  97. func fillWithSecureJsonData(cmd *m.UpdateDataSourceCommand) error {
  98. if len(cmd.SecureJsonData) == 0 {
  99. return nil
  100. }
  101. ds, err := getRawDataSourceById(cmd.Id, cmd.OrgId)
  102. if err != nil {
  103. return err
  104. }
  105. secureJsonData := ds.SecureJsonData.Decrypt()
  106. for k, v := range secureJsonData {
  107. if _, ok := cmd.SecureJsonData[k]; !ok {
  108. cmd.SecureJsonData[k] = v
  109. }
  110. }
  111. return nil
  112. }
  113. func getRawDataSourceById(id int64, orgId int64) (*m.DataSource, error) {
  114. query := m.GetDataSourceByIdQuery{
  115. Id: id,
  116. OrgId: orgId,
  117. }
  118. if err := bus.Dispatch(&query); err != nil {
  119. return nil, err
  120. }
  121. return query.Result, nil
  122. }
  123. // Get /api/datasources/name/:name
  124. func GetDataSourceByName(c *middleware.Context) Response {
  125. query := m.GetDataSourceByNameQuery{Name: c.Params(":name"), OrgId: c.OrgId}
  126. if err := bus.Dispatch(&query); err != nil {
  127. if err == m.ErrDataSourceNotFound {
  128. return ApiError(404, "Data source not found", nil)
  129. }
  130. return ApiError(500, "Failed to query datasources", err)
  131. }
  132. dtos := convertModelToDtos(query.Result)
  133. return Json(200, &dtos)
  134. }
  135. // Get /api/datasources/id/:name
  136. func GetDataSourceIdByName(c *middleware.Context) Response {
  137. query := m.GetDataSourceByNameQuery{Name: c.Params(":name"), OrgId: c.OrgId}
  138. if err := bus.Dispatch(&query); err != nil {
  139. if err == m.ErrDataSourceNotFound {
  140. return ApiError(404, "Data source not found", nil)
  141. }
  142. return ApiError(500, "Failed to query datasources", err)
  143. }
  144. ds := query.Result
  145. dtos := dtos.AnyId{
  146. Id: ds.Id,
  147. }
  148. return Json(200, &dtos)
  149. }
  150. func convertModelToDtos(ds *m.DataSource) dtos.DataSource {
  151. dto := dtos.DataSource{
  152. Id: ds.Id,
  153. OrgId: ds.OrgId,
  154. Name: ds.Name,
  155. Url: ds.Url,
  156. Type: ds.Type,
  157. Access: ds.Access,
  158. Password: ds.Password,
  159. Database: ds.Database,
  160. User: ds.User,
  161. BasicAuth: ds.BasicAuth,
  162. BasicAuthUser: ds.BasicAuthUser,
  163. BasicAuthPassword: ds.BasicAuthPassword,
  164. WithCredentials: ds.WithCredentials,
  165. IsDefault: ds.IsDefault,
  166. JsonData: ds.JsonData,
  167. }
  168. if len(ds.SecureJsonData) > 0 {
  169. dto.TLSAuth.CACertSet = len(ds.SecureJsonData["tlsCACert"]) > 0
  170. dto.TLSAuth.ClientCertSet = len(ds.SecureJsonData["tlsClientCert"]) > 0
  171. dto.TLSAuth.ClientKeySet = len(ds.SecureJsonData["tlsClientKey"]) > 0
  172. }
  173. for k, v := range ds.SecureJsonData {
  174. if len(v) > 0 {
  175. dto.EncryptedFields = append(dto.EncryptedFields, k)
  176. }
  177. }
  178. return dto
  179. }