Skip to content

Commit

Permalink
[build][feat] api list volume by name
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed May 26, 2024
1 parent 1695256 commit c910b01
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 8 deletions.
52 changes: 52 additions & 0 deletions test/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,55 @@ func TestDeleteVolumeByIdSuccess(t *ltesting.T) {
t.Log("Result: ", sdkerr)
t.Log("PASS")
}

func TestListBlockVolumeSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := v2.NewListBlockVolumesRequest(1, 10)
volumes, sdkerr := vngcloud.VServerGateway().V2().VolumeService().ListBlockVolumes(opt)
if sdkerr != nil {
t.Fatalf("Expect nil but got %v", sdkerr)
}

if volumes == nil {
t.Fatalf("Expect not nil but got nil")
}

t.Log("Result: ", volumes)
t.Log("PASS")
}

func TestListBlockVolumeWithNameSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := v2.NewListBlockVolumesRequest(1, 10).WithName("pvc-24182151-aa4a-4a55-9572-f551c3d003aa")
volumes, sdkerr := vngcloud.VServerGateway().V2().VolumeService().ListBlockVolumes(opt)
if sdkerr != nil {
t.Fatalf("Expect nil but got %v", sdkerr)
}

if volumes == nil {
t.Fatalf("Expect not nil but got nil")
}

if volumes.Len() != 1 {
t.Fatalf("Expect 1 but got %d", volumes.Len())
}

t.Log("Result: ", volumes)
t.Log("PASS")
}

func TestListBlockVolumeWithFailure(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := v2.NewListBlockVolumesRequest(1, -10)
volumes, sdkerr := vngcloud.VServerGateway().V2().VolumeService().ListBlockVolumes(opt)
if sdkerr == nil {
t.Fatalf("Expect error but got nil")
}

if volumes != nil {
t.Fatalf("Expect nil but got %v", volumes)
}

t.Log("Result: ", sdkerr)
t.Log("PASS")
}
8 changes: 8 additions & 0 deletions vngcloud/entity/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ type Volume struct {
PersistentVolume bool
AttachedMachine []string
}

type ListVolumes struct {
Items []*Volume
}

func (s *ListVolumes) Len() int {
return len(s.Items)
}
18 changes: 17 additions & 1 deletion vngcloud/sdk_error/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
)

const (
patternOutOfPoc = "you do not have sufficient credits to complete the purchase"
patternOutOfPoc = "you do not have sufficient credits to complete the purchase"
patternPagingInvalid = "page or size invalid"
)

func ErrorHandler(perr error, popts ...func(psdkErr ISdkError)) ISdkError {
Expand Down Expand Up @@ -75,3 +76,18 @@ func WithErrorOutOfPoc(perrResp IErrorRespone) func(sdkError ISdkError) {
}
}
}

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

errMsg := perrResp.GetMessage()
if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternPagingInvalid) {
sdkError.WithErrorCode(EcPagingInvalid).
WithMessage(errMsg).
WithErrors(perrResp.GetError())
}
}
}
1 change: 1 addition & 0 deletions vngcloud/sdk_error/error_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const (
EcUnknownError = ErrorCode("UnknownError")

EcInternalServerError = ErrorCode("VngCloudApiInternalServerError")
EcPagingInvalid = ErrorCode("VngCloudApiPagingInvalid")
)

// Internal SDK error
Expand Down
1 change: 1 addition & 0 deletions vngcloud/services/volume/ivolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ import (
type IVolumeServiceV2 interface {
CreateBlockVolume(popts lsvolumeSvcV2.ICreateBlockVolumeRequest) (*lsentity.Volume, lserr.ISdkError)
DeleteBlockVolumeById(popts lsvolumeSvcV2.IDeleteBlockVolumeByIdRequest) lserr.ISdkError
ListBlockVolumes(popts lsvolumeSvcV2.IListBlockVolumesRequest) (*lsentity.ListVolumes, lserr.ISdkError)
}
5 changes: 5 additions & 0 deletions vngcloud/services/volume/v2/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ type VolumeServiceV2 struct {
func (s *VolumeServiceV2) getProjectId() string {
return s.VServerClient.GetProjectId()
}

const (
defaultPageListBlockVolumesRequest = 1
defaultSizeListBlockVolumesRequest = 10000
)
19 changes: 19 additions & 0 deletions vngcloud/services/volume/v2/blockvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,22 @@ func (s *VolumeServiceV2) DeleteBlockVolumeById(popts IDeleteBlockVolumeByIdRequ

return nil
}

func (s *VolumeServiceV2) ListBlockVolumes(popts IListBlockVolumesRequest) (*lsentity.ListVolumes, lserr.ISdkError) {
url := listBlockVolumesUrl(s.VServerClient, popts)
resp := new(ListBlockVolumesResponse)
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.WithErrorPagingInvalid(errResp)).
WithKVparameters("projectId", s.getProjectId()).
WithParameters(popts.ToMap())
}

return resp.ToEntityListVolumes(), nil
}
46 changes: 46 additions & 0 deletions vngcloud/services/volume/v2/blockvolume_request.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package v2

import (
lfmt "fmt"
ljparser "github.com/cuongpiger/joat/parser"
)

