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