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