yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/modelarts_pool_sku.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 huawei
    16  
    17  import (
    18  	"strconv"
    19  	"time"
    20  
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	"yunion.io/x/cloudmux/pkg/apis/compute"
    24  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    25  	"yunion.io/x/cloudmux/pkg/multicloud"
    26  )
    27  
    28  type SModelartsPoolSku struct {
    29  	multicloud.SResourceBase
    30  	HuaweiTags
    31  	region *SRegion
    32  
    33  	Kind   string                          `json:"kind"`
    34  	Spec   SModelartsResourceflavorsSpec   `json:"spec"`
    35  	Status SModelartsResourceflavorsStatus `json:"status"`
    36  }
    37  
    38  type SModelartsResourceflavorsSpec struct {
    39  	BillingCode  string                           `json:"billingCode"`
    40  	BillingModes []int                            `json:"billingMods"`
    41  	Cpu          int                              `json:"cpu"`
    42  	CpuArch      string                           `json:"cpuArch"`
    43  	Gpu          SModelartsResourceflavorsGpuSpec `json:"gpu"`
    44  	Npu          SModelartsResourceflavorsGpuSpec `json:"npu"`
    45  	Memory       string                           `json:"memory"`
    46  	Type         string                           `json:"type"`
    47  }
    48  
    49  type SModelartsResourceflavorsGpuSpec struct {
    50  	Size int    `json:"size"`
    51  	Type string `json:"type"`
    52  }
    53  
    54  type SModelartsResourceflavorsStatus struct {
    55  	Phase map[string]interface{} `json:"phase"`
    56  }
    57  
    58  func (self *SRegion) GetIModelartsPoolSku() ([]cloudprovider.ICloudModelartsPoolSku, error) {
    59  	params := make(map[string]interface{})
    60  	resourceflavors := make([]SModelartsPoolSku, 0)
    61  	obj, err := self.client.modelartsResourceflavors("resourceflavors", params)
    62  	if err != nil {
    63  		return nil, errors.Wrap(err, "region.modelartsResourceflavors")
    64  	}
    65  	obj.Unmarshal(&resourceflavors, "items")
    66  	res := make([]cloudprovider.ICloudModelartsPoolSku, len(resourceflavors))
    67  	for i := 0; i < len(resourceflavors); i++ {
    68  		res[i] = &resourceflavors[i]
    69  	}
    70  	return res, nil
    71  }
    72  
    73  func (self *SModelartsPoolSku) GetCreatedAt() time.Time {
    74  	createdAt, _ := time.Parse("2006-01-02T15:04:05CST", time.Now().Format("2006-01-02T15:04:05CST"))
    75  	return createdAt
    76  }
    77  
    78  func (self *SModelartsPoolSku) GetGlobalId() string {
    79  	return self.Spec.BillingCode
    80  }
    81  
    82  func (self *SModelartsPoolSku) GetId() string {
    83  	return self.Spec.BillingCode
    84  }
    85  
    86  func (self *SModelartsPoolSku) GetName() string {
    87  	return self.Spec.BillingCode
    88  }
    89  
    90  func (self *SModelartsPoolSku) GetCpuArch() string {
    91  	return self.Spec.CpuArch
    92  }
    93  
    94  func (self *SModelartsPoolSku) GetCpuCoreCount() int {
    95  	return self.Spec.Cpu
    96  }
    97  
    98  func (self *SModelartsPoolSku) GetMemorySizeMB() int {
    99  	size, _ := strconv.Atoi(self.Spec.Memory[:len(self.Spec.Memory)-2])
   100  	return size * 1024
   101  }
   102  
   103  func (self *SModelartsPoolSku) GetStatus() string {
   104  	for _, v := range self.Status.Phase {
   105  		if v == "normal" {
   106  			return compute.MODELARTS_POOL_SKU_AVAILABLE
   107  		}
   108  	}
   109  	return compute.MODELARTS_POOL_SKU_SOLDOUT
   110  }
   111  
   112  func (self *SModelartsPoolSku) GetGpuSize() int {
   113  	return self.Spec.Gpu.Size
   114  }
   115  
   116  func (self *SModelartsPoolSku) GetGpuType() string {
   117  	return self.Spec.Gpu.Type
   118  }
   119  
   120  func (self *SModelartsPoolSku) GetNpuSize() int {
   121  	return self.Spec.Npu.Size
   122  }
   123  
   124  func (self *SModelartsPoolSku) GetNpuType() string {
   125  	return self.Spec.Npu.Type
   126  }
   127  
   128  func (self *SModelartsPoolSku) GetPoolType() string {
   129  	return self.Spec.Type
   130  }