zone.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 hcso
  15. import (
  16. "fmt"
  17. "yunion.io/x/log"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/cloudmux/pkg/apis/compute"
  20. "yunion.io/x/cloudmux/pkg/cloudprovider"
  21. "yunion.io/x/cloudmux/pkg/multicloud"
  22. "yunion.io/x/cloudmux/pkg/multicloud/huawei"
  23. )
  24. var StorageTypes = []string{
  25. api.STORAGE_HUAWEI_SAS,
  26. api.STORAGE_HUAWEI_SATA,
  27. api.STORAGE_HUAWEI_SSD,
  28. }
  29. type ZoneState struct {
  30. Available bool `json:"available"`
  31. }
  32. // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0065817728.html
  33. type SZone struct {
  34. multicloud.SResourceBase
  35. huawei.HuaweiTags
  36. region *SRegion
  37. ihosts []cloudprovider.ICloudHost
  38. iwires []cloudprovider.ICloudWire
  39. istorages []cloudprovider.ICloudStorage
  40. ZoneState ZoneState `json:"zoneState"`
  41. ZoneName string `json:"zoneName"`
  42. /* 支持的磁盘种类集合 */
  43. storageTypes []string
  44. }
  45. func (self *SZone) addWire(wire *SWire) {
  46. if self.iwires == nil {
  47. self.iwires = make([]cloudprovider.ICloudWire, 0)
  48. }
  49. self.iwires = append(self.iwires, wire)
  50. }
  51. func (self *SZone) getStorageType() {
  52. if len(self.storageTypes) == 0 {
  53. if sts, err := self.region.GetZoneSupportedDiskTypes(self.GetId()); err == nil {
  54. self.storageTypes = sts
  55. } else {
  56. log.Errorf("GetZoneSupportedDiskTypes %s %s", self.GetId(), err)
  57. self.storageTypes = StorageTypes
  58. }
  59. }
  60. }
  61. func (self *SZone) fetchStorages() error {
  62. self.getStorageType()
  63. self.istorages = make([]cloudprovider.ICloudStorage, len(self.storageTypes))
  64. for i, sc := range self.storageTypes {
  65. storage := SStorage{zone: self, storageType: sc}
  66. self.istorages[i] = &storage
  67. }
  68. return nil
  69. }
  70. // 华为私有云没有直接列出host的接口,所有账号下的host都是通过VM反向解析出来的
  71. // 当账号下没有虚拟机时,如果没有host,会导致调度找不到可用的HOST。
  72. // 因此,为了避免上述情况始终会在每个zone下返回一台虚拟的host
  73. func (self *SZone) getEmulatedHost() SHost {
  74. return SHost{
  75. zone: self,
  76. vms: nil,
  77. IsFake: true,
  78. projectId: self.region.client.projectId,
  79. Id: fmt.Sprintf("%s-%s", self.region.client.cpcfg.Id, self.GetId()),
  80. Name: fmt.Sprintf("%s-%s", self.region.client.cpcfg.Name, self.GetId()),
  81. }
  82. }
  83. func (self *SZone) getHosts() ([]cloudprovider.ICloudHost, error) {
  84. if self.ihosts != nil {
  85. return self.ihosts, nil
  86. }
  87. vms, err := self.region.GetInstances()
  88. if err != nil {
  89. return nil, errors.Wrap(err, "GetInstances")
  90. }
  91. hosts := map[string]string{}
  92. hostVms := map[string][]SInstance{}
  93. for i := range vms {
  94. vm := vms[i]
  95. if vm.OSEXTAZAvailabilityZone == self.GetId() {
  96. hosts[vm.HostID] = vm.OSEXTSRVATTRHost
  97. if _, ok := hostVms[vm.HostID]; ok {
  98. hostVms[vm.HostID] = append(hostVms[vm.HostID], vm)
  99. } else {
  100. hostVms[vm.HostID] = []SInstance{vm}
  101. }
  102. }
  103. }
  104. fakeHost := self.getEmulatedHost()
  105. ihosts := []cloudprovider.ICloudHost{&fakeHost}
  106. for k, _ := range hosts {
  107. h := SHost{
  108. zone: self,
  109. projectId: self.region.client.projectId,
  110. Id: k,
  111. Name: hosts[k],
  112. }
  113. for i := range hostVms[k] {
  114. hostVms[k][i].host = &h
  115. }
  116. h.vms = hostVms[k]
  117. ihosts = append(ihosts, &h)
  118. }
  119. return ihosts, nil
  120. }
  121. func (self *SZone) GetId() string {
  122. return self.ZoneName
  123. }
  124. func (self *SZone) GetName() string {
  125. return fmt.Sprintf("%s %s", CLOUD_PROVIDER_HUAWEI_CN, self.ZoneName)
  126. }
  127. func (self *SZone) GetI18n() cloudprovider.SModelI18nTable {
  128. en := fmt.Sprintf("%s %s", CLOUD_PROVIDER_HUAWEI_EN, self.ZoneName)
  129. table := cloudprovider.SModelI18nTable{}
  130. table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()).EN(en)
  131. return table
  132. }
  133. func (self *SZone) GetGlobalId() string {
  134. return fmt.Sprintf("%s/%s", self.region.GetGlobalId(), self.ZoneName)
  135. }
  136. func (self *SZone) GetStatus() string {
  137. return "enable"
  138. }
  139. func (self *SZone) Refresh() error {
  140. return nil
  141. }
  142. func (self *SZone) IsEmulated() bool {
  143. return false
  144. }
  145. func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
  146. return self.region
  147. }
  148. func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  149. return self.getHosts()
  150. }
  151. func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  152. ihosts, err := self.getHosts()
  153. if err != nil {
  154. return nil, errors.Wrap(err, "getHosts")
  155. }
  156. for i := range ihosts {
  157. if ihosts[i].GetGlobalId() == id {
  158. return ihosts[i], nil
  159. }
  160. }
  161. return nil, cloudprovider.ErrNotFound
  162. }
  163. func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  164. if self.istorages == nil {
  165. err := self.fetchStorages()
  166. if err != nil {
  167. return nil, errors.Wrapf(err, "fetchStorages")
  168. }
  169. }
  170. return self.istorages, nil
  171. }
  172. func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  173. if self.istorages == nil {
  174. err := self.fetchStorages()
  175. if err != nil {
  176. return nil, errors.Wrapf(err, "fetchStorages")
  177. }
  178. }
  179. for i := 0; i < len(self.istorages); i += 1 {
  180. if self.istorages[i].GetGlobalId() == id {
  181. return self.istorages[i], nil
  182. }
  183. }
  184. return nil, cloudprovider.ErrNotFound
  185. }
  186. func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
  187. return self.iwires, nil
  188. }
  189. func (self *SZone) getStorageByCategory(category string) (*SStorage, error) {
  190. storages, err := self.GetIStorages()
  191. if err != nil {
  192. return nil, err
  193. }
  194. for i := 0; i < len(storages); i += 1 {
  195. storage := storages[i].(*SStorage)
  196. if storage.storageType == category {
  197. return storage, nil
  198. }
  199. }
  200. return nil, fmt.Errorf("No such storage %s", category)
  201. }
  202. func (self *SRegion) getZoneById(id string) (*SZone, error) {
  203. izones, err := self.GetIZones()
  204. if err != nil {
  205. return nil, err
  206. }
  207. for i := 0; i < len(izones); i += 1 {
  208. zone := izones[i].(*SZone)
  209. if zone.GetId() == id {
  210. return zone, nil
  211. }
  212. }
  213. return nil, fmt.Errorf("no such zone %s", id)
  214. }
  215. func (self *SZone) getNetworkById(networkId string) *SNetwork {
  216. for i := 0; i < len(self.iwires); i += 1 {
  217. wire := self.iwires[i].(*SWire)
  218. net := wire.getNetworkById(networkId)
  219. if net != nil {
  220. return net
  221. }
  222. }
  223. return nil
  224. }