zone.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 huawei
  15. import (
  16. "fmt"
  17. "strings"
  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. )
  23. type ZoneState struct {
  24. Available bool `json:"available"`
  25. }
  26. type SZone struct {
  27. multicloud.SResourceBase
  28. HuaweiTags
  29. region *SRegion
  30. host *SHost
  31. ZoneState ZoneState `json:"zoneState"`
  32. ZoneName string `json:"zoneName"`
  33. }
  34. func (self *SZone) getHost() *SHost {
  35. if self.host == nil {
  36. self.host = &SHost{zone: self}
  37. }
  38. return self.host
  39. }
  40. func (self *SZone) GetId() string {
  41. return self.ZoneName
  42. }
  43. func (self *SZone) GetName() string {
  44. return fmt.Sprintf("%s %s", CLOUD_PROVIDER_HUAWEI_CN, self.ZoneName)
  45. }
  46. func (self *SZone) GetI18n() cloudprovider.SModelI18nTable {
  47. en := fmt.Sprintf("%s %s", CLOUD_PROVIDER_HUAWEI_EN, self.ZoneName)
  48. table := cloudprovider.SModelI18nTable{}
  49. table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()).EN(en)
  50. return table
  51. }
  52. func (self *SZone) GetGlobalId() string {
  53. return fmt.Sprintf("%s/%s/%s", api.CLOUD_PROVIDER_HUAWEI, self.region.getId(), self.ZoneName)
  54. }
  55. func (self *SZone) GetStatus() string {
  56. return "enable"
  57. }
  58. func (self *SZone) Refresh() error {
  59. return nil
  60. }
  61. func (self *SZone) IsEmulated() bool {
  62. return false
  63. }
  64. func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
  65. return self.region
  66. }
  67. func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  68. return []cloudprovider.ICloudHost{self.getHost()}, nil
  69. }
  70. func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  71. host := self.getHost()
  72. if host.GetGlobalId() == id {
  73. return host, nil
  74. }
  75. return nil, cloudprovider.ErrNotFound
  76. }
  77. func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  78. storageTypes, err := self.region.GetDiskTypes()
  79. if err != nil {
  80. return nil, err
  81. }
  82. ret := []cloudprovider.ICloudStorage{}
  83. for _, storageType := range storageTypes {
  84. if strings.Contains(storageType.ExtraSpecs.RESKEYAvailabilityZones, self.ZoneName) {
  85. ret = append(ret, &SStorage{
  86. zone: self,
  87. storageType: storageType.Name,
  88. volumeTypeId: storageType.Id,
  89. })
  90. }
  91. }
  92. return ret, nil
  93. }
  94. func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  95. storages, err := self.GetIStorages()
  96. if err != nil {
  97. return nil, err
  98. }
  99. for i := range storages {
  100. if storages[i].GetGlobalId() == id {
  101. return storages[i], nil
  102. }
  103. }
  104. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  105. }
  106. func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
  107. vpcs, err := self.region.GetVpcs()
  108. if err != nil {
  109. return nil, err
  110. }
  111. ret := []cloudprovider.ICloudWire{}
  112. for i := range vpcs {
  113. vpcs[i].region = self.region
  114. ret = append(ret, &SWire{vpc: &vpcs[i]})
  115. }
  116. return ret, nil
  117. }
  118. func (self *SZone) getStorageByCategory(category string) (*SStorage, error) {
  119. storages, err := self.GetIStorages()
  120. if err != nil {
  121. return nil, err
  122. }
  123. for i := 0; i < len(storages); i += 1 {
  124. storage := storages[i].(*SStorage)
  125. if storage.storageType == category {
  126. return storage, nil
  127. }
  128. }
  129. return nil, fmt.Errorf("No such storage %s", category)
  130. }
  131. func (self *SRegion) getZoneById(id string) (*SZone, error) {
  132. zones, err := self.GetZones()
  133. if err != nil {
  134. return nil, err
  135. }
  136. for i := 0; i < len(zones); i += 1 {
  137. zones[i].region = self
  138. zone := zones[i]
  139. if zone.GetId() == id {
  140. return &zone, nil
  141. }
  142. }
  143. return nil, fmt.Errorf("no such zone %s", id)
  144. }