| 
									
										
										
										
											2015-07-20 20:51:27 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/response" | 
					
						
							| 
									
										
										
										
											2015-07-20 20:51:27 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2015-09-11 01:51:12 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							| 
									
										
										
										
											2015-07-20 20:51:27 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 19:13:59 +08:00
										 |  |  | func (hs *HTTPServer) GetCurrentOrgQuotas(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	return hs.getOrgQuotasHelper(c, c.OrgId) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (hs *HTTPServer) GetOrgQuotas(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	return hs.getOrgQuotasHelper(c, c.ParamsInt64(":orgId")) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (hs *HTTPServer) getOrgQuotasHelper(c *models.ReqContext, orgID int64) response.Response { | 
					
						
							|  |  |  | 	if !hs.Cfg.Quota.Enabled { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(404, "Quotas not enabled", nil) | 
					
						
							| 
									
										
										
										
											2015-09-11 01:51:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-10-27 19:13:59 +08:00
										 |  |  | 	query := models.GetOrgQuotasQuery{OrgId: orgID} | 
					
						
							| 
									
										
										
										
											2015-07-20 20:51:27 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 19:13:59 +08:00
										 |  |  | 	if err := hs.SQLStore.GetOrgQuotas(c.Req.Context(), &query); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to get org quotas", err) | 
					
						
							| 
									
										
										
										
											2015-07-20 20:51:27 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.JSON(200, query.Result) | 
					
						
							| 
									
										
										
										
											2015-07-20 20:51:27 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | func (hs *HTTPServer) UpdateOrgQuota(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	cmd := models.UpdateOrgQuotaCmd{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &cmd); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-10-27 19:13:59 +08:00
										 |  |  | 	if !hs.Cfg.Quota.Enabled { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(404, "Quotas not enabled", nil) | 
					
						
							| 
									
										
										
										
											2015-09-11 01:51:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-11 23:17:10 +08:00
										 |  |  | 	cmd.OrgId = c.ParamsInt64(":orgId") | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	cmd.Target = web.Params(c.Req)[":target"] | 
					
						
							| 
									
										
										
										
											2015-09-11 23:17:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 19:13:59 +08:00
										 |  |  | 	if _, ok := hs.Cfg.Quota.Org.ToMap()[cmd.Target]; !ok { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(404, "Invalid quota target", nil) | 
					
						
							| 
									
										
										
										
											2015-09-11 23:17:10 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-27 19:13:59 +08:00
										 |  |  | 	if err := hs.SQLStore.UpdateOrgQuota(c.Req.Context(), &cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to update org quotas", err) | 
					
						
							| 
									
										
										
										
											2015-09-11 23:17:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.Success("Organization quota updated") | 
					
						
							| 
									
										
										
										
											2015-09-11 23:17:10 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | func GetUserQuotas(c *models.ReqContext) response.Response { | 
					
						
							| 
									
										
										
										
											2015-09-11 23:17:10 +08:00
										 |  |  | 	if !setting.Quota.Enabled { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(404, "Quotas not enabled", nil) | 
					
						
							| 
									
										
										
										
											2015-09-11 23:17:10 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-03-04 19:57:20 +08:00
										 |  |  | 	query := models.GetUserQuotasQuery{UserId: c.ParamsInt64(":id")} | 
					
						
							| 
									
										
										
										
											2015-09-11 01:47:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 23:05:30 +08:00
										 |  |  | 	if err := bus.DispatchCtx(c.Req.Context(), &query); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to get org quotas", err) | 
					
						
							| 
									
										
										
										
											2015-09-11 01:47:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.JSON(200, query.Result) | 
					
						
							| 
									
										
										
										
											2015-09-11 01:47:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 17:18:01 +08:00
										 |  |  | func UpdateUserQuota(c *models.ReqContext) response.Response { | 
					
						
							|  |  |  | 	cmd := models.UpdateUserQuotaCmd{} | 
					
						
							|  |  |  | 	if err := web.Bind(c.Req, &cmd); err != nil { | 
					
						
							|  |  |  | 		return response.Error(http.StatusBadRequest, "bad request data", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-11 01:51:12 +08:00
										 |  |  | 	if !setting.Quota.Enabled { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(404, "Quotas not enabled", nil) | 
					
						
							| 
									
										
										
										
											2015-09-11 01:51:12 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-11 23:17:10 +08:00
										 |  |  | 	cmd.UserId = c.ParamsInt64(":id") | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	cmd.Target = web.Params(c.Req)[":target"] | 
					
						
							| 
									
										
										
										
											2015-07-21 18:30:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-15 20:31:58 +08:00
										 |  |  | 	if _, ok := setting.Quota.User.ToMap()[cmd.Target]; !ok { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(404, "Invalid quota target", nil) | 
					
						
							| 
									
										
										
										
											2015-07-21 18:30:31 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-20 23:05:30 +08:00
										 |  |  | 	if err := bus.DispatchCtx(c.Req.Context(), &cmd); err != nil { | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 		return response.Error(500, "Failed to update org quotas", err) | 
					
						
							| 
									
										
										
										
											2015-07-20 20:51:27 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-01-15 21:43:20 +08:00
										 |  |  | 	return response.Success("Organization quota updated") | 
					
						
							| 
									
										
										
										
											2015-07-20 20:51:27 +08:00
										 |  |  | } |