quota.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 qcloud
  15. import (
  16. "strings"
  17. "yunion.io/x/log"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. )
  21. type ImageQuota struct {
  22. region *SRegion
  23. ImageNumQuota int
  24. }
  25. func (iq *ImageQuota) GetGlobalId() string {
  26. return "ImageNumQuota"
  27. }
  28. func (iq *ImageQuota) GetName() string {
  29. return "ImageNumQuota"
  30. }
  31. func (iq *ImageQuota) GetDesc() string {
  32. return ""
  33. }
  34. func (iq *ImageQuota) GetQuotaType() string {
  35. return "ImageNumQuota"
  36. }
  37. func (iq *ImageQuota) GetMaxQuotaCount() int {
  38. return iq.ImageNumQuota
  39. }
  40. func (iq *ImageQuota) GetCurrentQuotaUsedCount() int {
  41. _, count, err := iq.region.GetImages("", "PRIVATE_IMAGE", nil, "", 0, iq.ImageNumQuota)
  42. if err != nil {
  43. log.Errorf("get private image error: %v", err)
  44. return -1
  45. }
  46. return count
  47. }
  48. func (region *SRegion) GetImageQuota() (*ImageQuota, error) {
  49. params := map[string]string{}
  50. resp, err := region.cvmRequest("DescribeImageQuota", params, true)
  51. if err != nil {
  52. return nil, errors.Wrap(err, "DescribeImageQuota")
  53. }
  54. imageQuota := &ImageQuota{region: region}
  55. return imageQuota, resp.Unmarshal(imageQuota)
  56. }
  57. type QuotaSet struct {
  58. QuotaId string
  59. QuotaCurrent int
  60. QuotaLimit int
  61. }
  62. func (qs *QuotaSet) GetGlobalId() string {
  63. return strings.ToLower(qs.QuotaId)
  64. }
  65. func (qs *QuotaSet) GetName() string {
  66. return qs.QuotaId
  67. }
  68. func (qs *QuotaSet) GetDesc() string {
  69. return ""
  70. }
  71. func (qs *QuotaSet) GetQuotaType() string {
  72. return qs.QuotaId
  73. }
  74. func (qs *QuotaSet) GetMaxQuotaCount() int {
  75. return qs.QuotaLimit
  76. }
  77. func (qs *QuotaSet) GetCurrentQuotaUsedCount() int {
  78. return qs.QuotaCurrent
  79. }
  80. func (region *SRegion) GetQuota(action string) ([]QuotaSet, error) {
  81. params := map[string]string{}
  82. resp, err := region.vpcRequest(action, params)
  83. if err != nil {
  84. return nil, errors.Wrap(err, action)
  85. }
  86. quotas := []QuotaSet{}
  87. err = resp.Unmarshal(&quotas, "QuotaSet")
  88. if err != nil {
  89. return nil, errors.Wrap(err, "resp.Unmarshal")
  90. }
  91. return quotas, nil
  92. }
  93. func (region *SRegion) GetICloudQuotas() ([]cloudprovider.ICloudQuota, error) {
  94. ret := []cloudprovider.ICloudQuota{}
  95. imageQ, err := region.GetImageQuota()
  96. if err != nil {
  97. return nil, errors.Wrap(err, "GetImageQuota")
  98. }
  99. ret = append(ret, imageQ)
  100. for _, action := range []string{"DescribeAddressQuota", "DescribeBandwidthPackageQuota"} {
  101. quotas, err := region.GetQuota(action)
  102. if err != nil {
  103. return nil, errors.Wrapf(err, "GetQuota(%s)", action)
  104. }
  105. for i := range quotas {
  106. ret = append(ret, &quotas[i])
  107. }
  108. }
  109. return ret, nil
  110. }