Skip to content

Commit

Permalink
Merge pull request #85 from vngcloud/v2-aap
Browse files Browse the repository at this point in the history
V2 + Allow address pair
  • Loading branch information
cuongpiger authored Sep 10, 2024
2 parents 23714d0 + 74efbef commit 164a1f6
Show file tree
Hide file tree
Showing 17 changed files with 423 additions and 1 deletion.
55 changes: 55 additions & 0 deletions test/address_pair_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package test

import (
ltesting "testing"

lsnetworkSvcV2 "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/services/network/v2"
)

func TestGetAllAddressPairByVirtualSubnetId(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lsnetworkSvcV2.NewGetAllAddressPairByVirtualSubnetIdRequest(getValueOfEnv("VIRTUAL_SUBNET_ID"))
network, err := vngcloud.VServerGateway().V2().NetworkService().GetAllAddressPairByVirtualSubnetId(opt)

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

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

t.Log("RESULT:", network)
for _, addressPair := range network {
t.Logf("AddressPair: %+v", addressPair)
}
t.Log("PASS")
}

func TestSetAddressPairInVirtualSubnet(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lsnetworkSvcV2.NewSetAddressPairInVirtualSubnetRequest(getValueOfEnv("VIRTUAL_SUBNET_ID"), getValueOfEnv("NETWORK_INTERFACE_ID"), "10.30.1.28/30")
network, err := vngcloud.VServerGateway().V2().NetworkService().SetAddressPairInVirtualSubnet(opt)

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

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

t.Log("RESULT:", network)
}

func TestDeleteAddressPair(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lsnetworkSvcV2.NewDeleteAddressPairRequest(getValueOfEnv("ADDRESS_PAIR_ID"))
err := vngcloud.VServerGateway().V2().NetworkService().DeleteAddressPair(opt)

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

t.Log("PASS")
}
28 changes: 27 additions & 1 deletion test/subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func TestGetSubnetByIdSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lsnetworkSvcV2.NewGetSubnetByIdRequest("net-4f35f173-e0fe-4202-9c2b-5121b558bcd3", "sub-27a0562d-07f9-4e87-81fd-e0ba9658f156")
opt := lsnetworkSvcV2.NewGetSubnetByIdRequest(getValueOfEnv("NETWORK_ID"), getValueOfEnv("SUBNET_ID"))
network, err := vngcloud.VServerGateway().V2().NetworkService().GetSubnetById(opt)

