zone.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 jdcloud
  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. )
  21. type sZoneIden struct {
  22. Id string
  23. Name string
  24. }
  25. var ZonesInRegion = map[string][]sZoneIden{
  26. "cn-north-1": {
  27. {
  28. Id: "cn-north-1a",
  29. Name: "可用区A",
  30. },
  31. {
  32. Id: "cn-north-1b",
  33. Name: "可用区B",
  34. },
  35. {
  36. Id: "cn-north-1c",
  37. Name: "可用区C",
  38. },
  39. },
  40. "cn-east-1": {
  41. {
  42. Id: "cn-east-1a",
  43. Name: "可用区A",
  44. },
  45. },
  46. "cn-east-2": {
  47. {
  48. Id: "cn-east-2a",
  49. Name: "可用区A",
  50. },
  51. {
  52. Id: "cn-east-2b",
  53. Name: "可用区B",
  54. },
  55. {
  56. Id: "cn-east-2c",
  57. Name: "可用区C",
  58. },
  59. },
  60. "cn-south-1": {
  61. {
  62. Id: "cn-south-1a",
  63. Name: "可用区A",
  64. },
  65. {
  66. Id: "cn-south-1b",
  67. Name: "可用区B",
  68. },
  69. {
  70. Id: "cn-south-1c",
  71. Name: "可用区C",
  72. },
  73. },
  74. }
  75. type SZone struct {
  76. multicloud.SResourceBase
  77. JdcloudTags
  78. region *SRegion
  79. ihost cloudprovider.ICloudHost
  80. istorages []cloudprovider.ICloudStorage
  81. ID string
  82. Name string
  83. }
  84. func (z *SZone) GetId() string {
  85. return z.ID
  86. }
  87. func (z *SZone) GetName() string {
  88. return z.Name
  89. }
  90. func (z *SZone) GetGlobalId() string {
  91. return fmt.Sprintf("%s/%s", z.region.GetGlobalId(), z.ID)
  92. }
  93. func (z *SZone) GetStatus() string {
  94. return api.ZONE_ENABLE
  95. }
  96. func (z *SZone) Refresh() error {
  97. return nil
  98. }
  99. func (z *SZone) IsEmulated() bool {
  100. return false
  101. }
  102. func (z *SZone) GetI18n() cloudprovider.SModelI18nTable {
  103. table := cloudprovider.SModelI18nTable{}
  104. table["name"] = cloudprovider.NewSModelI18nEntry(z.GetName()).CN(z.GetName()).EN(fmt.Sprintf("%s %s", CLOUD_PROVIDER_JDCLOUD_EN, z.Name))
  105. return table
  106. }
  107. func (z *SZone) GetIRegion() cloudprovider.ICloudRegion {
  108. return z.region
  109. }
  110. func (z *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  111. if z.ihost != nil {
  112. return []cloudprovider.ICloudHost{z.ihost}, nil
  113. }
  114. z.ihost = &SHost{
  115. zone: z,
  116. }
  117. return []cloudprovider.ICloudHost{z.ihost}, nil
  118. }
  119. func (z *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  120. ihosts, err := z.GetIHosts()
  121. if err != nil {
  122. return nil, err
  123. }
  124. for i := range ihosts {
  125. if ihosts[i].GetGlobalId() == id {
  126. return ihosts[i], nil
  127. }
  128. }
  129. return nil, cloudprovider.ErrNotFound
  130. }
  131. func (z *SZone) fetchStorages() error {
  132. istorages := make([]cloudprovider.ICloudStorage, len(storageTypes))
  133. for i := range istorages {
  134. istorages[i] = &SStorage{
  135. zone: z,
  136. storageType: storageTypes[i],
  137. }
  138. }
  139. z.istorages = istorages
  140. return nil
  141. }
  142. func (z *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  143. if z.istorages == nil {
  144. err := z.fetchStorages()
  145. if err != nil {
  146. return nil, err
  147. }
  148. }
  149. return z.istorages, nil
  150. }
  151. func (z *SZone) getStorageByType(t string) (*SStorage, error) {
  152. istorages, err := z.GetIStorages()
  153. if err != nil {
  154. return nil, err
  155. }
  156. for i := range istorages {
  157. if istorages[i].GetStorageType() == t {
  158. return istorages[i].(*SStorage), nil
  159. }
  160. }
  161. return nil, cloudprovider.ErrNotFound
  162. }
  163. func (z *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  164. istorages, err := z.GetIStorages()
  165. if err != nil {
  166. return nil, err
  167. }
  168. for i := range istorages {
  169. if istorages[i].GetGlobalId() == id {
  170. return istorages[i], nil
  171. }
  172. }
  173. return nil, cloudprovider.ErrNotFound
  174. }