zone.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 baidu
  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. )
  22. type SZone struct {
  23. multicloud.SResourceBase
  24. SBaiduTag
  25. region *SRegion
  26. ZoneName string `json:"zoneName"`
  27. }
  28. func (zone *SZone) GetId() string {
  29. return zone.ZoneName
  30. }
  31. func (zone *SZone) GetGlobalId() string {
  32. return fmt.Sprintf("%s/%s", zone.region.GetGlobalId(), zone.ZoneName)
  33. }
  34. func (zone *SZone) GetName() string {
  35. return zone.ZoneName
  36. }
  37. func (zone *SZone) GetStatus() string {
  38. return api.ZONE_ENABLE
  39. }
  40. func (zone *SZone) IsEmulated() bool {
  41. return false
  42. }
  43. func (zone *SZone) Refresh() error {
  44. return nil
  45. }
  46. func (zone *SZone) GetIRegion() cloudprovider.ICloudRegion {
  47. return zone.region
  48. }
  49. func (zone *SZone) GetI18n() cloudprovider.SModelI18nTable {
  50. table := cloudprovider.SModelI18nTable{}
  51. return table
  52. }
  53. func (region *SRegion) GetZones() ([]SZone, error) {
  54. resp, err := region.bccList("v2/zone", nil)
  55. if err != nil {
  56. return nil, err
  57. }
  58. ret := []SZone{}
  59. err = resp.Unmarshal(&ret, "zones")
  60. if err != nil {
  61. return nil, err
  62. }
  63. for i := range ret {
  64. ret[i].region = region
  65. }
  66. return ret, nil
  67. }
  68. func (zone *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  69. storages, err := zone.region.GetStorageTypes(zone.ZoneName)
  70. if err != nil {
  71. return nil, err
  72. }
  73. ret := []cloudprovider.ICloudStorage{}
  74. for i := range storages {
  75. storages[i].zone = zone
  76. ret = append(ret, &storages[i])
  77. }
  78. return ret, nil
  79. }
  80. func (zone *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  81. storages, err := zone.GetIStorages()
  82. if err != nil {
  83. return nil, err
  84. }
  85. for i := range storages {
  86. if storages[i].GetGlobalId() == id {
  87. return storages[i], nil
  88. }
  89. }
  90. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  91. }
  92. func (zone *SZone) getHost() *SHost {
  93. return &SHost{zone: zone}
  94. }
  95. func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  96. return []cloudprovider.ICloudHost{zone.getHost()}, nil
  97. }
  98. func (zone *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  99. hosts, err := zone.GetIHosts()
  100. if err != nil {
  101. return nil, err
  102. }
  103. for i := range hosts {
  104. if hosts[i].GetGlobalId() == id {
  105. return hosts[i], nil
  106. }
  107. }
  108. return nil, cloudprovider.ErrNotFound
  109. }
  110. func (zone *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
  111. vpcs, err := zone.region.GetVpcs()
  112. if err != nil {
  113. return nil, err
  114. }
  115. ret := []cloudprovider.ICloudWire{}
  116. for i := range vpcs {
  117. wire := &SWire{zone: zone, vpc: &vpcs[i]}
  118. ret = append(ret, wire)
  119. }
  120. return ret, nil
  121. }