Skip to content

Commit

Permalink
[feat] api get network by id
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed May 18, 2024
1 parent c5dd85f commit 077efe1
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package test

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

func TestGetNetworkByIdFailure(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lsnetworkSvcV2.NewGetNetworkByIdRequest("net-4f35f173-e0fe-4202-9c2b-5121b558bcd2")
network, err := vngcloud.VServerGateway().V2().NetworkService().GetNetworkById(opt)

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

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

t.Log("RESULT:", err)
t.Log("PASS")
}

func TestGetNetworkByIdSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lsnetworkSvcV2.NewGetNetworkByIdRequest("net-4f35f173-e0fe-4202-9c2b-5121b558bcd3")
network, err := vngcloud.VServerGateway().V2().NetworkService().GetNetworkById(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")
}
10 changes: 10 additions & 0 deletions vngcloud/entity/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package entity

type Network struct {
Status string
ElasticIps []string
Name string
Id string
CreatedAt string
Cidr string
}
6 changes: 6 additions & 0 deletions vngcloud/sdk_error/error_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ const (
const (
EcVServerSecgroupRuleNotFound = ErrorCode("VngCloudVServerSecgroupRuleNotFound")
)

// VServer network

const (
EcVServerNetworkNotFound = ErrorCode("VngCloudVServerNetworkNotFound")
)
22 changes: 22 additions & 0 deletions vngcloud/sdk_error/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sdk_error

import lstr "strings"

const (
patternNetworkNotFound = "is not found"
)

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

errMsg := perrResp.GetMessage()
if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternNetworkNotFound) {
sdkError.WithErrorCode(EcVServerNetworkNotFound).
WithMessage(errMsg).
WithErrors(perrResp.GetError())
}
}
}
4 changes: 4 additions & 0 deletions vngcloud/services/network/inetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
)

type INetworkServiceV2 interface {
// The group of Network APIs

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

// The group of Secgroup APIs

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

type NetworkCommon struct {
NetworkId string
}

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

// Network

type IGetNetworkByIdRequest interface {
GetNetworkId() string
}
4 changes: 4 additions & 0 deletions vngcloud/services/network/v2/iresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ type ICreateSecgroupRuleResponse interface {
type IListSecgroupRulesBySecgroupIdResponse interface {
ToEntityListSecgroupRules() *lsentity.ListSecgroupRules
}

type IGetNetworkByIdResponse interface {
ToEntityNetwork() *lsentity.Network
}
27 changes: 27 additions & 0 deletions vngcloud/services/network/v2/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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) GetNetworkById(popts IGetNetworkByIdRequest) (*lsentity.Network, lserr.ISdkError) {
url := getNetworkByIdUrl(s.VserverClient, popts)
resp := new(GetNetworkByIdResponse)
errResp := lserr.NewErrorResponse(lserr.NormalErrorType)
req := lsclient.NewRequest().
WithOkCodes(200).
WithJsonResponse(resp).
WithJsonError(errResp)

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

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

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

type GetNetworkByIdRequest struct {
NetworkCommon
}
23 changes: 23 additions & 0 deletions vngcloud/services/network/v2/network_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package v2

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

type GetNetworkByIdResponse struct {
Status string `json:"status"`
ElasticIps []string `json:"elasticIps"`
DisplayName string `json:"displayName"`
ID string `json:"id"`
CreatedAt string `json:"createdAt"`
Cidr string `json:"cidr"`
}

func (s *GetNetworkByIdResponse) ToEntityNetwork() *lsentity.Network {
return &lsentity.Network{
Status: s.Status,
ElasticIps: s.ElasticIps,
Name: s.DisplayName,
Id: s.ID,
CreatedAt: s.CreatedAt,
Cidr: s.Cidr,
}
}
7 changes: 7 additions & 0 deletions vngcloud/services/network/v2/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ func listSecgroupRulesBySecgroupIdUrl(psc lsclient.IServiceClient, popts IListSe
popts.GetSecgroupId(),
"secGroupRules")
}

func getNetworkByIdUrl(psc lsclient.IServiceClient, popts IGetNetworkByIdRequest) string {
return psc.ServiceURL(
psc.GetProjectId(),
"networks",
popts.GetNetworkId())
}

0 comments on commit 077efe1

Please sign in to comment.