yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/jdcloud/instancetype.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 jdcloud
    16  
    17  import (
    18  	commodels "github.com/jdcloud-api/jdcloud-sdk-go/services/common/models"
    19  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vm/apis"
    20  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vm/client"
    21  	"github.com/jdcloud-api/jdcloud-sdk-go/services/vm/models"
    22  )
    23  
    24  type SInstanceType struct {
    25  	models.InstanceType
    26  }
    27  
    28  func (it *SInstanceType) GetCpu() int {
    29  	return it.Cpu
    30  }
    31  
    32  func (it *SInstanceType) GetMemoryMB() int {
    33  	return it.MemoryMB
    34  }
    35  
    36  func (r *SRegion) InstanceTypes(instanceTypes ...string) ([]SInstanceType, error) {
    37  	vm := "vm"
    38  	req := apis.NewDescribeInstanceTypesRequestWithAllParams(r.ID, &vm, []commodels.Filter{
    39  		{
    40  			Name:   "instanceTypes",
    41  			Values: instanceTypes,
    42  		},
    43  	})
    44  	client := client.NewVmClient(r.getCredential())
    45  	client.Logger = Logger{debug: r.client.debug}
    46  	resp, err := client.DescribeInstanceTypes(req)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	its := make([]SInstanceType, len(resp.Result.InstanceTypes))
    51  	for i := range resp.Result.InstanceTypes {
    52  		its = append(its, SInstanceType{
    53  			InstanceType: resp.Result.InstanceTypes[i],
    54  		})
    55  	}
    56  	return its, nil
    57  }