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