yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/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 aliyun 16 17 import ( 18 "context" 19 "fmt" 20 21 "yunion.io/x/jsonutils" 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 Rule struct { 29 RuleId string 30 RuleName string 31 Domain string 32 Url string 33 } 34 35 type Rules struct { 36 Rule []Rule 37 } 38 39 type Listener struct { 40 Protocol string 41 Port int 42 } 43 44 type Listeners struct { 45 Listener []Listener 46 } 47 48 type AssociatedObjects struct { 49 Rules Rules 50 Listeners Listeners 51 } 52 53 type SLoadbalancerBackendGroup struct { 54 multicloud.SResourceBase 55 AliyunTags 56 lb *SLoadbalancer 57 58 VServerGroupId string 59 VServerGroupName string 60 AssociatedObjects AssociatedObjects 61 } 62 63 func (backendgroup *SLoadbalancerBackendGroup) GetILoadbalancer() cloudprovider.ICloudLoadbalancer { 64 return backendgroup.lb 65 } 66 67 func (backendgroup *SLoadbalancerBackendGroup) GetLoadbalancerId() string { 68 return backendgroup.lb.GetId() 69 } 70 71 func (backendgroup *SLoadbalancerBackendGroup) GetProtocolType() string { 72 return "" 73 } 74 75 func (backendgroup *SLoadbalancerBackendGroup) GetScheduler() string { 76 return "" 77 } 78 79 func (backendgroup *SLoadbalancerBackendGroup) GetHealthCheck() (*cloudprovider.SLoadbalancerHealthCheck, error) { 80 return nil, nil 81 } 82 83 func (backendgroup *SLoadbalancerBackendGroup) GetStickySession() (*cloudprovider.SLoadbalancerStickySession, error) { 84 return nil, nil 85 } 86 87 func (backendgroup *SLoadbalancerBackendGroup) GetName() string { 88 return backendgroup.VServerGroupName 89 } 90 91 func (backendgroup *SLoadbalancerBackendGroup) GetId() string { 92 return backendgroup.VServerGroupId 93 } 94 95 func (backendgroup *SLoadbalancerBackendGroup) GetGlobalId() string { 96 return backendgroup.VServerGroupId 97 } 98 99 func (backendgroup *SLoadbalancerBackendGroup) GetStatus() string { 100 return api.LB_STATUS_ENABLED 101 } 102 103 func (backendgroup *SLoadbalancerBackendGroup) IsDefault() bool { 104 return false 105 } 106 107 func (backendgroup *SLoadbalancerBackendGroup) GetType() string { 108 return api.LB_BACKENDGROUP_TYPE_NORMAL 109 } 110 111 func (backendgroup *SLoadbalancerBackendGroup) IsEmulated() bool { 112 return false 113 } 114 115 func (backendgroup *SLoadbalancerBackendGroup) Refresh() error { 116 loadbalancerBackendgroups, err := backendgroup.lb.region.GetLoadbalancerBackendgroups(backendgroup.lb.LoadBalancerId) 117 if err != nil { 118 return err 119 } 120 for _, loadbalancerBackendgroup := range loadbalancerBackendgroups { 121 if loadbalancerBackendgroup.VServerGroupId == backendgroup.VServerGroupId { 122 return jsonutils.Update(backendgroup, loadbalancerBackendgroup) 123 } 124 } 125 return cloudprovider.ErrNotFound 126 } 127 128 func (region *SRegion) GetLoadbalancerBackendgroups(loadbalancerId string) ([]SLoadbalancerBackendGroup, error) { 129 params := map[string]string{} 130 params["RegionId"] = region.RegionId 131 params["LoadBalancerId"] = loadbalancerId 132 body, err := region.lbRequest("DescribeVServerGroups", params) 133 if err != nil { 134 return nil, err 135 } 136 backendgroups := []SLoadbalancerBackendGroup{} 137 return backendgroups, body.Unmarshal(&backendgroups, "VServerGroups", "VServerGroup") 138 } 139 140 func (backendgroup *SLoadbalancerBackendGroup) GetILoadbalancerBackends() ([]cloudprovider.ICloudLoadbalancerBackend, error) { 141 backends, err := backendgroup.lb.region.GetLoadbalancerBackends(backendgroup.VServerGroupId) 142 if err != nil { 143 return nil, err 144 } 145 ibackends := []cloudprovider.ICloudLoadbalancerBackend{} 146 for i := 0; i < len(backends); i++ { 147 backends[i].lbbg = backendgroup 148 ibackends = append(ibackends, &backends[i]) 149 } 150 return ibackends, nil 151 } 152 153 func (backendgroup *SLoadbalancerBackendGroup) GetILoadbalancerBackendById(backendId string) (cloudprovider.ICloudLoadbalancerBackend, error) { 154 backends, err := backendgroup.GetILoadbalancerBackends() 155 if err != nil { 156 return nil, err 157 } 158 for i := 0; i < len(backends); i++ { 159 if backends[i].GetGlobalId() == backendId { 160 return backends[i], nil 161 } 162 } 163 return nil, cloudprovider.ErrNotFound 164 } 165 166 func (region *SRegion) CreateLoadbalancerBackendGroup(name, loadbalancerId string, backends []cloudprovider.SLoadbalancerBackend) (*SLoadbalancerBackendGroup, error) { 167 params := map[string]string{} 168 params["RegionId"] = region.RegionId 169 params["VServerGroupName"] = name 170 params["LoadBalancerId"] = loadbalancerId 171 if len(backends) > 0 { 172 servers := jsonutils.NewArray() 173 for _, backend := range backends { 174 servers.Add( 175 jsonutils.Marshal( 176 map[string]string{ 177 "ServerId": backend.ExternalID, 178 "Port": fmt.Sprintf("%d", backend.Port), 179 "Weight": fmt.Sprintf("%d", backend.Weight), 180 }, 181 )) 182 } 183 params["BackendServers"] = servers.String() 184 } 185 body, err := region.lbRequest("CreateVServerGroup", params) 186 if err != nil { 187 return nil, err 188 } 189 groupId, err := body.GetString("VServerGroupId") 190 if err != nil { 191 return nil, err 192 } 193 return region.GetLoadbalancerBackendgroupById(groupId) 194 } 195 196 func (region *SRegion) GetLoadbalancerBackendgroupById(groupId string) (*SLoadbalancerBackendGroup, error) { 197 params := map[string]string{} 198 params["RegionId"] = region.RegionId 199 params["VServerGroupId"] = groupId 200 body, err := region.lbRequest("DescribeVServerGroupAttribute", params) 201 if err != nil { 202 return nil, err 203 } 204 group := &SLoadbalancerBackendGroup{} 205 return group, body.Unmarshal(group) 206 } 207 208 func (region *SRegion) UpdateLoadBalancerBackendGroupName(name, groupId string) error { 209 params := map[string]string{} 210 params["RegionId"] = region.RegionId 211 params["VServerGroupId"] = groupId 212 params["VServerGroupName"] = name 213 _, err := region.lbRequest("SetVServerGroupAttribute", params) 214 return err 215 } 216 217 func (backendgroup *SLoadbalancerBackendGroup) Sync(ctx context.Context, group *cloudprovider.SLoadbalancerBackendGroup) error { 218 if group == nil { 219 return nil 220 } 221 222 if backendgroup.VServerGroupName != group.Name { 223 return backendgroup.lb.region.UpdateLoadBalancerBackendGroupName(backendgroup.VServerGroupId, group.Name) 224 } 225 return nil 226 } 227 228 func (region *SRegion) DeleteLoadBalancerBackendGroup(groupId string) error { 229 params := map[string]string{} 230 params["RegionId"] = region.RegionId 231 params["VServerGroupId"] = groupId 232 _, err := region.lbRequest("DeleteVServerGroup", params) 233 return err 234 } 235 236 func (backendgroup *SLoadbalancerBackendGroup) Delete(ctx context.Context) error { 237 return backendgroup.lb.region.DeleteLoadBalancerBackendGroup(backendgroup.VServerGroupId) 238 } 239 240 func (region *SRegion) AddBackendVServer(loadbalancerId, backendGroupId, serverId string, weight, port int) error { 241 params := map[string]string{} 242 params["RegionId"] = region.RegionId 243 params["LoadBalancerId"] = loadbalancerId 244 params["VServerGroupId"] = backendGroupId 245 servers := jsonutils.NewArray() 246 servers.Add(jsonutils.Marshal(map[string]string{"ServerId": serverId, "Weight": fmt.Sprintf("%d", weight), "Port": fmt.Sprintf("%d", port)})) 247 params["BackendServers"] = servers.String() 248 _, err := region.lbRequest("AddVServerGroupBackendServers", params) 249 return err 250 } 251 252 func (region *SRegion) RemoveBackendVServer(loadbalancerId, backendgroupId, serverId string, port int) error { 253 params := map[string]string{} 254 params["RegionId"] = region.RegionId 255 params["LoadBalancerId"] = loadbalancerId 256 params["VServerGroupId"] = backendgroupId 257 servers := jsonutils.NewArray() 258 servers.Add(jsonutils.Marshal(map[string]string{"ServerId": serverId, "Port": fmt.Sprintf("%d", port)})) 259 params["BackendServers"] = servers.String() 260 _, err := region.lbRequest("RemoveVServerGroupBackendServers", params) 261 return err 262 } 263 264 func (backendgroup *SLoadbalancerBackendGroup) AddBackendServer(serverId string, weight, port int) (cloudprovider.ICloudLoadbalancerBackend, error) { 265 if err := backendgroup.lb.region.AddBackendVServer(backendgroup.lb.LoadBalancerId, backendgroup.VServerGroupId, serverId, weight, port); err != nil { 266 return nil, err 267 } 268 return &SLoadbalancerBackend{lbbg: backendgroup, ServerId: serverId, Weight: weight, Port: port}, nil 269 } 270 271 func (backendgroup *SLoadbalancerBackendGroup) RemoveBackendServer(serverId string, weight, port int) error { 272 return backendgroup.lb.region.RemoveBackendVServer(backendgroup.lb.LoadBalancerId, backendgroup.VServerGroupId, serverId, port) 273 } 274 275 func (backendgroup *SLoadbalancerBackendGroup) GetProjectId() string { 276 return backendgroup.lb.GetProjectId() 277 }