yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/loadbalancerbackend.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 SLoadbalancerBackend struct {
    29  	multicloud.SResourceBase
    30  	ApsaraTags
    31  	lbbg *SLoadbalancerBackendGroup
    32  
    33  	ServerId string
    34  	Port     int
    35  	Weight   int
    36  	DepartmentInfo
    37  }
    38  
    39  func (backend *SLoadbalancerBackend) GetName() string {
    40  	return backend.ServerId
    41  }
    42  
    43  func (backend *SLoadbalancerBackend) GetId() string {
    44  	return fmt.Sprintf("%s/%s", backend.lbbg.VServerGroupId, backend.ServerId)
    45  }
    46  
    47  func (backend *SLoadbalancerBackend) GetGlobalId() string {
    48  	return backend.GetId()
    49  }
    50  
    51  func (backend *SLoadbalancerBackend) GetStatus() string {
    52  	return api.LB_STATUS_ENABLED
    53  }
    54  
    55  func (backend *SLoadbalancerBackend) IsEmulated() bool {
    56  	return false
    57  }
    58  
    59  func (backend *SLoadbalancerBackend) Refresh() error {
    60  	loadbalancerBackends, err := backend.lbbg.lb.region.GetLoadbalancerBackends(backend.lbbg.VServerGroupId)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	for _, loadbalancerBackend := range loadbalancerBackends {
    65  		if loadbalancerBackend.ServerId == backend.ServerId {
    66  			return jsonutils.Update(backend, loadbalancerBackend)
    67  		}
    68  	}
    69  	return cloudprovider.ErrNotFound
    70  }
    71  
    72  func (backend *SLoadbalancerBackend) GetWeight() int {
    73  	return backend.Weight
    74  }
    75  
    76  func (backend *SLoadbalancerBackend) GetPort() int {
    77  	return backend.Port
    78  }
    79  
    80  func (backend *SLoadbalancerBackend) GetBackendType() string {
    81  	return api.LB_BACKEND_GUEST
    82  }
    83  
    84  func (backend *SLoadbalancerBackend) GetBackendRole() string {
    85  	return api.LB_BACKEND_ROLE_DEFAULT
    86  }
    87  
    88  func (backend *SLoadbalancerBackend) GetBackendId() string {
    89  	return backend.ServerId
    90  }
    91  
    92  func (backend *SLoadbalancerBackend) GetIpAddress() string {
    93  	return ""
    94  }
    95  
    96  func (region *SRegion) GetLoadbalancerBackends(backendgroupId string) ([]SLoadbalancerBackend, error) {
    97  	params := map[string]string{}
    98  	params["RegionId"] = region.RegionId
    99  	params["VServerGroupId"] = backendgroupId
   100  	body, err := region.lbRequest("DescribeVServerGroupAttribute", params)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  	backends := []SLoadbalancerBackend{}
   105  	return backends, body.Unmarshal(&backends, "BackendServers", "BackendServer")
   106  }
   107  
   108  func (backend *SLoadbalancerBackend) SyncConf(ctx context.Context, port, weight int) error {
   109  	err := backend.lbbg.lb.region.RemoveBackendVServer(backend.lbbg.lb.LoadBalancerId, backend.lbbg.VServerGroupId, backend.ServerId, backend.Port)
   110  	if err != nil {
   111  		return err
   112  	}
   113  	return backend.lbbg.lb.region.AddBackendVServer(backend.lbbg.lb.LoadBalancerId, backend.lbbg.VServerGroupId, backend.ServerId, weight, port)
   114  }