mod_quotas.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 quota
  15. import (
  16. "fmt"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/util/httputils"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. "yunion.io/x/onecloud/pkg/mcclient/modulebase"
  21. "yunion.io/x/onecloud/pkg/mcclient/modules"
  22. )
  23. type QuotaManager struct {
  24. modulebase.ResourceManager
  25. }
  26. func (this *QuotaManager) getURL(params jsonutils.JSONObject) string {
  27. return this.getURL2(params, "")
  28. }
  29. func (this *QuotaManager) getURL2(params jsonutils.JSONObject, extra string) string {
  30. url := fmt.Sprintf("/%s", this.URLPath())
  31. query := jsonutils.NewDict()
  32. if params != nil {
  33. tenant, _ := params.GetString("tenant")
  34. if len(tenant) > 0 {
  35. url = fmt.Sprintf("%s/projects/%s", url, tenant)
  36. } else {
  37. domain := jsonutils.GetAnyString(params, []string{"domain", "project_domain"})
  38. if len(domain) > 0 {
  39. url = fmt.Sprintf("%s/domains/%s", url, domain)
  40. } else {
  41. scope, _ := params.GetString("scope")
  42. if len(scope) > 0 {
  43. query.Add(jsonutils.NewString(scope), "scope")
  44. }
  45. }
  46. }
  47. refresh := jsonutils.QueryBoolean(params, "refresh", false)
  48. if refresh {
  49. query.Add(jsonutils.JSONTrue, "refresh")
  50. }
  51. primary := jsonutils.QueryBoolean(params, "primary", false)
  52. if primary {
  53. query.Add(jsonutils.JSONTrue, "primary")
  54. }
  55. }
  56. if len(extra) > 0 {
  57. url = httputils.JoinPath(url, extra)
  58. }
  59. if query.Size() > 0 {
  60. url += "?" + query.QueryString()
  61. }
  62. return url
  63. }
  64. func (this *QuotaManager) GetQuota(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  65. quotas, err := modulebase.Get(this.ResourceManager, s, this.getURL(params), this.KeywordPlural)
  66. if err != nil {
  67. return nil, err
  68. }
  69. ret := jsonutils.NewDict()
  70. ret.Add(quotas, "data")
  71. return ret, nil
  72. }
  73. func (this *QuotaManager) DoCleanPendingUsage(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  74. url := this.getURL2(params, "pending")
  75. results, err := modulebase.Delete(this.ResourceManager, s, url, nil, "")
  76. if err != nil {
  77. return nil, err
  78. }
  79. return results, nil
  80. }
  81. func (this *QuotaManager) GetQuotaList(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  82. var reqUrl string
  83. query := jsonutils.NewDict()
  84. domainId := jsonutils.GetAnyString(params, []string{"domain", "project_domain", "domain_id", "project_domain_id"})
  85. if len(domainId) > 0 {
  86. query.Add(jsonutils.NewString(domainId), "project_domain")
  87. reqUrl = fmt.Sprintf("%s/projects", this.URLPath())
  88. } else {
  89. reqUrl = fmt.Sprintf("%s/domains", this.URLPath())
  90. }
  91. refresh := jsonutils.QueryBoolean(params, "refresh", false)
  92. if refresh {
  93. query.Add(jsonutils.JSONTrue, "refresh")
  94. }
  95. primary := jsonutils.QueryBoolean(params, "primary", false)
  96. if primary {
  97. query.Add(jsonutils.JSONTrue, "primary")
  98. }
  99. if query.Size() > 0 {
  100. reqUrl += "?" + query.QueryString()
  101. }
  102. computeQuotaList, err := modulebase.List(this.ResourceManager, s, reqUrl, this.KeywordPlural)
  103. if err != nil {
  104. return nil, err
  105. }
  106. ret := jsonutils.NewDict()
  107. ret.Add(jsonutils.NewArray(computeQuotaList.Data...), "data")
  108. return ret, nil
  109. }
  110. func (this *QuotaManager) DoQuotaSet(s *mcclient.ClientSession, params jsonutils.JSONObject) (jsonutils.JSONObject, error) {
  111. url := this.getURL(params)
  112. body := jsonutils.NewDict()
  113. body.Add(params, this.KeywordPlural)
  114. results, err := modulebase.Post(this.ResourceManager, s, url, body, this.KeywordPlural)
  115. if err != nil {
  116. return nil, err
  117. }
  118. ret := jsonutils.NewDict()
  119. ret.Add(results, "data")
  120. return ret, nil
  121. }
  122. var (
  123. Quotas QuotaManager
  124. ProjectQuotas QuotaManager
  125. RegionQuotas QuotaManager
  126. ZoneQuotas QuotaManager
  127. DomainQuotas QuotaManager
  128. InfrasQuotas QuotaManager
  129. ImageQuotas QuotaManager
  130. IdentityQuotas QuotaManager
  131. quotaColumns = []string{}
  132. /*quotaColumns = []string{
  133. "domain", "domain_id",
  134. "tenant", "tenant_id",
  135. "provider",
  136. "brand",
  137. "cloud_env",
  138. "account", "account_id",
  139. "manager", "manager_id",
  140. "region", "region_id",
  141. "zone", "zone_id",
  142. "hypervisor",
  143. }*/
  144. )
  145. func init() {
  146. Quotas = QuotaManager{modules.NewComputeManager("quota", "quotas",
  147. quotaColumns,
  148. []string{})}
  149. modules.Register(&Quotas)
  150. ProjectQuotas = QuotaManager{modules.NewComputeManager("project_quota", "project_quotas",
  151. quotaColumns,
  152. []string{})}
  153. modules.Register(&ProjectQuotas)
  154. RegionQuotas = QuotaManager{modules.NewComputeManager("region_quota", "region_quotas",
  155. quotaColumns,
  156. []string{})}
  157. modules.Register(&RegionQuotas)
  158. ZoneQuotas = QuotaManager{modules.NewComputeManager("zone_quota", "zone_quotas",
  159. quotaColumns,
  160. []string{})}
  161. modules.Register(&ZoneQuotas)
  162. DomainQuotas = QuotaManager{modules.NewComputeManager("domain_quota", "domain_quotas",
  163. quotaColumns,
  164. []string{})}
  165. modules.Register(&DomainQuotas)
  166. InfrasQuotas = QuotaManager{modules.NewComputeManager("infras_quota", "infras_quotas",
  167. quotaColumns,
  168. []string{})}
  169. modules.Register(&InfrasQuotas)
  170. ImageQuotas = QuotaManager{modules.NewImageManager("image_quota", "image_quotas",
  171. quotaColumns,
  172. []string{})}
  173. modules.Register(&ImageQuotas)
  174. IdentityQuotas = QuotaManager{modules.NewIdentityV3Manager("identity_quota", "identity_quotas",
  175. quotaColumns,
  176. []string{})}
  177. modules.Register(&IdentityQuotas)
  178. }