yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/zstack/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 zstack 16 17 import ( 18 "strconv" 19 20 "yunion.io/x/log" 21 "yunion.io/x/pkg/util/fileutils" 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 type SZone struct { 29 multicloud.SResourceBase 30 ZStackTags 31 region *SRegion 32 33 ZStackBasic 34 Type string 35 State string 36 cpuCmtbound float32 37 memCmtbound float32 38 reservedMemeoryMb int 39 40 iwires []cloudprovider.ICloudWire 41 istorages []cloudprovider.ICloudStorage 42 43 ihosts []cloudprovider.ICloudHost 44 } 45 46 func (zone *SZone) GetId() string { 47 return zone.Name 48 } 49 50 func (zone *SZone) GetName() string { 51 return zone.Name 52 } 53 54 func (zone *SZone) GetI18n() cloudprovider.SModelI18nTable { 55 table := cloudprovider.SModelI18nTable{} 56 table["name"] = cloudprovider.NewSModelI18nEntry(zone.GetName()).CN(zone.GetName()) 57 return table 58 } 59 60 func (zone *SZone) GetGlobalId() string { 61 return zone.GetId() 62 } 63 64 func (zone *SZone) IsEmulated() bool { 65 return false 66 } 67 68 func (zone *SZone) GetStatus() string { 69 if zone.State == "Enabled" { 70 return api.ZONE_ENABLE 71 } 72 return api.ZONE_DISABLE 73 } 74 75 func (zone *SZone) Refresh() error { 76 // do nothing 77 return nil 78 } 79 80 func (zone *SZone) GetIRegion() cloudprovider.ICloudRegion { 81 return zone.region 82 } 83 84 func (zone *SZone) fetchHostCmtbound() { 85 if zone.cpuCmtbound > 0 || zone.memCmtbound > 0 || zone.reservedMemeoryMb > 0 { 86 return 87 } 88 configurations, err := zone.region.GetConfigrations() 89 if err != nil { 90 log.Errorf("failed to get global configurations error: %v", err) 91 return 92 } 93 for _, config := range configurations { 94 if config.Name == "cpu.overProvisioning.ratio" && config.Category == "host" { 95 if cpuCmtbound, err := strconv.ParseFloat(config.Value, 32); err == nil { 96 zone.cpuCmtbound = float32(cpuCmtbound) 97 } 98 } 99 if config.Name == "reservedMemory" && config.Category == "kvm" { 100 zone.reservedMemeoryMb, _ = fileutils.GetSizeMb(config.Value, 'M', 1024) 101 } 102 if config.Name == "overProvisioning.memory" && config.Category == "mevoco" { 103 if memCmtbound, err := strconv.ParseFloat(config.Value, 32); err == nil { 104 zone.memCmtbound = float32(memCmtbound) 105 } 106 } 107 } 108 } 109 110 func (zone *SZone) fetchStorages(clusterId string) error { 111 storages, err := zone.region.getIStorages(zone.UUID) 112 if err != nil { 113 return err 114 } 115 zone.istorages = storages 116 return nil 117 } 118 119 func (zone *SZone) GetIStorages() ([]cloudprovider.ICloudStorage, error) { 120 if zone.istorages == nil || len(zone.istorages) == 0 { 121 return zone.istorages, zone.fetchStorages("") 122 } 123 return zone.istorages, nil 124 } 125 126 func (zone *SZone) GetIStorageById(storageId string) (cloudprovider.ICloudStorage, error) { 127 err := zone.fetchStorages("") 128 if err != nil { 129 return nil, err 130 } 131 for i := 0; i < len(zone.istorages); i++ { 132 if zone.istorages[i].GetGlobalId() == storageId { 133 return zone.istorages[i], nil 134 } 135 } 136 return nil, cloudprovider.ErrNotFound 137 } 138 139 func (zone *SZone) GetIHostById(hostId string) (cloudprovider.ICloudHost, error) { 140 host, err := zone.region.GetHost(hostId) 141 if err != nil { 142 return nil, err 143 } 144 if host.ZoneUUID != zone.UUID { 145 return nil, cloudprovider.ErrNotFound 146 } 147 host.zone = zone 148 return host, nil 149 } 150 151 func (zone *SZone) fetchHosts() error { 152 hosts, err := zone.region.GetHosts(zone.UUID, "") 153 if err != nil { 154 return err 155 } 156 zone.ihosts = []cloudprovider.ICloudHost{} 157 for i := 0; i < len(hosts); i++ { 158 hosts[i].zone = zone 159 zone.ihosts = append(zone.ihosts, &hosts[i]) 160 } 161 return nil 162 } 163 164 func (zone *SZone) GetIHosts() ([]cloudprovider.ICloudHost, error) { 165 if zone.ihosts == nil || len(zone.ihosts) == 0 { 166 return zone.ihosts, zone.fetchHosts() 167 } 168 return zone.ihosts, nil 169 } 170 171 func (zone *SZone) GetIWires() ([]cloudprovider.ICloudWire, error) { 172 if zone.iwires == nil || len(zone.iwires) == 0 { 173 wires, err := zone.region.GetWires(zone.UUID, "", "") 174 if err != nil { 175 return nil, err 176 } 177 zone.iwires = []cloudprovider.ICloudWire{} 178 for i := 0; i < len(wires); i++ { 179 zone.iwires = append(zone.iwires, &wires[i]) 180 } 181 } 182 return zone.iwires, nil 183 }