zone.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 ecloud
  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 SZone struct {
  22. multicloud.SResourceBase
  23. EcloudTags
  24. region *SRegion
  25. ZoneId string `json:"zoneId"`
  26. ZoneName string `json:"zoneName"`
  27. ZoneCode string `json:"zoneCode"`
  28. }
  29. func (z *SZone) GetId() string {
  30. return z.ZoneCode
  31. }
  32. func (z *SZone) GetName() string {
  33. return fmt.Sprintf("%s %s", z.region.GetName(), z.ZoneName)
  34. }
  35. func (z *SZone) GetGlobalId() string {
  36. return fmt.Sprintf("%s/%s", z.region.GetGlobalId(), z.ZoneCode)
  37. }
  38. func (z *SZone) GetStatus() string {
  39. return api.ZONE_ENABLE
  40. }
  41. func (z *SZone) Refresh() error {
  42. return nil
  43. }
  44. func (z *SZone) IsEmulated() bool {
  45. return false
  46. }
  47. func (z *SZone) GetI18n() cloudprovider.SModelI18nTable {
  48. table := cloudprovider.SModelI18nTable{}
  49. return table
  50. }
  51. func (z *SZone) GetIRegion() cloudprovider.ICloudRegion {
  52. return z.region
  53. }
  54. func (z *SZone) GetHost() *SHost {
  55. return &SHost{zone: z}
  56. }
  57. func (z *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  58. return []cloudprovider.ICloudHost{z.GetHost()}, nil
  59. }
  60. func (z *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  61. host := z.GetHost()
  62. if host.GetGlobalId() == id {
  63. return host, nil
  64. }
  65. return nil, cloudprovider.ErrNotFound
  66. }
  67. func (z *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  68. // 直接复用区域级 GetStorages 能力,并按当前 Zone 过滤。
  69. storages, err := z.region.GetStorages(z.ZoneCode)
  70. if err != nil {
  71. return nil, err
  72. }
  73. istorages := make([]cloudprovider.ICloudStorage, len(storages))
  74. for i := range storages {
  75. // 确保 zone 指针为当前 z
  76. storages[i].zone = z
  77. istorages[i] = &storages[i]
  78. }
  79. return istorages, nil
  80. }
  81. func (z *SZone) GetStorageByType(t string) (*SStorage, error) {
  82. istorages, err := z.GetIStorages()
  83. if err != nil {
  84. return nil, err
  85. }
  86. for i := range istorages {
  87. if istorages[i].GetStorageType() == t {
  88. return istorages[i].(*SStorage), nil
  89. }
  90. }
  91. return nil, cloudprovider.ErrNotFound
  92. }
  93. func (z *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  94. istorages, err := z.GetIStorages()
  95. if err != nil {
  96. return nil, err
  97. }
  98. for i := range istorages {
  99. if istorages[i].GetGlobalId() == id {
  100. return istorages[i], nil
  101. }
  102. }
  103. return nil, cloudprovider.ErrNotFound
  104. }
  105. type SZoneRegionBase struct {
  106. Region string
  107. }
  108. func (z *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
  109. vpcs, err := z.region.GetVpcs()
  110. if err != nil {
  111. return nil, err
  112. }
  113. for i := range vpcs {
  114. vpcs[i].region = z.region
  115. }
  116. wires := []cloudprovider.ICloudWire{}
  117. for i := range vpcs {
  118. wires = append(wires, &SWire{zone: z, vpc: &vpcs[i]})
  119. }
  120. return wires, nil
  121. }