quota.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 apsara
  15. import (
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. )
  22. type ValueItem struct {
  23. Value string
  24. DiskCategory string
  25. }
  26. type AttributeValues struct {
  27. ValueItem []ValueItem
  28. }
  29. type SAccountAttributeItem struct {
  30. AttributeValues AttributeValues
  31. AttributeName string
  32. }
  33. type SQuota struct {
  34. Name string
  35. UsedCount int
  36. MaxCount int
  37. }
  38. func (q *SQuota) GetGlobalId() string {
  39. return q.Name
  40. }
  41. func (q *SQuota) GetDesc() string {
  42. return q.Name
  43. }
  44. func (q *SQuota) GetQuotaType() string {
  45. return q.Name
  46. }
  47. func (q *SQuota) GetMaxQuotaCount() int {
  48. return q.MaxCount
  49. }
  50. func (q *SQuota) GetCurrentQuotaUsedCount() int {
  51. return q.UsedCount
  52. }
  53. func (region *SRegion) GetAccountAttributes() ([]SAccountAttributeItem, error) {
  54. params := map[string]string{
  55. "RegionId": region.RegionId,
  56. }
  57. resp, err := region.ecsRequest("DescribeAccountAttributes", params)
  58. if err != nil {
  59. return nil, errors.Wrap(err, "ecsRequest")
  60. }
  61. quotas := []SAccountAttributeItem{}
  62. err = resp.Unmarshal(&quotas, "AccountAttributeItems", "AccountAttributeItem")
  63. if err != nil {
  64. return nil, errors.Wrap(err, "resp.Unmarshal")
  65. }
  66. return quotas, nil
  67. }
  68. func (region *SRegion) GetQuotas() ([]SQuota, error) {
  69. attrs, err := region.GetAccountAttributes()
  70. if err != nil {
  71. return nil, errors.Wrap(err, "GetAccountAttributes")
  72. }
  73. quotas := map[string]SQuota{}
  74. for _, attr := range attrs {
  75. for _, item := range attr.AttributeValues.ValueItem {
  76. value, err := strconv.ParseInt(item.Value, 10, 64)
  77. if err != nil {
  78. continue
  79. }
  80. used := false
  81. name := attr.AttributeName
  82. if strings.HasPrefix(name, "used-") {
  83. used = true
  84. name = strings.TrimPrefix(name, "used-")
  85. }
  86. if len(item.DiskCategory) > 0 {
  87. name = fmt.Sprintf("%s/%s", name, item.DiskCategory)
  88. }
  89. if _, ok := quotas[name]; !ok {
  90. quotas[name] = SQuota{
  91. Name: name,
  92. UsedCount: -1,
  93. }
  94. }
  95. quota := quotas[name]
  96. if used {
  97. quota.UsedCount = int(value)
  98. } else {
  99. quota.MaxCount = int(value)
  100. }
  101. quotas[name] = quota
  102. }
  103. }
  104. ret := []SQuota{}
  105. for _, quota := range quotas {
  106. ret = append(ret, quota)
  107. }
  108. return ret, nil
  109. }
  110. func (region *SRegion) GetICloudQuotas() ([]cloudprovider.ICloudQuota, error) {
  111. quotas, err := region.GetQuotas()
  112. if err != nil {
  113. return nil, errors.Wrap(err, "GetQuotas")
  114. }
  115. ret := []cloudprovider.ICloudQuota{}
  116. for i := range quotas {
  117. ret = append(ret, &quotas[i])
  118. }
  119. return ret, nil
  120. }