zone.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 aws
  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. var StorageTypes = []string{
  23. api.STORAGE_GP2_SSD,
  24. api.STORAGE_GP3_SSD,
  25. api.STORAGE_IO1_SSD,
  26. api.STORAGE_IO2_SSD,
  27. api.STORAGE_ST1_HDD,
  28. api.STORAGE_SC1_HDD,
  29. api.STORAGE_STANDARD_HDD,
  30. }
  31. type SZone struct {
  32. multicloud.SResourceBase
  33. AwsTags
  34. region *SRegion
  35. ZoneName string `xml:"zoneName"`
  36. ZoneId string `xml:"zoneId"`
  37. ZoneState string `xml:"zoneState"`
  38. RegionName string `xml:"regionName"`
  39. GroupName string `xml:"groupName"`
  40. OptInStatus string `xml:"optInStatus"`
  41. NetworkBorderGroup string `xml:"networkBorderGroup"`
  42. ZoneType string `xml:"zoneType"`
  43. }
  44. func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
  45. vpcs, err := self.region.GetVpcs(nil)
  46. if err != nil {
  47. return nil, errors.Wrapf(err, "GetVpcs")
  48. }
  49. ret := []cloudprovider.ICloudWire{}
  50. for i := range vpcs {
  51. vpcs[i].region = self.region
  52. ret = append(ret, &SWire{zone: self, vpc: &vpcs[i]})
  53. }
  54. return ret, nil
  55. }
  56. func (self *SZone) GetId() string {
  57. return self.ZoneName
  58. }
  59. func (self *SZone) GetName() string {
  60. return fmt.Sprintf("%s %s", CLOUD_PROVIDER_AWS_CN, self.ZoneName)
  61. }
  62. func (self *SZone) GetI18n() cloudprovider.SModelI18nTable {
  63. en := fmt.Sprintf("%s %s", CLOUD_PROVIDER_AWS_EN, self.ZoneName)
  64. table := cloudprovider.SModelI18nTable{}
  65. table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()).EN(en)
  66. return table
  67. }
  68. func (self *SZone) GetGlobalId() string {
  69. return fmt.Sprintf("%s/%s", self.region.GetGlobalId(), self.ZoneName)
  70. }
  71. func (self *SZone) GetStatus() string {
  72. if self.ZoneState == "available" {
  73. return api.ZONE_ENABLE
  74. }
  75. return api.ZONE_SOLDOUT
  76. }
  77. func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
  78. return self.region
  79. }
  80. func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  81. host := &SHost{zone: self}
  82. return []cloudprovider.ICloudHost{host}, nil
  83. }
  84. func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  85. hosts, err := self.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 (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  97. ret := []cloudprovider.ICloudStorage{}
  98. for i := range StorageTypes {
  99. storage := &SStorage{zone: self, storageType: StorageTypes[i]}
  100. ret = append(ret, storage)
  101. }
  102. return ret, nil
  103. }
  104. func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  105. storages, err := self.GetIStorages()
  106. if err != nil {
  107. return nil, err
  108. }
  109. for i := 0; i < len(storages); i += 1 {
  110. if storages[i].GetGlobalId() == id {
  111. return storages[i], nil
  112. }
  113. }
  114. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "not found %s", id)
  115. }
  116. func (self *SZone) getStorageByCategory(category string) (*SStorage, error) {
  117. storages, err := self.GetIStorages()
  118. if err != nil {
  119. return nil, errors.Wrap(err, "GetIStorages")
  120. }
  121. for i := 0; i < len(storages); i += 1 {
  122. storage := storages[i].(*SStorage)
  123. if storage.storageType == category {
  124. return storage, nil
  125. }
  126. }
  127. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", category)
  128. }
  129. func (self *SZone) GetDescription() string {
  130. return self.AwsTags.GetDescription()
  131. }