func NewCreateBlockVolumeRequest(pvolumeName, pvolumeType string, psize int64) ICreateBlockVolumeRequest {
opt := new(CreateBlockVolumeRequest)
opt.VolumeTypeId = pvolumeType
Expand All @@ -16,6 +21,13 @@ func NewDeleteBlockVolumeByIdRequest(pvolumeId string) IDeleteBlockVolumeByIdReq
return opt
}

func NewListBlockVolumesRequest(ppage, psize int) IListBlockVolumesRequest {
opt := new(ListBlockVolumesRequest)
opt.Page = ppage
opt.Size = psize
return opt
}

const (
CreateFromNew = CreateVolumeFrom("NEW")
CreateFromSnapshot = CreateVolumeFrom("SNAPSHOT")
Expand All @@ -39,6 +51,12 @@ type DeleteBlockVolumeByIdRequest struct {
BlockVolumeCommon
}

type ListBlockVolumesRequest struct {
Name string `q:"name,beempty"`
Page int `q:"page"`
Size int `q:"size"`
}

type (
CreateVolumeFrom string

Expand Down Expand Up @@ -123,3 +141,31 @@ func (s *CreateBlockVolumeRequest) WithTags(ptags ...string) ICreateBlockVolumeR

return s
}

func (s *ListBlockVolumesRequest) ToQuery() (string, error) {
parser, _ := ljparser.GetParser()
url, err := parser.UrlMe(s)

if err != nil {
return "", err
}

return url.String(), err
}

func (s *ListBlockVolumesRequest) GetDefaultQuery() string {
return lfmt.Sprintf("page=%d&size=%d&name=", defaultPageListBlockVolumesRequest, defaultSizeListBlockVolumesRequest)
}

func (s *ListBlockVolumesRequest) ToMap() map[string]interface{} {
return map[string]interface{}{
"name": s.Name,
"page": s.Page,
"size": s.Size,
}
}

func (s *ListBlockVolumesRequest) WithName(pname string) IListBlockVolumesRequest {
s.Name = pname
return s
}
52 changes: 45 additions & 7 deletions vngcloud/services/volume/v2/blockvolume_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ type CreateBlockVolumeResponse struct {
Data BlockVolume `json:"data"`
}

type ListBlockVolumesResponse struct {
Page int64 `json:"page"`
PageSize int64 `json:"pageSize"`
TotalPage int64 `json:"totalPage"`
TotalItem int64 `json:"totalItem"`
ListData []BlockVolume `json:"listData"`
}

type (
BlockVolume struct {
UUID string `json:"uuid"`
Expand All @@ -31,17 +39,47 @@ type (
)

func (s *CreateBlockVolumeResponse) ToEntityVolume() *lsentity.Volume {
return s.Data.toEntityVolume()
}

func (s *ListBlockVolumesResponse) ToEntityListVolumes() *lsentity.ListVolumes {
lstVolumes := new(lsentity.ListVolumes)
for _, vol := range s.ListData {
lstVolumes.Items = append(lstVolumes.Items, vol.toEntityVolume())
}

return lstVolumes
}

func (s *ListBlockVolumesResponse) toEntityVolume(pIdx int) *lsentity.Volume {
if s == nil {
return nil
}

if pIdx >= 0 && pIdx < len(s.ListData) {
vol := s.ListData[pIdx]
return &lsentity.Volume{
Id: vol.UUID,
Name: vol.Name,
Size: vol.Size,
Status: vol.Status,
CreatedAt: vol.CreatedAt,
UpdatedAt: vol.UpdatedAt,
VmId: vol.ServerID,
}
}

return nil
}

func (s *BlockVolume) toEntityVolume() *lsentity.Volume {
return &lsentity.Volume{
Id: s.Data.UUID,
Name: s.Data.Name,
Size: s.Data.Size,
Status: s.Data.Status,
CreatedAt: s.Data.CreatedAt,
UpdatedAt: s.Data.UpdatedAt,
VmId: s.Data.ServerID,
Id: s.UUID,
Name: s.Name,
Size: s.Size,
Status: s.Status,
CreatedAt: s.CreatedAt,
UpdatedAt: s.UpdatedAt,
VmId: s.ServerID,
}
}
7 changes: 7 additions & 0 deletions vngcloud/services/volume/v2/irequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ type ICreateBlockVolumeRequest interface {
type IDeleteBlockVolumeByIdRequest interface {
GetBlockVolumeId() string
}

type IListBlockVolumesRequest interface {
WithName(pname string) IListBlockVolumesRequest
ToQuery() (string, error)
GetDefaultQuery() string
ToMap() map[string]interface{}
}
11 changes: 11 additions & 0 deletions vngcloud/services/volume/v2/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ func deleteBlockVolumeByIdUrl(psc lsclient.IServiceClient, popts IDeleteBlockVol
"volumes",
popts.GetBlockVolumeId())
}

func listBlockVolumesUrl(psc lsclient.IServiceClient, popts IListBlockVolumesRequest) string {
query, err := popts.ToQuery()
if err != nil {
query = popts.GetDefaultQuery()
}

return psc.ServiceURL(
psc.GetProjectId(),
"volumes") + query
}

0 comments on commit c910b01

Please sign in to comment.