yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/loadbalancermasterslavebackendgroup.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 SLoadbalancerMasterSlaveBackendGroup struct { 29 multicloud.SResourceBase 30 AliyunTags 31 lb *SLoadbalancer 32 33 MasterSlaveServerGroupId string 34 MasterSlaveServerGroupName string 35 } 36 37 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetLoadbalancerId() string { 38 return backendgroup.lb.GetId() 39 } 40 41 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetProtocolType() string { 42 return "" 43 } 44 45 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetScheduler() string { 46 return "" 47 } 48 49 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetHealthCheck() (*cloudprovider.SLoadbalancerHealthCheck, error) { 50 return nil, nil 51 } 52 53 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetStickySession() (*cloudprovider.SLoadbalancerStickySession, error) { 54 return nil, nil 55 } 56 57 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetName() string { 58 return backendgroup.MasterSlaveServerGroupName 59 } 60 61 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetId() string { 62 return backendgroup.MasterSlaveServerGroupId 63 } 64 65 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetGlobalId() string { 66 return backendgroup.GetId() 67 } 68 69 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetStatus() string { 70 return api.LB_STATUS_ENABLED 71 } 72 73 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) IsEmulated() bool { 74 return false 75 } 76 77 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) Refresh() error { 78 return nil 79 } 80 81 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) IsDefault() bool { 82 return false 83 } 84 85 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetType() string { 86 return api.LB_BACKENDGROUP_TYPE_MASTER_SLAVE 87 } 88 89 func (region *SRegion) GetLoadbalancerMasterSlaveBackendgroups(loadbalancerId string) ([]SLoadbalancerMasterSlaveBackendGroup, error) { 90 params := map[string]string{} 91 params["RegionId"] = region.RegionId 92 params["LoadBalancerId"] = loadbalancerId 93 body, err := region.lbRequest("DescribeMasterSlaveServerGroups", params) 94 if err != nil { 95 return nil, err 96 } 97 backendgroups := []SLoadbalancerMasterSlaveBackendGroup{} 98 return backendgroups, body.Unmarshal(&backendgroups, "MasterSlaveServerGroups", "MasterSlaveServerGroup") 99 } 100 101 func (region *SRegion) GetLoadbalancerMasterSlaveBackends(backendgroupId string) ([]SLoadbalancerMasterSlaveBackend, error) { 102 params := map[string]string{} 103 params["RegionId"] = region.RegionId 104 params["MasterSlaveServerGroupId"] = backendgroupId 105 body, err := region.lbRequest("DescribeMasterSlaveServerGroupAttribute", params) 106 if err != nil { 107 return nil, err 108 } 109 backends := []SLoadbalancerMasterSlaveBackend{} 110 return backends, body.Unmarshal(&backends, "MasterSlaveBackendServers", "MasterSlaveBackendServer") 111 } 112 113 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetILoadbalancerBackends() ([]cloudprovider.ICloudLoadbalancerBackend, error) { 114 backends, err := backendgroup.lb.region.GetLoadbalancerMasterSlaveBackends(backendgroup.MasterSlaveServerGroupId) 115 if err != nil { 116 return nil, err 117 } 118 ibackends := []cloudprovider.ICloudLoadbalancerBackend{} 119 for i := 0; i < len(backends); i++ { 120 backends[i].lbbg = backendgroup 121 ibackends = append(ibackends, &backends[i]) 122 } 123 return ibackends, nil 124 } 125 126 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetILoadbalancerBackendById(backendId string) (cloudprovider.ICloudLoadbalancerBackend, error) { 127 backends, err := backendgroup.GetILoadbalancerBackends() 128 if err != nil { 129 return nil, err 130 } 131 for i := 0; i < len(backends); i++ { 132 if backends[i].GetGlobalId() == backendId { 133 return backends[i], nil 134 } 135 } 136 return nil, cloudprovider.ErrNotFound 137 } 138 139 func (region *SRegion) CreateLoadbalancerMasterSlaveBackendGroup(name, loadbalancerId string, backends []cloudprovider.SLoadbalancerBackend) (*SLoadbalancerMasterSlaveBackendGroup, error) { 140 params := map[string]string{} 141 params["RegionId"] = region.RegionId 142 params["MasterSlaveServerGroupName"] = name 143 params["LoadBalancerId"] = loadbalancerId 144 if len(backends) != 2 { 145 return nil, fmt.Errorf("master slave backendgorup must contain two backend") 146 } 147 servers := jsonutils.NewArray() 148 for _, backend := range backends { 149 serverType := "Slave" 150 if backend.Index == 0 { 151 serverType = "Master" 152 } 153 servers.Add( 154 jsonutils.Marshal( 155 map[string]string{ 156 "ServerId": backend.ExternalID, 157 "Port": fmt.Sprintf("%d", backend.Port), 158 "Weight": fmt.Sprintf("%d", backend.Weight), 159 "ServerType": serverType, 160 }, 161 )) 162 } 163 params["MasterSlaveBackendServers"] = servers.String() 164 body, err := region.lbRequest("CreateMasterSlaveServerGroup", params) 165 if err != nil { 166 return nil, err 167 } 168 groupId, err := body.GetString("MasterSlaveServerGroupId") 169 if err != nil { 170 return nil, err 171 } 172 return region.GetLoadbalancerMasterSlaveBackendgroupById(groupId) 173 } 174 175 func (region *SRegion) GetLoadbalancerMasterSlaveBackendgroupById(groupId string) (*SLoadbalancerMasterSlaveBackendGroup, error) { 176 params := map[string]string{} 177 params["RegionId"] = region.RegionId 178 params["MasterSlaveServerGroupId"] = groupId 179 params["NeedInstanceDetail"] = "true" 180 body, err := region.lbRequest("DescribeMasterSlaveServerGroupAttribute", params) 181 if err != nil { 182 return nil, err 183 } 184 group := &SLoadbalancerMasterSlaveBackendGroup{} 185 return group, body.Unmarshal(group) 186 } 187 188 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) Sync(ctx context.Context, group *cloudprovider.SLoadbalancerBackendGroup) error { 189 return nil 190 } 191 192 func (region *SRegion) DeleteLoadbalancerMasterSlaveBackendgroup(groupId string) error { 193 params := map[string]string{} 194 params["RegionId"] = region.RegionId 195 params["MasterSlaveServerGroupId"] = groupId 196 _, err := region.lbRequest("DeleteMasterSlaveServerGroup", params) 197 return err 198 } 199 200 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) Delete(ctx context.Context) error { 201 return backendgroup.lb.region.DeleteLoadbalancerMasterSlaveBackendgroup(backendgroup.MasterSlaveServerGroupId) 202 } 203 204 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) AddBackendServer(serverId string, weight, port int) (cloudprovider.ICloudLoadbalancerBackend, error) { 205 return nil, cloudprovider.ErrNotSupported 206 } 207 208 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) RemoveBackendServer(serverId string, weight, port int) error { 209 return cloudprovider.ErrNotSupported 210 } 211 212 func (backendgroup *SLoadbalancerMasterSlaveBackendGroup) GetProjectId() string { 213 return backendgroup.lb.GetProjectId() 214 }