yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/loadbalancertcplistener.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  	"yunion.io/x/pkg/utils"
    23  
    24  	api "yunion.io/x/cloudmux/pkg/apis/compute"
    25  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    26  	"yunion.io/x/cloudmux/pkg/multicloud"
    27  )
    28  
    29  type SLoadbalancerTCPListener struct {
    30  	multicloud.SResourceBase
    31  	multicloud.SLoadbalancerRedirectBase
    32  	ApsaraTags
    33  	lb *SLoadbalancer
    34  
    35  	ListenerPort      int    //	负载均衡实例前端使用的端口。
    36  	BackendServerPort int    //	负载均衡实例后端使用的端口。
    37  	Bandwidth         int    //	监听的带宽峰值。
    38  	Status            string //	当前监听的状态,取值:starting | running | configuring | stopping | stopped
    39  	Description       string
    40  
    41  	Scheduler                string //	调度算法。
    42  	VServerGroupId           string //	绑定的服务器组ID。
    43  	MasterSlaveServerGroupId string //	绑定的主备服务器组ID。
    44  	AclStatus                string //	是否开启访问控制功能。取值:on | off(默认值)
    45  	PersistenceTimeout       int    //是否开启了会话保持。取值为0时,表示没有开启。
    46  
    47  	AclType string //	访问控制类型
    48  
    49  	AclId string //	监听绑定的访问策略组ID。当AclStatus参数的值为on时,该参数必选。
    50  
    51  	HealthCheck               string //	是否开启健康检查。
    52  	HealthCheckType           string //TCP协议监听的健康检查方式。取值:tcp | http
    53  	HealthyThreshold          int    //	健康检查阈值。
    54  	UnhealthyThreshold        int    //	不健康检查阈值。
    55  	HealthCheckConnectTimeout int    //	每次健康检查响应的最大超时间,单位为秒。
    56  	HealthCheckInterval       int    //	健康检查的时间间隔,单位为秒。
    57  	HealthCheckConnectPort    int    //	健康检查的端口。
    58  	DepartmentInfo
    59  }
    60  
    61  func (listener *SLoadbalancerTCPListener) GetName() string {
    62  	if len(listener.Description) == 0 {
    63  		listener.Refresh()
    64  	}
    65  	if len(listener.Description) > 0 {
    66  		return listener.Description
    67  	}
    68  	return fmt.Sprintf("TCP:%d", listener.ListenerPort)
    69  }
    70  
    71  func (listerner *SLoadbalancerTCPListener) GetId() string {
    72  	return fmt.Sprintf("%s/%d", listerner.lb.LoadBalancerId, listerner.ListenerPort)
    73  }
    74  
    75  func (listerner *SLoadbalancerTCPListener) GetGlobalId() string {
    76  	return listerner.GetId()
    77  }
    78  
    79  func (listerner *SLoadbalancerTCPListener) GetStatus() string {
    80  	switch listerner.Status {
    81  	case "starting", "running":
    82  		return api.LB_STATUS_ENABLED
    83  	case "configuring", "stopping", "stopped":
    84  		return api.LB_STATUS_DISABLED
    85  	default:
    86  		return api.LB_STATUS_UNKNOWN
    87  	}
    88  }
    89  
    90  func (listerner *SLoadbalancerTCPListener) GetEgressMbps() int {
    91  	if listerner.Bandwidth < 1 {
    92  		return 0
    93  	}
    94  	return listerner.Bandwidth
    95  }
    96  
    97  func (listerner *SLoadbalancerTCPListener) IsEmulated() bool {
    98  	return false
    99  }
   100  
   101  func (listerner *SLoadbalancerTCPListener) Refresh() error {
   102  	lis, err := listerner.lb.region.GetLoadbalancerTCPListener(listerner.lb.LoadBalancerId, listerner.ListenerPort)
   103  	if err != nil {
   104  		return err
   105  	}
   106  	return jsonutils.Update(listerner, lis)
   107  }
   108  
   109  func (listerner *SLoadbalancerTCPListener) GetListenerType() string {
   110  	return "tcp"
   111  }
   112  func (listerner *SLoadbalancerTCPListener) GetListenerPort() int {
   113  	return listerner.ListenerPort
   114  }
   115  
   116  func (listerner *SLoadbalancerTCPListener) GetBackendGroupId() string {
   117  	if len(listerner.VServerGroupId) > 0 {
   118  		return listerner.VServerGroupId
   119  	}
   120  	return listerner.MasterSlaveServerGroupId
   121  }
   122  
   123  func (listerner *SLoadbalancerTCPListener) GetScheduler() string {
   124  	return listerner.Scheduler
   125  }
   126  
   127  func (listerner *SLoadbalancerTCPListener) GetAclStatus() string {
   128  	return listerner.AclStatus
   129  }
   130  
   131  func (listerner *SLoadbalancerTCPListener) GetAclType() string {
   132  	return listerner.AclType
   133  }
   134  
   135  func (listerner *SLoadbalancerTCPListener) GetAclId() string {
   136  	return listerner.AclId
   137  }
   138  
   139  func (listerner *SLoadbalancerTCPListener) GetHealthCheck() string {
   140  	return listerner.HealthCheck
   141  }
   142  
   143  func (listerner *SLoadbalancerTCPListener) GetHealthCheckType() string {
   144  	return listerner.HealthCheckType
   145  }
   146  
   147  func (listerner *SLoadbalancerTCPListener) GetHealthCheckDomain() string {
   148  	return ""
   149  }
   150  
   151  func (listerner *SLoadbalancerTCPListener) GetHealthCheckURI() string {
   152  	return ""
   153  }
   154  
   155  func (listerner *SLoadbalancerTCPListener) GetHealthCheckCode() string {
   156  	return ""
   157  }
   158  
   159  func (listerner *SLoadbalancerTCPListener) GetHealthCheckRise() int {
   160  	return listerner.HealthyThreshold
   161  }
   162  
   163  func (listerner *SLoadbalancerTCPListener) GetHealthCheckFail() int {
   164  	return listerner.UnhealthyThreshold
   165  }
   166  
   167  func (listerner *SLoadbalancerTCPListener) GetHealthCheckTimeout() int {
   168  	return listerner.HealthCheckConnectTimeout
   169  }
   170  
   171  func (listerner *SLoadbalancerTCPListener) GetHealthCheckInterval() int {
   172  	return listerner.HealthCheckInterval
   173  }
   174  
   175  func (listerner *SLoadbalancerTCPListener) GetHealthCheckReq() string {
   176  	return ""
   177  }
   178  
   179  func (listerner *SLoadbalancerTCPListener) GetHealthCheckExp() string {
   180  	return ""
   181  }
   182  
   183  func (listerner *SLoadbalancerTCPListener) GetStickySession() string {
   184  	return ""
   185  }
   186  
   187  func (listerner *SLoadbalancerTCPListener) GetStickySessionType() string {
   188  	return ""
   189  }
   190  
   191  func (listerner *SLoadbalancerTCPListener) GetStickySessionCookie() string {
   192  	return ""
   193  }
   194  
   195  func (listerner *SLoadbalancerTCPListener) GetStickySessionCookieTimeout() int {
   196  	return 0
   197  }
   198  
   199  func (listerner *SLoadbalancerTCPListener) XForwardedForEnabled() bool {
   200  	return false
   201  }
   202  
   203  func (listerner *SLoadbalancerTCPListener) GzipEnabled() bool {
   204  	return false
   205  }
   206  
   207  func (listerner *SLoadbalancerTCPListener) GetCertificateId() string {
   208  	return ""
   209  }
   210  
   211  func (listerner *SLoadbalancerTCPListener) GetTLSCipherPolicy() string {
   212  	return ""
   213  }
   214  
   215  func (listerner *SLoadbalancerTCPListener) HTTP2Enabled() bool {
   216  	return false
   217  }
   218  
   219  func (listerner *SLoadbalancerTCPListener) GetBackendServerPort() int {
   220  	return listerner.BackendServerPort
   221  }
   222  
   223  func (listerner *SLoadbalancerTCPListener) GetILoadbalancerListenerRules() ([]cloudprovider.ICloudLoadbalancerListenerRule, error) {
   224  	return []cloudprovider.ICloudLoadbalancerListenerRule{}, nil
   225  }
   226  
   227  func (region *SRegion) GetLoadbalancerTCPListener(loadbalancerId string, listenerPort int) (*SLoadbalancerTCPListener, error) {
   228  	params := map[string]string{}
   229  	params["RegionId"] = region.RegionId
   230  	params["LoadBalancerId"] = loadbalancerId
   231  	params["ListenerPort"] = fmt.Sprintf("%d", listenerPort)
   232  	body, err := region.lbRequest("DescribeLoadBalancerTCPListenerAttribute", params)
   233  	if err != nil {
   234  		return nil, err
   235  	}
   236  	listener := SLoadbalancerTCPListener{}
   237  	return &listener, body.Unmarshal(&listener)
   238  }
   239  
   240  func (region *SRegion) constructBaseCreateListenerParams(lb *SLoadbalancer, listener *cloudprovider.SLoadbalancerListener) map[string]string {
   241  	params := map[string]string{}
   242  	params["RegionId"] = region.RegionId
   243  	if listener.EgressMbps < 1 {
   244  		listener.EgressMbps = -1
   245  	}
   246  	params["Bandwidth"] = fmt.Sprintf("%d", listener.EgressMbps)
   247  	params["ListenerPort"] = fmt.Sprintf("%d", listener.ListenerPort)
   248  	params["LoadBalancerId"] = lb.LoadBalancerId
   249  	if len(listener.AccessControlListID) > 0 {
   250  		params["AclId"] = listener.AccessControlListID
   251  	}
   252  	if utils.IsInStringArray(listener.AccessControlListStatus, []string{"on", "off"}) {
   253  		params["AclStatus"] = listener.AccessControlListStatus
   254  	}
   255  	if utils.IsInStringArray(listener.AccessControlListType, []string{"white", "black"}) {
   256  		params["AclType"] = listener.AccessControlListType
   257  	}
   258  	switch listener.BackendGroupType {
   259  	case api.LB_BACKENDGROUP_TYPE_NORMAL:
   260  		params["VServerGroupId"] = listener.BackendGroupID
   261  		params["VServerGroup"] = "on"
   262  	case api.LB_BACKENDGROUP_TYPE_MASTER_SLAVE:
   263  		params["MasterSlaveServerGroupId"] = listener.BackendGroupID
   264  		params["MasterSlaveServerGroup"] = "on"
   265  	case api.LB_BACKENDGROUP_TYPE_DEFAULT:
   266  		params["BackendServerPort"] = fmt.Sprintf("%d", listener.BackendServerPort)
   267  	}
   268  	if len(listener.Name) > 0 {
   269  		params["Description"] = listener.Name
   270  	}
   271  
   272  	if utils.IsInStringArray(listener.ListenerType, []string{api.LB_LISTENER_TYPE_TCP, api.LB_LISTENER_TYPE_UDP}) {
   273  		if listener.HealthCheckTimeout >= 1 && listener.HealthCheckTimeout <= 300 {
   274  			params["HealthCheckConnectTimeout"] = fmt.Sprintf("%d", listener.HealthCheckTimeout)
   275  		}
   276  	}
   277  	switch listener.ListenerType {
   278  	case api.LB_LISTENER_TYPE_UDP:
   279  		if len(listener.HealthCheckReq) > 0 {
   280  			params["healthCheckReq"] = listener.HealthCheckReq
   281  		}
   282  		if len(listener.HealthCheckExp) > 0 {
   283  			params["healthCheckExp"] = listener.HealthCheckExp
   284  		}
   285  	}
   286  
   287  	if len(listener.HealthCheckDomain) > 0 {
   288  		params["HealthCheckDomain"] = listener.HealthCheckDomain
   289  	}
   290  
   291  	if len(listener.HealthCheckHttpCode) > 0 {
   292  		params["HealthCheckHttpCode"] = listener.HealthCheckHttpCode
   293  	}
   294  
   295  	if len(listener.HealthCheckURI) > 0 {
   296  		params["HealthCheckURI"] = listener.HealthCheckURI
   297  	}
   298  
   299  	if listener.HealthCheckRise >= 2 && listener.HealthCheckRise <= 10 {
   300  		params["HealthyThreshold"] = fmt.Sprintf("%d", listener.HealthCheckRise)
   301  	}
   302  
   303  	if listener.HealthCheckFail >= 2 && listener.HealthCheckFail <= 10 {
   304  		params["UnhealthyThreshold"] = fmt.Sprintf("%d", listener.HealthCheckFail)
   305  	}
   306  
   307  	if listener.HealthCheckInterval >= 1 && listener.HealthCheckInterval <= 50 {
   308  		params["healthCheckInterval"] = fmt.Sprintf("%d", listener.HealthCheckInterval)
   309  	}
   310  
   311  	params["Scheduler"] = listener.Scheduler
   312  	return params
   313  }
   314  
   315  func (region *SRegion) CreateLoadbalancerTCPListener(lb *SLoadbalancer, listener *cloudprovider.SLoadbalancerListener) (cloudprovider.ICloudLoadbalancerListener, error) {
   316  	params := region.constructBaseCreateListenerParams(lb, listener)
   317  	_, err := region.lbRequest("CreateLoadBalancerTCPListener", params)
   318  	if err != nil {
   319  		return nil, err
   320  	}
   321  	iListener, err := region.GetLoadbalancerTCPListener(lb.LoadBalancerId, listener.ListenerPort)
   322  	if err != nil {
   323  		return nil, err
   324  	}
   325  	iListener.lb = lb
   326  	return iListener, nil
   327  }
   328  
   329  func (listerner *SLoadbalancerTCPListener) Delete(ctx context.Context) error {
   330  	return listerner.lb.region.DeleteLoadbalancerListener(listerner.lb.LoadBalancerId, listerner.ListenerPort)
   331  }
   332  
   333  func (listerner *SLoadbalancerTCPListener) CreateILoadBalancerListenerRule(rule *cloudprovider.SLoadbalancerListenerRule) (cloudprovider.ICloudLoadbalancerListenerRule, error) {
   334  	return nil, cloudprovider.ErrNotSupported
   335  }
   336  
   337  func (listerner *SLoadbalancerTCPListener) GetILoadBalancerListenerRuleById(ruleId string) (cloudprovider.ICloudLoadbalancerListenerRule, error) {
   338  	return nil, cloudprovider.ErrNotSupported
   339  }
   340  
   341  func (listerner *SLoadbalancerTCPListener) Start() error {
   342  	return listerner.lb.region.startListener(listerner.ListenerPort, listerner.lb.LoadBalancerId)
   343  }
   344  
   345  func (listerner *SLoadbalancerTCPListener) Stop() error {
   346  	return listerner.lb.region.stopListener(listerner.ListenerPort, listerner.lb.LoadBalancerId)
   347  }
   348  
   349  func (region *SRegion) SyncLoadbalancerTCPListener(lb *SLoadbalancer, listener *cloudprovider.SLoadbalancerListener) error {
   350  	params := region.constructBaseCreateListenerParams(lb, listener)
   351  	_, err := region.lbRequest("SetLoadBalancerTCPListenerAttribute", params)
   352  	return err
   353  }
   354  
   355  func (listerner *SLoadbalancerTCPListener) Sync(ctx context.Context, lblis *cloudprovider.SLoadbalancerListener) error {
   356  	return listerner.lb.region.SyncLoadbalancerTCPListener(listerner.lb, lblis)
   357  }
   358  
   359  func (listerner *SLoadbalancerTCPListener) GetClientIdleTimeout() int {
   360  	return 0
   361  }
   362  
   363  func (listerner *SLoadbalancerTCPListener) GetBackendConnectTimeout() int {
   364  	return 0
   365  }