yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/bandwidthpackage.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  	"fmt"
    19  	"time"
    20  
    21  	"yunion.io/x/pkg/errors"
    22  )
    23  
    24  type SBandwidthPackageSet struct {
    25  	AddressIp    string
    26  	ResourceId   string
    27  	ResourceType string
    28  }
    29  
    30  type SBandwidthPackage struct {
    31  	BandwidthPackageId   string
    32  	BandwidthPackageName string
    33  	ChargeType           string
    34  	CreatedTime          time.Time
    35  	NetworkType          string
    36  	Protocol             string
    37  	ResourceSet          []SBandwidthPackageSet
    38  }
    39  
    40  func (region *SRegion) GetBandwidthPackages(ids []string, offset int, limit int) ([]SBandwidthPackage, int, error) {
    41  	if limit < 1 || limit > 50 {
    42  		limit = 50
    43  	}
    44  	params := map[string]string{
    45  		"Region": region.Region,
    46  		"Limit":  fmt.Sprintf("%d", limit),
    47  		"Offset": fmt.Sprintf("%d", offset),
    48  	}
    49  	for idx, id := range ids {
    50  		params[fmt.Sprintf("BandwidthPackageIds.%d", idx)] = id
    51  	}
    52  	packages := []SBandwidthPackage{}
    53  	resp, err := region.vpcRequest("DescribeBandwidthPackages", params)
    54  	if err != nil {
    55  		return nil, 0, errors.Wrap(err, "DescribeBandwidthPackages")
    56  	}
    57  	err = resp.Unmarshal(&packages, "BandwidthPackageSet")
    58  	if err != nil {
    59  		return nil, 0, errors.Wrap(err, "resp.Unmarshal")
    60  	}
    61  	total, _ := resp.Float("TotalCount")
    62  	return packages, int(total), nil
    63  }