2015-02-23 13:07:49 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-11-03 05:31:56 -05:00
|
|
|
"context"
|
2020-11-19 06:34:28 -06:00
|
|
|
"errors"
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2022-01-14 10:55:57 -06:00
|
|
|
"strconv"
|
2020-11-19 06:34:28 -06:00
|
|
|
|
2015-09-08 07:22:44 -05:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
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"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-02-23 13:07:49 -06:00
|
|
|
)
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
// GET /api/org
|
2022-02-23 04:12:37 -06:00
|
|
|
func (hs *HTTPServer) GetCurrentOrg(c *models.ReqContext) response.Response {
|
|
|
|
return hs.getOrgHelper(c.Req.Context(), c.OrgId)
|
2015-05-19 03:16:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/orgs/:orgId
|
2022-02-23 04:12:37 -06:00
|
|
|
func (hs *HTTPServer) GetOrgByID(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
orgId, err := strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "orgId is invalid", err)
|
|
|
|
}
|
2022-02-23 04:12:37 -06:00
|
|
|
return hs.getOrgHelper(c.Req.Context(), orgId)
|
2015-05-19 03:16:32 -05:00
|
|
|
}
|
|
|
|
|
2021-11-17 08:19:45 -06:00
|
|
|
// GET /api/orgs/name/:name
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) GetOrgByName(c *models.ReqContext) response.Response {
|
2021-10-11 07:30:59 -05:00
|
|
|
org, err := hs.SQLStore.GetOrgByName(web.Params(c.Req)[":name"])
|
2021-01-07 04:36:13 -06:00
|
|
|
if err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrOrgNotFound) {
|
2022-07-07 07:50:38 -05:00
|
|
|
return response.Error(http.StatusNotFound, "Organization not found", err)
|
2016-01-12 15:50:56 -06:00
|
|
|
}
|
|
|
|
|
2022-07-07 07:50:38 -05:00
|
|
|
return response.Error(http.StatusInternalServerError, "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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, &result)
|
2016-01-12 15:50:56 -06:00
|
|
|
}
|
|
|
|
|
2022-02-23 04:12:37 -06:00
|
|
|
func (hs *HTTPServer) getOrgHelper(ctx context.Context, orgID int64) response.Response {
|
2020-03-04 05:57:20 -06:00
|
|
|
query := models.GetOrgByIdQuery{Id: orgID}
|
2015-02-23 13:07:49 -06:00
|
|
|
|
2022-02-23 04:12:37 -06:00
|
|
|
if err := hs.SQLStore.GetOrgById(ctx, &query); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrOrgNotFound) {
|
2022-07-07 07:50:38 -05:00
|
|
|
return response.Error(http.StatusNotFound, "Organization not found", err)
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
2022-07-07 07:50:38 -05:00
|
|
|
return response.Error(http.StatusInternalServerError, "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
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, &result)
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
// POST /api/orgs
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) CreateOrg(c *models.ReqContext) response.Response {
|
|
|
|
cmd := models.CreateOrgCommand{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-04-25 03:42:09 -05:00
|
|
|
acEnabled := !hs.AccessControl.IsDisabled()
|
2021-10-27 06:13:59 -05:00
|
|
|
if !acEnabled && !(setting.AllowUserOrgCreate || c.IsGrafanaAdmin) {
|
2022-07-07 07:50:38 -05:00
|
|
|
return response.Error(http.StatusForbidden, "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
|
2022-04-25 12:07:11 -05:00
|
|
|
if err := hs.SQLStore.CreateOrg(c.Req.Context(), &cmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrOrgNameTaken) {
|
2022-07-07 07:50:38 -05:00
|
|
|
return response.Error(http.StatusConflict, "Organization name taken", err)
|
2015-09-08 06:06:18 -05:00
|
|
|
}
|
2022-07-07 07:50:38 -05:00
|
|
|
return response.Error(http.StatusInternalServerError, "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
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, &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
|
2022-01-25 13:30:08 -06:00
|
|
|
func (hs *HTTPServer) UpdateCurrentOrg(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
form := dtos.UpdateOrgForm{}
|
|
|
|
if err := web.Bind(c.Req, &form); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-01-25 13:30:08 -06:00
|
|
|
return hs.updateOrgHelper(c.Req.Context(), form, c.OrgId)
|
2015-05-19 03:16:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PUT /api/orgs/:orgId
|
2022-01-25 13:30:08 -06:00
|
|
|
func (hs *HTTPServer) UpdateOrg(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
form := dtos.UpdateOrgForm{}
|
|
|
|
if err := web.Bind(c.Req, &form); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-01-14 10:55:57 -06:00
|
|
|
orgId, err := strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "orgId is invalid", err)
|
|
|
|
}
|
2022-01-25 13:30:08 -06:00
|
|
|
return hs.updateOrgHelper(c.Req.Context(), form, orgId)
|
2015-05-19 03:16:32 -05:00
|
|
|
}
|
2015-02-23 13:07:49 -06:00
|
|
|
|
2022-01-25 13:30:08 -06:00
|
|
|
func (hs *HTTPServer) updateOrgHelper(ctx context.Context, form dtos.UpdateOrgForm, orgID int64) response.Response {
|
2020-03-04 05:57:20 -06:00
|
|
|
cmd := models.UpdateOrgCommand{Name: form.Name, OrgId: orgID}
|
2022-01-25 13:30:08 -06:00
|
|
|
if err := hs.SQLStore.UpdateOrg(ctx, &cmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrOrgNameTaken) {
|
2022-07-07 07:50:38 -05:00
|
|
|
return response.Error(http.StatusBadRequest, "Organization name taken", err)
|
2015-09-08 06:06:18 -05:00
|
|
|
}
|
2022-07-07 07:50:38 -05:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to update organization", err)
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.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
|
2022-01-25 13:30:08 -06:00
|
|
|
func (hs *HTTPServer) UpdateCurrentOrgAddress(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
form := dtos.UpdateOrgAddressForm{}
|
|
|
|
if err := web.Bind(c.Req, &form); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-01-25 13:30:08 -06:00
|
|
|
return hs.updateOrgAddressHelper(c.Req.Context(), form, c.OrgId)
|
2015-09-08 07:22:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PUT /api/orgs/:orgId/address
|
2022-01-25 13:30:08 -06:00
|
|
|
func (hs *HTTPServer) UpdateOrgAddress(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
form := dtos.UpdateOrgAddressForm{}
|
|
|
|
if err := web.Bind(c.Req, &form); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-01-14 10:55:57 -06:00
|
|
|
orgId, err := strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "orgId is invalid", err)
|
|
|
|
}
|
2022-01-25 13:30:08 -06:00
|
|
|
return hs.updateOrgAddressHelper(c.Req.Context(), form, orgId)
|
2015-09-08 07:22:44 -05:00
|
|
|
}
|
|
|
|
|
2022-01-25 13:30:08 -06:00
|
|
|
func (hs *HTTPServer) updateOrgAddressHelper(ctx context.Context, form dtos.UpdateOrgAddressForm, orgID int64) response.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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-01-25 13:30:08 -06:00
|
|
|
if err := hs.SQLStore.UpdateOrgAddress(ctx, &cmd); err != nil {
|
2022-07-07 07:50:38 -05:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to update org address", err)
|
2015-09-08 07:22:44 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Address updated")
|
2015-09-08 07:22:44 -05:00
|
|
|
}
|
|
|
|
|
2021-10-27 06:13:59 -05:00
|
|
|
// DELETE /api/orgs/:orgId
|
2022-01-25 13:30:08 -06:00
|
|
|
func (hs *HTTPServer) DeleteOrgByID(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
orgID, err := strconv.ParseInt(web.Params(c.Req)[":orgId"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "orgId is invalid", err)
|
|
|
|
}
|
2021-08-23 01:58:35 -05:00
|
|
|
// before deleting an org, check if user does not belong to the current org
|
|
|
|
if c.OrgId == orgID {
|
2022-07-07 07:50:38 -05:00
|
|
|
return response.Error(http.StatusBadRequest, "Can not delete org for current user", nil)
|
2021-08-23 01:58:35 -05:00
|
|
|
}
|
|
|
|
|
2022-01-25 13:30:08 -06:00
|
|
|
if err := hs.SQLStore.DeleteOrg(c.Req.Context(), &models.DeleteOrgCommand{Id: orgID}); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrOrgNotFound) {
|
2022-07-07 07:50:38 -05:00
|
|
|
return response.Error(http.StatusNotFound, "Failed to delete organization. ID not found", nil)
|
2016-11-27 16:09:01 -06:00
|
|
|
}
|
2022-07-07 07:50:38 -05:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to update organization", err)
|
2015-08-12 01:59:25 -05:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("Organization deleted")
|
2015-08-12 01:59:25 -05:00
|
|
|
}
|
|
|
|
|
2022-02-09 04:45:31 -06:00
|
|
|
func (hs *HTTPServer) SearchOrgs(c *models.ReqContext) response.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
|
|
|
}
|
|
|
|
|
2022-02-09 04:45:31 -06:00
|
|
|
if err := hs.SQLStore.SearchOrgs(c.Req.Context(), &query); err != nil {
|
2022-07-07 07:50:38 -05:00
|
|
|
return response.Error(http.StatusInternalServerError, "Failed to search orgs", err)
|
2015-05-19 04:47:14 -05:00
|
|
|
}
|
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, query.Result)
|
2015-05-19 04:47:14 -05:00
|
|
|
}
|