zone.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2023 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 volcengine
  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 SSupportedResource struct {
  24. Status string
  25. Value string
  26. }
  27. type SAvailableResource struct {
  28. Type string
  29. SupportedResources []SSupportedResource
  30. }
  31. type SZone struct {
  32. multicloud.SResourceBase
  33. VolcEngineTags
  34. region *SRegion
  35. host *SHost
  36. Status string
  37. AvailableResources []SAvailableResource
  38. ZoneId string
  39. RegionId string
  40. LocalName string
  41. }
  42. func (zone *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
  43. vpcs, err := zone.region.GetAllVpcs()
  44. if err != nil {
  45. return nil, errors.Wrapf(err, "GetVpcs")
  46. }
  47. ret := []cloudprovider.ICloudWire{}
  48. for i := range vpcs {
  49. vpcs[i].region = zone.region
  50. ret = append(ret, &SWire{zone: zone, vpc: &vpcs[i]})
  51. }
  52. return ret, nil
  53. }
  54. func (zone *SZone) GetId() string {
  55. return zone.ZoneId
  56. }
  57. func (zone *SZone) GetGlobalId() string {
  58. return fmt.Sprintf("%s/%s", zone.region.GetGlobalId(), zone.ZoneId)
  59. }
  60. func (zone *SZone) GetName() string {
  61. return fmt.Sprintf("%s", zone.ZoneId)
  62. }
  63. func (zone *SZone) GetI18n() cloudprovider.SModelI18nTable {
  64. table := cloudprovider.SModelI18nTable{}
  65. table["name"] = cloudprovider.NewSModelI18nEntry(zone.GetName()).CN(zone.GetName())
  66. return table
  67. }
  68. func (zone *SZone) GetStatus() string {
  69. return api.ZONE_ENABLE
  70. }
  71. func (zone *SZone) GetIRegion() cloudprovider.ICloudRegion {
  72. return zone.region
  73. }
  74. // Host
  75. func (zone *SZone) getHost() *SHost {
  76. if zone.host == nil {
  77. zone.host = &SHost{zone: zone}
  78. }
  79. return zone.host
  80. }
  81. func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  82. return []cloudprovider.ICloudHost{zone.getHost()}, nil
  83. }
  84. func (zone *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  85. hosts, err := zone.GetIHosts()
  86. if err != nil {
  87. return nil, err
  88. }
  89. for i := range hosts {
  90. if hosts[i].GetGlobalId() == id {
  91. return hosts[i], nil
  92. }
  93. }
  94. return nil, errors.Wrap(cloudprovider.ErrNotFound, "GetIHostById")
  95. }
  96. func (zone *SZone) getStorageByCategory(category string) (*SStorage, error) {
  97. storages, err := zone.GetIStorages()
  98. if err != nil {
  99. return nil, err
  100. }
  101. for i := 0; i < len(storages); i += 1 {
  102. storage := storages[i].(*SStorage)
  103. if storage.storageType == category {
  104. return storage, nil
  105. }
  106. }
  107. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "getStorageByCategory %s", category)
  108. }
  109. func (zone *SZone) GetStorages() ([]SStorage, error) {
  110. storages, err := zone.region.GetStorageTypes("")
  111. if err != nil {
  112. return nil, err
  113. }
  114. ret := []SStorage{}
  115. for i := range storages {
  116. if utils.IsInStringArray(zone.ZoneId, storages[i].Zones) {
  117. ret = append(ret, SStorage{storageType: storages[i].Id, zone: zone})
  118. }
  119. }
  120. ret = append(ret, SStorage{storageType: api.STORAGE_VOLCENGINE_PTSSD, zone: zone})
  121. return ret, nil
  122. }
  123. func (zone *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  124. storages, err := zone.GetStorages()
  125. if err != nil {
  126. return nil, err
  127. }
  128. ret := []cloudprovider.ICloudStorage{}
  129. for i := range storages {
  130. ret = append(ret, &storages[i])
  131. }
  132. return ret, nil
  133. }
  134. func (zone *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  135. storages, err := zone.GetIStorages()
  136. if err != nil {
  137. return nil, err
  138. }
  139. for i := range storages {
  140. if storages[i].GetGlobalId() == id {
  141. return storages[i], nil
  142. }
  143. }
  144. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  145. }
  146. func (self *SRegion) GetStorageTypes(zoneId string) ([]sStorageType, error) {
  147. if len(self.storageTypes) > 0 {
  148. return self.storageTypes, nil
  149. }
  150. params := map[string]string{
  151. "PageSize": "100",
  152. }
  153. if len(zoneId) > 0 {
  154. params["ZoneId"] = zoneId
  155. }
  156. resp, err := self.storageRequest("DescribeVolumeType", params)
  157. if err != nil {
  158. return nil, err
  159. }
  160. self.storageTypes = []sStorageType{}
  161. err = resp.Unmarshal(&self.storageTypes, "VolumeTypes")
  162. if err != nil {
  163. return nil, errors.Wrapf(err, "Unmarshal VolumeTypes")
  164. }
  165. return self.storageTypes, nil
  166. }