if err != nil {
Expand All @@ -21,3 +21,29 @@ func TestGetSubnetByIdSuccess(t *ltesting.T) {
t.Log("RESULT:", network)
t.Log("PASS")
}

func TestUpdateSubnetById(t *ltesting.T) {
vngcloud := validSdkConfig()
updateBody := lsnetworkSvcV2.UpdateSubnetBody{
Name: "subnet-1",
CIDR: "10.30.0.0/24",
SecondarySubnetRequests: []lsnetworkSvcV2.SecondarySubnetUpdateBody{
{Name: "subnet3", CIDR: "10.30.6.0/24"},
{Name: "subnet2", CIDR: "10.30.7.0/24"},
},
}

opt := lsnetworkSvcV2.NewUpdateSubnetByIdRequest(getValueOfEnv("NETWORK_ID"), getValueOfEnv("SUBNET_ID"), &updateBody)
network, err := vngcloud.VServerGateway().V2().NetworkService().UpdateSubnetById(opt)

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

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

t.Log("RESULT:", network)
t.Log("PASS")
}
2 changes: 2 additions & 0 deletions vngcloud/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ func (s *httpClient) DoRequest(purl string, preq IRequest) (*lreq.Response, lser
resp, err = req.Delete(purl)
case "PUT":
resp, err = req.Put(purl)
case "PATCH":
resp, err = req.Patch(purl)
}

if err != nil && resp == nil {
Expand Down
1 change: 1 addition & 0 deletions vngcloud/client/iservice_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type IServiceClient interface {
Get(purl string, preq IRequest) (*lreq.Response, lsdkErr.IError)
Delete(purl string, preq IRequest) (*lreq.Response, lsdkErr.IError)
Put(purl string, preq IRequest) (*lreq.Response, lsdkErr.IError)
Patch(purl string, preq IRequest) (*lreq.Response, lsdkErr.IError)
}

type ISdkAuthentication interface {
Expand Down
1 change: 1 addition & 0 deletions vngcloud/client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
MethodGet = requestMethod("GET")
MethodPost = requestMethod("POST")
MethodPut = requestMethod("PUT")
MethodPatch = requestMethod("PATCH")
MethodDelete = requestMethod("DELETE")
)

Expand Down
4 changes: 4 additions & 0 deletions vngcloud/client/service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ func (s *serviceClient) Put(purl string, preq IRequest) (*lreq.Response, lsdkErr
return s.client.DoRequest(purl, preq.WithRequestMethod(MethodPut))
}

func (s *serviceClient) Patch(purl string, preq IRequest) (*lreq.Response, lsdkErr.IError) {
return s.client.DoRequest(purl, preq.WithRequestMethod(MethodPatch))
}

func (s *serviceClient) GetProjectId() string {
return s.projectId
}
Expand Down
10 changes: 10 additions & 0 deletions vngcloud/entity/address_pair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package entity

type AddressPair struct {
Id string
VirtualIpAddressId string
VirtualSubnetId string
NetworkInterfaceIp string
NetworkInterfaceId string
CIDR string
}
7 changes: 7 additions & 0 deletions vngcloud/entity/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ type Subnet struct {
RouteTableId string
InterfaceAclPolicyId string
InterfaceAclPolicyName string
SecondarySubnets []SubnetSecondaryRange
}

type SubnetSecondaryRange struct {
Id string
Name string
Cidr string
}
6 changes: 6 additions & 0 deletions vngcloud/services/network/inetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ type INetworkServiceV2 interface {

// Subnet
GetSubnetById(popts lsnetworkSvcV2.IGetSubnetByIdRequest) (*lsentity.Subnet, lserr.IError)
UpdateSubnetById(popts lsnetworkSvcV2.IUpdateSubnetByIdRequest) (*lsentity.Subnet, lserr.IError)

// Address Pair
GetAllAddressPairByVirtualSubnetId(popts lsnetworkSvcV2.IGetAllAddressPairByVirtualSubnetIdRequest) ([]*lsentity.AddressPair, lserr.IError)
SetAddressPairInVirtualSubnet(popts lsnetworkSvcV2.ISetAddressPairInVirtualSubnetRequest) (*lsentity.AddressPair, lserr.IError)
DeleteAddressPair(popts lsnetworkSvcV2.IDeleteAddressPairRequest) lserr.IError
}
60 changes: 60 additions & 0 deletions vngcloud/services/network/v2/address_pair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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) GetAllAddressPairByVirtualSubnetId(popts IGetAllAddressPairByVirtualSubnetIdRequest) ([]*lsentity.AddressPair, lserr.IError) {
url := getAllAddressPairByVirtualSubnetIdUrl(s.VserverClient, popts)
resp := new(GetAllAddressPairByVirtualSubnetIdResponse)
errResp := lserr.NewErrorResponse(lserr.NormalErrorType)
req := lsclient.NewRequest().
WithHeader("User-Agent", popts.ParseUserAgent()).
WithOkCodes(200).
WithJsonResponse(resp).
WithJsonError(errResp)

if _, sdkErr := s.VserverClient.Get(url, req); sdkErr != nil {
return nil, lserr.SdkErrorHandler(sdkErr, errResp)
// lserr.WithErrorSubnetNotBelongNetwork(sdkErr),
// lserr.WithErrorSubnetNotFound(errResp)).
// WithKVparameters(
// "virtualSubnetId", popts.GetVirtualSubnetId(),
// "projectId", s.getProjectId(),

}
return resp.ToListAddressPair(), nil
}

func (s *NetworkServiceV2) SetAddressPairInVirtualSubnet(popts ISetAddressPairInVirtualSubnetRequest) (*lsentity.AddressPair, lserr.IError) {
url := setAddressPairInVirtualSubnetUrl(s.VserverClient, popts)
resp := new(SetAddressPairInVirtualSubnetResponse)
errResp := lserr.NewErrorResponse(lserr.NormalErrorType)
req := lsclient.NewRequest().
WithHeader("User-Agent", popts.ParseUserAgent()).
WithOkCodes(200, 201, 202, 203, 204).
WithJsonBody(popts.ToRequestBody()).
WithJsonResponse(resp).
WithJsonError(errResp)

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

func (s *NetworkServiceV2) DeleteAddressPair(popts IDeleteAddressPairRequest) lserr.IError {
url := deleteAddressPairUrl(s.VserverClient, popts)
errResp := lserr.NewErrorResponse(lserr.NormalErrorType)
req := lsclient.NewRequest().
WithHeader("User-Agent", popts.ParseUserAgent()).
WithOkCodes(200, 201, 202, 203, 204).
WithJsonError(errResp)

if _, sdkErr := s.VserverClient.Delete(url, req); sdkErr != nil {
return lserr.SdkErrorHandler(sdkErr, errResp)
}
return nil
}
68 changes: 68 additions & 0 deletions vngcloud/services/network/v2/address_pair_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package v2

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

func NewGetAllAddressPairByVirtualSubnetIdRequest(psubnetId string) IGetAllAddressPairByVirtualSubnetIdRequest {
opt := new(GetAllAddressPairByVirtualSubnetIdRequest)
opt.VirtualSubnetId = psubnetId
return opt
}

type GetAllAddressPairByVirtualSubnetIdRequest struct {
lscommon.UserAgent
VirtualSubnetId string
}

func (s *GetAllAddressPairByVirtualSubnetIdRequest) GetVirtualSubnetId() string {
return s.VirtualSubnetId
}

// --------------------------------------------------------

func NewSetAddressPairInVirtualSubnetRequest(psubnetId, networkInterfaceID, CIDR string) ISetAddressPairInVirtualSubnetRequest {
opt := new(SetAddressPairInVirtualSubnetRequest)
opt.VirtualSubnetId = psubnetId
opt.AddressPairRequest = AddressPairRequest{
CIDR: CIDR,
InternalNetworkInterfaceId: networkInterfaceID,
}
return opt
}

type SetAddressPairInVirtualSubnetRequest struct {
lscommon.UserAgent
VirtualSubnetId string
AddressPairRequest AddressPairRequest
}

func (s *SetAddressPairInVirtualSubnetRequest) GetVirtualSubnetId() string {
return s.VirtualSubnetId
}

func (s *SetAddressPairInVirtualSubnetRequest) ToRequestBody() interface{} {
return s.AddressPairRequest
}

type AddressPairRequest struct {
CIDR string `json:"cidr"`
InternalNetworkInterfaceId string `json:"internalNetworkInterfaceId"`
}

// --------------------------------------------------------

func NewDeleteAddressPairRequest(addressPairID string) IDeleteAddressPairRequest {
opt := new(DeleteAddressPairRequest)
opt.AddressPairID = addressPairID
return opt
}

type DeleteAddressPairRequest struct {
lscommon.UserAgent
AddressPairID string
}

func (s *DeleteAddressPairRequest) GetAddressPairID() string {
return s.AddressPairID
}

// --------------------------------------------------------
52 changes: 52 additions & 0 deletions vngcloud/services/network/v2/address_pair_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package v2

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

type GetAllAddressPairByVirtualSubnetIdResponse struct {
Data []*AddressPairResponse `json:"data"`
}

type AddressPairResponse struct {
UUID string `json:"uuid"`
VirtualIpAddressId string `json:"virtualIpAddressId"`
VirtualSubnetId string `json:"virtualSubnetId"`
NetworkInterfaceIp string `json:"networkInterfaceIp"`
NetworkInterfaceId string `json:"networkInterfaceId"`

CIDR string `json:"cidr"`
// ID string `json:"id"`
// BackendSubnetId int `json:"backendSubnetId"`
// ProjectId string `json:"projectId"`
// CreatedAt string `json:"createdAt"`
// DeletedAt string `json:"deletedAt"`
}

func (s *GetAllAddressPairByVirtualSubnetIdResponse) ToListAddressPair() []*lsentity.AddressPair {
addressPairs := make([]*lsentity.AddressPair, 0)
for _, addressPair := range s.Data {
addressPairs = append(addressPairs, &lsentity.AddressPair{
Id: addressPair.UUID,
VirtualIpAddressId: addressPair.VirtualIpAddressId,
VirtualSubnetId: addressPair.VirtualSubnetId,
NetworkInterfaceIp: addressPair.NetworkInterfaceIp,
NetworkInterfaceId: addressPair.NetworkInterfaceId,
CIDR: addressPair.CIDR,
})
}
return addressPairs
}

type SetAddressPairInVirtualSubnetResponse struct {
Data *AddressPairResponse `json:"data"`
}

func (s *SetAddressPairInVirtualSubnetResponse) ToAddressPair() *lsentity.AddressPair {
return &lsentity.AddressPair{
Id: s.Data.UUID,
VirtualIpAddressId: s.Data.VirtualIpAddressId,
VirtualSubnetId: s.Data.VirtualSubnetId,
NetworkInterfaceIp: s.Data.NetworkInterfaceIp,
NetworkInterfaceId: s.Data.NetworkInterfaceId,
CIDR: s.Data.CIDR,
}
}
25 changes: 25 additions & 0 deletions vngcloud/services/network/v2/irequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,28 @@ type IGetSubnetByIdRequest interface {
GetNetworkId() string
GetSubnetId() string
}

type IUpdateSubnetByIdRequest interface {
ParseUserAgent() string
GetNetworkId() string
GetSubnetId() string
ToRequestBody() interface{}
}

// Address Pair

type IGetAllAddressPairByVirtualSubnetIdRequest interface {
GetVirtualSubnetId() string
ParseUserAgent() string
}

type ISetAddressPairInVirtualSubnetRequest interface {
GetVirtualSubnetId() string
ParseUserAgent() string
ToRequestBody() interface{}
}

type IDeleteAddressPairRequest interface {
ParseUserAgent() string
GetAddressPairID() string
}
19 changes: 19 additions & 0 deletions vngcloud/services/network/v2/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,22 @@ func (s *NetworkServiceV2) GetSubnetById(popts IGetSubnetByIdRequest) (*lsentity

return resp.ToEntitySubnet(), nil
}

func (s *NetworkServiceV2) UpdateSubnetById(popts IUpdateSubnetByIdRequest) (*lsentity.Subnet, lserr.IError) {
url := updateSubnetByIdUrl(s.VserverClient, popts)
resp := new(UpdateSubnetByIdResponse)
errResp := lserr.NewErrorResponse(lserr.NormalErrorType)
req := lsclient.NewRequest().
WithHeader("User-Agent", popts.ParseUserAgent()).
WithOkCodes(200, 201, 202, 203, 204).
WithJsonBody(popts.ToRequestBody()).
WithJsonResponse(resp).
WithJsonError(errResp)

_, sdkErr := s.VserverClient.Patch(url, req)
if sdkErr != nil {
return nil, lserr.SdkErrorHandler(sdkErr, errResp)
}

return resp.ToEntitySubnet(), nil
}
Loading

0 comments on commit 164a1f6

Please sign in to comment.