2015-02-23 13:07:49 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-11-19 06:34:28 -06:00
|
|
|
"errors"
|
|
|
|
|
2015-09-08 07:22:44 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2015-02-23 13:07:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2019-02-23 16:35:26 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-03-30 03:12:24 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-06-22 01:13:21 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2015-02-23 13:07:49 -06:00
|
|
|
)
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
// GET /api/org
|
2020-03-04 05:57:20 -06:00
|
|
|
func GetOrgCurrent(c *models.ReqContext) Response {
|
2015-05-19 03:16:32 -05:00
|
|
|
return getOrgHelper(c.OrgId)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/orgs/:orgId
|
2020-03-04 05:57:20 -06:00
|
|
|
func GetOrgByID(c *models.ReqContext) Response {
|
2015-05-19 03:16:32 -05:00
|
|
|
return getOrgHelper(c.ParamsInt64(":orgId"))
|
|
|
|
}
|
|
|
|
|
2016-01-13 12:42:57 -06:00
|
|
|
// Get /api/orgs/name/:name
|
2021-01-07 04:36:13 -06:00
|
|
|
func (hs *HTTPServer) GetOrgByName(c *models.ReqContext) Response {
|
|
|
|
org, err := hs.SQLStore.GetOrgByName(c.Params(":name"))
|
|
|
|
if err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrOrgNotFound) {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(404, "Organization not found", err)
|
2016-01-12 15:50:56 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to get organization", err)
|
2016-01-12 15:50:56 -06:00
|
|
|
}
|
2020-03-04 05:57:20 -06:00
|
|
|
result := models.OrgDetailsDTO{
|
2016-01-12 15:50:56 -06:00
|
|
|
Id: org.Id,
|
|
|
|
Name: org.Name,
|
2020-03-04 05:57:20 -06:00
|
|
|
Address: models.Address{
|
2016-01-12 15:50:56 -06:00
|
|
|
Address1: org.Address1,
|
|
|
|
Address2: org.Address2,
|
|
|
|
City: org.City,
|
|
|
|
ZipCode: org.ZipCode,
|
|
|
|
State: org.State,
|
|
|
|
Country: org.Country,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, &result)
|
2016-01-12 15:50:56 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
func getOrgHelper(orgID int64) Response {
|
2020-03-04 05:57:20 -06:00
|
|
|
query := models.GetOrgByIdQuery{Id: orgID}
|
2015-02-23 13:07:49 -06:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrOrgNotFound) {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(404, "Organization not found", err)
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to get organization", err)
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
2015-09-08 07:22:44 -05:00
|
|
|
org := query.Result
|
2020-03-04 05:57:20 -06:00
|
|
|
result := models.OrgDetailsDTO{
|
2015-09-08 07:22:44 -05:00
|
|
|
Id: org.Id,
|
|
|
|
Name: org.Name,
|
2020-03-04 05:57:20 -06:00
|
|
|
Address: models.Address{
|
2015-09-08 07:22:44 -05:00
|
|
|
Address1: org.Address1,
|
|
|
|
Address2: org.Address2,
|
|
|
|
City: org.City,
|
|
|
|
ZipCode: org.ZipCode,
|
|
|
|
State: org.State,
|
|
|
|
Country: org.Country,
|
|
|
|
},
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, &result)
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
// POST /api/orgs
|
2020-03-04 05:57:20 -06:00
|
|
|
func CreateOrg(c *models.ReqContext, cmd models.CreateOrgCommand) Response {
|
2015-06-25 10:06:43 -05:00
|
|
|
if !c.IsSignedIn || (!setting.AllowUserOrgCreate && !c.IsGrafanaAdmin) {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(403, "Access denied", nil)
|
2015-03-30 03:12:24 -05:00
|
|
|
}
|
2015-02-23 13:07:49 -06:00
|
|
|
|
2015-03-30 03:12:24 -05:00
|
|
|
cmd.UserId = c.UserId
|
2015-02-23 13:07:49 -06:00
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrOrgNameTaken) {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(409, "Organization name taken", err)
|
2015-09-08 06:06:18 -05:00
|
|
|
}
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to create organization", err)
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MApiOrgCreate.Inc()
|
2015-03-22 14:14:00 -05:00
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, &util.DynMap{
|
2015-06-22 01:13:21 -05:00
|
|
|
"orgId": cmd.Result.Id,
|
|
|
|
"message": "Organization created",
|
|
|
|
})
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
// PUT /api/org
|
2020-03-04 05:57:20 -06:00
|
|
|
func UpdateOrgCurrent(c *models.ReqContext, form dtos.UpdateOrgForm) Response {
|
2015-09-08 07:22:44 -05:00
|
|
|
return updateOrgHelper(form, c.OrgId)
|
2015-05-19 03:16:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PUT /api/orgs/:orgId
|
2020-03-04 05:57:20 -06:00
|
|
|
func UpdateOrg(c *models.ReqContext, form dtos.UpdateOrgForm) Response {
|
2015-09-08 07:22:44 -05:00
|
|
|
return updateOrgHelper(form, c.ParamsInt64(":orgId"))
|
2015-05-19 03:16:32 -05:00
|
|
|
}
|
2015-02-23 13:07:49 -06:00
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
func updateOrgHelper(form dtos.UpdateOrgForm, orgID int64) Response {
|
2020-03-04 05:57:20 -06:00
|
|
|
cmd := models.UpdateOrgCommand{Name: form.Name, OrgId: orgID}
|
2015-02-23 13:07:49 -06:00
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrOrgNameTaken) {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(400, "Organization name taken", err)
|
2015-09-08 06:06:18 -05:00
|
|
|
}
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to update organization", err)
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Success("Organization updated")
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
2015-05-19 04:47:14 -05:00
|
|
|
|
2015-09-08 07:22:44 -05:00
|
|
|
// PUT /api/org/address
|
2020-03-04 05:57:20 -06:00
|
|
|
func UpdateOrgAddressCurrent(c *models.ReqContext, form dtos.UpdateOrgAddressForm) Response {
|
2015-09-08 07:22:44 -05:00
|
|
|
return updateOrgAddressHelper(form, c.OrgId)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PUT /api/orgs/:orgId/address
|
2020-03-04 05:57:20 -06:00
|
|
|
func UpdateOrgAddress(c *models.ReqContext, form dtos.UpdateOrgAddressForm) Response {
|
2015-09-08 07:22:44 -05:00
|
|
|
return updateOrgAddressHelper(form, c.ParamsInt64(":orgId"))
|
|
|
|
}
|
|
|
|
|
2018-03-22 06:37:35 -05:00
|
|
|
func updateOrgAddressHelper(form dtos.UpdateOrgAddressForm, orgID int64) Response {
|
2020-03-04 05:57:20 -06:00
|
|
|
cmd := models.UpdateOrgAddressCommand{
|
2018-03-22 06:37:35 -05:00
|
|
|
OrgId: orgID,
|
2020-03-04 05:57:20 -06:00
|
|
|
Address: models.Address{
|
2015-09-08 07:22:44 -05:00
|
|
|
Address1: form.Address1,
|
|
|
|
Address2: form.Address2,
|
|
|
|
City: form.City,
|
|
|
|
State: form.State,
|
|
|
|
ZipCode: form.ZipCode,
|
|
|
|
Country: form.Country,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to update org address", err)
|
2015-09-08 07:22:44 -05:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return Success("Address updated")
|
2015-09-08 07:22:44 -05:00
|
|
|
}
|
|
|
|
|
2015-08-12 01:59:25 -05:00
|
|
|
// GET /api/orgs/:orgId
|
2020-03-04 05:57:20 -06:00
|
|
|
func DeleteOrgByID(c *models.ReqContext) Response {
|
|
|
|
if err := bus.Dispatch(&models.DeleteOrgCommand{Id: c.ParamsInt64(":orgId")}); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrOrgNotFound) {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(404, "Failed to delete organization. ID not found", nil)
|
2016-11-27 16:09:01 -06:00
|
|
|
}
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to update organization", err)
|
2015-08-12 01:59:25 -05:00
|
|
|
}
|
2018-03-22 16:13:46 -05:00
|
|
|
return Success("Organization deleted")
|
2015-08-12 01:59:25 -05:00
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func SearchOrgs(c *models.ReqContext) Response {
|
2020-08-12 02:59:07 -05:00
|
|
|
perPage := c.QueryInt("perpage")
|
|
|
|
if perPage <= 0 {
|
|
|
|
perPage = 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
page := c.QueryInt("page")
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
query := models.SearchOrgsQuery{
|
2015-05-19 04:47:14 -05:00
|
|
|
Query: c.Query("query"),
|
|
|
|
Name: c.Query("name"),
|
2020-08-12 02:59:07 -05:00
|
|
|
Page: page,
|
|
|
|
Limit: perPage,
|
2015-05-19 04:47:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
2018-03-22 16:13:46 -05:00
|
|
|
return Error(500, "Failed to search orgs", err)
|
2015-05-19 04:47:14 -05:00
|
|
|
}
|
|
|
|
|
2018-03-22 16:13:46 -05:00
|
|
|
return JSON(200, query.Result)
|
2015-05-19 04:47:14 -05:00
|
|
|
}
|