2015-02-23 13:07:49 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2015-03-22 14:14:00 -05:00
|
|
|
"github.com/grafana/grafana/pkg/metrics"
|
2015-02-23 13:07:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
|
|
|
m "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
|
|
|
|
func GetOrgCurrent(c *middleware.Context) Response {
|
|
|
|
return getOrgHelper(c.OrgId)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/orgs/:orgId
|
|
|
|
func GetOrgById(c *middleware.Context) Response {
|
|
|
|
return getOrgHelper(c.ParamsInt64(":orgId"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getOrgHelper(orgId int64) Response {
|
|
|
|
query := m.GetOrgByIdQuery{Id: orgId}
|
2015-02-23 13:07:49 -06:00
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
if err == m.ErrOrgNotFound {
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiError(404, "Organization not found", err)
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiError(500, "Failed to get organization", err)
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
org := m.OrgDTO{
|
|
|
|
Id: query.Result.Id,
|
|
|
|
Name: query.Result.Name,
|
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
return Json(200, &org)
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
// POST /api/orgs
|
|
|
|
func CreateOrg(c *middleware.Context, cmd m.CreateOrgCommand) Response {
|
2015-03-30 03:12:24 -05:00
|
|
|
if !setting.AllowUserOrgCreate && !c.IsGrafanaAdmin {
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiError(401, "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 {
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiError(500, "Failed to create organization", err)
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
2015-03-22 14:14:00 -05:00
|
|
|
metrics.M_Api_Org_Create.Inc(1)
|
|
|
|
|
2015-06-22 01:13:21 -05:00
|
|
|
return Json(200, &util.DynMap{
|
|
|
|
"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
|
|
|
|
func UpdateOrgCurrent(c *middleware.Context, cmd m.UpdateOrgCommand) Response {
|
2015-02-23 13:07:49 -06:00
|
|
|
cmd.OrgId = c.OrgId
|
2015-05-19 03:16:32 -05:00
|
|
|
return updateOrgHelper(cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PUT /api/orgs/:orgId
|
|
|
|
func UpdateOrg(c *middleware.Context, cmd m.UpdateOrgCommand) Response {
|
|
|
|
cmd.OrgId = c.ParamsInt64(":orgId")
|
|
|
|
return updateOrgHelper(cmd)
|
|
|
|
}
|
2015-02-23 13:07:49 -06:00
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
func updateOrgHelper(cmd m.UpdateOrgCommand) Response {
|
2015-02-23 13:07:49 -06:00
|
|
|
if err := bus.Dispatch(&cmd); err != nil {
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiError(500, "Failed to update organization", err)
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
|
|
|
|
2015-05-19 03:16:32 -05:00
|
|
|
return ApiSuccess("Organization updated")
|
2015-02-23 13:07:49 -06:00
|
|
|
}
|
2015-05-19 04:47:14 -05:00
|
|
|
|
|
|
|
func SearchOrgs(c *middleware.Context) Response {
|
|
|
|
query := m.SearchOrgsQuery{
|
|
|
|
Query: c.Query("query"),
|
|
|
|
Name: c.Query("name"),
|
|
|
|
Page: 0,
|
|
|
|
Limit: 1000,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := bus.Dispatch(&query); err != nil {
|
|
|
|
return ApiError(500, "Failed to search orgs", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Json(200, query.Result)
|
|
|
|
}
|