forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 4
/
quotas.go
130 lines (115 loc) · 2.99 KB
/
quotas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package models
import (
"errors"
"github.com/grafana/grafana/pkg/setting"
"time"
)
var ErrInvalidQuotaTarget = errors.New("Invalid quota target")
type Quota struct {
Id int64
OrgId int64
UserId int64
Target string
Limit int64
Created time.Time
Updated time.Time
}
type QuotaScope struct {
Name string
Target string
DefaultLimit int64
}
type OrgQuotaDTO struct {
OrgId int64 `json:"org_id"`
Target string `json:"target"`
Limit int64 `json:"limit"`
Used int64 `json:"used"`
}
type UserQuotaDTO struct {
UserId int64 `json:"user_id"`
Target string `json:"target"`
Limit int64 `json:"limit"`
Used int64 `json:"used"`
}
type GlobalQuotaDTO struct {
Target string `json:"target"`
Limit int64 `json:"limit"`
Used int64 `json:"used"`
}
type GetOrgQuotaByTargetQuery struct {
Target string
OrgId int64
Default int64
Result *OrgQuotaDTO
}
type GetOrgQuotasQuery struct {
OrgId int64
Result []*OrgQuotaDTO
}
type GetUserQuotaByTargetQuery struct {
Target string
UserId int64
Default int64
Result *UserQuotaDTO
}
type GetUserQuotasQuery struct {
UserId int64
Result []*UserQuotaDTO
}
type GetGlobalQuotaByTargetQuery struct {
Target string
Default int64
Result *GlobalQuotaDTO
}
type UpdateOrgQuotaCmd struct {
Target string `json:"target"`
Limit int64 `json:"limit"`
OrgId int64 `json:"-"`
}
type UpdateUserQuotaCmd struct {
Target string `json:"target"`
Limit int64 `json:"limit"`
UserId int64 `json:"-"`
}
func GetQuotaScopes(target string) ([]QuotaScope, error) {
scopes := make([]QuotaScope, 0)
switch target {
case "user":
scopes = append(scopes,
QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.User},
QuotaScope{Name: "org", Target: "org_user", DefaultLimit: setting.Quota.Org.User},
)
return scopes, nil
case "org":
scopes = append(scopes,
QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.Org},
QuotaScope{Name: "user", Target: "org_user", DefaultLimit: setting.Quota.User.Org},
)
return scopes, nil
case "dashboard":
scopes = append(scopes,
QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.Dashboard},
QuotaScope{Name: "org", Target: target, DefaultLimit: setting.Quota.Org.Dashboard},
)
return scopes, nil
case "data_source":
scopes = append(scopes,
QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.DataSource},
QuotaScope{Name: "org", Target: target, DefaultLimit: setting.Quota.Org.DataSource},
)
return scopes, nil
case "api_key":
scopes = append(scopes,
QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.ApiKey},
QuotaScope{Name: "org", Target: target, DefaultLimit: setting.Quota.Org.ApiKey},
)
return scopes, nil
case "session":
scopes = append(scopes,
QuotaScope{Name: "global", Target: target, DefaultLimit: setting.Quota.Global.Session},
)
return scopes, nil
default:
return scopes, ErrInvalidQuotaTarget
}
}