-
Notifications
You must be signed in to change notification settings - Fork 43
/
quota.go
133 lines (115 loc) · 3.2 KB
/
quota.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
131
132
133
package api
import (
"github.com/baetyl/baetyl-go/v2/log"
"github.com/baetyl/baetyl-cloud/v2/common"
"github.com/baetyl/baetyl-cloud/v2/models"
)
// GetQuota for admin api
func (api *API) GetQuota(c *common.Context) (interface{}, error) {
ns := c.GetNamespace()
quotas, err := api.Quota.GetQuota(ns)
return quotas, err
}
// GetQuota for mis server api
// - param namespace string
func (api *API) GetQuotaForMis(c *common.Context) (interface{}, error) {
ns := c.Param(common.KeyContextNamespace)
quota := &models.Quota{
Namespace: ns,
}
quotas, err := api.Quota.GetQuota(quota.Namespace)
if err != nil {
return nil, err
}
var quotaDatas []models.Quota
for k, v := range quotas {
quotaDatas = append(quotaDatas, models.Quota{
Namespace: ns,
QuotaName: k,
Quota: v,
})
}
return models.MisData{
Count: len(quotaDatas),
Rows: quotaDatas,
}, nil
}
// CreateQuota for mis server api
// - param namespace string
// - param quotaName string
// - param quota int
func (api *API) CreateQuota(c *common.Context) (interface{}, error) {
quota := &models.Quota{}
if err := c.LoadBody(quota); err != nil {
return nil, err
}
quotas := map[string]int{
quota.QuotaName: quota.Quota,
}
err := api.CreateQuotas(quota.Namespace, quotas)
return nil, err
}
// UpdateQuota for mis server api
// - param namespace string
// - param quotaName string
// - param quota int
func (api *API) UpdateQuota(c *common.Context) (interface{}, error) {
quota := &models.Quota{}
if err := c.LoadBody(quota); err != nil {
return nil, err
}
err := api.Quota.UpdateQuota(quota.Namespace, quota.QuotaName, quota.Quota)
return nil, err
}
// DeleteQuota for mis server api
// - param namespace string
// - param quotaName string
func (api *API) DeleteQuota(c *common.Context) (interface{}, error) {
quota := &models.Quota{}
if err := c.LoadBody(quota); err != nil {
return nil, err
}
err := api.Quota.DeleteQuota(quota.Namespace, quota.QuotaName)
return nil, err
}
// InitQuotas
// - param namespace string
func (api *API) InitQuotas(namespace string) error {
quotas, err := api.Quota.GetDefaultQuotas(namespace)
if err != nil {
return err
}
return api.CreateQuotas(namespace, quotas)
}
// DeleteQuotaByNamespace
// - param namespace string
func (api *API) DeleteQuotaByNamespace(namespace string) error {
if err := api.Quota.DeleteQuotaByNamespace(namespace); err != nil {
common.LogDirtyData(err,
log.Any("type", "DeleteQuotaByNamespace"),
log.Any(common.KeyContextNamespace, namespace))
return err
}
return nil
}
func (api *API) ReleaseQuota(namespace, quotaName string, number int) error {
if err := api.Quota.ReleaseQuota(namespace, quotaName, number); err != nil {
common.LogDirtyData(err,
log.Any("type", "QuotaRelease"),
log.Any(common.KeyContextNamespace, namespace),
log.Any("name", quotaName),
log.Any("quota", number))
return err
}
return nil
}
func (api *API) CreateQuotas(namespace string, quotas map[string]int) error {
if err := api.Quota.CreateQuota(namespace, quotas); err != nil {
common.LogDirtyData(err,
log.Any("type", "CreateQuotas"),
log.Any(common.KeyContextNamespace, namespace),
log.Any("quotas", quotas))
return err
}
return nil
}