yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/loadbalancerbackendgroup.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 azure 16 17 import ( 18 "context" 19 "fmt" 20 "strconv" 21 "strings" 22 23 "github.com/pkg/errors" 24 25 "yunion.io/x/jsonutils" 26 27 api "yunion.io/x/cloudmux/pkg/apis/compute" 28 "yunion.io/x/cloudmux/pkg/cloudprovider" 29 "yunion.io/x/cloudmux/pkg/multicloud" 30 ) 31 32 // todo: 虚拟机规模集不支持 33 // 注: 因为与onecloud后端服务器组存在配置差异,不支持同步未关联的后端服务器组 34 // 应用型LB: HTTP 设置 + 后端池 = onecloud 后端服务器组 35 // 4层LB: loadBalancingRules(backendPort)+ 后端池 = onecloud 后端服务器组 36 type SLoadbalancerBackendGroup struct { 37 multicloud.SResourceBase 38 lb *SLoadbalancer 39 lbbs []cloudprovider.ICloudLoadbalancerBackend 40 41 Pool BackendAddressPool 42 DefaultPort int 43 HttpSettings *BackendHTTPSettingsCollection 44 45 BackendIps []BackendIPConfiguration 46 } 47 48 func (self *SLoadbalancerBackendGroup) GetId() string { 49 return self.Pool.ID + "::" + strconv.Itoa(self.DefaultPort) 50 } 51 52 func (self *SLoadbalancerBackendGroup) GetName() string { 53 if self.HttpSettings != nil { 54 return self.Pool.Name + "::" + self.HttpSettings.Name 55 } 56 57 return self.Pool.Name + "::" + strconv.Itoa(self.DefaultPort) 58 } 59 60 func (self *SLoadbalancerBackendGroup) GetGlobalId() string { 61 return strings.ToLower(self.GetId()) 62 } 63 64 func (self *SLoadbalancerBackendGroup) GetStatus() string { 65 switch self.Pool.Properties.ProvisioningState { 66 case "Succeeded": 67 return api.LB_STATUS_ENABLED 68 default: 69 return api.LB_STATUS_UNKNOWN 70 } 71 } 72 73 func (self *SLoadbalancerBackendGroup) Refresh() error { 74 lbbg, err := self.lb.GetILoadBalancerBackendGroupById(self.GetId()) 75 if err != nil { 76 return errors.Wrap(err, "GetILoadBalancerBackendGroupById") 77 } 78 79 err = jsonutils.Update(self, lbbg) 80 if err != nil { 81 return errors.Wrap(err, "refresh.Update") 82 } 83 84 self.lbbs = nil 85 return nil 86 } 87 88 func (self *SLoadbalancerBackendGroup) IsEmulated() bool { 89 return true 90 } 91 92 func (self *SLoadbalancerBackendGroup) GetSysTags() map[string]string { 93 return nil 94 } 95 96 func (self *SLoadbalancerBackendGroup) GetTags() (map[string]string, error) { 97 return map[string]string{}, nil 98 } 99 100 func (self *SLoadbalancerBackendGroup) SetTags(tags map[string]string, replace bool) error { 101 return errors.Wrap(cloudprovider.ErrNotImplemented, "SetTags") 102 } 103 104 func (self *SLoadbalancerBackendGroup) GetProjectId() string { 105 return getResourceGroup(self.GetId()) 106 } 107 108 func (self *SLoadbalancerBackendGroup) IsDefault() bool { 109 return false 110 } 111 112 func (self *SLoadbalancerBackendGroup) GetType() string { 113 return api.LB_BACKENDGROUP_TYPE_NORMAL 114 } 115 116 func (self *SLoadbalancerBackendGroup) GetLoadbalancerId() string { 117 return self.lb.GetId() 118 } 119 120 func (self *SLoadbalancerBackendGroup) GetILoadbalancerBackends() ([]cloudprovider.ICloudLoadbalancerBackend, error) { 121 if self.lbbs != nil { 122 return self.lbbs, nil 123 } 124 125 var ret []cloudprovider.ICloudLoadbalancerBackend 126 ips := self.Pool.Properties.BackendIPConfigurations 127 for i := range ips { 128 ip := ips[i] 129 nic, err := self.lb.region.GetNetworkInterface(strings.Split(ip.ID, "/ipConfigurations")[0]) 130 if err != nil { 131 return nil, errors.Wrap(err, "GetNetworkInterface") 132 } 133 134 if len(nic.Properties.VirtualMachine.ID) == 0 { 135 continue 136 } 137 138 name := nic.Properties.VirtualMachine.Name 139 vid := nic.Properties.VirtualMachine.ID 140 if len(name) == 0 && len(vid) > 0 { 141 segs := strings.Split(vid, "/virtualMachines/") 142 name = segs[len(segs)-1] 143 } 144 bg := SLoadbalancerBackend{ 145 SResourceBase: multicloud.SResourceBase{}, 146 lbbg: self, 147 Name: name, 148 ID: vid, 149 Type: api.LB_BACKEND_GUEST, 150 BackendPort: self.DefaultPort, 151 } 152 153 ret = append(ret, &bg) 154 } 155 156 ips2 := self.Pool.Properties.BackendAddresses 157 for i := range ips2 { 158 name := fmt.Sprintf("ip-%s", ips2[i].IPAddress) 159 bg := SLoadbalancerBackend{ 160 SResourceBase: multicloud.SResourceBase{}, 161 lbbg: self, 162 Name: name, 163 ID: fmt.Sprintf("%s-%s", self.GetId(), name), 164 Type: api.LB_BACKEND_IP, 165 BackendIP: ips2[i].IPAddress, 166 BackendPort: self.DefaultPort, 167 } 168 169 ret = append(ret, &bg) 170 } 171 172 self.lbbs = ret 173 return ret, nil 174 } 175 176 func (self *SLoadbalancerBackendGroup) GetILoadbalancerBackendById(backendId string) (cloudprovider.ICloudLoadbalancerBackend, error) { 177 lbbs, err := self.GetILoadbalancerBackends() 178 if err != nil { 179 return nil, errors.Wrap(err, "GetILoadbalancerBackends") 180 } 181 182 for i := range lbbs { 183 if lbbs[i].GetId() == backendId { 184 return lbbs[i], nil 185 } 186 } 187 188 return nil, errors.Wrap(cloudprovider.ErrNotFound, "GetILoadbalancerBackendById") 189 } 190 191 func (self *SLoadbalancerBackendGroup) GetProtocolType() string { 192 return "" 193 } 194 195 func (self *SLoadbalancerBackendGroup) GetScheduler() string { 196 return "" 197 } 198 199 func (self *SLoadbalancerBackendGroup) GetHealthCheck() (*cloudprovider.SLoadbalancerHealthCheck, error) { 200 return nil, nil 201 } 202 203 func (self *SLoadbalancerBackendGroup) GetStickySession() (*cloudprovider.SLoadbalancerStickySession, error) { 204 return nil, nil 205 } 206 207 func (self *SLoadbalancerBackendGroup) AddBackendServer(serverId string, weight int, port int) (cloudprovider.ICloudLoadbalancerBackend, error) { 208 return nil, errors.Wrap(cloudprovider.ErrNotImplemented, "AddBackendServer") 209 } 210 211 func (self *SLoadbalancerBackendGroup) RemoveBackendServer(serverId string, weight int, port int) error { 212 return errors.Wrap(cloudprovider.ErrNotImplemented, "RemoveBackendServer") 213 } 214 215 func (self *SLoadbalancerBackendGroup) Delete(ctx context.Context) error { 216 return errors.Wrap(cloudprovider.ErrNotImplemented, "Delete") 217 } 218 219 func (self *SLoadbalancerBackendGroup) Sync(ctx context.Context, group *cloudprovider.SLoadbalancerBackendGroup) error { 220 return errors.Wrap(cloudprovider.ErrNotImplemented, "Sync") 221 }