domainquota.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/util/rbacscope"
  19. "yunion.io/x/onecloud/pkg/cloudcommon/db/quotas"
  20. commonOptions "yunion.io/x/onecloud/pkg/cloudcommon/options"
  21. "yunion.io/x/onecloud/pkg/compute/options"
  22. )
  23. var (
  24. DomainQuota SDomainQuota
  25. DomainQuotaManager *SQuotaManager
  26. DomainUsageManager *SQuotaManager
  27. DomainPendingUsageManager *SQuotaManager
  28. )
  29. func init() {
  30. DomainQuota = SDomainQuota{}
  31. DomainUsageManager = &SQuotaManager{
  32. SQuotaBaseManager: quotas.NewQuotaUsageManager(DomainQuota,
  33. rbacscope.ScopeDomain,
  34. "domain_quota_usage_tbl",
  35. "domain_quota_usage",
  36. "domain_quota_usages",
  37. ),
  38. }
  39. DomainPendingUsageManager = &SQuotaManager{
  40. SQuotaBaseManager: quotas.NewQuotaUsageManager(DomainQuota,
  41. rbacscope.ScopeDomain,
  42. "domain_quota_pending_usage_tbl",
  43. "domain_quota_pending_usage",
  44. "domain_quota_pending_usages",
  45. ),
  46. }
  47. DomainQuotaManager = &SQuotaManager{
  48. SQuotaBaseManager: quotas.NewQuotaBaseManager(DomainQuota,
  49. rbacscope.ScopeDomain,
  50. "domain_quota_tbl",
  51. DomainPendingUsageManager,
  52. DomainUsageManager,
  53. "domain_quota",
  54. "domain_quotas",
  55. ),
  56. }
  57. quotas.Register(DomainQuotaManager)
  58. }
  59. type SDomainQuota struct {
  60. quotas.SQuotaBase
  61. quotas.SBaseDomainQuotaKeys
  62. Cloudaccount int `default:"-1" allow_zero:"true" json:"cloudaccount"`
  63. Globalvpc int `default:"-1" allow_zero:"true" json:"globalvpc"`
  64. DnsZone int `default:"-1" allow_zero:"true" json:"dns_zone"`
  65. }
  66. func (self *SDomainQuota) GetKeys() quotas.IQuotaKeys {
  67. return self.SBaseDomainQuotaKeys
  68. }
  69. func (self *SDomainQuota) SetKeys(keys quotas.IQuotaKeys) {
  70. self.SBaseDomainQuotaKeys = keys.(quotas.SBaseDomainQuotaKeys)
  71. }
  72. func (self *SDomainQuota) FetchSystemQuota() {
  73. base := 0
  74. switch options.Options.DefaultQuotaValue {
  75. case commonOptions.DefaultQuotaUnlimit:
  76. base = -1
  77. case commonOptions.DefaultQuotaZero:
  78. base = 0
  79. case commonOptions.DefaultQuotaDefault:
  80. base = 1
  81. }
  82. defaultValue := func(def int) int {
  83. if base < 0 {
  84. return -1
  85. } else {
  86. return def * base
  87. }
  88. }
  89. self.Globalvpc = defaultValue(options.Options.DefaultGlobalvpcQuota)
  90. self.Cloudaccount = defaultValue(options.Options.DefaultCloudaccountQuota)
  91. self.DnsZone = defaultValue(options.Options.DefaultDnsZoneQuota)
  92. }
  93. func (self *SDomainQuota) FetchUsage(ctx context.Context) error {
  94. keys := self.SBaseDomainQuotaKeys
  95. scope := keys.Scope()
  96. ownerId := keys.OwnerId()
  97. self.Globalvpc = GlobalVpcManager.totalCount(scope, ownerId)
  98. self.Cloudaccount = CloudaccountManager.totalCount(scope, ownerId)
  99. self.DnsZone = DnsZoneManager.totalCount(scope, ownerId)
  100. return nil
  101. }
  102. func (self *SDomainQuota) ResetNegative() {
  103. if self.Globalvpc < 0 {
  104. self.Globalvpc = 0
  105. }
  106. if self.Cloudaccount < 0 {
  107. self.Cloudaccount = 0
  108. }
  109. if self.DnsZone < 0 {
  110. self.DnsZone = 0
  111. }
  112. }
  113. func (self *SDomainQuota) IsEmpty() bool {
  114. if self.Globalvpc > 0 {
  115. return false
  116. }
  117. if self.Cloudaccount > 0 {
  118. return false
  119. }
  120. if self.DnsZone > 0 {
  121. return false
  122. }
  123. return true
  124. }
  125. func (self *SDomainQuota) Add(quota quotas.IQuota) {
  126. squota := quota.(*SDomainQuota)
  127. self.Globalvpc = self.Globalvpc + quotas.NonNegative(squota.Globalvpc)
  128. self.Cloudaccount = self.Cloudaccount + quotas.NonNegative(squota.Cloudaccount)
  129. self.DnsZone = self.DnsZone + quotas.NonNegative(squota.DnsZone)
  130. }
  131. func (self *SDomainQuota) Sub(quota quotas.IQuota) {
  132. squota := quota.(*SDomainQuota)
  133. self.Globalvpc = nonNegative(self.Globalvpc - squota.Globalvpc)
  134. self.Cloudaccount = nonNegative(self.Cloudaccount - squota.Cloudaccount)
  135. self.DnsZone = nonNegative(self.DnsZone - squota.DnsZone)
  136. }
  137. func (self *SDomainQuota) Allocable(request quotas.IQuota) int {
  138. squota := request.(*SDomainQuota)
  139. cnt := -1
  140. if self.Globalvpc >= 0 && squota.Globalvpc > 0 && (cnt < 0 || cnt > self.Globalvpc/squota.Globalvpc) {
  141. cnt = self.Globalvpc / squota.Globalvpc
  142. }
  143. if self.Cloudaccount >= 0 && squota.Cloudaccount > 0 && (cnt < 0 || cnt > self.Cloudaccount/squota.Cloudaccount) {
  144. cnt = self.Cloudaccount / squota.Cloudaccount
  145. }
  146. if self.DnsZone >= 0 && squota.DnsZone > 0 && (cnt < 0 || cnt > self.DnsZone/squota.DnsZone) {
  147. cnt = self.DnsZone / squota.DnsZone
  148. }
  149. return cnt
  150. }
  151. func (self *SDomainQuota) Update(quota quotas.IQuota) {
  152. squota := quota.(*SDomainQuota)
  153. if squota.Globalvpc > 0 {
  154. self.Globalvpc = squota.Globalvpc
  155. }
  156. if squota.Cloudaccount > 0 {
  157. self.Cloudaccount = squota.Cloudaccount
  158. }
  159. if squota.DnsZone > 0 {
  160. self.DnsZone = squota.DnsZone
  161. }
  162. }
  163. func (used *SDomainQuota) Exceed(request quotas.IQuota, quota quotas.IQuota) error {
  164. err := quotas.NewOutOfQuotaError()
  165. sreq := request.(*SDomainQuota)
  166. squota := quota.(*SDomainQuota)
  167. if quotas.Exceed(used.Globalvpc, sreq.Globalvpc, squota.Globalvpc) {
  168. err.Add(used, "globalvpc", squota.Globalvpc, used.Globalvpc, sreq.Globalvpc)
  169. }
  170. if quotas.Exceed(used.Cloudaccount, sreq.Cloudaccount, squota.Cloudaccount) {
  171. err.Add(used, "cloudaccount", squota.Cloudaccount, used.Cloudaccount, sreq.Cloudaccount)
  172. }
  173. if quotas.Exceed(used.DnsZone, sreq.DnsZone, squota.DnsZone) {
  174. err.Add(used, "dns_zone", squota.DnsZone, used.DnsZone, sreq.DnsZone)
  175. }
  176. if err.IsError() {
  177. return err
  178. } else {
  179. return nil
  180. }
  181. }
  182. func (self *SDomainQuota) ToJSON(prefix string) jsonutils.JSONObject {
  183. ret := jsonutils.NewDict()
  184. ret.Add(jsonutils.NewInt(int64(self.Globalvpc)), keyName(prefix, "globalvpc"))
  185. ret.Add(jsonutils.NewInt(int64(self.Cloudaccount)), keyName(prefix, "cloudaccount"))
  186. ret.Add(jsonutils.NewInt(int64(self.DnsZone)), keyName(prefix, "dns_zone"))
  187. return ret
  188. }