quota.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 openstack
  15. import (
  16. "fmt"
  17. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. )
  20. type QuotaDetail struct {
  21. Reserved int
  22. Limit int
  23. InUse int
  24. }
  25. type QuotaSet struct {
  26. InjectedFileContentBytes QuotaDetail
  27. MetadataItems QuotaDetail
  28. ServerGroupMembers QuotaDetail
  29. ServerGroups QuotaDetail
  30. Ram QuotaDetail
  31. FloatingIps QuotaDetail
  32. KeyPairs QuotaDetail
  33. Id string
  34. Instances QuotaDetail
  35. SecurityGroupRules QuotaDetail
  36. InjectedFiles QuotaDetail
  37. Cores QuotaDetail
  38. FixedIps QuotaDetail
  39. InjectedFilePathBytes QuotaDetail
  40. SecurityGroups QuotaDetail
  41. }
  42. type SQuota struct {
  43. Cores int
  44. Instances int
  45. KeyPairs int
  46. FixedIps int
  47. MetadataItems int
  48. Ram int
  49. ServerGroups int
  50. ServerGroupMembers int
  51. InjectedFileContentBytes int
  52. InjectedFilePathBytes int
  53. InjectedFiles int
  54. Floatingips int
  55. Networks int
  56. Port int
  57. RbacPolicy int
  58. Router int
  59. SecurityGroups int
  60. SecurityGroupRules int
  61. }
  62. func (region *SRegion) GetQuota() (*QuotaSet, error) {
  63. resource := fmt.Sprintf("/os-quota-sets/%s/detail", region.client.tokenCredential.GetTenantId())
  64. resp, err := region.ecsGet(resource)
  65. if err != nil {
  66. return nil, errors.Wrap(err, "ecsGet")
  67. }
  68. quota := &QuotaSet{}
  69. return quota, resp.Unmarshal(quota, "quota_set")
  70. }
  71. func (region *SRegion) SetQuota(quota *SQuota) error {
  72. params := map[string]map[string]interface{}{
  73. "quota_set": {
  74. "force": "True",
  75. },
  76. }
  77. if quota.Floatingips > 0 {
  78. params["quota_set"]["floating_ips"] = quota.Floatingips
  79. }
  80. if quota.SecurityGroups > 0 {
  81. params["quota_set"]["security_group"] = quota.SecurityGroups
  82. }
  83. if quota.SecurityGroupRules > 0 {
  84. params["quota_set"]["security_group_rules"] = quota.SecurityGroupRules
  85. }
  86. if quota.FixedIps > 0 {
  87. params["quota_set"]["fixed_ips"] = quota.FixedIps
  88. }
  89. if quota.Networks > 0 {
  90. params["quota_set"]["networks"] = quota.Networks
  91. }
  92. resource := "/os-quota-sets/" + region.client.tokenCredential.GetTenantId()
  93. _, err := region.ecsUpdate(resource, params)
  94. return err
  95. }
  96. type IQuota struct {
  97. Name string
  98. Limit int
  99. InUse int
  100. }
  101. func (iq *IQuota) GetGlobalId() string {
  102. return iq.Name
  103. }
  104. func (iq *IQuota) GetName() string {
  105. return iq.Name
  106. }
  107. func (iq *IQuota) GetDesc() string {
  108. return ""
  109. }
  110. func (iq *IQuota) GetQuotaType() string {
  111. return iq.Name
  112. }
  113. func (iq *IQuota) GetMaxQuotaCount() int {
  114. return iq.Limit
  115. }
  116. func (iq *IQuota) GetCurrentQuotaUsedCount() int {
  117. return iq.InUse
  118. }
  119. func (region *SRegion) GetICloudQuotas() ([]cloudprovider.ICloudQuota, error) {
  120. quota, err := region.GetQuota()
  121. if err != nil {
  122. return nil, errors.Wrap(err, "GetQuota")
  123. }
  124. ret := []cloudprovider.ICloudQuota{}
  125. ret = append(ret, &IQuota{Name: "injected_file_content_bytes", Limit: quota.InjectedFileContentBytes.Limit, InUse: quota.InjectedFileContentBytes.InUse})
  126. ret = append(ret, &IQuota{Name: "metadata_items", Limit: quota.MetadataItems.Limit, InUse: quota.MetadataItems.InUse})
  127. ret = append(ret, &IQuota{Name: "server_group_members", Limit: quota.ServerGroupMembers.Limit, InUse: quota.ServerGroupMembers.InUse})
  128. ret = append(ret, &IQuota{Name: "server_groups", Limit: quota.ServerGroups.Limit, InUse: quota.ServerGroups.InUse})
  129. ret = append(ret, &IQuota{Name: "ram", Limit: quota.Ram.Limit, InUse: quota.Ram.InUse})
  130. ret = append(ret, &IQuota{Name: "floating_ips", Limit: quota.FloatingIps.Limit, InUse: quota.FloatingIps.InUse})
  131. ret = append(ret, &IQuota{Name: "key_pairs", Limit: quota.KeyPairs.Limit, InUse: quota.KeyPairs.InUse})
  132. ret = append(ret, &IQuota{Name: "instances", Limit: quota.Instances.Limit, InUse: quota.Instances.InUse})
  133. ret = append(ret, &IQuota{Name: "security_group_rules", Limit: quota.SecurityGroupRules.Limit, InUse: quota.SecurityGroupRules.InUse})
  134. ret = append(ret, &IQuota{Name: "injected_files", Limit: quota.InjectedFiles.Limit, InUse: quota.InjectedFiles.InUse})
  135. ret = append(ret, &IQuota{Name: "cores", Limit: quota.Cores.Limit, InUse: quota.Cores.InUse})
  136. ret = append(ret, &IQuota{Name: "fixed_ips", Limit: quota.FixedIps.Limit, InUse: quota.FixedIps.InUse})
  137. ret = append(ret, &IQuota{Name: "injected_file_path_bytes", Limit: quota.InjectedFilePathBytes.Limit, InUse: quota.InjectedFilePathBytes.InUse})
  138. ret = append(ret, &IQuota{Name: "security_groups", Limit: quota.SecurityGroups.Limit, InUse: quota.SecurityGroups.InUse})
  139. return ret, nil
  140. }