diff --git a/test/lb_test.go b/test/lb_test.go index 0d1c466..ef1c429 100644 --- a/test/lb_test.go +++ b/test/lb_test.go @@ -131,10 +131,23 @@ func TestCreateInterVPCLoadBalancerWithPoolAndListenerSuccess(t *ltesting.T) { "sub-27a0562d-07f9-4e87-81fd-e0ba9658f156", "sub-888d8fd2-3fed-4aaa-a62d-8554c0aff651"). WithTags("cuongdm3", "cuongdm33333", "vinhnt8", "vinhnt8888888"). - WithListener(lsinter.NewCreateListenerRequest("cuongdm3-test-listener", lsinter.CreateOptsListenerProtocolOptTCP, 80)). + WithListener(lsinter.NewCreateListenerRequest("cuongdm3-test-listener", lsinter.ListenerProtocolTCP, 80). + WithAllowedCidrs("0.0.0.0/0"). + WithTimeoutClient(50). + WithTimeoutMember(50). + WithTimeoutConnection(5)). WithPool(lsinter.NewCreatePoolRequest("cuongdm3-test-pool", lsinter.PoolProtocolTCP). WithMembers(lsinter.NewMember("cuongdm3-member-1", "10.84.0.22", 80, 80)). - WithHealthMonitor(lsinter.NewHealthMonitor(lsinter.HealthCheckProtocolTCP))) + WithHealthMonitor(lsinter.NewHealthMonitor(lsinter.HealthCheckProtocolTCP). + WithHealthCheckMethod(lsinter.HealthCheckMethodGET). + WithHttpVersion(lsinter.HealthCheckHttpVersionHttp1). + WithHealthyThreshold(30). + WithUnhealthyThreshold(30). + WithTimeout(30). + WithInterval(40). + WithHealthCheckPath("/health"). + WithDomainName("vngcloud.com"). + WithSuccessCode("200"))) lb, sdkerr := vngcloud.VLBGateway().Internal().LoadBalancerService().CreateLoadBalancer(opt) if sdkerr != nil { diff --git a/vngcloud/services/loadbalancer/inter/irequest.go b/vngcloud/services/loadbalancer/inter/irequest.go index e616283..e5ad7c6 100644 --- a/vngcloud/services/loadbalancer/inter/irequest.go +++ b/vngcloud/services/loadbalancer/inter/irequest.go @@ -8,28 +8,45 @@ type ICreateLoadBalancerRequest interface { WithProjectId(pprojectId string) ICreateLoadBalancerRequest WithTags(ptags ...string) ICreateLoadBalancerRequest GetMapHeaders() map[string]string + ParseUserAgent() string } type ICreateListenerRequest interface { ToRequestBody() interface{} WithAllowedCidrs(pcidrs ...string) ICreateListenerRequest + WithLoadBalancerId(plbid string) ICreateListenerRequest + WithDefaultPoolId(ppoolId string) ICreateListenerRequest + WithTimeoutClient(ptoc int) ICreateListenerRequest + WithTimeoutConnection(ptoc int) ICreateListenerRequest + WithTimeoutMember(ptom int) ICreateListenerRequest AddCidrs(pcidrs ...string) ICreateListenerRequest + ParseUserAgent() string + GetLoadBalancerId() string + ToMap() map[string]interface{} } type ICreatePoolRequest interface { ToRequestBody() interface{} WithHealthMonitor(pmonitor IHealthMonitorRequest) ICreatePoolRequest WithMembers(pmembers ...IMemberRequest) ICreatePoolRequest + WithAlgorithm(palgorithm PoolAlgorithm) ICreatePoolRequest } type IHealthMonitorRequest interface { ToRequestBody() interface{} -} - -type IHealthMonitorTCPRequest interface { - ToRequestBody() interface{} + ToMap() map[string]interface{} + WithHealthyThreshold(pht int) IHealthMonitorRequest + WithUnhealthyThreshold(puht int) IHealthMonitorRequest + WithInterval(pinterval int) IHealthMonitorRequest + WithTimeout(pto int) IHealthMonitorRequest + WithHealthCheckMethod(pmethod HealthCheckMethod) IHealthMonitorRequest + WithHttpVersion(pversion HealthCheckHttpVersion) IHealthMonitorRequest + WithHealthCheckPath(ppath string) IHealthMonitorRequest + WithSuccessCode(pcode string) IHealthMonitorRequest + WithDomainName(pdomain string) IHealthMonitorRequest } type IMemberRequest interface { ToRequestBody() interface{} + ToMap() map[string]interface{} } diff --git a/vngcloud/services/loadbalancer/inter/listener_request.go b/vngcloud/services/loadbalancer/inter/listener_request.go index ee11722..d9d120d 100644 --- a/vngcloud/services/loadbalancer/inter/listener_request.go +++ b/vngcloud/services/loadbalancer/inter/listener_request.go @@ -6,10 +6,10 @@ import ( ) const ( - CreateOptsListenerProtocolOptTCP ListenerProtocol = "TCP" - CreateOptsListenerProtocolOptUDP ListenerProtocol = "UDP" - CreateOptsListenerProtocolOptHTTP ListenerProtocol = "HTTP" - CreateOptsListenerProtocolOptHTTPS ListenerProtocol = "HTTPS" + ListenerProtocolTCP ListenerProtocol = "TCP" + ListenerProtocolUDP ListenerProtocol = "UDP" + ListenerProtocolHTTP ListenerProtocol = "HTTP" + ListenerProtocolHTTPS ListenerProtocol = "HTTPS" ) func NewCreateListenerRequest(pname string, pprotocol ListenerProtocol, pport int) ICreateListenerRequest { @@ -41,6 +41,7 @@ type CreateListenerRequest struct { DefaultCertificateAuthority *string `json:"defaultCertificateAuthority"` lscommon.LoadBalancerCommon + lscommon.UserAgent } func (s *CreateListenerRequest) ToRequestBody() interface{} { @@ -48,7 +49,7 @@ func (s *CreateListenerRequest) ToRequestBody() interface{} { return nil } - if s.ListenerProtocol == CreateOptsListenerProtocolOptHTTPS { + if s.ListenerProtocol == ListenerProtocolHTTPS { return s } @@ -68,6 +69,31 @@ func (s *CreateListenerRequest) WithAllowedCidrs(pcidrs ...string) ICreateListen return s } +func (s *CreateListenerRequest) WithTimeoutClient(ptoc int) ICreateListenerRequest { + s.TimeoutClient = ptoc + return s +} + +func (s *CreateListenerRequest) WithTimeoutConnection(ptoc int) ICreateListenerRequest { + s.TimeoutConnection = ptoc + return s +} + +func (s *CreateListenerRequest) WithTimeoutMember(ptom int) ICreateListenerRequest { + s.TimeoutMember = ptom + return s +} + +func (s *CreateListenerRequest) WithLoadBalancerId(plbid string) ICreateListenerRequest { + s.LoadBalancerId = plbid + return s +} + +func (s *CreateListenerRequest) WithDefaultPoolId(ppoolId string) ICreateListenerRequest { + s.DefaultPoolId = &ppoolId + return s +} + func (s *CreateListenerRequest) AddCidrs(pcidrs ...string) ICreateListenerRequest { if len(pcidrs) < 1 { return s @@ -81,3 +107,16 @@ func (s *CreateListenerRequest) AddCidrs(pcidrs ...string) ICreateListenerReques return s } + +func (s *CreateListenerRequest) ToMap() map[string]interface{} { + return map[string]interface{}{ + "listenerName": s.ListenerName, + "listenerProtocol": s.ListenerProtocol, + "listenerProtocolPort": s.ListenerProtocolPort, + "timeoutClient": s.TimeoutClient, + "timeoutConnection": s.TimeoutConnection, + "timeoutMember": s.TimeoutMember, + "allowedCidrs": s.AllowedCidrs, + "defaultPoolId": s.DefaultPoolId, + } +} diff --git a/vngcloud/services/loadbalancer/inter/loadbalancer.go b/vngcloud/services/loadbalancer/inter/loadbalancer.go index d1a3a9e..7ae7e79 100644 --- a/vngcloud/services/loadbalancer/inter/loadbalancer.go +++ b/vngcloud/services/loadbalancer/inter/loadbalancer.go @@ -12,6 +12,7 @@ func (s *LoadBalancerServiceInternal) CreateLoadBalancer(popts ICreateLoadBalanc errResp := lserr.NewErrorResponse(lserr.NormalErrorType) req := lsclient.NewRequest(). WithMapHeaders(popts.GetMapHeaders()). + WithHeader("User-Agent", popts.ParseUserAgent()). WithOkCodes(202). WithJsonBody(popts.WithProjectId(s.VLBClient.GetProjectId()).ToRequestBody()). WithJsonResponse(resp). diff --git a/vngcloud/services/loadbalancer/inter/pool_requests.go b/vngcloud/services/loadbalancer/inter/pool_requests.go index 11700f6..893886d 100644 --- a/vngcloud/services/loadbalancer/inter/pool_requests.go +++ b/vngcloud/services/loadbalancer/inter/pool_requests.go @@ -48,10 +48,14 @@ func NewCreatePoolRequest(pname string, pprotocol PoolProtocol) ICreatePoolReque } func NewHealthMonitor(pcheckProtocol HealthCheckProtocol) IHealthMonitorRequest { - switch pcheckProtocol { - default: - return newHealthMonitorTCPRequest() - } + opts := new(HealthMonitor) + opts.HealthCheckProtocol = pcheckProtocol + opts.HealthyThreshold = 3 + opts.UnhealthyThreshold = 3 + opts.Interval = 30 + opts.Timeout = 5 + + return opts } func NewMember(pname, pipAddress string, pport int, pmonitorPort int) IMemberRequest { @@ -65,21 +69,6 @@ func NewMember(pname, pipAddress string, pport int, pmonitorPort int) IMemberReq } } -func newHealthMonitorTCPRequest() IHealthMonitorTCPRequest { - return &HealthMonitor{ - HealthCheckProtocol: HealthCheckProtocolTCP, - HealthyThreshold: 3, - UnhealthyThreshold: 3, - Interval: 30, - Timeout: 5, - HealthCheckPath: nil, - HttpVersion: nil, - SuccessCode: nil, - HealthCheckMethod: nil, - DomainName: nil, - } -} - type ( PoolAlgorithm string PoolProtocol string @@ -98,6 +87,7 @@ type CreatePoolRequest struct { Members []IMemberRequest `json:"members"` lscommon.LoadBalancerCommon + lscommon.UserAgent } type HealthMonitor struct { @@ -123,10 +113,11 @@ type Member struct { } func (s *CreatePoolRequest) ToRequestBody() interface{} { + s.HealthMonitor = s.HealthMonitor.(*HealthMonitor).toRequestBody() return s } -func (s *HealthMonitor) validate() { +func (s *HealthMonitor) toRequestBody() IHealthMonitorRequest { switch s.HealthCheckProtocol { case HealthCheckProtocolPINGUDP, HealthCheckProtocolTCP: s.HealthCheckPath = nil @@ -150,6 +141,8 @@ func (s *HealthMonitor) validate() { } } } + + return s } func (s *CreatePoolRequest) WithHealthMonitor(pmonitor IHealthMonitorRequest) ICreatePoolRequest { @@ -162,10 +155,125 @@ func (s *CreatePoolRequest) WithMembers(pmembers ...IMemberRequest) ICreatePoolR return s } +func (s *CreatePoolRequest) WithLoadBalancerId(plbId string) ICreatePoolRequest { + s.LoadBalancerId = plbId + return s +} + +func (s *CreatePoolRequest) ToMap() map[string]interface{} { + return map[string]interface{}{ + "algorithm": s.Algorithm, + "poolName": s.PoolName, + "poolProtocol": s.PoolProtocol, + "stickiness": s.Stickiness, + "tlsEncryption": s.TLSEncryption, + "healthMonitor": s.HealthMonitor.ToMap(), + "members": func() []map[string]interface{} { + var members []map[string]interface{} + for _, member := range s.Members { + members = append(members, member.ToMap()) + } + return members + }(), + } +} + +func (s *CreatePoolRequest) WithAlgorithm(palgorithm PoolAlgorithm) ICreatePoolRequest { + s.Algorithm = palgorithm + return s +} + func (s *HealthMonitor) ToRequestBody() interface{} { return s } +func (s *HealthMonitor) WithHealthyThreshold(pht int) IHealthMonitorRequest { + if pht < 1 { + pht = 3 + } + + s.HealthyThreshold = pht + return s +} + +func (s *HealthMonitor) WithUnhealthyThreshold(puht int) IHealthMonitorRequest { + if puht < 1 { + puht = 3 + } + + s.UnhealthyThreshold = puht + return s +} + +func (s *HealthMonitor) WithInterval(pinterval int) IHealthMonitorRequest { + if pinterval < 1 { + pinterval = 30 + } + + s.Interval = pinterval + return s +} + +func (s *HealthMonitor) WithTimeout(pto int) IHealthMonitorRequest { + if pto < 1 { + pto = 5 + } + + s.Timeout = pto + return s +} + +func (s *HealthMonitor) WithHealthCheckMethod(pmethod HealthCheckMethod) IHealthMonitorRequest { + s.HealthCheckMethod = &pmethod + return s +} + +func (s *HealthMonitor) WithHttpVersion(pversion HealthCheckHttpVersion) IHealthMonitorRequest { + s.HttpVersion = &pversion + return s +} + +func (s *HealthMonitor) WithHealthCheckPath(ppath string) IHealthMonitorRequest { + s.HealthCheckPath = &ppath + return s +} + +func (s *HealthMonitor) WithDomainName(pdomain string) IHealthMonitorRequest { + s.DomainName = &pdomain + return s +} + +func (s *HealthMonitor) WithSuccessCode(pcode string) IHealthMonitorRequest { + s.SuccessCode = &pcode + return s +} + +func (s *HealthMonitor) ToMap() map[string]interface{} { + return map[string]interface{}{ + "healthCheckProtocol": s.HealthCheckProtocol, + "healthyThreshold": s.HealthyThreshold, + "unhealthyThreshold": s.UnhealthyThreshold, + "interval": s.Interval, + "timeout": s.Timeout, + "healthCheckMethod": s.HealthCheckMethod, + "httpVersion": s.HttpVersion, + "healthCheckPath": s.HealthCheckPath, + "domainName": s.DomainName, + "successCode": s.SuccessCode, + } +} + func (s *Member) ToRequestBody() interface{} { return s } + +func (s *Member) ToMap() map[string]interface{} { + return map[string]interface{}{ + "backup": s.Backup, + "ipAddress": s.IpAddress, + "monitorPort": s.MonitorPort, + "name": s.Name, + "port": s.Port, + "weight": s.Weight, + } +}