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