Skip to content

Commit

Permalink
[feat] api list server group policues
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed Mar 6, 2025
1 parent a65e89d commit b7a069a
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 5 deletions.
Binary file added .DS_Store
Binary file not shown.
23 changes: 23 additions & 0 deletions test/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ func getEnv() (string, string) {
return clientId, clientSecret
}

func getEnvCuongDm4() (string, string) {
envFile, _ := lgodotenv.Read("/Users/cuongdm8499/Me/git-cuongpiger/secret/work/vngcloud/iam/env")
clientId := envFile["CUONGDM4_CLIENT_ID"]
clientSecret := envFile["CUONGDM4_CLIENT_SECRET"]

return clientId, clientSecret
}

func getEnvDevOps() (string, string) {
envFile, _ := lgodotenv.Read("./env.yaml")
clientId := envFile["CLIENT_ID_DEVOPS"]
Expand Down Expand Up @@ -69,6 +77,21 @@ func validUserSdkConfig() lsclient.IClient {
return lsclient.NewClient(lctx.TODO()).WithRetryCount(1).WithSleep(10).Configure(sdkConfig)
}

func validUserSdkConfigForCuongDm4() lsclient.IClient {
clientId, clientSecret := getEnvCuongDm4()
sdkConfig := lsclient.NewSdkConfigure().
WithClientId(clientId).
WithClientSecret(clientSecret).
WithProjectId("pro-462803f3-6858-466f-bf05-df2b33faa360").
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.vngcloud.vn/vnetwork-gateway/vnetwork").
WithVNetworkEndpoint("https://hcm-3.console.vngcloud.vn/vserver/vnetwork-gateway/vnetwork")

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

func validSdkConfigHanRegion() lsclient.IClient {
clientId, clientSecret := getEnv()
sdkConfig := lsclient.NewSdkConfigure().
Expand Down
14 changes: 12 additions & 2 deletions test/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ func TestCreateDnsServer(t *ltesting.T) {
t.Log("PASS")
}


func TestCreateServerVks(t *ltesting.T) {
vngcloud := validUserSdkConfig()

Expand All @@ -340,4 +339,15 @@ func TestCreateServerVks(t *ltesting.T) {

t.Logf("Result: %v", server)
t.Logf("PASS")
}
}

func TestListServerGroupPolicies(t *ltesting.T) {
vngcloud := validUserSdkConfigForCuongDm4()
opt := lscomputeSvcV2.NewListServerGroupPoliciesRequest()
policies, sdkerr := vngcloud.VServerGateway().V2().ComputeService().ListServerGroupPolicies(opt)
if sdkerr != nil {
t.Fatalf("Expect nil but got %v", sdkerr.GetErrorCode())
}

t.Logf("Result: %v", policies)
}
2 changes: 0 additions & 2 deletions vngcloud/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client

import (
lctx "context"
"fmt"
lhttp "net/http"
lstr "strings"
lsync "sync"
Expand Down Expand Up @@ -101,7 +100,6 @@ func (s *httpClient) DoRequest(purl string, preq IRequest) (*lreq.Response, lser
req := s.client.R().SetContext(s.context).SetHeaders(s.getDefaultHeaders()).SetHeaders(preq.GetMoreHeaders())
if opt := preq.GetRequestBody(); opt != nil {
req.SetBodyJsonMarshal(opt)
fmt.Println("RRRRRRRRRRRR: ", string(req.Body))
}

if opt := preq.GetJsonResponse(); opt != nil {
Expand Down
16 changes: 16 additions & 0 deletions vngcloud/entity/servergroup_policies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package entity

type ServerGroupPolicy struct {
Name string
UUID string
Status string
Descriptions map[string]string
}

type ListServerGroupPolicies struct {
Items []*ServerGroupPolicy
}

func (s *ListServerGroupPolicies) Add(item *ServerGroupPolicy) {
s.Items = append(s.Items, item)
}
1 change: 1 addition & 0 deletions vngcloud/services/compute/icompute.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ type IComputeServiceV2 interface {
DetachBlockVolume(popts lscomputeSvcV2.IDetachBlockVolumeRequest) lserr.IError
AttachFloatingIp(popts lscomputeSvcV2.IAttachFloatingIpRequest) lserr.IError
DetachFloatingIp(popts lscomputeSvcV2.IDetachFloatingIpRequest) lserr.IError
ListServerGroupPolicies(popts lscomputeSvcV2.IListServerGroupPoliciesRequest) (*lsentity.ListServerGroupPolicies, lserr.IError)
}
5 changes: 5 additions & 0 deletions vngcloud/services/compute/v2/irequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ type IDetachFloatingIpRequest interface {
ParseUserAgent() string
ToMap() map[string]interface{}
}

type IListServerGroupPoliciesRequest interface {
AddUserAgent(pagent ...string) IListServerGroupPoliciesRequest
ParseUserAgent() string
}
6 changes: 5 additions & 1 deletion vngcloud/services/compute/v2/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@ func NewDetachFloatingIpRequest(pserverId, pwanId, pniid string) IDetachFloating
opt.NetworkInterfaceId = pniid
opt.WanId = pwanId
return opt
}
}

func NewListServerGroupPoliciesRequest() IListServerGroupPoliciesRequest {
return new(ListServerGroupPoliciesRequest)
}
21 changes: 21 additions & 0 deletions vngcloud/services/compute/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,24 @@ func (s *ComputeServiceV2) DetachFloatingIp(popts IDetachFloatingIpRequest) lser

return nil
}

func (s *ComputeServiceV2) ListServerGroupPolicies(popts IListServerGroupPoliciesRequest) (*lsentity.ListServerGroupPolicies, lserr.IError) {
url := listServerGroupPolicies(s.VServerClient)
resp := new(ListServerGroupPoliciesResponse)
errResp := lserr.NewErrorResponse(lserr.NormalErrorType)
req := lsclient.NewRequest().
WithOkCodes(200).
WithJsonResponse(resp).
WithJsonError(errResp)

if popts != nil {
req = req.WithHeader("User-Agent", popts.ParseUserAgent())
}

if _, sdkErr := s.VServerClient.Get(url, req); sdkErr != nil {
return nil, lserr.SdkErrorHandler(sdkErr, errResp).
WithKVparameters("projectId", s.getProjectId())
}

return resp.ToEntityListServerGroupPolicies(), nil
}
9 changes: 9 additions & 0 deletions vngcloud/services/compute/v2/server_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,3 +298,12 @@ func (s *DetachFloatingIpRequest) ToMap() map[string]interface{} {
"wanId": s.WanId,
}
}

type ListServerGroupPoliciesRequest struct {
lscommon.UserAgent
}

func (s *ListServerGroupPoliciesRequest) AddUserAgent(pagent ...string) IListServerGroupPoliciesRequest {
s.UserAgent.Agent = append(s.UserAgent.Agent, pagent...)
return s
}
32 changes: 32 additions & 0 deletions vngcloud/services/compute/v2/server_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ type (
Name string `json:"name"`
Uuid string `json:"uuid"`
}

ServerSecgroupPolicy struct {
Name string `json:"name"`
UUID string `json:"uuid"`
Status string `json:"status"`
Description string `json:"description"`
DescriptionVi string `json:"descriptionVi"`
}

ListServerGroupPoliciesResponse struct {
Data []ServerSecgroupPolicy `json:"data"`
}
)

func (s Image) toEntityImage() lsentity.Image {
Expand All @@ -96,6 +108,18 @@ func (s Image) toEntityImage() lsentity.Image {
}
}

func (s ServerSecgroupPolicy) toEntityServerGroupPolicy() *lsentity.ServerGroupPolicy {
return &lsentity.ServerGroupPolicy{
Name: s.Name,
UUID: s.UUID,
Status: s.Status,
Descriptions: map[string]string{
"en": s.Description,
"vi": s.DescriptionVi,
},
}
}

func (s NetworkInterface) toEntityNetworkInterface() lsentity.NetworkInterface {
return lsentity.NetworkInterface{
CreatedAt: s.CreatedAt,
Expand Down Expand Up @@ -199,3 +223,11 @@ type UpdateServerSecgroupsByServerIdResponse struct {
func (s *UpdateServerSecgroupsByServerIdResponse) ToEntityServer() *lsentity.Server {
return s.Data.toEntityServer()
}

func (s *ListServerGroupPoliciesResponse) ToEntityListServerGroupPolicies() *lsentity.ListServerGroupPolicies {
serverGroupPolicies := &lsentity.ListServerGroupPolicies{}
for _, itemServerGroupPolicy := range s.Data {
serverGroupPolicies.Add(itemServerGroupPolicy.toEntityServerGroupPolicy())
}
return serverGroupPolicies
}
8 changes: 8 additions & 0 deletions vngcloud/services/compute/v2/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ func detachFloatingIpUrl(psc lsclient.IServiceClient, popts IDetachFloatingIpReq
popts.GetWanId(),
"detach")
}

func listServerGroupPolicies(psc lsclient.IServiceClient) string {
return psc.ServiceURL(
psc.GetProjectId(),
"serverGroups",
"policies",
)
}

0 comments on commit b7a069a

Please sign in to comment.