yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/cloudpods/host.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 cloudpods 16 17 import ( 18 "yunion.io/x/jsonutils" 19 "yunion.io/x/pkg/errors" 20 "yunion.io/x/pkg/tristate" 21 22 api "yunion.io/x/onecloud/pkg/apis/compute" 23 modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute" 24 25 "yunion.io/x/cloudmux/pkg/cloudprovider" 26 "yunion.io/x/cloudmux/pkg/multicloud" 27 ) 28 29 type SHost struct { 30 multicloud.SHostBase 31 zone *SZone 32 33 api.HostDetails 34 } 35 36 func (self *SHost) GetGlobalId() string { 37 return self.Id 38 } 39 40 func (self *SHost) GetId() string { 41 return self.Id 42 } 43 44 func (self *SHost) GetName() string { 45 return self.Name 46 } 47 48 func (self *SHost) GetStatus() string { 49 return self.Status 50 } 51 52 func (self *SHost) GetAccessIp() string { 53 return self.AccessIp 54 } 55 56 func (self *SHost) GetOvnVersion() string { 57 return self.OvnVersion 58 } 59 60 func (self *SHost) Refresh() error { 61 host, err := self.zone.region.GetHost(self.Id) 62 if err != nil { 63 return err 64 } 65 return jsonutils.Update(self, host) 66 } 67 68 func (self *SHost) GetIWires() ([]cloudprovider.ICloudWire, error) { 69 wires, err := self.zone.region.GetWires("", self.Id) 70 if err != nil { 71 return nil, err 72 } 73 ret := []cloudprovider.ICloudWire{} 74 for i := range wires { 75 vpc, _ := self.zone.region.GetVpc(wires[i].VpcId) 76 wires[i].vpc = vpc 77 ret = append(ret, &wires[i]) 78 } 79 return ret, nil 80 } 81 82 func (self *SHost) GetIStorages() ([]cloudprovider.ICloudStorage, error) { 83 storages, err := self.zone.region.GetStorages("", self.Id) 84 if err != nil { 85 return nil, err 86 } 87 ret := []cloudprovider.ICloudStorage{} 88 for i := range storages { 89 storages[i].region = self.zone.region 90 ret = append(ret, &storages[i]) 91 } 92 return ret, nil 93 } 94 95 func (self *SHost) GetIStorageById(id string) (cloudprovider.ICloudStorage, error) { 96 storage, err := self.zone.region.GetStorage(id) 97 if err != nil { 98 return nil, err 99 } 100 return storage, nil 101 } 102 103 func (self *SHost) GetEnabled() bool { 104 return self.Enabled != nil && *self.Enabled 105 } 106 107 func (self *SHost) GetHostStatus() string { 108 return self.HostStatus 109 } 110 111 func (self *SHost) GetAccessMac() string { 112 return self.AccessMac 113 } 114 115 func (self *SHost) GetSysInfo() jsonutils.JSONObject { 116 return jsonutils.Marshal(self.SysInfo) 117 } 118 119 func (self *SHost) GetSN() string { 120 return self.SN 121 } 122 123 func (self *SHost) GetCpuCount() int { 124 return self.CpuCount 125 } 126 127 func (self *SHost) GetNodeCount() int8 { 128 return int8(self.NodeCount) 129 } 130 131 func (self *SHost) GetCpuDesc() string { 132 return self.CpuDesc 133 } 134 135 func (self *SHost) GetCpuMhz() int { 136 return self.CpuMhz 137 } 138 139 func (self *SHost) GetCpuCmtbound() float32 { 140 return self.CpuCmtbound 141 } 142 143 func (self *SHost) GetMemSizeMB() int { 144 return self.MemSize 145 } 146 147 func (self *SHost) GetMemCmtbound() float32 { 148 return self.MemCmtbound 149 } 150 151 func (self *SHost) GetReservedMemoryMb() int { 152 return self.MemReserved 153 } 154 155 func (self *SHost) GetStorageSizeMB() int { 156 return self.StorageSize 157 } 158 159 func (self *SHost) GetStorageType() string { 160 return self.StorageType 161 } 162 163 func (self *SHost) GetHostType() string { 164 return api.HOST_TYPE_CLOUDPODS 165 } 166 167 func (self *SHost) GetIsMaintenance() bool { 168 return self.IsMaintenance 169 } 170 171 func (self *SHost) GetVersion() string { 172 return self.Version 173 } 174 175 func (self *SHost) GetSchedtags() ([]string, error) { 176 ret := []string{} 177 for _, tag := range self.Schedtags { 178 ret = append(ret, tag.Name) 179 } 180 return ret, nil 181 } 182 183 type SHostNic struct { 184 api.HostnetworkDetails 185 } 186 187 func (self *SHostNic) GetDriver() string { 188 return "" 189 } 190 191 func (self *SHostNic) GetDevice() string { 192 return "" 193 } 194 195 func (self *SHostNic) GetMac() string { 196 return self.MacAddr 197 } 198 199 func (self *SHostNic) GetIndex() int8 { 200 return int8(self.RowId) 201 } 202 203 func (self *SHostNic) IsLinkUp() tristate.TriState { 204 return tristate.True 205 } 206 207 func (self *SHostNic) GetMtu() int32 { 208 return 0 209 } 210 211 func (self *SHostNic) GetNicType() string { 212 return self.NicType 213 } 214 215 func (self *SHostNic) GetBridge() string { 216 return "" 217 } 218 219 func (self *SHostNic) GetIpAddr() string { 220 return self.IpAddr 221 } 222 223 func (self *SHost) GetIHostNics() ([]cloudprovider.ICloudHostNetInterface, error) { 224 nics := []SHostNic{} 225 params := map[string]interface{}{ 226 "scope": "system", 227 "details": "true", 228 } 229 resp, err := modules.Baremetalnetworks.ListDescendent(self.zone.region.cli.s, self.Id, jsonutils.Marshal(params)) 230 if err != nil { 231 return nil, err 232 } 233 err = jsonutils.Update(&nics, resp.Data) 234 if err != nil { 235 return nil, err 236 } 237 ret := []cloudprovider.ICloudHostNetInterface{} 238 for i := range nics { 239 ret = append(ret, &nics[i]) 240 } 241 return ret, nil 242 } 243 244 func (self *SHost) CreateVM(opts *cloudprovider.SManagedVMCreateConfig) (cloudprovider.ICloudVM, error) { 245 hypervisor := api.HOSTTYPE_HYPERVISOR[self.HostType] 246 ins, err := self.zone.region.CreateInstance(self.Id, hypervisor, opts) 247 if err != nil { 248 return nil, err 249 } 250 ins.host = self 251 return ins, nil 252 } 253 254 func (self *SRegion) GetHost(id string) (*SHost, error) { 255 host := &SHost{} 256 return host, self.cli.get(&modules.Hosts, id, nil, host) 257 } 258 259 func (self *SZone) GetIHostById(id string) (cloudprovider.ICloudHost, error) { 260 host, err := self.region.GetHost(id) 261 if err != nil { 262 return nil, err 263 } 264 host.zone = self 265 return host, nil 266 } 267 268 func (self *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) { 269 hosts, err := self.region.GetHosts(self.Id) 270 if err != nil { 271 return nil, errors.Wrapf(err, "GetHosts") 272 } 273 ret := []cloudprovider.ICloudHost{} 274 for i := range hosts { 275 hosts[i].zone = self 276 ret = append(ret, &hosts[i]) 277 } 278 return ret, nil 279 } 280 281 func (self *SRegion) GetHosts(zoneId string) ([]SHost, error) { 282 params := map[string]interface{}{ 283 "baremetal": false, 284 } 285 if len(zoneId) > 0 { 286 params["zone_id"] = zoneId 287 } 288 ret := []SHost{} 289 return ret, self.list(&modules.Hosts, params, &ret) 290 }