yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/loadbalancerudplistener.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 SLoadbalancerUDPListener struct {
    29  	multicloud.SResourceBase
    30  	multicloud.SLoadbalancerRedirectBase
    31  	ApsaraTags
    32  	lb *SLoadbalancer
    33  
    34  	ListenerPort      int    //	负载均衡实例前端使用的端口。
    35  	BackendServerPort int    //	负载均衡实例后端使用的端口。
    36  	Bandwidth         int    //	监听的带宽峰值。
    37  	Status            string //	当前监听的状态,取值:starting | running | configuring | stopping | stopped
    38  	Description       string
    39  
    40  	Scheduler                string //	调度算法
    41  	VServerGroupId           string //	绑定的服务器组ID。
    42  	MasterSlaveServerGroupId string //	绑定的主备服务器组ID。
    43  	AclStatus                string //	是否开启访问控制功能。取值:on | off(默认值)
    44  
    45  	AclType string //	访问控制类型:
    46  
    47  	AclId string //	监听绑定的访问策略组ID。当AclStatus参数的值为on时,该参数必选。
    48  
    49  	HealthCheck               string //	是否开启健康检查。
    50  	HealthyThreshold          int    //	健康检查阈值。
    51  	UnhealthyThreshold        int    //	不健康检查阈值。
    52  	HealthCheckConnectTimeout int    //	每次健康检查响应的最大超时间,单位为秒。
    53  	HealthCheckInterval       int    //	健康检查的时间间隔,单位为秒。
    54  	HealthCheckConnectPort    int    //	健康检查的端口。
    55  
    56  	HealthCheckExp string // UDP监听健康检查的响应串
    57  	HealthCheckReq string // UDP监听健康检查的请求串
    58  	DepartmentInfo
    59  }
    60  
    61  func (listener *SLoadbalancerUDPListener) 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("UDP:%d", listener.ListenerPort)
    69  }
    70  
    71  func (listerner *SLoadbalancerUDPListener) GetId() string {
    72  	return fmt.Sprintf("%s/%d", listerner.lb.LoadBalancerId, listerner.ListenerPort)
    73  }
    74  
    75  func (listerner *SLoadbalancerUDPListener) GetGlobalId() string {
    76  	return listerner.GetId()
    77  }
    78  
    79  func (listerner *SLoadbalancerUDPListener) 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 *SLoadbalancerUDPListener) GetEgressMbps() int {
    91  	if listerner.Bandwidth < 1 {
    92  		return 0
    93  	}
    94  	return listerner.Bandwidth
    95  }
    96  
    97  func (listerner *SLoadbalancerUDPListener) IsEmulated() bool {
    98  	return false
    99  }
   100  
   101  func (listerner *SLoadbalancerUDPListener) Refresh() error {
   102  	lis, err := listerner.lb.region.GetLoadbalancerUDPListener(listerner.lb.LoadBalancerId, listerner.ListenerPort)
   103  	if err != nil {
   104  		return err
   105  	}
   106  	return jsonutils.Update(listerner, lis)
   107  }
   108  
   109  func (listerner *SLoadbalancerUDPListener) GetListenerType() string {
   110  	return "udp"
   111  }
   112  
   113  func (listerner *SLoadbalancerUDPListener) GetListenerPort() int {
   114  	return listerner.ListenerPort
   115  }
   116  
   117  func (listerner *SLoadbalancerUDPListener) GetBackendGroupId() string {
   118  	if len(listerner.VServerGroupId) > 0 {
   119  		return listerner.VServerGroupId
   120  	}
   121  	return listerner.MasterSlaveServerGroupId
   122  }
   123  
   124  func (listerner *SLoadbalancerUDPListener) GetBackendServerPort() int {
   125  	return listerner.BackendServerPort
   126  }
   127  
   128  func (listerner *SLoadbalancerUDPListener) GetScheduler() string {
   129  	return listerner.Scheduler
   130  }
   131  
   132  func (listerner *SLoadbalancerUDPListener) GetAclStatus() string {
   133  	return listerner.AclStatus
   134  }
   135  
   136  func (listerner *SLoadbalancerUDPListener) GetAclType() string {
   137  	return listerner.AclType
   138  }
   139  
   140  func (listerner *SLoadbalancerUDPListener) GetAclId() string {
   141  	return listerner.AclId
   142  }
   143  
   144  func (listerner *SLoadbalancerUDPListener) GetHealthCheck() string {
   145  	return listerner.HealthCheck
   146  }
   147  
   148  func (listerner *SLoadbalancerUDPListener) GetHealthCheckType() string {
   149  	return api.LB_HEALTH_CHECK_UDP
   150  }
   151  
   152  func (listerner *SLoadbalancerUDPListener) GetHealthCheckDomain() string {
   153  	return ""
   154  }
   155  
   156  func (listerner *SLoadbalancerUDPListener) GetHealthCheckURI() string {
   157  	return ""
   158  }
   159  
   160  func (listerner *SLoadbalancerUDPListener) GetHealthCheckCode() string {
   161  	return ""
   162  }
   163  
   164  func (listerner *SLoadbalancerUDPListener) GetHealthCheckRise() int {
   165  	return listerner.HealthyThreshold
   166  }
   167  
   168  func (listerner *SLoadbalancerUDPListener) GetHealthCheckFail() int {
   169  	return listerner.UnhealthyThreshold
   170  }
   171  
   172  func (listerner *SLoadbalancerUDPListener) GetHealthCheckTimeout() int {
   173  	return listerner.HealthCheckConnectTimeout
   174  }
   175  
   176  func (listerner *SLoadbalancerUDPListener) GetHealthCheckInterval() int {
   177  	return listerner.HealthCheckInterval
   178  }
   179  
   180  func (listerner *SLoadbalancerUDPListener) GetHealthCheckReq() string {
   181  	return listerner.HealthCheckReq
   182  }
   183  
   184  func (listerner *SLoadbalancerUDPListener) GetHealthCheckExp() string {
   185  	return listerner.HealthCheckExp
   186  }
   187  
   188  func (listerner *SLoadbalancerUDPListener) GetStickySession() string {
   189  	return ""
   190  }
   191  
   192  func (listerner *SLoadbalancerUDPListener) GetStickySessionType() string {
   193  	return ""
   194  }
   195  
   196  func (listerner *SLoadbalancerUDPListener) GetStickySessionCookie() string {
   197  	return ""
   198  }
   199  
   200  func (listerner *SLoadbalancerUDPListener) GetStickySessionCookieTimeout() int {
   201  	return 0
   202  }
   203  
   204  func (listerner *SLoadbalancerUDPListener) XForwardedForEnabled() bool {
   205  	return false
   206  }
   207  
   208  func (listerner *SLoadbalancerUDPListener) GzipEnabled() bool {
   209  	return false
   210  }
   211  
   212  func (listerner *SLoadbalancerUDPListener) GetCertificateId() string {
   213  	return ""
   214  }
   215  
   216  func (listerner *SLoadbalancerUDPListener) GetTLSCipherPolicy() string {
   217  	return ""
   218  }
   219  
   220  func (listerner *SLoadbalancerUDPListener) HTTP2Enabled() bool {
   221  	return false
   222  }
   223  
   224  func (listerner *SLoadbalancerUDPListener) GetILoadbalancerListenerRules() ([]cloudprovider.ICloudLoadbalancerListenerRule, error) {
   225  	return []cloudprovider.ICloudLoadbalancerListenerRule{}, nil
   226  }
   227  
   228  func (region *SRegion) GetLoadbalancerUDPListener(loadbalancerId string, listenerPort int) (*SLoadbalancerUDPListener, error) {
   229  	params := map[string]string{}
   230  	params["RegionId"] = region.RegionId
   231  	params["LoadBalancerId"] = loadbalancerId
   232  	params["ListenerPort"] = fmt.Sprintf("%d", listenerPort)
   233  	body, err := region.lbRequest("DescribeLoadBalancerUDPListenerAttribute", params)
   234  	if err != nil {
   235  		return nil, err
   236  	}
   237  	listener := SLoadbalancerUDPListener{}
   238  	return &listener, body.Unmarshal(&listener)
   239  }
   240  
   241  func (region *SRegion) CreateLoadbalancerUDPListener(lb *SLoadbalancer, listener *cloudprovider.SLoadbalancerListener) (cloudprovider.ICloudLoadbalancerListener, error) {
   242  	params := region.constructBaseCreateListenerParams(lb, listener)
   243  	_, err := region.lbRequest("CreateLoadBalancerUDPListener", params)
   244  	if err != nil {
   245  		return nil, err
   246  	}
   247  	iListener, err := region.GetLoadbalancerUDPListener(lb.LoadBalancerId, listener.ListenerPort)
   248  	if err != nil {
   249  		return nil, err
   250  	}
   251  	iListener.lb = lb
   252  	return iListener, nil
   253  }
   254  
   255  func (listerner *SLoadbalancerUDPListener) Delete(ctx context.Context) error {
   256  	return listerner.lb.region.DeleteLoadbalancerListener(listerner.lb.LoadBalancerId, listerner.ListenerPort)
   257  }
   258  
   259  func (listerner *SLoadbalancerUDPListener) CreateILoadBalancerListenerRule(rule *cloudprovider.SLoadbalancerListenerRule) (cloudprovider.ICloudLoadbalancerListenerRule, error) {
   260  	return nil, cloudprovider.ErrNotSupported
   261  }
   262  
   263  func (listerner *SLoadbalancerUDPListener) GetILoadBalancerListenerRuleById(ruleId string) (cloudprovider.ICloudLoadbalancerListenerRule, error) {
   264  	return nil, cloudprovider.ErrNotSupported
   265  }
   266  
   267  func (listerner *SLoadbalancerUDPListener) Start() error {
   268  	return listerner.lb.region.startListener(listerner.ListenerPort, listerner.lb.LoadBalancerId)
   269  }
   270  
   271  func (listerner *SLoadbalancerUDPListener) Stop() error {
   272  	return listerner.lb.region.stopListener(listerner.ListenerPort, listerner.lb.LoadBalancerId)
   273  }
   274  
   275  func (region *SRegion) SyncLoadbalancerUDPListener(lb *SLoadbalancer, listener *cloudprovider.SLoadbalancerListener) error {
   276  	params := region.constructBaseCreateListenerParams(lb, listener)
   277  	_, err := region.lbRequest("SetLoadBalancerUDPListenerAttribute", params)
   278  	return err
   279  }
   280  
   281  func (listerner *SLoadbalancerUDPListener) Sync(ctx context.Context, lblis *cloudprovider.SLoadbalancerListener) error {
   282  	return listerner.lb.region.SyncLoadbalancerUDPListener(listerner.lb, lblis)
   283  }
   284  
   285  func (listerner *SLoadbalancerUDPListener) GetClientIdleTimeout() int {
   286  	return 0
   287  }
   288  
   289  func (listerner *SLoadbalancerUDPListener) GetBackendConnectTimeout() int {
   290  	return 0
   291  }