yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/loadbalancerdefaultbackendgroup.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 apsara 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 SLoadbalancerDefaultBackendGroup struct { 29 multicloud.SResourceBase 30 ApsaraTags 31 lb *SLoadbalancer 32 DepartmentInfo 33 } 34 35 func (backendgroup *SLoadbalancerDefaultBackendGroup) GetILoadbalancer() cloudprovider.ICloudLoadbalancer { 36 return backendgroup.lb 37 } 38 39 func (backendgroup *SLoadbalancerDefaultBackendGroup) GetLoadbalancerId() string { 40 return backendgroup.lb.GetId() 41 } 42 43 func (backendgroup *SLoadbalancerDefaultBackendGroup) GetProtocolType() string { 44 return "" 45 } 46 47 func (backendgroup *SLoadbalancerDefaultBackendGroup) GetScheduler() string { 48 return "" 49 } 50 51 func (backendgroup *SLoadbalancerDefaultBackendGroup) GetHealthCheck() (*cloudprovider.SLoadbalancerHealthCheck, error) { 52 return nil, nil 53 } 54 55 func (backendgroup *SLoadbalancerDefaultBackendGroup) GetStickySession() (*cloudprovider.SLoadbalancerStickySession, error) { 56 return nil, nil 57 } 58 59 func (backendgroup *SLoadbalancerDefaultBackendGroup) GetName() string { 60 return fmt.Sprintf("%s(%s)-default", backendgroup.lb.LoadBalancerName, backendgroup.lb.Address) 61 } 62 63 func (backendgroup *SLoadbalancerDefaultBackendGroup) GetId() string { 64 return fmt.Sprintf("%s/default", backendgroup.lb.LoadBalancerId) 65 } 66 67 func (backendgroup *SLoadbalancerDefaultBackendGroup) GetGlobalId() string { 68 return backendgroup.GetId() 69 } 70 71 func (backendgroup *SLoadbalancerDefaultBackendGroup) GetStatus() string { 72 return api.LB_STATUS_ENABLED 73 } 74 75 func (backendgroup *SLoadbalancerDefaultBackendGroup) IsDefault() bool { 76 return true 77 } 78 79 func (backendgroup *SLoadbalancerDefaultBackendGroup) GetType() string { 80 return api.LB_BACKENDGROUP_TYPE_DEFAULT 81 } 82 83 func (backendgroup *SLoadbalancerDefaultBackendGroup) IsEmulated() bool { 84 return false 85 } 86 87 func (backendgroup *SLoadbalancerDefaultBackendGroup) Refresh() error { 88 return nil 89 } 90 91 func (backendgroup *SLoadbalancerDefaultBackendGroup) GetILoadbalancerBackends() ([]cloudprovider.ICloudLoadbalancerBackend, error) { 92 loadbalancer, err := backendgroup.lb.region.GetLoadbalancerDetail(backendgroup.lb.LoadBalancerId) 93 if err != nil { 94 return nil, err 95 } 96 ibackends := []cloudprovider.ICloudLoadbalancerBackend{} 97 for i := 0; i < len(loadbalancer.BackendServers.BackendServer); i++ { 98 loadbalancer.BackendServers.BackendServer[i].lbbg = backendgroup 99 ibackends = append(ibackends, &loadbalancer.BackendServers.BackendServer[i]) 100 } 101 return ibackends, nil 102 } 103 104 func (backendgroup *SLoadbalancerDefaultBackendGroup) GetILoadbalancerBackendById(backendId string) (cloudprovider.ICloudLoadbalancerBackend, error) { 105 backends, err := backendgroup.GetILoadbalancerBackends() 106 if err != nil { 107 return nil, err 108 } 109 for i := 0; i < len(backends); i++ { 110 if backends[i].GetGlobalId() == backendId { 111 return backends[i], nil 112 } 113 } 114 return nil, cloudprovider.ErrNotFound 115 } 116 117 func (backendgroup *SLoadbalancerDefaultBackendGroup) Sync(ctx context.Context, group *cloudprovider.SLoadbalancerBackendGroup) error { 118 return cloudprovider.ErrNotSupported 119 } 120 121 func (backendgroup *SLoadbalancerDefaultBackendGroup) Delete(ctx context.Context) error { 122 return cloudprovider.ErrNotSupported 123 } 124 125 func (region *SRegion) AddBackendServer(loadbalancerId, serverId string, weight, port int) error { 126 params := map[string]string{} 127 params["RegionId"] = region.RegionId 128 params["LoadBalancerId"] = loadbalancerId 129 servers := jsonutils.NewArray() 130 servers.Add(jsonutils.Marshal(map[string]string{"ServerId": serverId, "Weight": fmt.Sprintf("%d", weight)})) 131 params["BackendServers"] = servers.String() 132 _, err := region.lbRequest("AddBackendServers", params) 133 return err 134 } 135 136 func (backendgroup *SLoadbalancerDefaultBackendGroup) AddBackendServer(serverId string, weight, port int) (cloudprovider.ICloudLoadbalancerBackend, error) { 137 if err := backendgroup.lb.region.AddBackendServer(backendgroup.lb.LoadBalancerId, serverId, weight, port); err != nil { 138 return nil, err 139 } 140 return &SLoadbalancerDefaultBackend{lbbg: backendgroup, ServerId: serverId, Weight: weight}, nil 141 } 142 143 func (region *SRegion) RemoveBackendServer(loadbalancerId, serverId string) error { 144 params := map[string]string{} 145 params["RegionId"] = region.RegionId 146 params["LoadBalancerId"] = loadbalancerId 147 servers := jsonutils.NewArray() 148 servers.Add(jsonutils.NewString(serverId)) 149 params["BackendServers"] = servers.String() 150 _, err := region.lbRequest("RemoveBackendServers", params) 151 return err 152 } 153 154 func (backendgroup *SLoadbalancerDefaultBackendGroup) RemoveBackendServer(serverId string, weight, port int) error { 155 return backendgroup.lb.region.RemoveBackendServer(backendgroup.lb.LoadBalancerId, serverId) 156 }