zone.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 remotefile
  15. import (
  16. "yunion.io/x/pkg/errors"
  17. "yunion.io/x/cloudmux/pkg/cloudprovider"
  18. )
  19. type SZone struct {
  20. SResourceBase
  21. region *SRegion
  22. RegionId string
  23. }
  24. func (self *SZone) GetIRegion() cloudprovider.ICloudRegion {
  25. return self.region
  26. }
  27. func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) {
  28. hosts, err := self.region.client.GetHosts()
  29. if err != nil {
  30. return nil, err
  31. }
  32. ret := []cloudprovider.ICloudHost{}
  33. for i := range hosts {
  34. if hosts[i].ZoneId != self.GetId() {
  35. continue
  36. }
  37. hosts[i].zone = self
  38. ret = append(ret, &hosts[i])
  39. }
  40. return ret, nil
  41. }
  42. func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) {
  43. hosts, err := self.GetIHosts()
  44. if err != nil {
  45. return nil, err
  46. }
  47. for i := range hosts {
  48. if hosts[i].GetGlobalId() == id {
  49. return hosts[i], nil
  50. }
  51. }
  52. return nil, errors.Wrapf(cloudprovider.ErrNotFound, "%s", id)
  53. }
  54. func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) {
  55. storages, err := self.region.client.GetStorages()
  56. if err != nil {
  57. return nil, err
  58. }
  59. ret := []cloudprovider.ICloudStorage{}
  60. for i := range storages {
  61. if storages[i].ZoneId != self.GetId() {
  62. continue
  63. }
  64. storages[i].zone = self
  65. ret = append(ret, &storages[i])
  66. }
  67. return ret, nil
  68. }
  69. func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) {
  70. storages, err := self.GetIStorages()
  71. if err != nil {
  72. return nil, err
  73. }
  74. for i := range storages {
  75. if storages[i].GetGlobalId() == id {
  76. return storages[i], nil
  77. }
  78. }
  79. return nil, cloudprovider.ErrNotFound
  80. }