yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/usage.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 azure
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  	"strings"
    21  
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  )
    26  
    27  type UsageName struct {
    28  	Value          string
    29  	LocalizedValue string
    30  }
    31  
    32  // {"value":[{"unit":"Count","currentValue":0,"limit":250,"name":{"value":"StorageAccounts","localizedValue":"Storage Accounts"}}]}
    33  type SUsage struct {
    34  	Unit         string
    35  	CurrentValue int
    36  	Limit        int
    37  	Name         UsageName
    38  }
    39  
    40  func (u *SUsage) GetGlobalId() string {
    41  	return strings.ToLower(u.Name.Value)
    42  }
    43  
    44  func (u *SUsage) GetQuotaType() string {
    45  	return u.Name.Value
    46  }
    47  
    48  func (u *SUsage) GetDesc() string {
    49  	return u.Name.LocalizedValue
    50  }
    51  
    52  func (u *SUsage) GetMaxQuotaCount() int {
    53  	return u.Limit
    54  }
    55  
    56  func (u *SUsage) GetCurrentQuotaUsedCount() int {
    57  	return u.CurrentValue
    58  }
    59  
    60  func (region *SRegion) GetUsage(resourceType string) ([]SUsage, error) {
    61  	usage := []SUsage{}
    62  	resource := fmt.Sprintf("%s/locations/%s/usages", resourceType, region.Name)
    63  	err := region.client.list(resource, url.Values{}, &usage)
    64  	if err != nil {
    65  		return nil, errors.Wrapf(err, "ListAll(%s)", resource)
    66  	}
    67  	return usage, nil
    68  }
    69  
    70  func (region *SRegion) GetICloudQuotas() ([]cloudprovider.ICloudQuota, error) {
    71  	ret := []cloudprovider.ICloudQuota{}
    72  	for _, resourceType := range []string{"Microsoft.Network", "Microsoft.Storage", "Microsoft.Compute"} {
    73  		usages, err := region.GetUsage(resourceType)
    74  		if err != nil {
    75  			return nil, errors.Wrap(err, "GetUsage")
    76  		}
    77  		for i := range usages {
    78  			ret = append(ret, &usages[i])
    79  		}
    80  	}
    81  	return ret, nil
    82  }