yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/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 huawei 16 17 import ( 18 "fmt" 19 20 "yunion.io/x/log" 21 "yunion.io/x/pkg/errors" 22 23 api "yunion.io/x/cloudmux/pkg/apis/compute" 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 "yunion.io/x/cloudmux/pkg/multicloud" 26 ) 27 28 var StorageTypes = []string{ 29 api.STORAGE_HUAWEI_SAS, 30 api.STORAGE_HUAWEI_SATA, 31 api.STORAGE_HUAWEI_SSD, 32 } 33 34 type ZoneState struct { 35 Available bool `json:"available"` 36 } 37 38 // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0065817728.html 39 type SZone struct { 40 multicloud.SResourceBase 41 HuaweiTags 42 region *SRegion 43 host *SHost 44 45 iwires []cloudprovider.ICloudWire 46 istorages []cloudprovider.ICloudStorage 47 48 ZoneState ZoneState `json:"zoneState"` 49 ZoneName string `json:"zoneName"` 50 51 /* 支持的磁盘种类集合 */ 52 storageTypes []string 53 } 54 55 func (self *SZone) addWire(wire *SWire) { 56 if self.iwires == nil { 57 self.iwires = make([]cloudprovider.ICloudWire, 0) 58 } 59 self.iwires = append(self.iwires, wire) 60 } 61 62 func (self *SZone) getStorageType() { 63 if len(self.storageTypes) == 0 { 64 if sts, err := self.region.GetZoneSupportedDiskTypes(self.GetId()); err == nil { 65 self.storageTypes = sts 66 } else { 67 log.Errorf("GetZoneSupportedDiskTypes %s %s", self.GetId(), err) 68 self.storageTypes = StorageTypes 69 } 70 } 71 } 72 73 func (self *SZone) fetchStorages() error { 74 self.getStorageType() 75 self.istorages = make([]cloudprovider.ICloudStorage, len(self.storageTypes)) 76 77 for i, sc := range self.storageTypes { 78 storage := SStorage{zone: self, storageType: sc} 79 self.istorages[i] = &storage 80 } 81 return nil 82 } 83 84 func (self *SZone) getHost() *SHost { 85 if self.host == nil { 86 self.host = &SHost{zone: self, projectId: self.region.client.projectId} 87 } 88 return self.host 89 } 90 91 func (self *SZone) GetId() string { 92 return self.ZoneName 93 } 94 95 func (self *SZone) GetName() string { 96 return fmt.Sprintf("%s %s", CLOUD_PROVIDER_HUAWEI_CN, self.ZoneName) 97 } 98 99 func (self *SZone) GetI18n() cloudprovider.SModelI18nTable { 100 en := fmt.Sprintf("%s %s", CLOUD_PROVIDER_HUAWEI_EN, self.ZoneName) 101 table := cloudprovider.SModelI18nTable{} 102 table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()).EN(en) 103 return table 104 } 105 106 func (self *SZone) GetGlobalId() string { 107 return fmt.Sprintf("%s/%s", self.region.GetGlobalId(), self.ZoneName) 108 } 109 110 func (self *SZone) GetStatus() string { 111 return "enable" 112 } 113 114 func (self *SZone) Refresh() error { 115 return nil 116 } 117 118 func (self *SZone) IsEmulated() bool { 119 return false 120 } 121 122 func (self *SZone) GetIRegion() cloudprovider.ICloudRegion { 123 return self.region 124 } 125 126 func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) { 127 return []cloudprovider.ICloudHost{self.getHost()}, nil 128 } 129 130 func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) { 131 host := self.getHost() 132 if host.GetGlobalId() == id { 133 return host, nil 134 } 135 return nil, cloudprovider.ErrNotFound 136 } 137 138 func (self *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) { 139 if self.istorages == nil { 140 err := self.fetchStorages() 141 if err != nil { 142 return nil, errors.Wrapf(err, "fetchStorages") 143 } 144 } 145 return self.istorages, nil 146 } 147 148 func (self *SZone) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) { 149 if self.istorages == nil { 150 err := self.fetchStorages() 151 if err != nil { 152 return nil, errors.Wrapf(err, "fetchStorages") 153 } 154 } 155 for i := 0; i < len(self.istorages); i += 1 { 156 if self.istorages[i].GetGlobalId() == id { 157 return self.istorages[i], nil 158 } 159 } 160 return nil, cloudprovider.ErrNotFound 161 } 162 163 func (self *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) { 164 return self.iwires, nil 165 } 166 167 func (self *SZone) getStorageByCategory(category string) (*SStorage, error) { 168 storages, err := self.GetIStorages() 169 if err != nil { 170 return nil, err 171 } 172 for i := 0; i < len(storages); i += 1 { 173 storage := storages[i].(*SStorage) 174 if storage.storageType == category { 175 return storage, nil 176 } 177 } 178 return nil, fmt.Errorf("No such storage %s", category) 179 } 180 181 func (self *SRegion) getZoneById(id string) (*SZone, error) { 182 izones, err := self.GetIZones() 183 if err != nil { 184 return nil, err 185 } 186 for i := 0; i < len(izones); i += 1 { 187 zone := izones[i].(*SZone) 188 if zone.GetId() == id { 189 return zone, nil 190 } 191 } 192 return nil, fmt.Errorf("no such zone %s", id) 193 } 194 195 func (self *SZone) getNetworkById(networkId string) *SNetwork { 196 for i := 0; i < len(self.iwires); i += 1 { 197 wire := self.iwires[i].(*SWire) 198 net := wire.getNetworkById(networkId) 199 if net != nil { 200 return net 201 } 202 } 203 return nil 204 }