zonequota.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. )
  21. var (
  22. ZoneQuota SZoneQuota
  23. ZoneQuotaManager *SQuotaManager
  24. ZoneUsageManager *SQuotaManager
  25. ZonePendingUsageManager *SQuotaManager
  26. )
  27. func init() {
  28. ZoneQuota = SZoneQuota{}
  29. ZoneUsageManager = &SQuotaManager{
  30. SQuotaBaseManager: quotas.NewQuotaUsageManager(ZoneQuota,
  31. rbacscope.ScopeDomain,
  32. "zone_quota_usage_tbl",
  33. "zone_quota_usage",
  34. "zone_quota_usages",
  35. ),
  36. }
  37. ZonePendingUsageManager = &SQuotaManager{
  38. SQuotaBaseManager: quotas.NewQuotaUsageManager(ZoneQuota,
  39. rbacscope.ScopeDomain,
  40. "zone_quota_pending_usage_tbl",
  41. "zone_quota_pending_usage",
  42. "zone_quota_pending_usages",
  43. ),
  44. }
  45. ZoneQuotaManager = &SQuotaManager{
  46. SQuotaBaseManager: quotas.NewQuotaBaseManager(ZoneQuota,
  47. rbacscope.ScopeDomain,
  48. "zone_quota_tbl",
  49. ZonePendingUsageManager,
  50. ZoneUsageManager,
  51. "zone_quota",
  52. "zone_quotas",
  53. ),
  54. }
  55. quotas.Register(ZoneQuotaManager)
  56. }
  57. type SZoneQuota struct {
  58. quotas.SQuotaBase
  59. quotas.SZonalCloudResourceKeys
  60. // Loadbalancer int `default:"-1" allow_zero:"true"`
  61. }
  62. func (self *SZoneQuota) GetKeys() quotas.IQuotaKeys {
  63. return self.SZonalCloudResourceKeys
  64. }
  65. func (self *SZoneQuota) SetKeys(keys quotas.IQuotaKeys) {
  66. self.SZonalCloudResourceKeys = keys.(quotas.SZonalCloudResourceKeys)
  67. }
  68. func (self *SZoneQuota) FetchSystemQuota() {
  69. /*keys := self.SBaseQuotaKeys
  70. base := 0
  71. switch options.Options.DefaultQuotaValue {
  72. case commonOptions.DefaultQuotaUnlimit:
  73. base = -1
  74. case commonOptions.DefaultQuotaZero:
  75. base = 0
  76. if keys.Scope() == rbacutils.ScopeDomain { // domain level quota
  77. base = 10
  78. } else if keys.DomainId == identityapi.DEFAULT_DOMAIN_ID && keys.ProjectId == auth.AdminCredential().GetProjectId() {
  79. base = 1
  80. }
  81. case commonOptions.DefaultQuotaDefault:
  82. base = 1
  83. if keys.Scope() == rbacutils.ScopeDomain {
  84. base = 10
  85. }
  86. }
  87. defaultValue := func(def int) int {
  88. if base < 0 {
  89. return -1
  90. } else {
  91. return def * base
  92. }
  93. }*/
  94. // self.Loadbalancer = defaultValue(options.Options.DefaultLoadbalancerQuota)
  95. }
  96. func (self *SZoneQuota) FetchUsage(ctx context.Context) error {
  97. /*keys := self.SZonalCloudResourceKeys
  98. scope := keys.Scope()
  99. ownerId := keys.OwnerId()
  100. rangeObjs := make([]db.IStandaloneModel, 0)
  101. if len(keys.ManagerId) > 0 {
  102. obj, err := CloudproviderManager.FetchById(keys.ManagerId)
  103. if err != nil {
  104. return errors.Wrap(err, "CloudproviderManager.FetchById")
  105. }
  106. rangeObjs = append(rangeObjs, obj.(db.IStandaloneModel))
  107. } else if len(keys.AccountId) > 0 {
  108. obj, err := CloudaccountManager.FetchById(keys.AccountId)
  109. if err != nil {
  110. return errors.Wrap(err, "CloudaccountManager.FetchById")
  111. }
  112. rangeObjs = append(rangeObjs, obj.(db.IStandaloneModel))
  113. }
  114. if len(keys.ZoneId) > 0 {
  115. obj, err := ZoneManager.FetchById(keys.ZoneId)
  116. if err != nil {
  117. return errors.Wrap(err, "ZoneManager.FetchById")
  118. }
  119. rangeObjs = append(rangeObjs, obj.(db.IStandaloneModel))
  120. } else if len(keys.RegionId) > 0 {
  121. obj, err := CloudregionManager.FetchById(keys.RegionId)
  122. if err != nil {
  123. return errors.Wrap(err, "CloudregionManager.FetchById")
  124. }
  125. rangeObjs = append(rangeObjs, obj.(db.IStandaloneModel))
  126. }
  127. var providers []string
  128. if len(keys.Provider) > 0 {
  129. providers = []string{keys.Provider}
  130. }
  131. var brands []string
  132. if len(keys.Brand) > 0 {
  133. brands = []string{keys.Brand}
  134. }
  135. self.Loadbalancer, _ = LoadbalancerManager.TotalCount(scope, ownerId, rangeObjs, providers, brands, keys.CloudEnv)
  136. */
  137. return nil
  138. }
  139. func (self *SZoneQuota) ResetNegative() {
  140. /*if self.Loadbalancer < 0 {
  141. self.Loadbalancer = 0
  142. }*/
  143. }
  144. func (self *SZoneQuota) IsEmpty() bool {
  145. /*if self.Loadbalancer > 0 {
  146. return false
  147. }*/
  148. return true
  149. }
  150. func (self *SZoneQuota) Add(quota quotas.IQuota) {
  151. // squota := quota.(*SZoneQuota)
  152. // self.Loadbalancer = self.Loadbalancer + quotas.NonNegative(squota.Loadbalancer)
  153. }
  154. func (self *SZoneQuota) Sub(quota quotas.IQuota) {
  155. // squota := quota.(*SZoneQuota)
  156. // self.Loadbalancer = nonNegative(self.Loadbalancer - squota.Loadbalancer)
  157. }
  158. func (self *SZoneQuota) Allocable(request quotas.IQuota) int {
  159. return -1
  160. }
  161. func (self *SZoneQuota) Update(quota quotas.IQuota) {
  162. // squota := quota.(*SZoneQuota)
  163. // if squota.Loadbalancer > 0 {
  164. // self.Loadbalancer = squota.Loadbalancer
  165. // }
  166. }
  167. func (used *SZoneQuota) Exceed(request quotas.IQuota, quota quotas.IQuota) error {
  168. err := quotas.NewOutOfQuotaError()
  169. /*sreq := request.(*SZoneQuota)
  170. squota := quota.(*SZoneQuota)
  171. if quotas.Exceed(used.Loadbalancer, sreq.Loadbalancer, squota.Loadbalancer) {
  172. err.Add("loadbalancer", squota.Loadbalancer, used.Loadbalancer, sreq.Loadbalancer)
  173. }*/
  174. if err.IsError() {
  175. return err
  176. } else {
  177. return nil
  178. }
  179. }
  180. func (self *SZoneQuota) ToJSON(prefix string) jsonutils.JSONObject {
  181. ret := jsonutils.NewDict()
  182. // ret.Add(jsonutils.NewInt(int64(self.Loadbalancer)), keyName(prefix, "loadbalancer"))
  183. return ret
  184. }