yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/nutanix/zone.go (about) 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 15 package nutanix 16 17 import ( 18 "yunion.io/x/pkg/errors" 19 20 api "yunion.io/x/cloudmux/pkg/apis/compute" 21 "yunion.io/x/cloudmux/pkg/cloudprovider" 22 "yunion.io/x/cloudmux/pkg/multicloud" 23 ) 24 25 type SZone struct { 26 multicloud.STagBase 27 multicloud.SResourceBase 28 29 SCluster 30 region *SRegion 31 } 32 33 func (self *SZone) GetName() string { 34 return self.Name 35 } 36 37 func (self *SZone) GetId() string { 38 return self.UUID 39 } 40 41 func (self *SZone) GetGlobalId() string { 42 return self.UUID 43 } 44 45 func (self *SZone) GetI18n() cloudprovider.SModelI18nTable { 46 table := cloudprovider.SModelI18nTable{} 47 table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()) 48 return table 49 } 50 51 func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) { 52 hosts, err := self.region.GetHosts() 53 if err != nil { 54 return nil, errors.Wrapf(err, "GetIHosts") 55 } 56 firstHost := true 57 ret := []cloudprovider.ICloudHost{} 58 for i := range hosts { 59 if hosts[i].ClusterUUID != self.UUID { 60 continue 61 } 62 hosts[i].zone = self 63 hosts[i].firstHost = firstHost 64 ret = append(ret, &hosts[i]) 65 if firstHost { 66 firstHost = false 67 } 68 } 69 return ret, nil 70 } 71 72 func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) { 73 host, err := self.region.GetHost(id) 74 if err != nil { 75 return nil, errors.Wrapf(err, "GetIHostById(%s)", id) 76 } 77 if host.ClusterUUID != self.UUID { 78 return nil, errors.Wrapf(cloudprovider.ErrNotFound, id) 79 } 80 host.zone = self 81 return host, nil 82 } 83 84 func (self *SZone) GetIRegion() cloudprovider.ICloudRegion { 85 return self.region 86 } 87 88 func (self *SZone) GetStatus() string { 89 return api.ZONE_ENABLE 90 } 91 92 func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) { 93 storages, err := self.region.GetStorages() 94 if err != nil { 95 return nil, errors.Wrapf(err, "GetStorages") 96 } 97 ret := []cloudprovider.ICloudStorage{} 98 for i := range storages { 99 if storages[i].ClusterUUID != self.UUID { 100 continue 101 } 102 storages[i].zone = self 103 ret = append(ret, &storages[i]) 104 } 105 return ret, nil 106 } 107 108 func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) { 109 storage, err := self.region.GetStorage(id) 110 if err != nil { 111 return nil, errors.Wrapf(err, "GetStorage %s", id) 112 } 113 if storage.ClusterUUID != self.UUID { 114 return nil, errors.Wrapf(cloudprovider.ErrNotFound, id) 115 } 116 storage.zone = self 117 return storage, nil 118 }