2015-07-20 20:51:27 +08:00
package api
import (
2021-11-29 17:18:01 +08:00
"net/http"
2022-01-15 00:55:57 +08:00
"strconv"
2021-11-29 17:18:01 +08:00
2021-01-15 21:43:20 +08:00
"github.com/grafana/grafana/pkg/api/response"
2023-01-27 15:50:36 +08:00
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
2022-11-15 03:08:10 +08:00
"github.com/grafana/grafana/pkg/services/quota"
2021-10-11 20:30:59 +08:00
"github.com/grafana/grafana/pkg/web"
2015-07-20 20:51:27 +08:00
)
2022-11-15 03:08:10 +08:00
// swagger:route GET /org/quotas getCurrentOrg getCurrentOrgQuota
//
// Fetch Organization quota.
//
// If you are running Grafana Enterprise and have Fine-grained access control enabled, you need to have a permission with action `orgs.quotas:read` and scope `org:id:1` (orgIDScope).
//
// Responses:
// 200: getQuotaResponse
// 401: unauthorisedError
// 403: forbiddenError
// 404: notFoundError
// 500: internalServerError
2023-01-27 15:50:36 +08:00
func ( hs * HTTPServer ) GetCurrentOrgQuotas ( c * contextmodel . ReqContext ) response . Response {
2025-04-10 20:42:23 +08:00
return hs . getOrgQuotasHelper ( c , c . GetOrgID ( ) )
2021-10-27 19:13:59 +08:00
}
2022-07-27 21:54:37 +08:00
// swagger:route GET /orgs/{org_id}/quotas orgs getOrgQuota
//
// Fetch Organization quota.
//
// If you are running Grafana Enterprise and have Fine-grained access control enabled, you need to have a permission with action `orgs.quotas:read` and scope `org:id:1` (orgIDScope).
2022-09-12 15:40:35 +08:00
//
2022-07-27 21:54:37 +08:00
// Responses:
// 200: getQuotaResponse
// 401: unauthorisedError
// 403: forbiddenError
// 404: notFoundError
// 500: internalServerError
2023-01-27 15:50:36 +08:00
func ( hs * HTTPServer ) GetOrgQuotas ( c * contextmodel . ReqContext ) response . Response {
2022-01-15 00:55:57 +08:00
orgId , err := strconv . ParseInt ( web . Params ( c . Req ) [ ":orgId" ] , 10 , 64 )
if err != nil {
2022-11-15 03:08:10 +08:00
return response . Err ( quota . ErrBadRequest . Errorf ( "orgId is invalid: %w" , err ) )
2022-01-15 00:55:57 +08:00
}
return hs . getOrgQuotasHelper ( c , orgId )
2021-10-27 19:13:59 +08:00
}
2023-01-27 15:50:36 +08:00
func ( hs * HTTPServer ) getOrgQuotasHelper ( c * contextmodel . ReqContext , orgID int64 ) response . Response {
2024-08-22 01:24:45 +08:00
ctx , span := hs . tracer . Start ( c . Req . Context ( ) , "api.getOrgQuotasHelper" )
defer span . End ( )
q , err := hs . QuotaService . GetQuotasByScope ( ctx , quota . OrgScope , orgID )
2022-11-15 03:08:10 +08:00
if err != nil {
return response . ErrOrFallback ( http . StatusInternalServerError , "failed to get quota" , err )
2022-11-08 17:52:07 +08:00
}
2022-11-15 03:08:10 +08:00
return response . JSON ( http . StatusOK , q )
2015-07-20 20:51:27 +08:00
}
2022-07-27 21:54:37 +08:00
// swagger:route PUT /orgs/{org_id}/quotas/{quota_target} orgs updateOrgQuota
//
// Update user quota.
//
// If you are running Grafana Enterprise and have Fine-grained access control enabled, you need to have a permission with action `orgs.quotas:write` and scope `org:id:1` (orgIDScope).
//
// Security:
// - basic:
//
// Responses:
// 200: okResponse
// 401: unauthorisedError
// 403: forbiddenError
// 404: notFoundError
// 500: internalServerError
2023-01-27 15:50:36 +08:00
func ( hs * HTTPServer ) UpdateOrgQuota ( c * contextmodel . ReqContext ) response . Response {
2024-08-22 01:24:45 +08:00
ctx , span := hs . tracer . Start ( c . Req . Context ( ) , "api.UpdateOrgQuota" )
defer span . End ( )
2022-11-15 03:08:10 +08:00
cmd := quota . UpdateQuotaCmd { }
2022-01-15 00:55:57 +08:00
var err error
2021-11-29 17:18:01 +08:00
if err := web . Bind ( c . Req , & cmd ) ; err != nil {
2022-11-15 03:08:10 +08:00
return response . Err ( quota . ErrBadRequest . Errorf ( "bad request data: %w" , err ) )
2015-09-11 01:51:12 +08:00
}
2022-11-15 03:08:10 +08:00
cmd . OrgID , err = strconv . ParseInt ( web . Params ( c . Req ) [ ":orgId" ] , 10 , 64 )
2022-01-15 00:55:57 +08:00
if err != nil {
2022-11-15 03:08:10 +08:00
return response . Err ( quota . ErrBadRequest . Errorf ( "orgId is invalid: %w" , err ) )
2022-01-15 00:55:57 +08:00
}
2021-10-11 20:30:59 +08:00
cmd . Target = web . Params ( c . Req ) [ ":target" ]
2015-09-11 23:17:10 +08:00
2024-08-22 01:24:45 +08:00
if err := hs . QuotaService . Update ( ctx , & cmd ) ; err != nil {
2022-11-15 03:08:10 +08:00
return response . ErrOrFallback ( http . StatusInternalServerError , "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
}
2022-07-27 21:54:37 +08:00
// swagger:route GET /admin/users/{user_id}/quotas admin_users getUserQuota
//
// Fetch user quota.
//
// If you are running Grafana Enterprise and have Fine-grained access control enabled, you need to have a permission with action `users.quotas:list` and scope `global.users:1` (userIDScope).
//
// Security:
// - basic:
//
// Responses:
// 200: getQuotaResponse
// 401: unauthorisedError
// 403: forbiddenError
// 404: notFoundError
// 500: internalServerError
// swagger:route GET /user/quotas signed_in_user getUserQuotas
//
// Fetch user quota.
//
// Responses:
// 200: getQuotaResponse
// 401: unauthorisedError
// 403: forbiddenError
// 404: notFoundError
// 500: internalServerError
2023-01-27 15:50:36 +08:00
func ( hs * HTTPServer ) GetUserQuotas ( c * contextmodel . ReqContext ) response . Response {
2024-08-22 01:24:45 +08:00
ctx , span := hs . tracer . Start ( c . Req . Context ( ) , "api.GetUserQuotas" )
defer span . End ( )
2022-01-15 00:55:57 +08:00
id , err := strconv . ParseInt ( web . Params ( c . Req ) [ ":id" ] , 10 , 64 )
if err != nil {
2022-11-15 03:08:10 +08:00
return response . Err ( quota . ErrBadRequest . Errorf ( "id is invalid: %w" , err ) )
2022-01-15 00:55:57 +08:00
}
2024-08-22 01:24:45 +08:00
q , err := hs . QuotaService . GetQuotasByScope ( ctx , quota . UserScope , id )
2022-11-15 03:08:10 +08:00
if err != nil {
return response . ErrOrFallback ( http . StatusInternalServerError , "Failed to get org quotas" , err )
2015-09-11 01:47:33 +08:00
}
2022-11-15 03:08:10 +08:00
return response . JSON ( http . StatusOK , q )
2015-09-11 01:47:33 +08:00
}
2022-07-27 21:54:37 +08:00
// swagger:route PUT /admin/users/{user_id}/quotas/{quota_target} admin_users updateUserQuota
//
// Update user quota.
//
// If you are running Grafana Enterprise and have Fine-grained access control enabled, you need to have a permission with action `users.quotas:update` and scope `global.users:1` (userIDScope).
//
// Security:
// - basic:
//
// Responses:
// 200: okResponse
// 401: unauthorisedError
// 403: forbiddenError
// 404: notFoundError
// 500: internalServerError
2023-01-27 15:50:36 +08:00
func ( hs * HTTPServer ) UpdateUserQuota ( c * contextmodel . ReqContext ) response . Response {
2024-08-22 01:24:45 +08:00
ctx , span := hs . tracer . Start ( c . Req . Context ( ) , "api.UpdateUserQuota" )
defer span . End ( )
2022-11-15 03:08:10 +08:00
cmd := quota . UpdateQuotaCmd { }
2022-01-15 00:55:57 +08:00
var err error
2021-11-29 17:18:01 +08:00
if err := web . Bind ( c . Req , & cmd ) ; err != nil {
2022-11-15 03:08:10 +08:00
return response . Err ( quota . ErrBadRequest . Errorf ( "bad request data: %w" , err ) )
2021-11-29 17:18:01 +08:00
}
2022-11-15 03:08:10 +08:00
cmd . UserID , err = strconv . ParseInt ( web . Params ( c . Req ) [ ":id" ] , 10 , 64 )
2022-01-15 00:55:57 +08:00
if err != nil {
2022-11-15 03:08:10 +08:00
return response . Err ( quota . ErrBadRequest . Errorf ( "id is invalid: %w" , err ) )
2022-01-15 00:55:57 +08:00
}
2021-10-11 20:30:59 +08:00
cmd . Target = web . Params ( c . Req ) [ ":target" ]
2015-07-21 18:30:31 +08:00
2024-08-22 01:24:45 +08:00
if err := hs . QuotaService . Update ( ctx , & cmd ) ; err != nil {
2022-11-15 03:08:10 +08:00
return response . ErrOrFallback ( http . StatusInternalServerError , "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
}
2022-07-27 21:54:37 +08:00
// swagger:parameters updateUserQuota
type UpdateUserQuotaParams struct {
// in:body
// required:true
2022-11-15 03:08:10 +08:00
Body quota . UpdateQuotaCmd ` json:"body" `
2022-07-27 21:54:37 +08:00
// in:path
// required:true
QuotaTarget string ` json:"quota_target" `
// in:path
// required:true
UserID int64 ` json:"user_id" `
}
// swagger:parameters getUserQuota
type GetUserQuotaParams struct {
// in:path
// required:true
UserID int64 ` json:"user_id" `
}
// swagger:parameters getOrgQuota
type GetOrgQuotaParams struct {
// in:path
// required:true
OrgID int64 ` json:"org_id" `
}
// swagger:parameters updateOrgQuota
type UpdateOrgQuotaParam struct {
// in:body
// required:true
2022-11-15 03:08:10 +08:00
Body quota . UpdateQuotaCmd ` json:"body" `
2022-07-27 21:54:37 +08:00
// in:path
// required:true
QuotaTarget string ` json:"quota_target" `
// in:path
// required:true
OrgID int64 ` json:"org_id" `
}
// swagger:response getQuotaResponse
type GetQuotaResponseResponse struct {
// in:body
2022-11-15 03:08:10 +08:00
Body [ ] * quota . QuotaDTO ` json:"body" `
2022-07-27 21:54:37 +08:00
}