fix: prevent quota usage from being negative (#4162)

This commit is contained in:
Jian Qiu
2019-12-14 12:11:21 +08:00
committed by GitHub
parent 14b38a3126
commit 489c8b3ca9
7 changed files with 85 additions and 3 deletions
+1
View File
@@ -41,6 +41,7 @@ type IQuota interface {
Update(quota IQuota)
Add(quota IQuota)
Sub(quota IQuota)
ResetNegative()
Exceed(request IQuota, quota IQuota) error
// IsEmpty() bool
ToJSON(prefix string) jsonutils.JSONObject
+9 -3
View File
@@ -37,7 +37,7 @@ type SQuotaBaseManager struct {
pendingStore IQuotaStore
usageStore IQuotaStore
autoCreate bool
nonNegative bool
}
func NewQuotaBaseManager(model interface{}, tableName string, pendingStore IQuotaStore, usageStore IQuotaStore, keyword, keywordPlural string) SQuotaBaseManager {
@@ -47,13 +47,14 @@ func NewQuotaBaseManager(model interface{}, tableName string, pendingStore IQuot
SResourceBaseManager: db.NewResourceBaseManager(model, tableName, keyword, keywordPlural),
pendingStore: pendingStore,
usageStore: usageStore,
autoCreate: true,
nonNegative: false,
}
}
func NewQuotaUsageManager(model interface{}, tableName string, keyword, keywordPlural string) SQuotaBaseManager {
return SQuotaBaseManager{
SResourceBaseManager: db.NewResourceBaseManager(model, tableName, keyword, keywordPlural),
nonNegative: true,
}
}
@@ -88,7 +89,9 @@ func (manager *SQuotaBaseManager) getQuotaByKeys(ctx context.Context, keys IQuot
return errors.Wrap(err, "q.First")
}
}
quota.SetKeys(keys)
if manager.nonNegative {
quota.ResetNegative()
}
return nil
}
@@ -140,6 +143,9 @@ func (manager *SQuotaBaseManager) getQuotasInternal(ctx context.Context, keys IQ
if err != nil {
return nil, errors.Wrap(err, "q.Row2Struct")
}
if manager.nonNegative {
r.ResetNegative()
}
results = append(results, r)
}
sort.Sort(TQuotaList(results))
+6
View File
@@ -118,6 +118,12 @@ func (self *SProjectQuota) FetchUsage(ctx context.Context) error {
return nil
}
func (self *SProjectQuota) ResetNegative() {
if self.Secgroup < 0 {
self.Secgroup = 0
}
}
func (self *SProjectQuota) IsEmpty() bool {
if self.Secgroup > 0 {
return false
+21
View File
@@ -191,6 +191,27 @@ func (self *SQuota) FetchUsage(ctx context.Context) error {
return nil
}
func (self *SQuota) ResetNegative() {
if self.Count < 0 {
self.Count = 0
}
if self.Cpu < 0 {
self.Cpu = 0
}
if self.Memory < 0 {
self.Memory = 0
}
if self.Storage < 0 {
self.Storage = 0
}
if self.Group < 0 {
self.Group = 0
}
if self.IsolatedDevice < 0 {
self.IsolatedDevice = 0
}
}
func (self *SQuota) IsEmpty() bool {
if self.Count > 0 {
return false
+36
View File
@@ -196,6 +196,42 @@ func (self *SRegionQuota) FetchUsage(ctx context.Context) error {
return nil
}
func (self *SRegionQuota) ResetNegative() {
if self.Port < 0 {
self.Port = 0
}
if self.Eip < 0 {
self.Eip = 0
}
if self.Eport < 0 {
self.Eport = 0
}
if self.Bw < 0 {
self.Bw = 0
}
if self.Ebw < 0 {
self.Ebw = 0
}
if self.Snapshot < 0 {
self.Snapshot = 0
}
if self.Bucket < 0 {
self.Bucket = 0
}
if self.ObjectGB < 0 {
self.ObjectGB = 0
}
if self.ObjectCnt < 0 {
self.ObjectCnt = 0
}
if self.Rds < 0 {
self.Rds = 0
}
if self.Cache < 0 {
self.Cache = 0
}
}
func (self *SRegionQuota) IsEmpty() bool {
if self.Port > 0 {
return false
+6
View File
@@ -158,6 +158,12 @@ func (self *SZoneQuota) FetchUsage(ctx context.Context) error {
return nil
}
func (self *SZoneQuota) ResetNegative() {
if self.Loadbalancer < 0 {
self.Loadbalancer = 0
}
}
func (self *SZoneQuota) IsEmpty() bool {
if self.Loadbalancer > 0 {
return false
+6
View File
@@ -132,6 +132,12 @@ func (self *SQuota) FetchUsage(ctx context.Context) error {
return nil
}
func (self *SQuota) ResetNegative() {
if self.Image < 0 {
self.Image = 0
}
}
func (self *SQuota) IsEmpty() bool {
if self.Image > 0 {
return false