yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/dbinstance_skus.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 hcs
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  	"strings"
    21  
    22  	"yunion.io/x/pkg/errors"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  )
    27  
    28  type SDBInstanceSku struct {
    29  	Vcpus        int
    30  	Ram          int //单位GB
    31  	SpecCode     string
    32  	InstanceMode string //实例模型
    33  
    34  	StorageType   string
    35  	Az1           string
    36  	Az2           string
    37  	Engine        string
    38  	EngineVersion string
    39  }
    40  
    41  func (self *SDBInstanceSku) GetName() string {
    42  	return self.SpecCode
    43  }
    44  
    45  func (self *SDBInstanceSku) GetGlobalId() string {
    46  	return fmt.Sprintf("%s-%s-%s-%s-%s-%s", self.Engine, self.EngineVersion, self.InstanceMode, self.SpecCode, self.StorageType, self.GetZoneId())
    47  }
    48  
    49  func (self *SDBInstanceSku) GetCategory() string {
    50  	return self.InstanceMode
    51  }
    52  
    53  func (self *SDBInstanceSku) GetDiskSizeStep() int {
    54  	return 10
    55  }
    56  
    57  func (self *SDBInstanceSku) GetIOPS() int {
    58  	return 0
    59  }
    60  
    61  func (self *SDBInstanceSku) GetTPS() int {
    62  	return 0
    63  }
    64  
    65  func (self *SDBInstanceSku) GetZone1Id() string {
    66  	return self.Az1
    67  }
    68  
    69  func (self *SDBInstanceSku) GetZone2Id() string {
    70  	return self.Az2
    71  }
    72  
    73  func (self *SDBInstanceSku) GetZone3Id() string {
    74  	return ""
    75  }
    76  
    77  func (self *SDBInstanceSku) GetZoneId() string {
    78  	zones := []string{}
    79  	for _, zone := range []string{self.Az1, self.Az2} {
    80  		if len(zone) > 0 {
    81  			zones = append(zones, zone)
    82  		}
    83  	}
    84  	return strings.Join(zones, ",")
    85  }
    86  
    87  func (self *SDBInstanceSku) GetMaxConnections() int {
    88  	return 0
    89  }
    90  
    91  func (self *SDBInstanceSku) GetMaxDiskSizeGb() int {
    92  	return 4000
    93  }
    94  
    95  func (self *SDBInstanceSku) GetMinDiskSizeGb() int {
    96  	return 40
    97  }
    98  
    99  func (self *SDBInstanceSku) GetVcpuCount() int {
   100  	return self.Vcpus
   101  }
   102  
   103  func (self *SDBInstanceSku) GetVmemSizeMb() int {
   104  	return self.Ram * 1024
   105  }
   106  
   107  func (self *SDBInstanceSku) GetStorageType() string {
   108  	return self.StorageType
   109  }
   110  
   111  func (sku *SDBInstanceSku) GetEngine() string {
   112  	return sku.Engine
   113  }
   114  
   115  func (sku *SDBInstanceSku) GetEngineVersion() string {
   116  	return sku.EngineVersion
   117  }
   118  
   119  func (self *SDBInstanceSku) GetStatus() string {
   120  	return api.DBINSTANCE_SKU_AVAILABLE
   121  }
   122  
   123  func (sku *SDBInstanceSku) GetQPS() int {
   124  	return 0
   125  }
   126  
   127  type SDBInstanceFlavor struct {
   128  	Vcpus        int
   129  	Ram          int //单位GB
   130  	SpecCode     string
   131  	InstanceMode string //实例模型
   132  
   133  	StorageType   string
   134  	AzStatus      map[string]string
   135  	Engine        string
   136  	EngineVersion string
   137  }
   138  
   139  func (self *SDBInstanceFlavor) GetISkus() []cloudprovider.ICloudDBInstanceSku {
   140  	ret := []cloudprovider.ICloudDBInstanceSku{}
   141  	switch self.InstanceMode {
   142  	case api.HUAWEI_DBINSTANCE_CATEGORY_HA:
   143  		for az1, status1 := range self.AzStatus {
   144  			if status1 != "normal" {
   145  				continue
   146  			}
   147  			for az2, status2 := range self.AzStatus {
   148  				if status2 != "normal" {
   149  					continue
   150  				}
   151  				sku := &SDBInstanceSku{
   152  					Vcpus:         self.Vcpus,
   153  					Ram:           self.Ram,
   154  					SpecCode:      self.SpecCode,
   155  					InstanceMode:  self.InstanceMode,
   156  					StorageType:   self.StorageType,
   157  					Az1:           az1,
   158  					Az2:           az2,
   159  					Engine:        self.Engine,
   160  					EngineVersion: self.EngineVersion,
   161  				}
   162  				ret = append(ret, sku)
   163  			}
   164  		}
   165  	case api.HUAWEI_DBINSTANCE_CATEGORY_SINGLE:
   166  		for az, status := range self.AzStatus {
   167  			if status != "normal" {
   168  				continue
   169  			}
   170  			sku := &SDBInstanceSku{
   171  				Vcpus:         self.Vcpus,
   172  				Ram:           self.Ram,
   173  				SpecCode:      self.SpecCode,
   174  				InstanceMode:  self.InstanceMode,
   175  				StorageType:   self.StorageType,
   176  				Az1:           az,
   177  				Engine:        self.Engine,
   178  				EngineVersion: self.EngineVersion,
   179  			}
   180  			ret = append(ret, sku)
   181  		}
   182  	}
   183  	return ret
   184  }
   185  
   186  func (self *SRegion) GetIDBInstanceSkus() ([]cloudprovider.ICloudDBInstanceSku, error) {
   187  	skus, err := self.GetDBInstanceSkus()
   188  	if err != nil {
   189  		return nil, err
   190  	}
   191  	ret := []cloudprovider.ICloudDBInstanceSku{}
   192  	for i := range skus {
   193  		ret = append(ret, skus[i].GetISkus()...)
   194  	}
   195  	return ret, nil
   196  }
   197  
   198  type SDBInstanceDatastore struct {
   199  	Id   string
   200  	Name string
   201  }
   202  
   203  func (self *SRegion) GetDBInstanceSkus() ([]SDBInstanceFlavor, error) {
   204  	skus := []SDBInstanceFlavor{}
   205  	for _, engine := range []string{api.DBINSTANCE_TYPE_MYSQL, api.DBINSTANCE_TYPE_POSTGRESQL, api.DBINSTANCE_TYPE_SQLSERVER} {
   206  		stores, err := self.GetDBInstanceDatastores(engine)
   207  		if err != nil {
   208  			return nil, err
   209  		}
   210  		for _, version := range stores {
   211  			storages, err := self.GetDBInstanceStorages(engine, version.Name)
   212  			if err != nil {
   213  				return nil, errors.Wrapf(err, "GetDBInstanceStorages(%s,%s)", engine, version.Name)
   214  			}
   215  			flavors, err := self.GetDBInstanceFlavors(engine, version.Name)
   216  			if err != nil {
   217  				return nil, errors.Wrapf(err, "GetDBInstanceFlavors(%s, %s)", engine, version.Name)
   218  			}
   219  			for i := range flavors {
   220  				flavors[i].Engine = engine
   221  				flavors[i].EngineVersion = version.Name
   222  				for j := range storages {
   223  					flavors[i].StorageType = storages[j].Name
   224  					flavors[i].AzStatus = storages[j].AzStatus
   225  					skus = append(skus, flavors[i])
   226  				}
   227  			}
   228  		}
   229  	}
   230  	return skus, nil
   231  }
   232  
   233  // rds规格信息
   234  func (region *SRegion) GetDBInstanceFlavors(engine string, version string) ([]SDBInstanceFlavor, error) {
   235  	flavors := []SDBInstanceFlavor{}
   236  	query := url.Values{}
   237  	query.Add("version_name", version)
   238  	resource := fmt.Sprintf("%s/%s", "flavor", engine)
   239  	err := region.rdsList(resource, query, flavors)
   240  	if err != nil {
   241  		return nil, err
   242  	}
   243  	return flavors, nil
   244  }
   245  
   246  // rds数据库引擎版本信息
   247  func (region *SRegion) GetDBInstanceDatastores(engine string) ([]SDBInstanceDatastore, error) {
   248  	stores := []SDBInstanceDatastore{}
   249  	resource := fmt.Sprintf("%s/%s", "datastores", engine)
   250  	err := region.rdsList(resource, nil, stores)
   251  	if err != nil {
   252  		return nil, err
   253  	}
   254  	return stores, nil
   255  }
   256  
   257  type SDBInstanceStorage struct {
   258  	Name     string
   259  	AzStatus map[string]string
   260  }
   261  
   262  // rds数据库磁盘类型
   263  func (region *SRegion) GetDBInstanceStorages(engine, engineVersion string) ([]SDBInstanceStorage, error) {
   264  	storages := []SDBInstanceStorage{}
   265  	resource := fmt.Sprintf("%s/%s", "storage-type", engine)
   266  	query := url.Values{}
   267  	query.Add("version_name", engineVersion)
   268  	err := region.rdsList(resource, query, storages)
   269  	if err != nil {
   270  		return nil, err
   271  	}
   272  	return storages, nil
   273  }