zone.go 8.2 KB

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