zone.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 qcloud
  15. import (
  16. "fmt"
  17. "strings"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/utils"
  20. api "yunion.io/x/cloudmux/pkg/apis/compute"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/cloudmux/pkg/multicloud"
  23. )
  24. type InstanceChargeType string
  25. const (
  26. PrePaidInstanceChargeType InstanceChargeType = "PREPAID"
  27. PostPaidInstanceChargeType InstanceChargeType = "POSTPAID_BY_HOUR"
  28. CdhPaidInstanceChargeType InstanceChargeType = "CDHPAID"
  29. DefaultInstanceChargeType = PostPaidInstanceChargeType
  30. )
  31. type SZone struct {
  32. multicloud.SResourceBase
  33. QcloudTags
  34. region *SRegion
  35. host *SHost
  36. Zone string
  37. ZoneName string
  38. ZoneState string
  39. }
  40. func (self *SZone) GetId() string {
  41. return self.Zone
  42. }
  43. func (self *SZone) GetName() string {
  44. if len(self.ZoneName) > 0 {
  45. return self.ZoneName
  46. }
  47. return self.Zone
  48. }
  49. func (self *SZone) GetI18n() cloudprovider.SModelI18nTable {
  50. en := self.ZoneName
  51. table := cloudprovider.SModelI18nTable{}
  52. table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()).EN(en)
  53. return table
  54. }
  55. func (self *SZone) GetGlobalId() string {
  56. return fmt.Sprintf("%s/%s", self.region.GetGlobalId(), self.Zone)
  57. }
  58. func (self *SZone) GetStatus() string {
  59. if self.ZoneState == "AVAILABLE" {
  60. return api.ZONE_ENABLE
  61. }
  62. return api.ZONE_SOLDOUT
  63. }
  64. func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  65. host := self.getHost()
  66. if host.GetGlobalId() == id {
  67. return host, nil
  68. }
  69. return nil, cloudprovider.ErrNotFound
  70. }
  71. func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  72. return []cloudprovider.ICloudHost{self.getHost()}, nil
  73. }
  74. func (self *SZone) getHost() *SHost {
  75. if self.host == nil {
  76. self.host = &SHost{zone: self}
  77. }
  78. return self.host
  79. }
  80. func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
  81. return self.region
  82. }
  83. type SDiskConfigSet struct {
  84. Available bool
  85. DeviceClass string
  86. DiskChargeType string
  87. DiskType string
  88. DiskUsage string
  89. InstanceFamily string
  90. MaxDiskSize int
  91. MinDiskSize int
  92. Zone string
  93. }
  94. func (self *SRegion) GetDiskConfigSet(zoneName string) ([]SDiskConfigSet, error) {
  95. params := map[string]string{}
  96. params["Region"] = self.Region
  97. params["Zones.0"] = zoneName
  98. params["InquiryType"] = "INQUIRY_CBS_CONFIG"
  99. body, err := self.cbsRequest("DescribeDiskConfigQuota", params)
  100. if err != nil {
  101. return nil, err
  102. }
  103. diskConfigSet := []SDiskConfigSet{}
  104. return diskConfigSet, body.Unmarshal(&diskConfigSet, "DiskConfigSet")
  105. }
  106. func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  107. ret := []cloudprovider.ICloudStorage{}
  108. diskConfigSet, err := self.region.GetDiskConfigSet(self.Zone)
  109. if err != nil {
  110. return nil, err
  111. }
  112. cloudstorages := []string{}
  113. for _, diskConfig := range diskConfigSet {
  114. if !utils.IsInStringArray(strings.ToUpper(diskConfig.DiskType), cloudstorages) {
  115. cloudstorages = append(cloudstorages, strings.ToUpper(diskConfig.DiskType))
  116. storage := &SStorage{zone: self, storageType: diskConfig.DiskType, available: diskConfig.Available}
  117. ret = append(ret, storage)
  118. }
  119. }
  120. for _, storageType := range []string{"CLOUD_PREMIUM", "CLOUD_SSD", "CLOUD_BASIC"} {
  121. if !utils.IsInStringArray(storageType, cloudstorages) {
  122. cloudstorages = append(cloudstorages, storageType)
  123. storage := SStorage{zone: self, storageType: storageType, available: false}
  124. ret = append(ret, &storage)
  125. }
  126. }
  127. localstorages, err := self.region.GetZoneLocalStorages(self.Zone)
  128. if err != nil && errors.Cause(err) != cloudprovider.ErrNotSupported {
  129. return nil, errors.Wrapf(err, "GetZoneLocalStorages")
  130. }
  131. for _, localstorageType := range localstorages {
  132. storage := SLocalStorage{zone: self, storageType: localstorageType, available: true}
  133. ret = append(ret, &storage)
  134. }
  135. for _, storageType := range QCLOUD_LOCAL_STORAGE_TYPES {
  136. if !utils.IsInStringArray(storageType, localstorages) {
  137. storage := SLocalStorage{zone: self, storageType: storageType, available: false}
  138. ret = append(ret, &storage)
  139. }
  140. }
  141. return ret, nil
  142. }
  143. func (self *SZone) getLocalStorageByCategory(category string) (*SLocalStorage, error) {
  144. localstorages, err := self.region.GetZoneLocalStorages(self.Zone)
  145. if err != nil {
  146. return nil, errors.Wrapf(err, "GetZoneLocalStorages")
  147. }
  148. if utils.IsInStringArray(strings.ToUpper(category), localstorages) {
  149. return &SLocalStorage{zone: self, storageType: strings.ToUpper(category)}, nil
  150. }
  151. return nil, fmt.Errorf("No such storage %s", category)
  152. }
  153. func (self *SZone) getStorageByCategory(category string) (*SStorage, error) {
  154. storages, err := self.GetIStorages()
  155. if err != nil {
  156. return nil, err
  157. }
  158. for i := 0; i < len(storages); i++ {
  159. storage, ok := storages[i].(*SStorage)
  160. if !ok {
  161. continue
  162. }
  163. if strings.ToLower(storage.storageType) == strings.ToLower(category) {
  164. return storage, nil
  165. }
  166. }
  167. return nil, fmt.Errorf("No such storage %s", category)
  168. }
  169. func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  170. storages, err := self.GetIStorages()
  171. if err != nil {
  172. return nil, err
  173. }
  174. for i := 0; i < len(storages); i += 1 {
  175. if storages[i].GetGlobalId() == id {
  176. return storages[i], nil
  177. }
  178. }
  179. return nil, cloudprovider.ErrNotFound
  180. }
  181. func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
  182. vpcs, err := self.region.GetVpcs(nil)
  183. if err != nil {
  184. return nil, err
  185. }
  186. ret := []cloudprovider.ICloudWire{}
  187. for i := range vpcs {
  188. wire := &SWire{vpc: &vpcs[i], zone: self}
  189. ret = append(ret, wire)
  190. }
  191. return ret, nil
  192. }
  193. func (self *SZone) getCosEndpoint() string {
  194. return fmt.Sprintf("cos.%s.myqcloud.com", self.GetId())
  195. }
  196. func (self *SZone) getCosWebsiteEndpoint() string {
  197. return fmt.Sprintf("cos-website.%s.myqcloud.com", self.GetId())
  198. }