updated CanCreateEnvironment for limit classes (#606)

This commit is contained in:
Michael Quigley 2024-06-04 16:27:05 -04:00
parent 97dbd197d6
commit 97c2cd79d2
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62

View File

@ -56,19 +56,43 @@ func (a *Agent) CanCreateEnvironment(acctId int, trx *sqlx.Tx) (bool, error) {
if err := a.str.LimitCheckLock(acctId, trx); err != nil {
return false, err
}
if empty, err := a.str.IsBandwidthLimitJournalEmpty(acctId, trx); err == nil && !empty {
alj, err := a.str.FindLatestBandwidthLimitJournal(acctId, trx)
if err != nil {
return false, err
}
if alj.Action == store.LimitLimitAction {
return false, nil
}
} else if err != nil {
alcs, err := a.str.FindAppliedLimitClassesForAccount(acctId, trx)
if err != nil {
return false, err
}
maxEnvironments := a.cfg.Environments
var lcId *int
for _, alc := range alcs {
if alc.ShareMode == "" && alc.BackendMode == "" && alc.Environments > maxEnvironments {
maxEnvironments = alc.Environments
lcId = &alc.Id
}
}
if a.cfg.Environments > Unlimited {
if lcId == nil {
if empty, err := a.str.IsBandwidthLimitJournalEmptyForGlobal(acctId, trx); err == nil && !empty {
lj, err := a.str.FindLatestBandwidthLimitJournalForGlobal(acctId, trx)
if err != nil {
return false, err
}
if lj.Action == store.LimitLimitAction {
return false, nil
}
}
} else {
if empty, err := a.str.IsBandwidthLimitJournalEmptyForLimitClass(acctId, *lcId, trx); err == nil && !empty {
lj, err := a.str.FindLatestBandwidthLimitJournalForLimitClass(acctId, *lcId, trx)
if err != nil {
return false, err
}
if lj.Action == store.LimitLimitAction {
return false, nil
}
}
}
if maxEnvironments > Unlimited {
envs, err := a.str.FindEnvironmentsForAccount(acctId, trx)
if err != nil {
return false, err
@ -78,6 +102,7 @@ func (a *Agent) CanCreateEnvironment(acctId int, trx *sqlx.Tx) (bool, error) {
}
}
}
return true, nil
}