yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/quota.go (about)

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