zone.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 openstack
  15. import (
  16. "fmt"
  17. "strings"
  18. "time"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  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 ZoneState struct {
  26. Available bool
  27. }
  28. type HostState struct {
  29. Available bool
  30. Active bool
  31. UpdatedAt time.Time
  32. }
  33. type SZone struct {
  34. multicloud.SResourceBase
  35. OpenStackTags
  36. region *SRegion
  37. ZoneName string
  38. ZoneState ZoneState
  39. Hosts map[string]jsonutils.JSONObject
  40. hosts []SHypervisor
  41. }
  42. func (zone *SZone) GetId() string {
  43. return zone.ZoneName
  44. }
  45. func (zone *SZone) GetName() string {
  46. return zone.ZoneName
  47. }
  48. func (zone *SZone) GetI18n() cloudprovider.SModelI18nTable {
  49. table := cloudprovider.SModelI18nTable{}
  50. table["name"] = cloudprovider.NewSModelI18nEntry(zone.GetName()).CN(zone.GetName())
  51. return table
  52. }
  53. func (zone *SZone) GetGlobalId() string {
  54. return fmt.Sprintf("%s/%s/%s", CLOUD_PROVIDER_OPENSTACK, zone.region.Name, zone.ZoneName)
  55. }
  56. func (zone *SZone) IsEmulated() bool {
  57. return false
  58. }
  59. func (zone *SZone) GetStatus() string {
  60. if zone.ZoneState.Available {
  61. return api.ZONE_ENABLE
  62. }
  63. return api.ZONE_DISABLE
  64. }
  65. func (zone *SZone) Refresh() error {
  66. // do nothing
  67. return nil
  68. }
  69. func (zone *SZone) GetIRegion() cloudprovider.ICloudRegion {
  70. return zone.region
  71. }
  72. func (zone *SZone) getStorageByCategory(category, host string) (*SStorage, error) {
  73. storages, err := zone.region.GetStorageTypes()
  74. if err != nil {
  75. return nil, errors.Wrap(err, "GetStorageTypes")
  76. }
  77. for i := range storages {
  78. if storages[i].Name == category || storages[i].ExtraSpecs.VolumeBackendName == category {
  79. storages[i].zone = zone
  80. return &storages[i], nil
  81. }
  82. }
  83. for i := range storages {
  84. if strings.HasSuffix(host, "#"+storages[i].Name) || strings.HasSuffix(host, "#"+storages[i].ExtraSpecs.VolumeBackendName) {
  85. storages[i].zone = zone
  86. return &storages[i], nil
  87. }
  88. }
  89. return nil, fmt.Errorf("No such storage [%s]", category)
  90. }
  91. func (zone *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  92. storages, err := zone.region.GetStorageTypes()
  93. if err != nil && errors.Cause(err) != ErrNoEndpoint {
  94. return nil, errors.Wrap(err, "GetStorageTypes")
  95. }
  96. istorages := []cloudprovider.ICloudStorage{}
  97. for i := range storages {
  98. storages[i].zone = zone
  99. istorages = append(istorages, &storages[i])
  100. }
  101. err = zone.fetchHosts()
  102. if err != nil {
  103. return nil, errors.Wrap(err, "fetchHosts")
  104. }
  105. for i := range zone.hosts {
  106. nova := &SNovaStorage{host: &zone.hosts[i], zone: zone}
  107. istorages = append(istorages, nova)
  108. }
  109. return istorages, nil
  110. }
  111. func (zone *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  112. istorages, err := zone.GetIStorages()
  113. if err != nil {
  114. return nil, errors.Wrap(err, "GetIStorages")
  115. }
  116. for i := 0; i < len(istorages); i++ {
  117. if istorages[i].GetGlobalId() == id {
  118. return istorages[i], nil
  119. }
  120. }
  121. return nil, cloudprovider.ErrNotFound
  122. }
  123. func (zone *SZone) fetchHosts() error {
  124. if len(zone.hosts) > 0 {
  125. return nil
  126. }
  127. zone.hosts = []SHypervisor{}
  128. hypervisors, err := zone.region.GetHypervisors()
  129. if err != nil {
  130. return errors.Wrap(err, "GetHypervisors")
  131. }
  132. for i := range hypervisors {
  133. hypervisor := strings.ToLower(hypervisors[i].HypervisorType)
  134. // 过滤vmware的机器
  135. if strings.Index(hypervisor, "vmware") != -1 {
  136. continue
  137. }
  138. _, ok1 := zone.Hosts[hypervisors[i].HypervisorHostname]
  139. _, ok2 := zone.Hosts[hypervisors[i].Service.Host]
  140. if !ok1 && !ok2 {
  141. continue
  142. }
  143. zone.hosts = append(zone.hosts, hypervisors[i])
  144. }
  145. return nil
  146. }
  147. func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  148. err := zone.fetchHosts()
  149. if err != nil {
  150. return nil, errors.Wrap(err, "fetchHosts")
  151. }
  152. ihosts := []cloudprovider.ICloudHost{}
  153. for i := range zone.hosts {
  154. zone.hosts[i].zone = zone
  155. ihosts = append(ihosts, &zone.hosts[i])
  156. }
  157. return ihosts, nil
  158. }
  159. func (zone *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  160. ihosts, err := zone.GetIHosts()
  161. if err != nil {
  162. return nil, errors.Wrap(err, "GetIHosts")
  163. }
  164. for i := range ihosts {
  165. if ihosts[i].GetGlobalId() == id {
  166. return ihosts[i], nil
  167. }
  168. }
  169. return nil, cloudprovider.ErrNotFound
  170. }
  171. func (region *SRegion) getZones() ([]SZone, error) {
  172. zones := []SZone{}
  173. resp, err := region.ecsList("os-availability-zone/detail", nil)
  174. if err != nil {
  175. return nil, errors.Wrap(err, "ecsList.os-availability-zone")
  176. }
  177. err = resp.Unmarshal(&zones, "availabilityZoneInfo")
  178. if err != nil {
  179. return nil, errors.Wrap(err, "resp.Unmarshal")
  180. }
  181. return zones, nil
  182. }