Skip to content

Commit

Permalink
[feat] vserver gateway v2 - api get subnet
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed Jun 18, 2024
1 parent d66a099 commit 3a26eb3
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 14 deletions.
23 changes: 23 additions & 0 deletions test/subnet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package test

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

func TestGetSubnetByIdSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lsnetworkSvcV2.NewGetSubnetByIdRequest("net-4f35f173-e0fe-4202-9c2b-5121b558bcd3", "sub-27a0562d-07f9-4e87-81fd-e0ba9658f156")
network, err := vngcloud.VServerGateway().V2().NetworkService().GetSubnetById(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")
}
12 changes: 12 additions & 0 deletions vngcloud/entity/subnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package entity

type Subnet struct {
Id string
NetworkId string
Name string
Status string
Cidr string
RouteTableId string
InterfaceAclPolicyId string
InterfaceAclPolicyName string
}
5 changes: 3 additions & 2 deletions vngcloud/sdk_error/error_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ const (
// VServer network

const (
EcVServerNetworkNotFound = ErrorCode("VngCloudVServerNetworkNotFound")
EcVServerSubnetNotFound = ErrorCode("VngCloudVServerSubnetNotFound")
EcVServerNetworkNotFound = ErrorCode("VngCloudVServerNetworkNotFound")
EcVServerSubnetNotFound = ErrorCode("VngCloudVServerSubnetNotFound")
EcVServerSubnetNotBelongNetwork = ErrorCode("VngCloudVServerSubnetNotBelongNetwork")
)

// vServer volume
Expand Down
23 changes: 20 additions & 3 deletions vngcloud/sdk_error/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import (
)

const (
patternNetworkNotFound = "is not found"
patternSubnetNotFound = `subnet with id [^.]+ is not found`
patternNetworkNotFound = "is not found"
patternSubnetNotFound = `subnet with id [^.]+ is not found`
patternSubnetNotBelongNetwork = `subnet id: [^.]+ belong to network id: [^.]+ not found`
)

var (
regexErrorSubnetNotFound = lregexp.MustCompile(patternSubnetNotFound)
regexErrorSubnetNotFound = lregexp.MustCompile(patternSubnetNotFound)
regexErrorSubnetNotBelongNetwork = lregexp.MustCompile(patternSubnetNotBelongNetwork)
)

func WithErrorNetworkNotFound(perrResp IErrorRespone) func(sdkError ISdkError) {
Expand All @@ -29,6 +31,21 @@ func WithErrorNetworkNotFound(perrResp IErrorRespone) func(sdkError ISdkError) {
}
}

func WithErrorSubnetNotBelongNetwork(perrResp IErrorRespone) func(sdkError ISdkError) {
return func(sdkError ISdkError) {
if perrResp == nil {
return
}

errMsg := lstr.ToLower(lstr.TrimSpace(perrResp.GetMessage()))
if regexErrorSubnetNotBelongNetwork.FindString(errMsg) != "" {
sdkError.WithErrorCode(EcVServerSubnetNotBelongNetwork).
WithMessage(errMsg).
WithErrors(perrResp.GetError())
}
}
}

func WithErrorSubnetNotFound(perrResp IErrorRespone) func(sdkError ISdkError) {
return func(sdkError ISdkError) {
if perrResp == nil {
Expand Down
9 changes: 9 additions & 0 deletions vngcloud/services/common/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package common

type NetworkCommon struct {
NetworkId string
}

func (s *NetworkCommon) GetNetworkId() string {
return s.NetworkId
}
9 changes: 9 additions & 0 deletions vngcloud/services/common/subnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package common

type SubnetCommon struct {
SubnetId string
}

func (s *SubnetCommon) GetSubnetId() string {
return s.SubnetId
}
3 changes: 3 additions & 0 deletions vngcloud/services/network/inetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ type INetworkServiceV2 interface {
CreateSecgroupRule(popts lsnetworkSvcV2.ICreateSecgroupRuleRequest) (*lsentity.SecgroupRule, lserr.ISdkError)
DeleteSecgroupRuleById(popts lsnetworkSvcV2.IDeleteSecgroupRuleByIdRequest) lserr.ISdkError
ListSecgroupRulesBySecgroupId(popts lsnetworkSvcV2.IListSecgroupRulesBySecgroupIdRequest) (*lsentity.ListSecgroupRules, lserr.ISdkError)

// Subnet
GetSubnetById(popts lsnetworkSvcV2.IGetSubnetByIdRequest) (*lsentity.Subnet, lserr.ISdkError)
}
8 changes: 0 additions & 8 deletions vngcloud/services/network/v2/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,3 @@ type SecgroupCommon struct {
func (s *SecgroupCommon) GetSecgroupId() string {
return s.SecgroupId
}

type NetworkCommon struct {
NetworkId string
}

func (s *NetworkCommon) GetNetworkId() string {
return s.NetworkId
}
8 changes: 8 additions & 0 deletions vngcloud/services/network/v2/irequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ type IListSecgroupRulesBySecgroupIdRequest interface {
type IGetNetworkByIdRequest interface {
GetNetworkId() string
}

// Subnet

type IGetSubnetByIdRequest interface {
ParseUserAgent() string
GetNetworkId() string
GetSubnetId() string
}
4 changes: 3 additions & 1 deletion vngcloud/services/network/v2/network_request.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package v2

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

func NewGetNetworkByIdRequest(pnetworkId string) IGetNetworkByIdRequest {
opt := new(GetNetworkByIdRequest)
opt.NetworkId = pnetworkId
return opt
}

type GetNetworkByIdRequest struct {
NetworkCommon
lscommon.NetworkCommon
}
29 changes: 29 additions & 0 deletions vngcloud/services/network/v2/subnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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) GetSubnetById(popts IGetSubnetByIdRequest) (*lsentity.Subnet, lserr.ISdkError) {
url := getSubnetByIdUrl(s.VserverClient, popts)
resp := new(GetSubnetByIdResponse)
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(
"subnetId", popts.GetSubnetId(),
"projectId", s.getProjectId())
}

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

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

func NewGetSubnetByIdRequest(pnetworkId, psubnetId string) IGetSubnetByIdRequest {
opt := new(GetSubnetByIdRequest)
opt.NetworkId = pnetworkId
opt.SubnetId = psubnetId
return opt
}

type GetSubnetByIdRequest struct {
lscommon.UserAgent
lscommon.SubnetCommon
lscommon.NetworkCommon
}
29 changes: 29 additions & 0 deletions vngcloud/services/network/v2/subnet_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package v2

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

type GetSubnetByIdResponse struct {
UUID string `json:"uuid"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt,omitempty"`
Status string `json:"status"`
Cidr string `json:"cidr"`
NetworkUuid string `json:"networkUuid"`
RouteTableUuid string `json:"routeTableUuid,omitempty"`
Name string `json:"name"`
InterfaceAclPolicyUuid string `json:"interfaceAclPolicyUuid,omitempty"`
InterfaceAclPolicyName string `json:"interfaceAclPolicyName,omitempty"`
}

func (s *GetSubnetByIdResponse) ToEntitySubnet() *lsentity.Subnet {
return &lsentity.Subnet{
Id: s.UUID,
NetworkId: s.NetworkUuid,
Name: s.Name,
Status: s.Status,
Cidr: s.Cidr,
RouteTableId: s.RouteTableUuid,
InterfaceAclPolicyId: s.InterfaceAclPolicyUuid,
InterfaceAclPolicyName: s.InterfaceAclPolicyName,
}
}
9 changes: 9 additions & 0 deletions vngcloud/services/network/v2/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,12 @@ func getNetworkByIdUrl(psc lsclient.IServiceClient, popts IGetNetworkByIdRequest
"networks",
popts.GetNetworkId())
}

func getSubnetByIdUrl(psc lsclient.IServiceClient, popts IGetSubnetByIdRequest) string {
return psc.ServiceURL(
psc.GetProjectId(),
"networks",
popts.GetNetworkId(),
"subnets",
popts.GetSubnetId())
}

0 comments on commit 3a26eb3

Please sign in to comment.