zone.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 ksyun
  15. import (
  16. "fmt"
  17. api "yunion.io/x/cloudmux/pkg/apis/compute"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/cloudmux/pkg/multicloud"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/utils"
  22. )
  23. type SZone struct {
  24. multicloud.SResourceBase
  25. region *SRegion
  26. host *SHost
  27. SKsyunTags
  28. AvailabilityZone string
  29. }
  30. func (region *SRegion) GetZones() ([]SZone, error) {
  31. params := map[string]interface{}{}
  32. if len(region.Region) > 0 {
  33. params = map[string]interface{}{"Region": region.Region}
  34. }
  35. resp, err := region.ecsRequest("DescribeAvailabilityZones", params)
  36. if err != nil {
  37. return nil, errors.Wrap(err, "request zone")
  38. }
  39. zones := []SZone{}
  40. err = resp.Unmarshal(&zones, "AvailabilityZoneSet")
  41. if err != nil {
  42. return nil, errors.Wrap(err, "unmarshal zones")
  43. }
  44. return zones, nil
  45. }
  46. func (zone *SZone) GetId() string {
  47. return zone.AvailabilityZone
  48. }
  49. func (zone *SZone) GetName() string {
  50. return zone.AvailabilityZone
  51. }
  52. func (zone *SZone) GetI18n() cloudprovider.SModelI18nTable {
  53. return nil
  54. }
  55. func (zone *SZone) GetGlobalId() string {
  56. return fmt.Sprintf("%s/%s/%s", api.CLOUD_PROVIDER_KSYUN, zone.region.GetId(), zone.AvailabilityZone)
  57. }
  58. func (zone *SZone) GetStatus() string {
  59. return api.ZONE_ENABLE
  60. }
  61. func (zone *SZone) Refresh() error {
  62. return nil
  63. }
  64. func (zone *SZone) GetIRegion() cloudprovider.ICloudRegion {
  65. return zone.region
  66. }
  67. func (zone *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  68. host := zone.getHost()
  69. if host.GetGlobalId() == id {
  70. return host, nil
  71. }
  72. return nil, cloudprovider.ErrNotFound
  73. }
  74. func (zone *SZone) getHost() *SHost {
  75. if zone.host == nil {
  76. zone.host = &SHost{zone: zone}
  77. }
  78. return zone.host
  79. }
  80. func (zone *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  81. storages, err := zone.GetStorages()
  82. if err != nil {
  83. return nil, errors.Wrap(err, "GetStorages")
  84. }
  85. istorages := []cloudprovider.ICloudStorage{}
  86. for i := range storages {
  87. storages[i].zone = zone
  88. istorages = append(istorages, &storages[i])
  89. }
  90. return istorages, nil
  91. }
  92. func (zone *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  93. istorages, err := zone.GetIStorages()
  94. if err != nil {
  95. return nil, errors.Wrap(err, "GetIStorages")
  96. }
  97. for _, istorage := range istorages {
  98. if istorage.GetGlobalId() == id {
  99. return istorage, nil
  100. }
  101. }
  102. return nil, errors.Wrapf(errors.ErrNotFound, "storage id:%s", id)
  103. }
  104. func (zone *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
  105. vpcs, err := zone.region.GetVpcs([]string{})
  106. if err != nil {
  107. return nil, errors.Wrap(err, "GetVpcs")
  108. }
  109. for i := range vpcs {
  110. vpcs[i].region = zone.region
  111. }
  112. iwires := []cloudprovider.ICloudWire{}
  113. for _, vpc := range vpcs {
  114. iwires = append(iwires, &SWire{
  115. vpc: &vpc,
  116. zone: zone,
  117. })
  118. }
  119. return iwires, nil
  120. }
  121. func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  122. return []cloudprovider.ICloudHost{zone.getHost()}, nil
  123. }
  124. func (zone *SZone) GetDescription() string {
  125. return ""
  126. }
  127. func (zone *SZone) GetStorages() ([]SStorage, error) {
  128. zoneDiskType := map[string]bool{}
  129. for _, storageType := range api.KSYUN_STORAGES {
  130. if storageType == api.STORAGE_KSYUN_LOCAL_SSD {
  131. zoneDiskType[storageType] = true
  132. continue
  133. }
  134. zoneList, err := zone.region.GetZonesByDiskType(storageType)
  135. if err != nil {
  136. return nil, errors.Wrapf(err, "GetZonesByDiskType: %s", storageType)
  137. }
  138. if utils.IsInStringArray(zone.GetName(), zoneList) {
  139. zoneDiskType[storageType] = true
  140. } else {
  141. zoneDiskType[storageType] = false
  142. }
  143. }
  144. storages := []SStorage{}
  145. for storageType, available := range zoneDiskType {
  146. storage := SStorage{zone: zone, StorageType: storageType, available: available}
  147. storages = append(storages, storage)
  148. }
  149. return storages, nil
  150. }
  151. func (region *SRegion) GetZonesByDiskType(diskType string) ([]string, error) {
  152. params := map[string]interface{}{
  153. "VolumeType": diskType,
  154. }
  155. resp, err := region.ebsRequest("DescribeAvailabilityZones", params)
  156. if err != nil {
  157. return nil, errors.Wrap(err, "DescribeAvailabilityZones")
  158. }
  159. ret := struct {
  160. AvailabilityZones []string `json:"AvailabilityZones"`
  161. }{}
  162. err = resp.Unmarshal(&ret)
  163. if err != nil {
  164. return nil, errors.Wrap(err, "unmarshal zoneList")
  165. }
  166. return ret.AvailabilityZones, nil
  167. }