Skip to content

Commit

Permalink
[build][enhance] enhance api create intervpc loadbalancer
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed Jun 23, 2024
1 parent 3b6be6b commit 1c981f4
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 31 deletions.
17 changes: 15 additions & 2 deletions test/lb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 21 additions & 4 deletions vngcloud/services/loadbalancer/inter/irequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
49 changes: 44 additions & 5 deletions vngcloud/services/loadbalancer/inter/listener_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -41,14 +41,15 @@ type CreateListenerRequest struct {
DefaultCertificateAuthority *string `json:"defaultCertificateAuthority"`

lscommon.LoadBalancerCommon
lscommon.UserAgent
}

func (s *CreateListenerRequest) ToRequestBody() interface{} {
if s == nil {
return nil
}

if s.ListenerProtocol == CreateOptsListenerProtocolOptHTTPS {
if s.ListenerProtocol == ListenerProtocolHTTPS {
return s
}

Expand All @@ -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
Expand All @@ -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,
}
}
1 change: 1 addition & 0 deletions vngcloud/services/loadbalancer/inter/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
148 changes: 128 additions & 20 deletions vngcloud/services/loadbalancer/inter/pool_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -98,6 +87,7 @@ type CreatePoolRequest struct {
Members []IMemberRequest `json:"members"`

lscommon.LoadBalancerCommon
lscommon.UserAgent
}

type HealthMonitor struct {
Expand All @@ -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
Expand All @@ -150,6 +141,8 @@ func (s *HealthMonitor) validate() {
}
}
}

return s
}

func (s *CreatePoolRequest) WithHealthMonitor(pmonitor IHealthMonitorRequest) ICreatePoolRequest {
Expand All @@ -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,
}
}

0 comments on commit 1c981f4

Please sign in to comment.