yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/rds_mysql_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 qcloud
    16  
    17  import (
    18  	"yunion.io/x/log"
    19  	"yunion.io/x/pkg/errors"
    20  	"yunion.io/x/pkg/utils"
    21  
    22  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    23  )
    24  
    25  type SellTypeConfig struct {
    26  	Device     string
    27  	Type       string
    28  	CdbType    string
    29  	Memory     int
    30  	Cpu        int
    31  	VolumeMin  int
    32  	VolumeMax  int
    33  	VolumeStep int
    34  	Connection int
    35  	Qps        int
    36  	Iops       int
    37  	Info       string
    38  	Status     string
    39  	Tag        string
    40  }
    41  
    42  type SSellType struct {
    43  	TypeName      string
    44  	EngineVersion []string
    45  	Configs       []SellTypeConfig
    46  }
    47  
    48  type SZoneConf struct {
    49  	DeployMode []int
    50  	MasterZone []string
    51  	SlaveZone  []string
    52  	BackupZone []string
    53  }
    54  
    55  type SZoneSellConf struct {
    56  	Status                 int
    57  	ZoneName               string
    58  	IsCustom               bool
    59  	IsSupportDr            bool
    60  	IsSupportVpc           bool
    61  	HourInstanceSaleMaxNum int
    62  	IsDefaultZone          bool
    63  	IsBm                   bool
    64  	PayType                []string
    65  	ProtectMode            string
    66  	Zone                   string
    67  	SellType               []SSellType
    68  	ZoneConf               SZoneConf
    69  	DrZone                 []string
    70  	IsSupportRemoteRo      bool
    71  }
    72  
    73  type SRegionSellConf struct {
    74  	RegionName      string
    75  	Area            string
    76  	IsDefaultRegion bool
    77  	Region          string
    78  	ZonesConf       []SZoneSellConf
    79  }
    80  
    81  func (self *SRegion) DescribeDBZoneConfig() ([]SRegionSellConf, error) {
    82  	resp, err := self.cdbRequest("DescribeDBZoneConfig", map[string]string{})
    83  	if err != nil {
    84  		return nil, errors.Wrapf(err, "DescribeDBZoneConfig")
    85  	}
    86  	skus := []SRegionSellConf{}
    87  	err = resp.Unmarshal(&skus, "Items")
    88  	if err != nil {
    89  		return nil, errors.Wrapf(err, "resp.Unmarshal")
    90  	}
    91  	return skus, nil
    92  }
    93  
    94  type SDBInstanceSku struct {
    95  	Region        string
    96  	Engine        string
    97  	EngineVersion string
    98  	Category      string
    99  	Iops          int
   100  	Qps           int
   101  	MaxConnection int
   102  	Cpu           int
   103  	MemoryMb      int
   104  	StorageType   string
   105  	StorageMin    int
   106  	StorageMax    int
   107  	StorageStep   int
   108  	Status        string
   109  	Description   string
   110  	Zone1         string
   111  	Zone2         string
   112  	Zone3         string
   113  }
   114  
   115  func (self *SRegion) ListMysqlSkus() ([]SDBInstanceSku, error) {
   116  	conf, err := self.DescribeDBZoneConfig()
   117  	if err != nil {
   118  		return nil, errors.Wrapf(err, "DescribeDBZoneConfig")
   119  	}
   120  	skus := []SDBInstanceSku{}
   121  	for _, item := range conf {
   122  		if item.Region != self.Region {
   123  			continue
   124  		}
   125  		sku := SDBInstanceSku{
   126  			Region: self.Region,
   127  			Engine: api.DBINSTANCE_TYPE_MYSQL,
   128  			Status: api.DBINSTANCE_SKU_SOLDOUT,
   129  		}
   130  		for _, zone := range item.ZonesConf {
   131  			//0-未上线;1-上线;2-开放;3-停售;4-不展示
   132  			if zone.Status == 0 || zone.Status == 4 {
   133  				continue
   134  			}
   135  			if zone.Status == 1 || zone.Status == 2 {
   136  				sku.Status = api.DBINSTANCE_SKU_AVAILABLE
   137  			}
   138  			if zone.IsBm { // 跳过黑石
   139  				continue
   140  			}
   141  			for _, sellType := range zone.SellType {
   142  				for _, sellConf := range sellType.Configs {
   143  					sku.Cpu = sellConf.Cpu
   144  					sku.MemoryMb = sellConf.Memory
   145  					sku.Iops = sellConf.Iops
   146  					sku.MaxConnection = sellConf.Connection
   147  					sku.Qps = sellConf.Qps
   148  					sku.StorageMin = sellConf.VolumeMin
   149  					sku.StorageMax = sellConf.VolumeMax
   150  					sku.StorageStep = sellConf.VolumeStep
   151  					sku.Zone1 = zone.Zone
   152  					for _, engineVersion := range sellType.EngineVersion {
   153  						sku.EngineVersion = engineVersion
   154  						sku.Category = sellConf.Type
   155  						sku.Zone2, sku.Zone3 = "", ""
   156  						sku.StorageType = api.STORAGE_LOCAL_SSD
   157  						switch sellConf.Type {
   158  						case "高可用版":
   159  							for _, zone2 := range zone.ZoneConf.SlaveZone {
   160  								sku.Zone2 = zone2
   161  								skus = append(skus, sku)
   162  							}
   163  							if utils.IsInStringArray(engineVersion, []string{"5.6", "5.7", "8.0"}) {
   164  								sku.Category = "金融版"
   165  								for _, zone2 := range zone.ZoneConf.SlaveZone {
   166  									sku.Zone2 = zone2
   167  									sku.Zone3 = zone2
   168  									skus = append(skus, sku)
   169  								}
   170  							}
   171  						case "基础版":
   172  							sku.StorageType = api.STORAGE_CLOUD_SSD
   173  							skus = append(skus, sku)
   174  						default:
   175  							log.Errorf("unknow %s", sellConf.Type)
   176  						}
   177  					}
   178  				}
   179  			}
   180  		}
   181  	}
   182  	return skus, nil
   183  }