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