From d66a0997e125db531b435a2b204fbf0541f484ca Mon Sep 17 00:00:00 2001 From: "Cuong. Duong Manh" Date: Tue, 18 Jun 2024 15:40:29 +0700 Subject: [PATCH] [feat] vlb gateway v2 - api delete loadbalancer --- test/lb_test.go | 12 ++++++++++++ vngcloud/services/loadbalancer/iloadbalancer.go | 1 + vngcloud/services/loadbalancer/v2/irequest.go | 5 +++++ .../services/loadbalancer/v2/loadbalancer.go | 17 +++++++++++++++++ .../loadbalancer/v2/loadbalancer_request.go | 11 +++++++++++ vngcloud/services/loadbalancer/v2/url.go | 7 +++++++ 6 files changed, 53 insertions(+) diff --git a/test/lb_test.go b/test/lb_test.go index 4d702cc..7ec0b54 100644 --- a/test/lb_test.go +++ b/test/lb_test.go @@ -334,3 +334,15 @@ func TestDeleteListenterSuccess(t *ltesting.T) { t.Log("Result: ", sdkerr) t.Log("PASS") } + +func TestDeleteLoadBalancer(t *ltesting.T) { + vngcloud := validSdkConfig() + opt := lslbv2.NewDeleteLoadBalancerByIdRequest("lb-f7adf4ba-7734-45f3-8cb5-9b0c3850cd6f") + sdkerr := vngcloud.VLBGateway().V2().LoadBalancerService().DeleteLoadBalancerById(opt) + if sdkerr != nil { + t.Fatalf("Expect nil but got %+v", sdkerr) + } + + t.Log("Result: ", sdkerr) + t.Log("PASS") +} diff --git a/vngcloud/services/loadbalancer/iloadbalancer.go b/vngcloud/services/loadbalancer/iloadbalancer.go index 8ed1010..3828d4b 100644 --- a/vngcloud/services/loadbalancer/iloadbalancer.go +++ b/vngcloud/services/loadbalancer/iloadbalancer.go @@ -20,6 +20,7 @@ type ILoadBalancerServiceV2 interface { ListPoolMembers(popts lslbSvcV2.IListPoolMembersRequest) (*lsentity.ListMembers, lserr.ISdkError) DeletePoolById(popt lslbSvcV2.IDeletePoolByIdRequest) lserr.ISdkError DeleteListenerById(popts lslbSvcV2.IDeleteListenerByIdRequest) lserr.ISdkError + DeleteLoadBalancerById(popts lslbSvcV2.IDeleteLoadBalancerByIdRequest) lserr.ISdkError } type ILoadBalancerServiceInternal interface { diff --git a/vngcloud/services/loadbalancer/v2/irequest.go b/vngcloud/services/loadbalancer/v2/irequest.go index fae181b..5ea4f9c 100644 --- a/vngcloud/services/loadbalancer/v2/irequest.go +++ b/vngcloud/services/loadbalancer/v2/irequest.go @@ -93,6 +93,11 @@ type IDeleteListenerByIdRequest interface { ParseUserAgent() string } +type IDeleteLoadBalancerByIdRequest interface { + GetLoadBalancerId() string + ParseUserAgent() string +} + type IHealthMonitorRequest interface { ToRequestBody() interface{} ToMap() map[string]interface{} diff --git a/vngcloud/services/loadbalancer/v2/loadbalancer.go b/vngcloud/services/loadbalancer/v2/loadbalancer.go index d6bd177..3606ddc 100644 --- a/vngcloud/services/loadbalancer/v2/loadbalancer.go +++ b/vngcloud/services/loadbalancer/v2/loadbalancer.go @@ -237,3 +237,20 @@ func (s *LoadBalancerServiceV2) DeleteListenerById(popts IDeleteListenerByIdRequ return nil } + +func (s *LoadBalancerServiceV2) DeleteLoadBalancerById(popts IDeleteLoadBalancerByIdRequest) lserr.ISdkError { + url := deleteLoadBalancerByIdUrl(s.VLBClient, popts) + errResp := lserr.NewErrorResponse(lserr.NormalErrorType) + req := lsclient.NewRequest(). + WithHeader("User-Agent", popts.ParseUserAgent()). + WithOkCodes(202). + WithJsonError(errResp) + + if _, sdkErr := s.VLBClient.Delete(url, req); sdkErr != nil { + return lserr.SdkErrorHandler(sdkErr, errResp, + lserr.WithErrorLoadBalancerNotFound(errResp), + lserr.WithErrorLoadBalancerNotReady(errResp)) + } + + return nil +} diff --git a/vngcloud/services/loadbalancer/v2/loadbalancer_request.go b/vngcloud/services/loadbalancer/v2/loadbalancer_request.go index 06fdef6..8b03e1b 100644 --- a/vngcloud/services/loadbalancer/v2/loadbalancer_request.go +++ b/vngcloud/services/loadbalancer/v2/loadbalancer_request.go @@ -46,6 +46,12 @@ func NewListLoadBalancersRequest(ppage, psize int) IListLoadBalancersRequest { return opts } +func NewDeleteLoadBalancerByIdRequest(plbId string) IDeleteLoadBalancerByIdRequest { + opts := new(DeleteLoadBalancerByIdRequest) + opts.LoadBalancerId = plbId + return opts +} + type CreateLoadBalancerRequest struct { Name string `json:"name"` PackageID string `json:"packageId"` @@ -73,6 +79,11 @@ type ListLoadBalancersRequest struct { lscommon.UserAgent } +type DeleteLoadBalancerByIdRequest struct { + lscommon.UserAgent + lscommon.LoadBalancerCommon +} + func (s *CreateLoadBalancerRequest) ToRequestBody() interface{} { if s.Pool != nil { s.Pool = s.Pool.ToRequestBody().(*CreatePoolRequest) diff --git a/vngcloud/services/loadbalancer/v2/url.go b/vngcloud/services/loadbalancer/v2/url.go index 8d4f3cc..a0c4d29 100644 --- a/vngcloud/services/loadbalancer/v2/url.go +++ b/vngcloud/services/loadbalancer/v2/url.go @@ -102,3 +102,10 @@ func deleteListenerByIdUrl(psc lsclient.IServiceClient, popts IDeleteListenerByI "listeners", popts.GetListenerId()) } + +func deleteLoadBalancerByIdUrl(psc lsclient.IServiceClient, popts IDeleteLoadBalancerByIdRequest) string { + return psc.ServiceURL( + psc.GetProjectId(), + "loadBalancers", + popts.GetLoadBalancerId()) +}