Skip to content

Commit

Permalink
[feat] api create virtual address cross project
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed Nov 2, 2024
1 parent 267309c commit a247f8a
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 3 deletions.
19 changes: 17 additions & 2 deletions test/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ func validSdkConfig() lsclient.IClient {
return lsclient.NewClient(lctx.TODO()).WithRetryCount(1).WithSleep(10).Configure(sdkConfig)
}

func validSdkConfigHanRegion() lsclient.IClient {
clientId, clientSecret := getEnv()
sdkConfig := lsclient.NewSdkConfigure().
WithClientId(clientId).
WithClientSecret(clientSecret).
WithUserId(getValueOfEnv("VNGCLOUD_USER_ID")).
WithZoneId(getValueOfEnv("VNGCLOUD_ZONE_ID")).
WithProjectId(getValueOfEnv("HAN01_PROJECT_ID")).
WithIamEndpoint("https://iamapis.vngcloud.vn/accounts-api").
WithVServerEndpoint("https://han-1.api.vngcloud.vn/vserver/vserver-gateway").
WithVLBEndpoint("https://han-1.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 validHcm3bSdkConfig() lsclient.IClient {
sdkConfig := lsclient.NewSdkConfigure().
WithClientId(getValueOfEnv("HCM3B_CLIENT_ID")).
Expand Down Expand Up @@ -242,8 +259,6 @@ func TestAuthenPass(t *ltesting.T) {
t.Log("PASS")
}



func TestASuperuthenPass(t *ltesting.T) {
clientId, clientSecret := getValueOfEnv("VNGCLOUD_SUPER_CLIENT_ID"), getValueOfEnv("VNGCLOUD_SUPER_CLIENT_SECRET")
vngcloud := validSdkConfig()
Expand Down
29 changes: 29 additions & 0 deletions test/virtualaddress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package test

import (
lsnetworkSvcV2 "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/services/network/v2"
ltesting "testing"
)

func TestCreateVirtualAddressCrossProject(t *ltesting.T) {
virtualAddressName := "cuongdm3-test-virtual-address"
projectId := "pro-5ce9da27-8ac9-40db-8743-d80f6cbf1491"
subnetId := "sub-33ec4719-915f-4818-85b0-f17bdf7f899b"

vngcloud := validSdkConfigHanRegion()
opt := lsnetworkSvcV2.NewCreateVirtualAddressCrossProjectRequest(
virtualAddressName, projectId, subnetId).
WithDescription("Private DNS endpoint address for VPC cuongdm3 created by vDNS. Please DO NOT delete this address.")
vaddr, err := vngcloud.VServerGateway().V2().NetworkService().CreateVirtualAddressCrossProject(opt)

if err != nil {
t.Errorf("Expect error to be nil but got %+v", err)
}

if vaddr == nil {
t.Errorf("Expect portal not to be nil but got nil")
}

t.Log("RESULT:", vaddr)
t.Log("PASS")
}
44 changes: 44 additions & 0 deletions vngcloud/entity/virtualaddress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package entity

type VirtualAddress struct {
Id string
Name string
EndpointAddress string
VpcId string
SubnetId string
Description string
SubnetCidr string
VpcCidr string
}

func (s *VirtualAddress) GetId() string {
return s.Id
}

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

func (s *VirtualAddress) GetEndpointAddress() string {
return s.EndpointAddress
}

func (s *VirtualAddress) GetVpcId() string {
return s.VpcId
}

func (s *VirtualAddress) GetSubnetId() string {
return s.SubnetId
}

func (s *VirtualAddress) GetDescription() string {
return s.Description
}

func (s *VirtualAddress) GetSubnetCidr() string {
return s.SubnetCidr
}

func (s *VirtualAddress) GetVpcCidr() string {
return s.VpcCidr
}
4 changes: 3 additions & 1 deletion vngcloud/services/network/inetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type INetworkServiceInternalV1 interface {

type INetworkServiceV2 interface {
// The group of Network APIs

GetNetworkById(popts lsnetworkSvcV2.IGetNetworkByIdRequest) (*lsentity.Network, lserr.IError)

// The group of Secgroup APIs
Expand All @@ -50,4 +49,7 @@ type INetworkServiceV2 interface {

// Servers
ListAllServersBySecgroupId(popts lsnetworkSvcV2.IListAllServersBySecgroupIdRequest) (*lsentity.ListServers, lserr.IError)

// Virtual Address API group
CreateVirtualAddressCrossProject(popts lsnetworkSvcV2.ICreateVirtualAddressCrossProjectRequest) (*lsentity.VirtualAddress, lserr.IError)
}
9 changes: 9 additions & 0 deletions vngcloud/services/network/v2/irequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,12 @@ type IDeleteAddressPairRequest interface {
type IListAllServersBySecgroupIdRequest interface {
GetSecgroupId() string
}

// The interface request group of Virtual Address API
type ICreateVirtualAddressCrossProjectRequest interface {
ToRequestBody() interface{}
ParseUserAgent() string
ToMap() map[string]interface{}
AddUserAgent(pagent ...string) ICreateVirtualAddressCrossProjectRequest
WithDescription(pdescription string) ICreateVirtualAddressCrossProjectRequest
}
9 changes: 9 additions & 0 deletions vngcloud/services/network/v2/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package v2

func NewCreateVirtualAddressCrossProjectRequest(pname, pprojectId, psubnetId string) ICreateVirtualAddressCrossProjectRequest {
opts := new(CreateVirtualAddressCrossProjectRequest)
opts.Name = pname
opts.CrossProjectRequest.ProjectId = pprojectId
opts.CrossProjectRequest.SubnetId = psubnetId
return opts
}
6 changes: 6 additions & 0 deletions vngcloud/services/network/v2/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,9 @@ func listAllServersBySecgroupIdUrl(psc lsclient.IServiceClient, popts IListAllSe
popts.GetSecgroupId(),
"servers")
}

func createVirtualAddressCrossProjectUrl(psc lsclient.IServiceClient) string {
return psc.ServiceURL(
psc.GetProjectId(),
"virtualIpAddress")
}
26 changes: 26 additions & 0 deletions vngcloud/services/network/v2/virtualaddress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package v2

import (
lsclient "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/client"
lsentity "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/entity"
lserr "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/sdk_error"
)

func (s *NetworkServiceV2) CreateVirtualAddressCrossProject(popts ICreateVirtualAddressCrossProjectRequest) (*lsentity.VirtualAddress, lserr.IError) {
url := createVirtualAddressCrossProjectUrl(s.VserverClient)
resp := new(CreateVirtualAddressCrossProjectResponse)
errResp := lserr.NewErrorResponse(lserr.NormalErrorType)
req := lsclient.NewRequest().
WithHeader("User-Agent", popts.ParseUserAgent()).
WithOkCodes(201).
WithJsonBody(popts.ToRequestBody()).
WithJsonResponse(resp).
WithJsonError(errResp)

if _, sdkErr := s.VserverClient.Post(url, req); sdkErr != nil {
return nil, lserr.SdkErrorHandler(sdkErr, errResp).
WithKVparameters(popts.ToMap())
}

return resp.ToEntityVirtualAddress(), nil
}
57 changes: 57 additions & 0 deletions vngcloud/services/network/v2/virtualaddress_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package v2

import lscommon "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/services/common"

/**
* The group of Virtual Address APIs
*/

// API Create virtual address cross project
type CreateVirtualAddressCrossProjectRequest struct {
// Name is the name of the virtual address.
Name string `json:"name"` // required

// Description is the description of the virtual address.
Description string `json:"description"`

// Contains the required information to create a virtual address cross project.
CrossProjectRequest struct {
// The project ID whose virtual address will be created.
ProjectId string `json:"projectId"` // required

// The subnet ID where the virtual address will be created.
SubnetId string `json:"subnetId"` // required

// The IP address of the virtual address.
IpAddress string `json:"ipAddress"`
} `json:"crossProjectRequest"` // required

// others...
lscommon.UserAgent
}

func (s *CreateVirtualAddressCrossProjectRequest) ToRequestBody() interface{} {
return s
}

func (s *CreateVirtualAddressCrossProjectRequest) ToMap() map[string]interface{} {
return map[string]interface{}{
"name": s.Name,
"description": s.Description,
"crossProjectRequest": map[string]interface{}{
"projectId": s.CrossProjectRequest.ProjectId,
"subnetId": s.CrossProjectRequest.SubnetId,
"ipAddress": s.CrossProjectRequest.IpAddress,
},
}
}

func (s *CreateVirtualAddressCrossProjectRequest) AddUserAgent(pagent ...string) ICreateVirtualAddressCrossProjectRequest {
s.UserAgent.AddUserAgent(pagent...)
return s
}

func (s *CreateVirtualAddressCrossProjectRequest) WithDescription(pdescription string) ICreateVirtualAddressCrossProjectRequest {
s.Description = pdescription
return s
}
32 changes: 32 additions & 0 deletions vngcloud/services/network/v2/virtualaddress_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package v2

import lsentity "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/entity"

type CreateVirtualAddressCrossProjectResponse struct {
Data struct {
Name string `json:"name"`
Description string `json:"description"`
NetworkName string `json:"networkName"`
NetworkCIDR string `json:"networkCIDR"`
SubnetCIDR string `json:"subnetCIDR"`
SubnetName string `json:"subnetName"`
UUID string `json:"uuid"`
NetworkID string `json:"networkId"`
SubnetID string `json:"subnetId"`
IPAddress string `json:"ipAddress"`
CreatedAt string `json:"createdAt"`
} `json:"data"`
}

func (s *CreateVirtualAddressCrossProjectResponse) ToEntityVirtualAddress() *lsentity.VirtualAddress {
return &lsentity.VirtualAddress{
Id: s.Data.UUID,
Name: s.Data.Name,
EndpointAddress: s.Data.IPAddress,
VpcId: s.Data.NetworkID,
SubnetId: s.Data.SubnetID,
Description: s.Data.Description,
SubnetCidr: s.Data.SubnetCIDR,
VpcCidr: s.Data.NetworkCIDR,
}
}

0 comments on commit a247f8a

Please sign in to comment.