org.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/api/dtos"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/infra/metrics"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/setting"
  8. "github.com/grafana/grafana/pkg/util"
  9. )
  10. // GET /api/org
  11. func GetOrgCurrent(c *m.ReqContext) Response {
  12. return getOrgHelper(c.OrgId)
  13. }
  14. // GET /api/orgs/:orgId
  15. func GetOrgByID(c *m.ReqContext) Response {
  16. return getOrgHelper(c.ParamsInt64(":orgId"))
  17. }
  18. // Get /api/orgs/name/:name
  19. func GetOrgByName(c *m.ReqContext) Response {
  20. query := m.GetOrgByNameQuery{Name: c.Params(":name")}
  21. if err := bus.Dispatch(&query); err != nil {
  22. if err == m.ErrOrgNotFound {
  23. return Error(404, "Organization not found", err)
  24. }
  25. return Error(500, "Failed to get organization", err)
  26. }
  27. org := query.Result
  28. result := m.OrgDetailsDTO{
  29. Id: org.Id,
  30. Name: org.Name,
  31. Address: m.Address{
  32. Address1: org.Address1,
  33. Address2: org.Address2,
  34. City: org.City,
  35. ZipCode: org.ZipCode,
  36. State: org.State,
  37. Country: org.Country,
  38. },
  39. }
  40. return JSON(200, &result)
  41. }
  42. func getOrgHelper(orgID int64) Response {
  43. query := m.GetOrgByIdQuery{Id: orgID}
  44. if err := bus.Dispatch(&query); err != nil {
  45. if err == m.ErrOrgNotFound {
  46. return Error(404, "Organization not found", err)
  47. }
  48. return Error(500, "Failed to get organization", err)
  49. }
  50. org := query.Result
  51. result := m.OrgDetailsDTO{
  52. Id: org.Id,
  53. Name: org.Name,
  54. Address: m.Address{
  55. Address1: org.Address1,
  56. Address2: org.Address2,
  57. City: org.City,
  58. ZipCode: org.ZipCode,
  59. State: org.State,
  60. Country: org.Country,
  61. },
  62. }
  63. return JSON(200, &result)
  64. }
  65. // POST /api/orgs
  66. func CreateOrg(c *m.ReqContext, cmd m.CreateOrgCommand) Response {
  67. if !c.IsSignedIn || (!setting.AllowUserOrgCreate && !c.IsGrafanaAdmin) {
  68. return Error(403, "Access denied", nil)
  69. }
  70. cmd.UserId = c.UserId
  71. if err := bus.Dispatch(&cmd); err != nil {
  72. if err == m.ErrOrgNameTaken {
  73. return Error(409, "Organization name taken", err)
  74. }
  75. return Error(500, "Failed to create organization", err)
  76. }
  77. metrics.MApiOrgCreate.Inc()
  78. return JSON(200, &util.DynMap{
  79. "orgId": cmd.Result.Id,
  80. "message": "Organization created",
  81. })
  82. }
  83. // PUT /api/org
  84. func UpdateOrgCurrent(c *m.ReqContext, form dtos.UpdateOrgForm) Response {
  85. return updateOrgHelper(form, c.OrgId)
  86. }
  87. // PUT /api/orgs/:orgId
  88. func UpdateOrg(c *m.ReqContext, form dtos.UpdateOrgForm) Response {
  89. return updateOrgHelper(form, c.ParamsInt64(":orgId"))
  90. }
  91. func updateOrgHelper(form dtos.UpdateOrgForm, orgID int64) Response {
  92. cmd := m.UpdateOrgCommand{Name: form.Name, OrgId: orgID}
  93. if err := bus.Dispatch(&cmd); err != nil {
  94. if err == m.ErrOrgNameTaken {
  95. return Error(400, "Organization name taken", err)
  96. }
  97. return Error(500, "Failed to update organization", err)
  98. }
  99. return Success("Organization updated")
  100. }
  101. // PUT /api/org/address
  102. func UpdateOrgAddressCurrent(c *m.ReqContext, form dtos.UpdateOrgAddressForm) Response {
  103. return updateOrgAddressHelper(form, c.OrgId)
  104. }
  105. // PUT /api/orgs/:orgId/address
  106. func UpdateOrgAddress(c *m.ReqContext, form dtos.UpdateOrgAddressForm) Response {
  107. return updateOrgAddressHelper(form, c.ParamsInt64(":orgId"))
  108. }
  109. func updateOrgAddressHelper(form dtos.UpdateOrgAddressForm, orgID int64) Response {
  110. cmd := m.UpdateOrgAddressCommand{
  111. OrgId: orgID,
  112. Address: m.Address{
  113. Address1: form.Address1,
  114. Address2: form.Address2,
  115. City: form.City,
  116. State: form.State,
  117. ZipCode: form.ZipCode,
  118. Country: form.Country,
  119. },
  120. }
  121. if err := bus.Dispatch(&cmd); err != nil {
  122. return Error(500, "Failed to update org address", err)
  123. }
  124. return Success("Address updated")
  125. }
  126. // GET /api/orgs/:orgId
  127. func DeleteOrgByID(c *m.ReqContext) Response {
  128. if err := bus.Dispatch(&m.DeleteOrgCommand{Id: c.ParamsInt64(":orgId")}); err != nil {
  129. if err == m.ErrOrgNotFound {
  130. return Error(404, "Failed to delete organization. ID not found", nil)
  131. }
  132. return Error(500, "Failed to update organization", err)
  133. }
  134. return Success("Organization deleted")
  135. }
  136. func SearchOrgs(c *m.ReqContext) Response {
  137. query := m.SearchOrgsQuery{
  138. Query: c.Query("query"),
  139. Name: c.Query("name"),
  140. Page: 0,
  141. Limit: 1000,
  142. }
  143. if err := bus.Dispatch(&query); err != nil {
  144. return Error(500, "Failed to search orgs", err)
  145. }
  146. return JSON(200, query.Result)
  147. }