From 077efe14a6b9b353c77f3a1c991f31f38ecffa4a Mon Sep 17 00:00:00 2001 From: "Cuong. Duong Manh" Date: Sat, 18 May 2024 18:21:46 +0700 Subject: [PATCH] [feat] api get network by id --- test/network_test.go | 40 +++++++++++++++++++ vngcloud/entity/network.go | 10 +++++ vngcloud/sdk_error/error_codes.go | 6 +++ vngcloud/sdk_error/network.go | 22 ++++++++++ vngcloud/services/network/inetwork.go | 4 ++ vngcloud/services/network/v2/common.go | 8 ++++ vngcloud/services/network/v2/irequest.go | 6 +++ vngcloud/services/network/v2/iresponse.go | 4 ++ vngcloud/services/network/v2/network.go | 27 +++++++++++++ .../services/network/v2/network_request.go | 11 +++++ .../services/network/v2/network_response.go | 23 +++++++++++ vngcloud/services/network/v2/url.go | 7 ++++ 12 files changed, 168 insertions(+) create mode 100644 test/network_test.go create mode 100644 vngcloud/entity/network.go create mode 100644 vngcloud/sdk_error/network.go create mode 100644 vngcloud/services/network/v2/network.go create mode 100644 vngcloud/services/network/v2/network_request.go create mode 100644 vngcloud/services/network/v2/network_response.go diff --git a/test/network_test.go b/test/network_test.go new file mode 100644 index 0000000..2d5bd08 --- /dev/null +++ b/test/network_test.go @@ -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") +} diff --git a/vngcloud/entity/network.go b/vngcloud/entity/network.go new file mode 100644 index 0000000..a766901 --- /dev/null +++ b/vngcloud/entity/network.go @@ -0,0 +1,10 @@ +package entity + +type Network struct { + Status string + ElasticIps []string + Name string + Id string + CreatedAt string + Cidr string +} diff --git a/vngcloud/sdk_error/error_codes.go b/vngcloud/sdk_error/error_codes.go index 784b2c3..381bbc4 100644 --- a/vngcloud/sdk_error/error_codes.go +++ b/vngcloud/sdk_error/error_codes.go @@ -35,3 +35,9 @@ const ( const ( EcVServerSecgroupRuleNotFound = ErrorCode("VngCloudVServerSecgroupRuleNotFound") ) + +// VServer network + +const ( + EcVServerNetworkNotFound = ErrorCode("VngCloudVServerNetworkNotFound") +) diff --git a/vngcloud/sdk_error/network.go b/vngcloud/sdk_error/network.go new file mode 100644 index 0000000..ef4de51 --- /dev/null +++ b/vngcloud/sdk_error/network.go @@ -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()) + } + } +} diff --git a/vngcloud/services/network/inetwork.go b/vngcloud/services/network/inetwork.go index 3f6d670..6f641f2 100644 --- a/vngcloud/services/network/inetwork.go +++ b/vngcloud/services/network/inetwork.go @@ -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) diff --git a/vngcloud/services/network/v2/common.go b/vngcloud/services/network/v2/common.go index adea78a..2b6386d 100644 --- a/vngcloud/services/network/v2/common.go +++ b/vngcloud/services/network/v2/common.go @@ -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 +} diff --git a/vngcloud/services/network/v2/irequest.go b/vngcloud/services/network/v2/irequest.go index 72c59c1..c889f3f 100644 --- a/vngcloud/services/network/v2/irequest.go +++ b/vngcloud/services/network/v2/irequest.go @@ -29,3 +29,9 @@ type IDeleteSecgroupRuleByIdRequest interface { type IListSecgroupRulesBySecgroupIdRequest interface { GetSecgroupId() string } + +// Network + +type IGetNetworkByIdRequest interface { + GetNetworkId() string +} diff --git a/vngcloud/services/network/v2/iresponse.go b/vngcloud/services/network/v2/iresponse.go index 5a1e78a..ac94d91 100644 --- a/vngcloud/services/network/v2/iresponse.go +++ b/vngcloud/services/network/v2/iresponse.go @@ -21,3 +21,7 @@ type ICreateSecgroupRuleResponse interface { type IListSecgroupRulesBySecgroupIdResponse interface { ToEntityListSecgroupRules() *lsentity.ListSecgroupRules } + +type IGetNetworkByIdResponse interface { + ToEntityNetwork() *lsentity.Network +} diff --git a/vngcloud/services/network/v2/network.go b/vngcloud/services/network/v2/network.go new file mode 100644 index 0000000..a19af5a --- /dev/null +++ b/vngcloud/services/network/v2/network.go @@ -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 +} diff --git a/vngcloud/services/network/v2/network_request.go b/vngcloud/services/network/v2/network_request.go new file mode 100644 index 0000000..4b069ce --- /dev/null +++ b/vngcloud/services/network/v2/network_request.go @@ -0,0 +1,11 @@ +package v2 + +func NewGetNetworkByIdRequest(pnetworkId string) IGetNetworkByIdRequest { + opt := new(GetNetworkByIdRequest) + opt.NetworkId = pnetworkId + return opt +} + +type GetNetworkByIdRequest struct { + NetworkCommon +} diff --git a/vngcloud/services/network/v2/network_response.go b/vngcloud/services/network/v2/network_response.go new file mode 100644 index 0000000..75ebf2d --- /dev/null +++ b/vngcloud/services/network/v2/network_response.go @@ -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, + } +} diff --git a/vngcloud/services/network/v2/url.go b/vngcloud/services/network/v2/url.go index 9826ba5..74bd308 100644 --- a/vngcloud/services/network/v2/url.go +++ b/vngcloud/services/network/v2/url.go @@ -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()) +}