| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							| 
									
										
										
										
											2015-03-23 03:14:00 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/metrics" | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/middleware" | 
					
						
							|  |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08: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")) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 02:42:57 +08:00
										 |  |  | // Get /api/orgs/name/:name
 | 
					
						
							| 
									
										
										
										
											2016-01-13 05:50:56 +08:00
										 |  |  | func GetOrgByName(c *middleware.Context) Response { | 
					
						
							|  |  |  | 	query := m.GetOrgByNameQuery{Name: c.Params(":name")} | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							|  |  |  | 		if err == m.ErrOrgNotFound { | 
					
						
							|  |  |  | 			return ApiError(404, "Organization not found", err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return ApiError(500, "Failed to get organization", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	org := query.Result | 
					
						
							|  |  |  | 	result := m.OrgDetailsDTO{ | 
					
						
							|  |  |  | 		Id:   org.Id, | 
					
						
							|  |  |  | 		Name: org.Name, | 
					
						
							|  |  |  | 		Address: m.Address{ | 
					
						
							|  |  |  | 			Address1: org.Address1, | 
					
						
							|  |  |  | 			Address2: org.Address2, | 
					
						
							|  |  |  | 			City:     org.City, | 
					
						
							|  |  |  | 			ZipCode:  org.ZipCode, | 
					
						
							|  |  |  | 			State:    org.State, | 
					
						
							|  |  |  | 			Country:  org.Country, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return Json(200, &result) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | func getOrgHelper(orgId int64) Response { | 
					
						
							|  |  |  | 	query := m.GetOrgByIdQuery{Id: orgId} | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&query); err != nil { | 
					
						
							|  |  |  | 		if err == m.ErrOrgNotFound { | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | 			return ApiError(404, "Organization not found", err) | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | 		return ApiError(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 | 
					
						
							|  |  |  | 	result := m.OrgDetailsDTO{ | 
					
						
							|  |  |  | 		Id:   org.Id, | 
					
						
							|  |  |  | 		Name: org.Name, | 
					
						
							|  |  |  | 		Address: m.Address{ | 
					
						
							|  |  |  | 			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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | 	return Json(200, &result) | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | // POST /api/orgs
 | 
					
						
							|  |  |  | func CreateOrg(c *middleware.Context, cmd m.CreateOrgCommand) Response { | 
					
						
							| 
									
										
										
										
											2015-06-25 23:06:43 +08:00
										 |  |  | 	if !c.IsSignedIn || (!setting.AllowUserOrgCreate && !c.IsGrafanaAdmin) { | 
					
						
							| 
									
										
										
										
											2015-09-08 19:06:18 +08:00
										 |  |  | 		return ApiError(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 | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2015-09-08 19:06:18 +08:00
										 |  |  | 		if err == m.ErrOrgNameTaken { | 
					
						
							| 
									
										
										
										
											2016-02-12 17:01:45 +08:00
										 |  |  | 			return ApiError(409, "Organization name taken", err) | 
					
						
							| 
									
										
										
										
											2015-09-08 19:06:18 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | 		return ApiError(500, "Failed to create organization", err) | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-06 17:23:52 +08:00
										 |  |  | 	metrics.M_Api_Org_Create.Inc() | 
					
						
							| 
									
										
										
										
											2015-03-23 03:14:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-22 14:13:21 +08:00
										 |  |  | 	return Json(200, &util.DynMap{ | 
					
						
							|  |  |  | 		"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
 | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | func UpdateOrgCurrent(c *middleware.Context, form dtos.UpdateOrgForm) Response { | 
					
						
							|  |  |  | 	return updateOrgHelper(form, c.OrgId) | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // PUT /api/orgs/:orgId
 | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | func UpdateOrg(c *middleware.Context, form dtos.UpdateOrgForm) Response { | 
					
						
							|  |  |  | 	return updateOrgHelper(form, c.ParamsInt64(":orgId")) | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 20:22:44 +08:00
										 |  |  | func updateOrgHelper(form dtos.UpdateOrgForm, orgId int64) Response { | 
					
						
							|  |  |  | 	cmd := m.UpdateOrgCommand{Name: form.Name, OrgId: orgId} | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2015-09-08 19:06:18 +08:00
										 |  |  | 		if err == m.ErrOrgNameTaken { | 
					
						
							|  |  |  | 			return ApiError(400, "Organization name taken", err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | 		return ApiError(500, "Failed to update organization", err) | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 16:16:32 +08:00
										 |  |  | 	return ApiSuccess("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
 | 
					
						
							|  |  |  | func UpdateOrgAddressCurrent(c *middleware.Context, form dtos.UpdateOrgAddressForm) Response { | 
					
						
							|  |  |  | 	return updateOrgAddressHelper(form, c.OrgId) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // PUT /api/orgs/:orgId/address
 | 
					
						
							|  |  |  | func UpdateOrgAddress(c *middleware.Context, form dtos.UpdateOrgAddressForm) Response { | 
					
						
							|  |  |  | 	return updateOrgAddressHelper(form, c.ParamsInt64(":orgId")) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func updateOrgAddressHelper(form dtos.UpdateOrgAddressForm, orgId int64) Response { | 
					
						
							|  |  |  | 	cmd := m.UpdateOrgAddressCommand{ | 
					
						
							|  |  |  | 		OrgId: orgId, | 
					
						
							|  |  |  | 		Address: m.Address{ | 
					
						
							|  |  |  | 			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 { | 
					
						
							|  |  |  | 		return ApiError(500, "Failed to update org address", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ApiSuccess("Address updated") | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-12 14:59:25 +08:00
										 |  |  | // GET /api/orgs/:orgId
 | 
					
						
							|  |  |  | func DeleteOrgById(c *middleware.Context) Response { | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&m.DeleteOrgCommand{Id: c.ParamsInt64(":orgId")}); err != nil { | 
					
						
							| 
									
										
										
										
											2016-11-28 06:09:01 +08:00
										 |  |  | 		if err == m.ErrOrgNotFound { | 
					
						
							|  |  |  | 			return ApiError(404, "Failed to delete organization. ID not found", nil) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-08-12 14:59:25 +08:00
										 |  |  | 		return ApiError(500, "Failed to update organization", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return ApiSuccess("Organization deleted") | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 17:47:14 +08: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) | 
					
						
							|  |  |  | } |