zone.go 7.9 KB

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