yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/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 aliyun
    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  	AliyunTags
    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  }
    59  
    60  func (listener *SLoadbalancerTCPListener) GetName() string {
    61  	if len(listener.Description) == 0 {
    62  		listener.Refresh()
    63  	}
    64  	if len(listener.Description) > 0 {
    65  		return listener.Description
    66  	}
    67  	return fmt.Sprintf("TCP:%d", listener.ListenerPort)
    68  }
    69  
    70  func (listerner *SLoadbalancerTCPListener) GetId() string {
    71  	return fmt.Sprintf("%s/%d", listerner.lb.LoadBalancerId, listerner.ListenerPort)
    72  }
    73  
    74  func (listerner *SLoadbalancerTCPListener) GetGlobalId() string {
    75  	return listerner.GetId()
    76  }
    77  
    78  func (listerner *SLoadbalancerTCPListener) GetStatus() string {
    79  	switch listerner.Status {
    80  	case "starting", "running":
    81  		return api.LB_STATUS_ENABLED
    82  	case "configuring", "stopping", "stopped":
    83  		return api.LB_STATUS_DISABLED
    84  	default:
    85  		return api.LB_STATUS_UNKNOWN
    86  	}
    87  }
    88  
    89  func (listerner *SLoadbalancerTCPListener) GetEgressMbps() int {
    90  	if listerner.Bandwidth < 1 {
    91  		return 0
    92  	}
    93  	return listerner.Bandwidth
    94  }
    95  
    96  func (listerner *SLoadbalancerTCPListener) IsEmulated() bool {
    97  	return false
    98  }
    99  
   100  func (listerner *SLoadbalancerTCPListener) Refresh() error {
   101  	lis, err := listerner.lb.region.GetLoadbalancerTCPListener(listerner.lb.LoadBalancerId, listerner.ListenerPort)
   102  	if err != nil {
   103  		return err
   104  	}
   105  	return jsonutils.Update(listerner, lis)
   106  }
   107  
   108  func (listerner *SLoadbalancerTCPListener) GetListenerType() string {
   109  	return "tcp"
   110  }
   111  func (listerner *SLoadbalancerTCPListener) GetListenerPort() int {
   112  	return listerner.ListenerPort
   113  }
   114  
   115  func (listerner *SLoadbalancerTCPListener) GetBackendGroupId() string {
   116  	if len(listerner.VServerGroupId) > 0 {
   117  		return listerner.VServerGroupId
   118  	}
   119  	return listerner.MasterSlaveServerGroupId
   120  }
   121  
   122  func (listerner *SLoadbalancerTCPListener) GetScheduler() string {
   123  	return listerner.Scheduler
   124  }
   125  
   126  func (listerner *SLoadbalancerTCPListener) GetAclStatus() string {
   127  	return listerner.AclStatus
   128  }
   129  
   130  func (listerner *SLoadbalancerTCPListener) GetAclType() string {
   131  	return listerner.AclType
   132  }
   133  
   134  func (listerner *SLoadbalancerTCPListener) GetAclId() string {
   135  	return listerner.AclId
   136  }
   137  
   138  func (listerner *SLoadbalancerTCPListener) GetHealthCheck() string {
   139  	return listerner.HealthCheck
   140  }
   141  
   142  func (listerner *SLoadbalancerTCPListener) GetHealthCheckType() string {
   143  	return listerner.HealthCheckType
   144  }
   145  
   146  func (listerner *SLoadbalancerTCPListener) GetHealthCheckDomain() string {
   147  	return ""
   148  }
   149  
   150  func (listerner *SLoadbalancerTCPListener) GetHealthCheckURI() string {
   151  	return ""
   152  }
   153  
   154  func (listerner *SLoadbalancerTCPListener) GetHealthCheckCode() string {
   155  	return ""
   156  }
   157  
   158  func (listerner *SLoadbalancerTCPListener) GetHealthCheckRise() int {
   159  	return listerner.HealthyThreshold
   160  }
   161  
   162  func (listerner *SLoadbalancerTCPListener) GetHealthCheckFail() int {
   163  	return listerner.UnhealthyThreshold
   164  }
   165  
   166  func (listerner *SLoadbalancerTCPListener) GetHealthCheckTimeout() int {
   167  	return listerner.HealthCheckConnectTimeout
   168  }
   169  
   170  func (listerner *SLoadbalancerTCPListener) GetHealthCheckInterval() int {
   171  	return listerner.HealthCheckInterval
   172  }
   173  
   174  func (listerner *SLoadbalancerTCPListener) GetHealthCheckReq() string {
   175  	return ""
   176  }
   177  
   178  func (listerner *SLoadbalancerTCPListener) GetHealthCheckExp() string {
   179  	return ""
   180  }
   181  
   182  func (listerner *SLoadbalancerTCPListener) GetStickySession() string {
   183  	return ""
   184  }
   185  
   186  func (listerner *SLoadbalancerTCPListener) GetStickySessionType() string {
   187  	return ""
   188  }
   189  
   190  func (listerner *SLoadbalancerTCPListener) GetStickySessionCookie() string {
   191  	return ""
   192  }
   193  
   194  func (listerner *SLoadbalancerTCPListener) GetStickySessionCookieTimeout() int {
   195  	return 0
   196  }
   197  
   198  func (listerner *SLoadbalancerTCPListener) XForwardedForEnabled() bool {
   199  	return false
   200  }
   201  
   202  func (listerner *SLoadbalancerTCPListener) GzipEnabled() bool {
   203  	return false
   204  }
   205  
   206  func (listerner *SLoadbalancerTCPListener) GetCertificateId() string {
   207  	return ""
   208  }
   209  
   210  func (listerner *SLoadbalancerTCPListener) GetTLSCipherPolicy() string {
   211  	return ""
   212  }
   213  
   214  func (listerner *SLoadbalancerTCPListener) HTTP2Enabled() bool {
   215  	return false
   216  }
   217  
   218  func (listerner *SLoadbalancerTCPListener) GetBackendServerPort() int {
   219  	return listerner.BackendServerPort
   220  }
   221  
   222  func (listerner *SLoadbalancerTCPListener) GetILoadbalancerListenerRules() ([]cloudprovider.ICloudLoadbalancerListenerRule, error) {
   223  	return []cloudprovider.ICloudLoadbalancerListenerRule{}, nil
   224  }
   225  
   226  func (region *SRegion) GetLoadbalancerTCPListener(loadbalancerId string, listenerPort int) (*SLoadbalancerTCPListener, error) {
   227  	params := map[string]string{}
   228  	params["RegionId"] = region.RegionId
   229  	params["LoadBalancerId"] = loadbalancerId
   230  	params["ListenerPort"] = fmt.Sprintf("%d", listenerPort)
   231  	body, err := region.lbRequest("DescribeLoadBalancerTCPListenerAttribute", params)
   232  	if err != nil {
   233  		return nil, err
   234  	}
   235  	listener := SLoadbalancerTCPListener{}
   236  	return &listener, body.Unmarshal(&listener)
   237  }
   238  
   239  func (region *SRegion) constructBaseCreateListenerParams(lb *SLoadbalancer, listener *cloudprovider.SLoadbalancerListener) map[string]string {
   240  	params := map[string]string{}
   241  	params["RegionId"] = region.RegionId
   242  	if listener.EgressMbps < 1 {
   243  		listener.EgressMbps = -1
   244  	}
   245  	params["Bandwidth"] = fmt.Sprintf("%d", listener.EgressMbps)
   246  	params["ListenerPort"] = fmt.Sprintf("%d", listener.ListenerPort)
   247  	params["LoadBalancerId"] = lb.LoadBalancerId
   248  	if len(listener.AccessControlListID) > 0 {
   249  		params["AclId"] = listener.AccessControlListID
   250  	}
   251  	if utils.IsInStringArray(listener.AccessControlListStatus, []string{"on", "off"}) {
   252  		params["AclStatus"] = listener.AccessControlListStatus
   253  	}
   254  	if utils.IsInStringArray(listener.AccessControlListType, []string{"white", "black"}) {
   255  		params["AclType"] = listener.AccessControlListType
   256  	}
   257  	switch listener.BackendGroupType {
   258  	case api.LB_BACKENDGROUP_TYPE_NORMAL:
   259  		params["VServerGroupId"] = listener.BackendGroupID
   260  		params["VServerGroup"] = "on"
   261  	case api.LB_BACKENDGROUP_TYPE_MASTER_SLAVE:
   262  		params["MasterSlaveServerGroupId"] = listener.BackendGroupID
   263  		params["MasterSlaveServerGroup"] = "on"
   264  	case api.LB_BACKENDGROUP_TYPE_DEFAULT:
   265  		params["BackendServerPort"] = fmt.Sprintf("%d", listener.BackendServerPort)
   266  	}
   267  	if len(listener.Name) > 0 {
   268  		params["Description"] = listener.Name
   269  	}
   270  
   271  	if utils.IsInStringArray(listener.ListenerType, []string{api.LB_LISTENER_TYPE_TCP, api.LB_LISTENER_TYPE_UDP}) {
   272  		if listener.HealthCheckTimeout >= 1 && listener.HealthCheckTimeout <= 300 {
   273  			params["HealthCheckConnectTimeout"] = fmt.Sprintf("%d", listener.HealthCheckTimeout)
   274  		}
   275  	}
   276  	switch listener.ListenerType {
   277  	case api.LB_LISTENER_TYPE_UDP:
   278  		if len(listener.HealthCheckReq) > 0 {
   279  			params["healthCheckReq"] = listener.HealthCheckReq
   280  		}
   281  		if len(listener.HealthCheckExp) > 0 {
   282  			params["healthCheckExp"] = listener.HealthCheckExp
   283  		}
   284  	}
   285  
   286  	if len(listener.HealthCheckDomain) > 0 {
   287  		params["HealthCheckDomain"] = listener.HealthCheckDomain
   288  	}
   289  
   290  	if len(listener.HealthCheckHttpCode) > 0 {
   291  		params["HealthCheckHttpCode"] = listener.HealthCheckHttpCode
   292  	}
   293  
   294  	if len(listener.HealthCheckURI) > 0 {
   295  		params["HealthCheckURI"] = listener.HealthCheckURI
   296  	}
   297  
   298  	if listener.HealthCheckRise >= 2 && listener.HealthCheckRise <= 10 {
   299  		params["HealthyThreshold"] = fmt.Sprintf("%d", listener.HealthCheckRise)
   300  	}
   301  
   302  	if listener.HealthCheckFail >= 2 && listener.HealthCheckFail <= 10 {
   303  		params["UnhealthyThreshold"] = fmt.Sprintf("%d", listener.HealthCheckFail)
   304  	}
   305  
   306  	if listener.HealthCheckInterval >= 1 && listener.HealthCheckInterval <= 50 {
   307  		params["healthCheckInterval"] = fmt.Sprintf("%d", listener.HealthCheckInterval)
   308  	}
   309  
   310  	params["Scheduler"] = listener.Scheduler
   311  	return params
   312  }
   313  
   314  func (region *SRegion) CreateLoadbalancerTCPListener(lb *SLoadbalancer, listener *cloudprovider.SLoadbalancerListener) (cloudprovider.ICloudLoadbalancerListener, error) {
   315  	params := region.constructBaseCreateListenerParams(lb, listener)
   316  	_, err := region.lbRequest("CreateLoadBalancerTCPListener", params)
   317  	if err != nil {
   318  		return nil, err
   319  	}
   320  	iListener, err := region.GetLoadbalancerTCPListener(lb.LoadBalancerId, listener.ListenerPort)
   321  	if err != nil {
   322  		return nil, err
   323  	}
   324  	iListener.lb = lb
   325  	return iListener, nil
   326  }
   327  
   328  func (listerner *SLoadbalancerTCPListener) Delete(ctx context.Context) error {
   329  	return listerner.lb.region.DeleteLoadbalancerListener(listerner.lb.LoadBalancerId, listerner.ListenerPort)
   330  }
   331  
   332  func (listerner *SLoadbalancerTCPListener) CreateILoadBalancerListenerRule(rule *cloudprovider.SLoadbalancerListenerRule) (cloudprovider.ICloudLoadbalancerListenerRule, error) {
   333  	return nil, cloudprovider.ErrNotSupported
   334  }
   335  
   336  func (listerner *SLoadbalancerTCPListener) GetILoadBalancerListenerRuleById(ruleId string) (cloudprovider.ICloudLoadbalancerListenerRule, error) {
   337  	return nil, cloudprovider.ErrNotSupported
   338  }
   339  
   340  func (listerner *SLoadbalancerTCPListener) Start() error {
   341  	return listerner.lb.region.startListener(listerner.ListenerPort, listerner.lb.LoadBalancerId)
   342  }
   343  
   344  func (listerner *SLoadbalancerTCPListener) Stop() error {
   345  	return listerner.lb.region.stopListener(listerner.ListenerPort, listerner.lb.LoadBalancerId)
   346  }
   347  
   348  func (region *SRegion) SyncLoadbalancerTCPListener(lb *SLoadbalancer, listener *cloudprovider.SLoadbalancerListener) error {
   349  	params := region.constructBaseCreateListenerParams(lb, listener)
   350  	_, err := region.lbRequest("SetLoadBalancerTCPListenerAttribute", params)
   351  	return err
   352  }
   353  
   354  func (listerner *SLoadbalancerTCPListener) Sync(ctx context.Context, lblis *cloudprovider.SLoadbalancerListener) error {
   355  	return listerner.lb.region.SyncLoadbalancerTCPListener(listerner.lb, lblis)
   356  }
   357  
   358  func (listerner *SLoadbalancerTCPListener) GetProjectId() string {
   359  	return listerner.lb.GetProjectId()
   360  }
   361  
   362  func (listerner *SLoadbalancerTCPListener) GetClientIdleTimeout() int {
   363  	return 0
   364  }
   365  
   366  func (listerner *SLoadbalancerTCPListener) GetBackendConnectTimeout() int {
   367  	return 0
   368  }