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