zone.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 ucloud
  15. import (
  16. "fmt"
  17. "yunion.io/x/pkg/errors"
  18. api "yunion.io/x/cloudmux/pkg/apis/compute"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/cloudmux/pkg/multicloud"
  21. )
  22. // https://docs.ucloud.cn/api/udisk-api/create_udisk
  23. // UDisk 类型: DataDisk(普通数据盘),SSDDataDisk(SSD数据盘),默认值(DataDisk)
  24. var StorageTypes = []string{
  25. api.STORAGE_UCLOUD_CLOUD_NORMAL,
  26. api.STORAGE_UCLOUD_CLOUD_SSD,
  27. api.STORAGE_UCLOUD_LOCAL_NORMAL, // 本地盘
  28. api.STORAGE_UCLOUD_LOCAL_SSD, // 本地SSD盘
  29. }
  30. type SZone struct {
  31. multicloud.SResourceBase
  32. UcloudTags
  33. region *SRegion
  34. host *SHost
  35. iwires []cloudprovider.ICloudWire
  36. istorages []cloudprovider.ICloudStorage
  37. RegionId string
  38. ZoneId string
  39. /* 支持的磁盘种类集合 */
  40. storageTypes []string
  41. }
  42. func (self *SZone) addWire(wire *SWire) {
  43. if self.iwires == nil {
  44. self.iwires = make([]cloudprovider.ICloudWire, 0)
  45. }
  46. self.iwires = append(self.iwires, wire)
  47. }
  48. func (self *SZone) getHost() *SHost {
  49. if self.host == nil {
  50. self.host = &SHost{zone: self, projectId: self.region.client.projectId}
  51. }
  52. return self.host
  53. }
  54. func (self *SZone) getStorageType() {
  55. if len(self.storageTypes) == 0 {
  56. self.storageTypes = StorageTypes
  57. }
  58. }
  59. func (self *SZone) fetchStorages() error {
  60. self.getStorageType()
  61. self.istorages = make([]cloudprovider.ICloudStorage, len(self.storageTypes))
  62. for i, sc := range self.storageTypes {
  63. storage := SStorage{zone: self, storageType: sc}
  64. self.istorages[i] = &storage
  65. }
  66. return nil
  67. }
  68. func (self *SZone) GetId() string {
  69. return self.ZoneId
  70. }
  71. func (self *SZone) GetName() string {
  72. if name, exists := UCLOUD_ZONE_NAMES[self.GetId()]; exists {
  73. return name
  74. }
  75. return self.GetId()
  76. }
  77. func (self *SZone) GetI18n() cloudprovider.SModelI18nTable {
  78. var en string
  79. if name, exists := UCLOUD_ZONE_NAMES_EN[self.GetId()]; exists {
  80. en = name
  81. } else {
  82. en = self.GetId()
  83. }
  84. table := cloudprovider.SModelI18nTable{}
  85. table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()).EN(en)
  86. return table
  87. }
  88. func (self *SZone) GetGlobalId() string {
  89. return fmt.Sprintf("%s/%s", self.region.GetGlobalId(), self.GetId())
  90. }
  91. func (self *SZone) GetStatus() string {
  92. return api.ZONE_ENABLE
  93. }
  94. func (self *SZone) Refresh() error {
  95. return nil
  96. }
  97. func (self *SZone) IsEmulated() bool {
  98. return false
  99. }
  100. func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
  101. return self.region
  102. }
  103. func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  104. return []cloudprovider.ICloudHost{self.getHost()}, nil
  105. }
  106. func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  107. host := self.getHost()
  108. if host.GetGlobalId() == id {
  109. return host, nil
  110. }
  111. return nil, cloudprovider.ErrNotFound
  112. }
  113. func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  114. if self.istorages == nil {
  115. err := self.fetchStorages()
  116. if err != nil {
  117. return nil, errors.Wrapf(err, "fetchStorages")
  118. }
  119. }
  120. return self.istorages, nil
  121. }
  122. func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  123. if self.istorages == nil {
  124. err := self.fetchStorages()
  125. if err != nil {
  126. return nil, errors.Wrapf(err, "fetchStorages")
  127. }
  128. }
  129. for i := 0; i < len(self.istorages); i += 1 {
  130. if self.istorages[i].GetGlobalId() == id {
  131. return self.istorages[i], nil
  132. }
  133. }
  134. return nil, cloudprovider.ErrNotFound
  135. }
  136. // https://docs.ucloud.cn/api/uhost-api/describe_uhost_instance
  137. func (self *SZone) GetInstances() ([]SInstance, error) {
  138. return self.region.GetInstances(self.GetId(), "")
  139. }
  140. func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
  141. return self.iwires, nil
  142. }
  143. // https://docs.ucloud.cn/api/uhost-api/describe_uhost_instance
  144. func (self *SRegion) GetInstances(zoneId string, instanceId string) ([]SInstance, error) {
  145. instances := make([]SInstance, 0)
  146. params := NewUcloudParams()
  147. if len(zoneId) > 0 {
  148. params.Set("Zone", zoneId)
  149. }
  150. if len(instanceId) > 0 {
  151. params.Set("UHostIds.0", instanceId)
  152. }
  153. err := self.DoListAll("DescribeUHostInstance", params, &instances)
  154. return instances, err
  155. }
  156. func (self *SZone) getStorageByCategory(category string) (*SStorage, error) {
  157. storages, err := self.GetIStorages()
  158. if err != nil {
  159. return nil, err
  160. }
  161. for i := 0; i < len(storages); i += 1 {
  162. storage := storages[i].(*SStorage)
  163. if storage.storageType == category {
  164. return storage, nil
  165. }
  166. }
  167. return nil, fmt.Errorf("No such storage %s", category)
  168. }