2015-07-20 07:51:27 -05:00
package api
import (
2021-11-29 03:18:01 -06:00
"net/http"
2022-01-14 10:55:57 -06:00
"strconv"
2021-11-29 03:18:01 -06:00
2021-01-15 07:43:20 -06:00
"github.com/grafana/grafana/pkg/api/response"
2023-01-27 01:50:36 -06:00
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
2022-11-14 13:08:10 -06:00
"github.com/grafana/grafana/pkg/services/quota"
2021-10-11 07:30:59 -05:00
"github.com/grafana/grafana/pkg/web"
2015-07-20 07:51:27 -05:00
)
2022-11-14 13:08:10 -06: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 01:50:36 -06:00
func ( hs * HTTPServer ) GetCurrentOrgQuotas ( c * contextmodel . ReqContext ) response . Response {
2023-10-06 04:34:36 -05:00
return hs . getOrgQuotasHelper ( c , c . SignedInUser . GetOrgID ( ) )
2021-10-27 06:13:59 -05:00
}
2022-07-27 08:54:37 -05: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 02:40:35 -05:00
//
2022-07-27 08:54:37 -05:00
// Responses:
// 200: getQuotaResponse
// 401: unauthorisedError
// 403: forbiddenError
// 404: notFoundError
// 500: internalServerError
2023-01-27 01:50:36 -06:00
func ( hs * HTTPServer ) GetOrgQuotas ( c * contextmodel . 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 {
2022-11-14 13:08:10 -06:00
return response . Err ( quota . ErrBadRequest . Errorf ( "orgId is invalid: %w" , err ) )
2022-01-14 10:55:57 -06:00
}
return hs . getOrgQuotasHelper ( c , orgId )
2021-10-27 06:13:59 -05:00
}
2023-01-27 01:50:36 -06:00
func ( hs * HTTPServer ) getOrgQuotasHelper ( c * contextmodel . ReqContext , orgID int64 ) response . Response {
2022-11-14 13:08:10 -06:00
q , err := hs . QuotaService . GetQuotasByScope ( c . Req . Context ( ) , quota . OrgScope , orgID )
if err != nil {
return response . ErrOrFallback ( http . StatusInternalServerError , "failed to get quota" , err )
2022-11-08 03:52:07 -06:00
}
2022-11-14 13:08:10 -06:00
return response . JSON ( http . StatusOK , q )
2015-07-20 07:51:27 -05:00
}
2022-07-27 08:54:37 -05: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 01:50:36 -06:00
func ( hs * HTTPServer ) UpdateOrgQuota ( c * contextmodel . ReqContext ) response . Response {
2022-11-14 13:08:10 -06:00
cmd := quota . UpdateQuotaCmd { }
2022-01-14 10:55:57 -06:00
var err error
2021-11-29 03:18:01 -06:00
if err := web . Bind ( c . Req , & cmd ) ; err != nil {
2022-11-14 13:08:10 -06:00
return response . Err ( quota . ErrBadRequest . Errorf ( "bad request data: %w" , err ) )
2015-09-10 12:51:12 -05:00
}
2022-11-14 13:08:10 -06:00
cmd . OrgID , err = strconv . ParseInt ( web . Params ( c . Req ) [ ":orgId" ] , 10 , 64 )
2022-01-14 10:55:57 -06:00
if err != nil {
2022-11-14 13:08:10 -06:00
return response . Err ( quota . ErrBadRequest . Errorf ( "orgId is invalid: %w" , err ) )
2022-01-14 10:55:57 -06:00
}
2021-10-11 07:30:59 -05:00
cmd . Target = web . Params ( c . Req ) [ ":target" ]
2015-09-11 10:17:10 -05:00
2022-11-14 13:08:10 -06:00
if err := hs . QuotaService . Update ( c . Req . Context ( ) , & cmd ) ; err != nil {
return response . ErrOrFallback ( http . StatusInternalServerError , "Failed to update org quotas" , err )
2015-09-11 10:17:10 -05:00
}
2021-01-15 07:43:20 -06:00
return response . Success ( "Organization quota updated" )
2015-09-11 10:17:10 -05:00
}
2022-07-27 08:54:37 -05: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 01:50:36 -06:00
func ( hs * HTTPServer ) GetUserQuotas ( c * contextmodel . ReqContext ) response . Response {
2022-01-14 10:55:57 -06:00
id , err := strconv . ParseInt ( web . Params ( c . Req ) [ ":id" ] , 10 , 64 )
if err != nil {
2022-11-14 13:08:10 -06:00
return response . Err ( quota . ErrBadRequest . Errorf ( "id is invalid: %w" , err ) )
2022-01-14 10:55:57 -06:00
}
2022-11-14 13:08:10 -06:00
q , err := hs . QuotaService . GetQuotasByScope ( c . Req . Context ( ) , quota . UserScope , id )
if err != nil {
return response . ErrOrFallback ( http . StatusInternalServerError , "Failed to get org quotas" , err )
2015-09-10 12:47:33 -05:00
}
2022-11-14 13:08:10 -06:00
return response . JSON ( http . StatusOK , q )
2015-09-10 12:47:33 -05:00
}
2022-07-27 08:54:37 -05: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 01:50:36 -06:00
func ( hs * HTTPServer ) UpdateUserQuota ( c * contextmodel . ReqContext ) response . Response {
2022-11-14 13:08:10 -06:00
cmd := quota . UpdateQuotaCmd { }
2022-01-14 10:55:57 -06:00
var err error
2021-11-29 03:18:01 -06:00
if err := web . Bind ( c . Req , & cmd ) ; err != nil {
2022-11-14 13:08:10 -06:00
return response . Err ( quota . ErrBadRequest . Errorf ( "bad request data: %w" , err ) )
2021-11-29 03:18:01 -06:00
}
2022-11-14 13:08:10 -06:00
cmd . UserID , err = strconv . ParseInt ( web . Params ( c . Req ) [ ":id" ] , 10 , 64 )
2022-01-14 10:55:57 -06:00
if err != nil {
2022-11-14 13:08:10 -06:00
return response . Err ( quota . ErrBadRequest . Errorf ( "id is invalid: %w" , err ) )
2022-01-14 10:55:57 -06:00
}
2021-10-11 07:30:59 -05:00
cmd . Target = web . Params ( c . Req ) [ ":target" ]
2015-07-21 05:30:31 -05:00
2022-11-14 13:08:10 -06:00
if err := hs . QuotaService . Update ( c . Req . Context ( ) , & cmd ) ; err != nil {
return response . ErrOrFallback ( http . StatusInternalServerError , "Failed to update org quotas" , err )
2015-07-20 07:51:27 -05:00
}
2021-01-15 07:43:20 -06:00
return response . Success ( "Organization quota updated" )
2015-07-20 07:51:27 -05:00
}
2022-07-27 08:54:37 -05:00
// swagger:parameters updateUserQuota
type UpdateUserQuotaParams struct {
// in:body
// required:true
2022-11-14 13:08:10 -06:00
Body quota . UpdateQuotaCmd ` json:"body" `
2022-07-27 08:54:37 -05: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-14 13:08:10 -06:00
Body quota . UpdateQuotaCmd ` json:"body" `
2022-07-27 08:54:37 -05: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-14 13:08:10 -06:00
Body [ ] * quota . QuotaDTO ` json:"body" `
2022-07-27 08:54:37 -05:00
}