| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package apsara
- import (
- "fmt"
- "strconv"
- "strings"
- "yunion.io/x/pkg/errors"
- "yunion.io/x/cloudmux/pkg/cloudprovider"
- )
- type ValueItem struct {
- Value string
- DiskCategory string
- }
- type AttributeValues struct {
- ValueItem []ValueItem
- }
- type SAccountAttributeItem struct {
- AttributeValues AttributeValues
- AttributeName string
- }
- type SQuota struct {
- Name string
- UsedCount int
- MaxCount int
- }
- func (q *SQuota) GetGlobalId() string {
- return q.Name
- }
- func (q *SQuota) GetDesc() string {
- return q.Name
- }
- func (q *SQuota) GetQuotaType() string {
- return q.Name
- }
- func (q *SQuota) GetMaxQuotaCount() int {
- return q.MaxCount
- }
- func (q *SQuota) GetCurrentQuotaUsedCount() int {
- return q.UsedCount
- }
- func (region *SRegion) GetAccountAttributes() ([]SAccountAttributeItem, error) {
- params := map[string]string{
- "RegionId": region.RegionId,
- }
- resp, err := region.ecsRequest("DescribeAccountAttributes", params)
- if err != nil {
- return nil, errors.Wrap(err, "ecsRequest")
- }
- quotas := []SAccountAttributeItem{}
- err = resp.Unmarshal("as, "AccountAttributeItems", "AccountAttributeItem")
- if err != nil {
- return nil, errors.Wrap(err, "resp.Unmarshal")
- }
- return quotas, nil
- }
- func (region *SRegion) GetQuotas() ([]SQuota, error) {
- attrs, err := region.GetAccountAttributes()
- if err != nil {
- return nil, errors.Wrap(err, "GetAccountAttributes")
- }
- quotas := map[string]SQuota{}
- for _, attr := range attrs {
- for _, item := range attr.AttributeValues.ValueItem {
- value, err := strconv.ParseInt(item.Value, 10, 64)
- if err != nil {
- continue
- }
- used := false
- name := attr.AttributeName
- if strings.HasPrefix(name, "used-") {
- used = true
- name = strings.TrimPrefix(name, "used-")
- }
- if len(item.DiskCategory) > 0 {
- name = fmt.Sprintf("%s/%s", name, item.DiskCategory)
- }
- if _, ok := quotas[name]; !ok {
- quotas[name] = SQuota{
- Name: name,
- UsedCount: -1,
- }
- }
- quota := quotas[name]
- if used {
- quota.UsedCount = int(value)
- } else {
- quota.MaxCount = int(value)
- }
- quotas[name] = quota
- }
- }
- ret := []SQuota{}
- for _, quota := range quotas {
- ret = append(ret, quota)
- }
- return ret, nil
- }
- func (region *SRegion) GetICloudQuotas() ([]cloudprovider.ICloudQuota, error) {
- quotas, err := region.GetQuotas()
- if err != nil {
- return nil, errors.Wrap(err, "GetQuotas")
- }
- ret := []cloudprovider.ICloudQuota{}
- for i := range quotas {
- ret = append(ret, "as[i])
- }
- return ret, nil
- }
|