zone.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 azure
  15. import (
  16. "yunion.io/x/pkg/errors"
  17. api "yunion.io/x/cloudmux/pkg/apis/compute"
  18. "yunion.io/x/cloudmux/pkg/cloudprovider"
  19. "yunion.io/x/cloudmux/pkg/multicloud"
  20. )
  21. type SZone struct {
  22. multicloud.SResourceBase
  23. AzureTags
  24. region *SRegion
  25. Name string
  26. }
  27. func (self *SZone) GetId() string {
  28. return self.region.client.cpcfg.Id
  29. }
  30. func (self *SZone) GetName() string {
  31. return self.region.GetName()
  32. }
  33. func (self *SZone) GetI18n() cloudprovider.SModelI18nTable {
  34. return self.region.GetI18n()
  35. }
  36. func (self *SZone) GetGlobalId() string {
  37. return self.region.GetGlobalId()
  38. }
  39. func (self *SZone) IsEmulated() bool {
  40. return true
  41. }
  42. func (self *SZone) GetStatus() string {
  43. return api.ZONE_ENABLE
  44. }
  45. func (self *SZone) Refresh() error {
  46. // do nothing
  47. return nil
  48. }
  49. func (self *SZone) getHost() *SHost {
  50. return &SHost{zone: self}
  51. }
  52. func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
  53. return self.region
  54. }
  55. func (self *SZone) ListStorageTypes() []SStorage {
  56. storages := []SStorage{}
  57. for _, storageType := range STORAGETYPES {
  58. storage := SStorage{zone: self, storageType: storageType}
  59. storages = append(storages, storage)
  60. }
  61. return storages
  62. }
  63. func (self *SZone) getIStorages() []cloudprovider.ICloudStorage {
  64. ret := []cloudprovider.ICloudStorage{}
  65. storages := self.ListStorageTypes()
  66. for i := range storages {
  67. storages[i].zone = self
  68. ret = append(ret, &storages[i])
  69. }
  70. return ret
  71. }
  72. func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  73. return self.getIStorages(), nil
  74. }
  75. func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  76. storages, err := self.GetIStorages()
  77. if err != nil {
  78. return nil, errors.Wrapf(err, "GetIStorages")
  79. }
  80. for i := range storages {
  81. if storages[i].GetGlobalId() == id {
  82. return storages[i], nil
  83. }
  84. }
  85. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  86. }
  87. func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  88. hosts, err := self.GetIHosts()
  89. if err != nil {
  90. return nil, errors.Wrapf(err, "GetIHosts")
  91. }
  92. for i := range hosts {
  93. if hosts[i].GetGlobalId() == id {
  94. return hosts[i], nil
  95. }
  96. }
  97. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  98. }
  99. func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  100. return []cloudprovider.ICloudHost{self.getHost()}, nil
  101. }
  102. func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) {
  103. vpcs, err := self.region.ListVpcs()
  104. if err != nil {
  105. return nil, errors.Wrapf(err, "ListVpcs")
  106. }
  107. wires := []cloudprovider.ICloudWire{}
  108. for i := range vpcs {
  109. vpcs[i].region = self.region
  110. wire := &SWire{vpc: &vpcs[i], zone: self}
  111. wires = append(wires, wire)
  112. }
  113. return wires, nil
  114. }