yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/zone.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 aliyun
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"yunion.io/x/log"
    22  	"yunion.io/x/pkg/errors"
    23  	"yunion.io/x/pkg/utils"
    24  
    25  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    26  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    27  	"yunion.io/x/cloudmux/pkg/multicloud"
    28  )
    29  
    30  type TChargeType string
    31  
    32  var LOCAL_STORAGES = []string{
    33  	api.STORAGE_LOCAL_HDD_PRO,
    34  	api.STORAGE_LOCAL_SSD_PRO,
    35  }
    36  
    37  const (
    38  	PrePaidInstanceChargeType    TChargeType = "PrePaid"
    39  	PrePaidDBInstanceChargeType  TChargeType = "Prepaid"
    40  	PostPaidInstanceChargeType   TChargeType = "PostPaid"
    41  	PostPaidDBInstanceChargeType TChargeType = "Postpaid"
    42  	DefaultInstanceChargeType                = PostPaidInstanceChargeType
    43  )
    44  
    45  type SpotStrategyType string
    46  
    47  const (
    48  	NoSpotStrategy             SpotStrategyType = "NoSpot"
    49  	SpotWithPriceLimitStrategy SpotStrategyType = "SpotWithPriceLimit"
    50  	SpotAsPriceGoStrategy      SpotStrategyType = "SpotAsPriceGo"
    51  	DefaultSpotStrategy                         = NoSpotStrategy
    52  )
    53  
    54  type SDedicatedHostGenerations struct {
    55  	DedicatedHostGeneration []string
    56  }
    57  
    58  type SVolumeCategories struct {
    59  	VolumeCategories []string
    60  }
    61  
    62  type SSupportedDataDiskCategories struct {
    63  	SupportedDataDiskCategory []string
    64  }
    65  
    66  type SSupportedInstanceGenerations struct {
    67  	SupportedInstanceGeneration []string
    68  }
    69  
    70  type SSupportedInstanceTypeFamilies struct {
    71  	SupportedInstanceTypeFamily []string
    72  }
    73  
    74  type SSupportedInstanceTypes struct {
    75  	SupportedInstanceType []string
    76  }
    77  
    78  type SSupportedNetworkTypes struct {
    79  	SupportedNetworkCategory []string
    80  }
    81  
    82  type SSupportedSystemDiskCategories struct {
    83  	SupportedSystemDiskCategory []string
    84  }
    85  
    86  type SResourcesInfo struct {
    87  	DataDiskCategories   SSupportedDataDiskCategories
    88  	InstanceGenerations  SSupportedInstanceGenerations
    89  	InstanceTypeFamilies SSupportedInstanceTypeFamilies
    90  	InstanceTypes        SSupportedInstanceTypes
    91  	IoOptimized          bool
    92  	NetworkTypes         SSupportedNetworkTypes
    93  	SystemDiskCategories SSupportedSystemDiskCategories
    94  }
    95  
    96  type SResources struct {
    97  	ResourcesInfo []SResourcesInfo
    98  }
    99  
   100  type SResourceCreation struct {
   101  	ResourceTypes []string
   102  }
   103  
   104  type SInstanceTypes struct {
   105  	InstanceTypes []string
   106  }
   107  
   108  type SDiskCategories struct {
   109  	DiskCategories []string
   110  }
   111  
   112  type SDedicatedHostTypes struct {
   113  	DedicatedHostType []string
   114  }
   115  
   116  type SZone struct {
   117  	multicloud.SResourceBase
   118  	AliyunTags
   119  	region *SRegion
   120  
   121  	iwires []cloudprovider.ICloudWire
   122  
   123  	host *SHost
   124  
   125  	istorages []cloudprovider.ICloudStorage
   126  
   127  	ZoneId                    string
   128  	LocalName                 string
   129  	DedicatedHostGenerations  SDedicatedHostGenerations
   130  	AvailableVolumeCategories SVolumeCategories
   131  	/* 可供创建的具体资源,AvailableResourcesType 组成的数组 */
   132  	AvailableResources SResources
   133  	/* 允许创建的资源类型集合 */
   134  	AvailableResourceCreation SResourceCreation
   135  	/* 允许创建的实例规格类型 */
   136  	AvailableInstanceTypes SInstanceTypes
   137  	/* 支持的磁盘种类集合 */
   138  	AvailableDiskCategories     SDiskCategories
   139  	AvailableDedicatedHostTypes SDedicatedHostTypes
   140  }
   141  
   142  func (self *SZone) GetId() string {
   143  	return self.ZoneId
   144  }
   145  
   146  func (self *SZone) GetName() string {
   147  	if self.region.GetCloudEnv() == ALIYUN_FINANCE_CLOUDENV && !strings.Contains(self.LocalName, "金融") {
   148  		i := strings.Index(self.LocalName, "可用")
   149  		var localname string
   150  		if i >= 0 {
   151  			localname = self.LocalName[0:i] + "金融云 " + self.LocalName[i:]
   152  		} else {
   153  			localname = self.LocalName + " 金融云"
   154  		}
   155  
   156  		return fmt.Sprintf("%s %s", CLOUD_PROVIDER_ALIYUN_CN, localname)
   157  	} else {
   158  		return fmt.Sprintf("%s %s", CLOUD_PROVIDER_ALIYUN_CN, self.LocalName)
   159  	}
   160  }
   161  
   162  func (self *SZone) GetI18n() cloudprovider.SModelI18nTable {
   163  	table := cloudprovider.SModelI18nTable{}
   164  	table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName())
   165  	return table
   166  }
   167  
   168  func (self *SZone) GetGlobalId() string {
   169  	return fmt.Sprintf("%s/%s", self.region.GetGlobalId(), self.ZoneId)
   170  }
   171  
   172  func (self *SZone) IsEmulated() bool {
   173  	return false
   174  }
   175  
   176  func (self *SZone) GetStatus() string {
   177  	if len(self.AvailableResourceCreation.ResourceTypes) == 0 || !utils.IsInStringArray("Instance", self.AvailableResourceCreation.ResourceTypes) {
   178  		return api.ZONE_SOLDOUT
   179  	} else {
   180  		return api.ZONE_ENABLE
   181  	}
   182  }
   183  
   184  func (self *SZone) Refresh() error {
   185  	// do nothing
   186  	return nil
   187  }
   188  
   189  func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
   190  	return self.region
   191  }
   192  
   193  func (self *SZone) fetchStorages() error {
   194  	categories := self.AvailableDiskCategories.DiskCategories
   195  	self.istorages = []cloudprovider.ICloudStorage{}
   196  	for _, sc := range categories {
   197  		storage := SStorage{zone: self, storageType: sc}
   198  		self.istorages = append(self.istorages, &storage)
   199  		if sc == api.STORAGE_CLOUD_ESSD {
   200  			storage_l2 := SStorage{zone: self, storageType: api.STORAGE_CLOUD_ESSD_PL2}
   201  			self.istorages = append(self.istorages, &storage_l2)
   202  			storage_l3 := SStorage{zone: self, storageType: api.STORAGE_CLOUD_ESSD_PL3}
   203  			self.istorages = append(self.istorages, &storage_l3)
   204  		}
   205  	}
   206  	for _, localStorage := range LOCAL_STORAGES {
   207  		storage := SStorage{zone: self, storageType: localStorage}
   208  		self.istorages = append(self.istorages, &storage)
   209  	}
   210  	return nil
   211  }
   212  
   213  func (self *SZone) getStorageByCategory(category string) (*SStorage, error) {
   214  	storages, err := self.GetIStorages()
   215  	if err != nil {
   216  		return nil, err
   217  	}
   218  	for i := 0; i < len(storages); i += 1 {
   219  		storage := storages[i].(*SStorage)
   220  		if storage.storageType == category {
   221  			return storage, nil
   222  		}
   223  	}
   224  	return nil, fmt.Errorf("No such storage %s", category)
   225  }
   226  
   227  func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
   228  	if self.istorages == nil {
   229  		err := self.fetchStorages()
   230  		if err != nil {
   231  			return nil, errors.Wrapf(err, "fetchStorages")
   232  		}
   233  	}
   234  	return self.istorages, nil
   235  }
   236  
   237  func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
   238  	if self.istorages == nil {
   239  		err := self.fetchStorages()
   240  		if err != nil {
   241  			return nil, errors.Wrapf(err, "fetchStorages")
   242  		}
   243  	}
   244  	for i := 0; i < len(self.istorages); i += 1 {
   245  		if self.istorages[i].GetGlobalId() == id {
   246  			return self.istorages[i], nil
   247  		}
   248  	}
   249  	return nil, cloudprovider.ErrNotFound
   250  }
   251  
   252  func (self *SZone) getHost() *SHost {
   253  	if self.host == nil {
   254  		self.host = &SHost{zone: self}
   255  	}
   256  	return self.host
   257  }
   258  
   259  func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
   260  	return []cloudprovider.ICloudHost{self.getHost()}, nil
   261  }
   262  
   263  func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
   264  	host := self.getHost()
   265  	if host.GetGlobalId() == id {
   266  		return host, nil
   267  	}
   268  	return nil, cloudprovider.ErrNotFound
   269  }
   270  
   271  func (self *SZone) addWire(wire *SWire) {
   272  	if self.iwires == nil {
   273  		self.iwires = make([]cloudprovider.ICloudWire, 0)
   274  	}
   275  	self.iwires = append(self.iwires, wire)
   276  }
   277  
   278  func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
   279  	return self.iwires, nil
   280  }
   281  
   282  func (self *SZone) getNetworkById(vswitchId string) *SVSwitch {
   283  	log.Debugf("Search in wires %d", len(self.iwires))
   284  	for i := 0; i < len(self.iwires); i += 1 {
   285  		log.Debugf("Search in wire %s", self.iwires[i].GetName())
   286  		wire := self.iwires[i].(*SWire)
   287  		net := wire.getNetworkById(vswitchId)
   288  		if net != nil {
   289  			return net
   290  		}
   291  	}
   292  	return nil
   293  }
   294  
   295  func (self *SZone) getSysDiskCategories() []string {
   296  	if len(self.AvailableResources.ResourcesInfo) > 0 {
   297  		if utils.IsInStringArray(api.STORAGE_CLOUD_ESSD, self.AvailableResources.ResourcesInfo[0].SystemDiskCategories.SupportedSystemDiskCategory) {
   298  			self.AvailableResources.ResourcesInfo[0].SystemDiskCategories.SupportedSystemDiskCategory = append(self.AvailableResources.ResourcesInfo[0].SystemDiskCategories.SupportedSystemDiskCategory, api.STORAGE_CLOUD_ESSD_PL2)
   299  			self.AvailableResources.ResourcesInfo[0].SystemDiskCategories.SupportedSystemDiskCategory = append(self.AvailableResources.ResourcesInfo[0].SystemDiskCategories.SupportedSystemDiskCategory, api.STORAGE_CLOUD_ESSD_PL3)
   300  		}
   301  		return self.AvailableResources.ResourcesInfo[0].SystemDiskCategories.SupportedSystemDiskCategory
   302  	}
   303  	return nil
   304  }