Skip to content

Commit

Permalink
Merge branch 'v2' into v2-update-pool
Browse files Browse the repository at this point in the history
  • Loading branch information
anngdinh committed Oct 31, 2024
2 parents 229d602 + fbf8ee1 commit d59c295
Show file tree
Hide file tree
Showing 47 changed files with 1,191 additions and 162 deletions.
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/vngcloud/vngcloud-go-sdk/v2

go 1.22

toolchain go1.22.0
go 1.22.2

require (
github.com/cuongpiger/joat v1.0.14
Expand Down
61 changes: 59 additions & 2 deletions test/endpoint_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package test

import (
lsnwv1 "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/services/network/v1"
"net/url"
ltesting "testing"

"net/url"
lsnwv1 "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/services/network/v1"
)

func TestGetEndpointSuccess(t *ltesting.T) {
Expand Down Expand Up @@ -76,3 +76,60 @@ func TestEndpoint(t *ltesting.T) {

t.Log("Encode: ", encode)
}

func TestListEndpointTags(t *ltesting.T) {
vngcloud := validSuperSdkConfig()
opt := lsnwv1.NewListTagsByEndpointIdRequest("54507", "pro-88265bae-d2ef-424b-b8a7-9eeb08aec1f7", "enp-4457d867-6e3d-4a75-8ceb-d11a957afbe8")

lb, sdkerr := vngcloud.VNetworkGateway().InternalV1().NetworkService().ListTagsByEndpointId(opt)
if sdkerr != nil {
t.Logf("Expect nil but got %+v", sdkerr.GetErrorCode())
}

if lb == nil {
t.Fatalf("Expect not nil but got nil")
}

t.Log("Result: ", lb.Items[1])
t.Log("PASS")
}

func TestCreateEndpointTags(t *ltesting.T) {
vngcloud := validSuperSdkConfig()
opt := lsnwv1.NewCreateTagsWithEndpointIdRequest("60108", "pro-88265bae-d2ef-424b-b8a7-9eeb08aec1f7", "enp-7e8e4476-feeb-414c-ac03-3501aae607d0").
AddTag("cuongdm3", "test")

sdkerr := vngcloud.VNetworkGateway().InternalV1().NetworkService().CreateTagsWithEndpointId(opt)
if sdkerr != nil {
t.Logf("Expect nil but got %+v", sdkerr.GetErrorCode())
}

t.Log("Result: ", sdkerr)
t.Log("PASS")
}

func TestDeleteTagByEndpointId(t *ltesting.T) {
vngcloud := validSuperSdkConfig()
opt := lsnwv1.NewDeleteTagOfEndpointRequest("60108", "pro-88265bae-d2ef-424b-b8a7-9eeb08aec1f7", "tag-6ceb41e1-47e9-43f0-94dd-521a1af870ee")

sdkerr := vngcloud.VNetworkGateway().InternalV1().NetworkService().DeleteTagOfEndpoint(opt)
if sdkerr != nil {
t.Logf("Expect nil but got %+v", sdkerr.GetErrorCode())
}

t.Log("Result: ", sdkerr)
t.Log("PASS")
}

func TestUpdateEndpointTag(t *ltesting.T) {
vngcloud := validSuperSdkConfig()
opt := lsnwv1.NewUpdateTagValueOfEndpointRequest("60108", "pro-88265bae-d2ef-424b-b8a7-9eeb08aec1f7", "tag-c6d6e343-ed13-4bf1-bf2e-e63a1a5e0eab", "cuonghahahah")

sdkerr := vngcloud.VNetworkGateway().InternalV1().NetworkService().UpdateTagValueOfEndpoint(opt)
if sdkerr != nil {
t.Logf("Expect nil but got %+v", sdkerr.GetErrorCode())
}

t.Log("Result: ", sdkerr)
t.Log("PASS")
}
26 changes: 24 additions & 2 deletions test/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
lctx "context"
"fmt"
ltesting "testing"

lgodotenv "github.com/joho/godotenv"
Expand Down Expand Up @@ -38,6 +39,7 @@ func validSdkConfig() lsclient.IClient {
sdkConfig := lsclient.NewSdkConfigure().
WithClientId(clientId).
WithClientSecret(clientSecret).
WithUserId(getValueOfEnv("VNGCLOUD_USER_ID")).
WithZoneId(getValueOfEnv("VNGCLOUD_ZONE_ID")).
WithProjectId(getValueOfEnv("VNGCLOUD_PROJECT_ID")).
WithIamEndpoint("https://iamapis.vngcloud.vn/accounts-api").
Expand Down Expand Up @@ -121,10 +123,12 @@ func validSuperSdkConfig() lsclient.IClient {
sdkConfig := lsclient.NewSdkConfigure().
WithClientId(clientId).
WithClientSecret(clientSecret).
WithZoneId(getValueOfEnv("VNGCLOUD_ZONE_ID")).
WithProjectId(getValueOfEnv("VNGCLOUD_PROJECT_ID")).
WithIamEndpoint("https://iamapis.vngcloud.vn/accounts-api").
WithVServerEndpoint("https://hcm-3.api.vngcloud.vn/vserver/vserver-gateway").
WithVLBEndpoint("https://hcm-3.api.vngcloud.vn/vserver/vlb-gateway")
WithVLBEndpoint("https://hcm-3.api.vngcloud.vn/vserver/vlb-gateway").
WithVNetworkEndpoint("https://vnetwork-hcm03-api.vngcloud.vn/vnetwork-gateway")

return lsclient.NewClient(lctx.TODO()).WithRetryCount(1).WithSleep(10).Configure(sdkConfig)
}
Expand All @@ -142,6 +146,20 @@ func validUser11412SdkConfig() lsclient.IClient {
return lsclient.NewClient(lctx.TODO()).WithRetryCount(1).WithSleep(10).Configure(sdkConfig)
}

func validUser11412() lsclient.IClient {
clientId, clientSecret := getValueOfEnv("USER_11412_CLIENT_ID"), getValueOfEnv("USER_11412_CLIENT_SECRET")
sdkConfig := lsclient.NewSdkConfigure().
WithClientId(clientId).
WithClientSecret(clientSecret).
WithProjectId(getValueOfEnv("USER_11412_PROJECT_ID")).
WithIamEndpoint("https://iamapis.vngcloud.vn/accounts-api").
WithVServerEndpoint("https://hcm-3.api.vngcloud.vn/vserver/vserver-gateway").
WithVLBEndpoint("https://hcm-3.api.vngcloud.vn/vserver/vlb-gateway").
WithVNetworkEndpoint("https://vnetwork-hcm03-api.vngcloud.vn/vnetwork-gateway")

return lsclient.NewClient(lctx.TODO()).WithRetryCount(1).WithSleep(10).Configure(sdkConfig)
}

func validSuperSdkConfig2() lsclient.IClient {
clientId, clientSecret := getValueOfEnv("VNGCLOUD_SUPER_CLIENT_ID"), getValueOfEnv("VNGCLOUD_SUPER_CLIENT_SECRET")
sdkConfig := lsclient.NewSdkConfigure().
Expand Down Expand Up @@ -219,10 +237,13 @@ func TestAuthenPass(t *ltesting.T) {
t.Error("This testcase MUST pass")
}

fmt.Println("RESULT 2:", token.Token)
t.Log("RESULT:", token)
t.Log("PASS")
}



func TestASuperuthenPass(t *ltesting.T) {
clientId, clientSecret := getValueOfEnv("VNGCLOUD_SUPER_CLIENT_ID"), getValueOfEnv("VNGCLOUD_SUPER_CLIENT_SECRET")
vngcloud := validSdkConfig()
Expand All @@ -233,7 +254,8 @@ func TestASuperuthenPass(t *ltesting.T) {
t.Error("This testcase MUST pass")
}

t.Log("RESULT:", token)
fmt.Println("RESULT 2:", token.Token)
t.Log("RESULT:", token.Token)
t.Log("PASS")
}

Expand Down
25 changes: 12 additions & 13 deletions test/lb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,9 @@ func TestCreateInterLoadBalancerSuccess3(t *ltesting.T) {

func TestCreateLoadBalancerSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lslbv2.NewCreateLoadBalancerRequest(
"cuongdm3-testlb-tags",
"lbp-96b6b072-aadb-4b58-9d5f-c16ad69d36aa",
"sub-27a0562d-07f9-4e87-81fd-e0ba9658f156").
opt := lslbv2.NewCreateLoadBalancerRequest("cuongdm3-testlb-tags", "", "").
WithPackageId("lbp-96b6b072-aadb-4b58-9d5f-c16ad69d36aa").
WithSubnetId("sub-27a0562d-07f9-4e87-81fd-e0ba9658f156").
WithTags("cuongdm3", "cuongdm33333", "vinhnt8", "vinhnt8888888").
WithListener(lslbv2.NewCreateListenerRequest("cuongdm3-test-listener", lslbv2.ListenerProtocolTCP, 80)).
WithPool(lslbv2.NewCreatePoolRequest("cuongdm3-test-pool", lslbv2.PoolProtocolTCP).
Expand All @@ -167,9 +166,9 @@ func TestCreateLoadBalancerSuccess(t *ltesting.T) {
func TestCreateLoadBalancerEmptyMemberSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lslbv2.NewCreateLoadBalancerRequest(
"cuongdm3-testlb-empty-members",
"lbp-96b6b072-aadb-4b58-9d5f-c16ad69d36aa",
"sub-27a0562d-07f9-4e87-81fd-e0ba9658f156").
"cuongdm3-testlb-empty-members", "", "").
WithPackageId("lbp-96b6b072-aadb-4b58-9d5f-c16ad69d36aa").
WithSubnetId("sub-27a0562d-07f9-4e87-81fd-e0ba9658f156").
WithTags("cuongdm3", "cuongdm33333", "vinhnt8", "vinhnt8888888").
WithListener(lslbv2.NewCreateListenerRequest("cuongdm3-test-listener", lslbv2.ListenerProtocolTCP, 80)).
WithPool(lslbv2.NewCreatePoolRequest("cuongdm3-test-pool", lslbv2.PoolProtocolTCP).
Expand Down Expand Up @@ -429,7 +428,7 @@ func TestUpdateListenerSuccess(t *ltesting.T) {

func TestListListenersByLoadBalancerId(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lslbv2.NewListListenersByLoadBalancerIdRequest("lb-8bd4ea07-ab40-483d-8387-124ed2f2cecb")
opt := lslbv2.NewListListenersByLoadBalancerIdRequest("lb-1d3a92bb-6ebd-4b19-ad4b-5f47f5953144")
listeners, sdkerr := vngcloud.VLBGateway().V2().LoadBalancerService().ListListenersByLoadBalancerId(opt)
if sdkerr != nil {
t.Fatalf("Expect nil but got %+v", sdkerr)
Expand All @@ -439,7 +438,7 @@ func TestListListenersByLoadBalancerId(t *ltesting.T) {
t.Fatalf("Expect not nil but got nil")
}

t.Log("Result: ", listeners)
t.Log("Result: ", listeners.At(0).DefaultPoolId)
t.Log("PASS")
}

Expand Down Expand Up @@ -546,8 +545,8 @@ func TestListTagsSuccess(t *ltesting.T) {

func TestCreateTagsSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lslbv2.NewCreateTagsRequest("lb-0d9bc46b-66db-4c57-8270-cd380226839d").
WithTags("cuongdm4", "cuongdm4", "vinhnt9", "vinhnt9")
opt := lslbv2.NewCreateTagsRequest("lb-3b53db2e-357a-406b-9c56-499f1c21a48c").
WithTags("vks-owned-cluster2", "none")
sdkErr := vngcloud.VLBGateway().V2().LoadBalancerService().CreateTags(opt)
if sdkErr != nil {
t.Fatalf("Expect nil but got %+v", sdkErr.GetMessage())
Expand All @@ -559,8 +558,8 @@ func TestCreateTagsSuccess(t *ltesting.T) {

func TestUpdateTagsSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lslbv2.NewUpdateTagsRequest("lb-0d9bc46b-66db-4c57-8270-cd380226839d").
WithTags("cuongdm4", "cuongdm5")
opt := lslbv2.NewUpdateTagsRequest("lb-39e1750b-7141-455e-a668-a03d53b0328b").
WithTags("vks-user", "cuongdm4")
sdkErr := vngcloud.VLBGateway().V2().LoadBalancerService().UpdateTags(opt)
if sdkErr != nil {
t.Fatalf("Expect nil but got %+v", sdkErr.GetMessage())
Expand Down
23 changes: 22 additions & 1 deletion test/portal_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package test

import (
ltesting "testing"

lserr "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/sdk_error"
lsportalV1 "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/services/portal/v1"
lsportalV2 "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/services/portal/v2"
ltesting "testing"
)

func TestGetPortalInfoFailed(t *ltesting.T) {
Expand Down Expand Up @@ -132,3 +133,23 @@ func TestGetQuotaByNamePass(t *ltesting.T) {
t.Log("RESULT:", quota)
t.Log("PASS")
}

func TestListProjects(t *ltesting.T) {
vngcloud := validSdkConfig()
projects, err := vngcloud.VServerGateway().V1().PortalService().ListProjects()
if err != nil {
t.Log("Error: ", err)
}

t.Log("Result: ", projects.At(0))
}

func TestListPortalUser11412(t *ltesting.T) {
vngcloud := validUser11412()
projects, err := vngcloud.VServerGateway().V1().PortalService().ListProjects()
if err != nil {
t.Log("Error: ", err)
}

t.Log("Result: ", projects.At(0))
}
17 changes: 14 additions & 3 deletions vngcloud/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package client
import (
lctx "context"
lhttp "net/http"
"strings"
lstr "strings"
lsync "sync"
ltime "time"

ljtime "github.com/cuongpiger/joat/timer"
lreq "github.com/imroc/req/v3"

lserr "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/sdk_error"
)

Expand Down Expand Up @@ -96,7 +97,7 @@ func (s *httpClient) WithReauthFunc(pauthOpt AuthOpts, preauthFunc func() (ISdkA
}

func (s *httpClient) DoRequest(purl string, preq IRequest) (*lreq.Response, lserr.IError) {
req := s.client.R().SetContext(s.context).SetHeaders(s.defaultHeaders).SetHeaders(preq.GetMoreHeaders())
req := s.client.R().SetContext(s.context).SetHeaders(s.getDefaultHeaders()).SetHeaders(preq.GetMoreHeaders())
if opt := preq.GetRequestBody(); opt != nil {
req.SetBodyJsonMarshal(opt)
}
Expand All @@ -111,7 +112,7 @@ func (s *httpClient) DoRequest(purl string, preq IRequest) (*lreq.Response, lser

var resp *lreq.Response
var err error
switch strings.ToUpper(preq.GetRequestMethod()) {
switch lstr.ToUpper(preq.GetRequestMethod()) {
case "POST":
resp, err = req.Post(purl)
case "GET":
Expand Down Expand Up @@ -216,6 +217,16 @@ func (s *httpClient) setAccessToken(pnewToken ISdkAuthentication) IHttpClient {
return s
}

func (s *httpClient) getDefaultHeaders() map[string]string {
s.mut.RLock()
defer s.mut.RUnlock()
if s.defaultHeaders == nil {
s.defaultHeaders = make(map[string]string)
}

return s.defaultHeaders
}

func newReauthFuture() *reauthFuture {
return &reauthFuture{
done: make(chan struct{}),
Expand Down
4 changes: 4 additions & 0 deletions vngcloud/entity/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func (s *LoadBalancer) GetId() string {
return s.UUID
}

func (s *LoadBalancer) GetName() string {
return s.Name
}

func (s *LoadBalancer) GetAddress() string {
return s.Address
}
Expand Down
18 changes: 18 additions & 0 deletions vngcloud/entity/portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,21 @@ func (s *ListQuotas) FindQuotaByName(name string) *Quota {

return nil
}

type ListPortals struct {
Items []*Portal
}

func NewListPortals() *ListPortals {
return &ListPortals{
Items: make([]*Portal, 0),
}
}

func (s *ListPortals) At(pindex int) *Portal {
if pindex < 0 || pindex >= len(s.Items) {
return nil
}

return s.Items[pindex]
}
8 changes: 5 additions & 3 deletions vngcloud/entity/tag.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package entity

type Tag struct {
Key string
Value string
SystemTag bool
Key string
Value string
SystemTag bool
ResourceId string
TagId string
}

type ListTags struct {
Expand Down
Loading

0 comments on commit d59c295

Please sign in to comment.