yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/loadbalancerdefaultbackend.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/multicloud"
    25  )
    26  
    27  type SLoadbalancerDefaultBackend struct {
    28  	multicloud.SResourceBase
    29  	AliyunTags
    30  	lbbg *SLoadbalancerDefaultBackendGroup
    31  
    32  	ServerId string
    33  	Weight   int
    34  }
    35  
    36  func (backend *SLoadbalancerDefaultBackend) GetName() string {
    37  	return backend.ServerId
    38  }
    39  
    40  func (backend *SLoadbalancerDefaultBackend) GetId() string {
    41  	return fmt.Sprintf("%s/%s", backend.lbbg.lb.LoadBalancerId, backend.ServerId)
    42  }
    43  
    44  func (backend *SLoadbalancerDefaultBackend) GetGlobalId() string {
    45  	return backend.GetId()
    46  }
    47  
    48  func (backend *SLoadbalancerDefaultBackend) GetStatus() string {
    49  	return api.LB_STATUS_ENABLED
    50  }
    51  
    52  func (backend *SLoadbalancerDefaultBackend) IsEmulated() bool {
    53  	return false
    54  }
    55  
    56  func (backend *SLoadbalancerDefaultBackend) Refresh() error {
    57  	return nil
    58  }
    59  
    60  func (backend *SLoadbalancerDefaultBackend) GetWeight() int {
    61  	return backend.Weight
    62  }
    63  
    64  func (backend *SLoadbalancerDefaultBackend) GetPort() int {
    65  	return 0
    66  }
    67  
    68  func (backend *SLoadbalancerDefaultBackend) GetBackendType() string {
    69  	return api.LB_BACKEND_GUEST
    70  }
    71  
    72  func (backend *SLoadbalancerDefaultBackend) GetBackendRole() string {
    73  	return api.LB_BACKEND_ROLE_DEFAULT
    74  }
    75  
    76  func (backend *SLoadbalancerDefaultBackend) GetBackendId() string {
    77  	return backend.ServerId
    78  }
    79  
    80  func (backend *SLoadbalancerDefaultBackend) GetIpAddress() string {
    81  	return ""
    82  }
    83  
    84  func (backend *SLoadbalancerDefaultBackend) GetProjectId() string {
    85  	return backend.lbbg.GetProjectId()
    86  }
    87  
    88  func (backend *SLoadbalancerDefaultBackend) SyncConf(ctx context.Context, port, weight int) error {
    89  	params := map[string]string{}
    90  	params["RegionId"] = backend.lbbg.lb.region.RegionId
    91  	params["LoadBalancerId"] = backend.lbbg.lb.LoadBalancerId
    92  	loadbalancer, err := backend.lbbg.lb.region.GetLoadbalancerDetail(backend.lbbg.lb.LoadBalancerId)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	servers := jsonutils.NewArray()
    97  	for i := 0; i < len(loadbalancer.BackendServers.BackendServer); i++ {
    98  		_backend := loadbalancer.BackendServers.BackendServer[i]
    99  		_backend.lbbg = backend.lbbg
   100  		if _backend.GetGlobalId() == backend.GetGlobalId() {
   101  			_backend.Weight = weight
   102  		}
   103  		servers.Add(
   104  			jsonutils.Marshal(
   105  				map[string]string{
   106  					"ServerId": _backend.ServerId,
   107  					"Weight":   fmt.Sprintf("%d", _backend.Weight),
   108  				},
   109  			))
   110  	}
   111  
   112  	params["BackendServers"] = servers.String()
   113  	_, err = backend.lbbg.lb.region.lbRequest("SetBackendServers", params)
   114  	return err
   115  }