zone.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 nutanix
  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.STagBase
  23. multicloud.SResourceBase
  24. SCluster
  25. region *SRegion
  26. }
  27. func (self *SZone) GetName() string {
  28. return self.Name
  29. }
  30. func (self *SZone) GetId() string {
  31. return self.UUID
  32. }
  33. func (self *SZone) GetGlobalId() string {
  34. return self.UUID
  35. }
  36. func (self *SZone) GetI18n() cloudprovider.SModelI18nTable {
  37. table := cloudprovider.SModelI18nTable{}
  38. table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName())
  39. return table
  40. }
  41. func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  42. hosts, err := self.region.GetHosts()
  43. if err != nil {
  44. return nil, errors.Wrapf(err, "GetIHosts")
  45. }
  46. firstHost := true
  47. ret := []cloudprovider.ICloudHost{}
  48. for i := range hosts {
  49. if hosts[i].ClusterUUID != self.UUID {
  50. continue
  51. }
  52. hosts[i].zone = self
  53. hosts[i].firstHost = firstHost
  54. ret = append(ret, &hosts[i])
  55. if firstHost {
  56. firstHost = false
  57. }
  58. }
  59. return ret, nil
  60. }
  61. func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  62. host, err := self.region.GetHost(id)
  63. if err != nil {
  64. return nil, errors.Wrapf(err, "GetIHostById(%s)", id)
  65. }
  66. if host.ClusterUUID != self.UUID {
  67. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  68. }
  69. host.zone = self
  70. return host, nil
  71. }
  72. func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
  73. return self.region
  74. }
  75. func (self *SZone) GetStatus() string {
  76. return api.ZONE_ENABLE
  77. }
  78. func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  79. storages, err := self.region.GetStorages()
  80. if err != nil {
  81. return nil, errors.Wrapf(err, "GetStorages")
  82. }
  83. ret := []cloudprovider.ICloudStorage{}
  84. for i := range storages {
  85. if storages[i].ClusterUUID != self.UUID {
  86. continue
  87. }
  88. storages[i].zone = self
  89. ret = append(ret, &storages[i])
  90. }
  91. return ret, nil
  92. }
  93. func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  94. storage, err := self.region.GetStorage(id)
  95. if err != nil {
  96. return nil, errors.Wrapf(err, "GetStorage %s", id)
  97. }
  98. if storage.ClusterUUID != self.UUID {
  99. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  100. }
  101. storage.zone = self
  102. return storage, nil
  103. }