Skip to content

Commit

Permalink
[feat] api get under volume id
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed May 30, 2024
1 parent 311cf79 commit ce611cd
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 1 deletion.
32 changes: 32 additions & 0 deletions test/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,35 @@ func TestResizeBlockVolumeFailure(t *ltesting.T) {
t.Log("Result: ", sdkerr)
t.Log("PASS")
}

func TestGetUnderVolumeIdFailure(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := v2.NewGetUnderVolumeIdRequest("vol-ae3fffe5-bd46-475f-besadd")
volume, sdkerr := vngcloud.VServerGateway().V2().VolumeService().GetUnderBlockVolumeId(opt)
if sdkerr == nil {
t.Fatalf("Expect error but got nil")
}

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

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

func TestGetUnderVolumeIdSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := v2.NewGetUnderVolumeIdRequest("vol-137f3dfc-9198-4d94-983f-6802e3c39e4f")
volume, sdkerr := vngcloud.VServerGateway().V2().VolumeService().GetUnderBlockVolumeId(opt)
if sdkerr != nil {
t.Fatalf("Expect nil but got %v", sdkerr)
}

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

t.Log("Result: ", volume)
t.Log("PASS")
}
1 change: 1 addition & 0 deletions vngcloud/entity/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Volume struct {
UpdatedAt *string
PersistentVolume bool
AttachedMachine []string
UnderId string
}

type ListVolumes struct {
Expand Down
4 changes: 3 additions & 1 deletion vngcloud/sdk_error/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
patternVolumeSizeOutOfRange = "field volume_size must from"
patternVolumeNewSizeOutOfRange = "field new_volume_size must from"
patternVolumeNotFound = `volume with id [^.]+ is not found`
patternVolumeNotFound2 = "cannot get volume with id"
patternVolumeAvailable = "this volume is available"
patternVolumeAlreadyAttached = "already attached to instance"
patternVolumeAlreadyAttachedThisServer = "this volume has been attached"
Expand Down Expand Up @@ -76,7 +77,8 @@ func WithErrorVolumeNotFound(perrResp IErrorRespone) func(sdkError ISdkError) {
}

errMsg := lstr.ToLower(lstr.TrimSpace(perrResp.GetMessage()))
if regexErrorVolumeNotFound.FindString(errMsg) != "" {
if regexErrorVolumeNotFound.FindString(errMsg) != "" ||
lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternVolumeNotFound2) {
sdkError.WithErrorCode(EcVServerVolumeNotFound).
WithMessage(errMsg).
WithErrors(perrResp.GetError())
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 @@ -12,6 +12,7 @@ type IVolumeServiceV2 interface {
ListBlockVolumes(popts lsvolumeSvcV2.IListBlockVolumesRequest) (*lsentity.ListVolumes, lserr.ISdkError)
GetBlockVolumeById(popts lsvolumeSvcV2.IGetBlockVolumeByIdRequest) (*lsentity.Volume, lserr.ISdkError)
ResizeBlockVolumeById(popts lsvolumeSvcV2.IResizeBlockVolumeByIdRequest) (*lsentity.Volume, lserr.ISdkError)
GetUnderBlockVolumeId(popts lsvolumeSvcV2.IGetUnderBlockVolumeIdRequest) (*lsentity.Volume, lserr.ISdkError)

// Snapshot
ListSnapshotByVolumeId(popts lsvolumeSvcV2.IListSnapshotsByBlockVolumeIdRequest) (*lsentity.ListSnapshots, lserr.ISdkError)
Expand Down
20 changes: 20 additions & 0 deletions vngcloud/services/volume/v2/blockvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,23 @@ func (s *VolumeServiceV2) ResizeBlockVolumeById(popts IResizeBlockVolumeByIdRequ

return resp.ToEntityVolume(), nil
}

func (s *VolumeServiceV2) GetUnderBlockVolumeId(popts IGetUnderBlockVolumeIdRequest) (*lsentity.Volume, lserr.ISdkError) {
url := getUnderBlockVolumeIdUrl(s.VServerClient, popts)
resp := new(GetUnderBlockVolumeIdResponse)
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.WithErrorVolumeNotFound(errResp)).
WithKVparameters(
"projectId", s.getProjectId(),
"volumeId", popts.GetBlockVolumeId())
}

return resp.ToEntityVolume(), nil
}
10 changes: 10 additions & 0 deletions vngcloud/services/volume/v2/blockvolume_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ func NewResizeBlockVolumeByIdRequest(pvolumeId, pvolumeType string, psize int) I
return opt
}

func NewGetUnderVolumeIdRequest(pvolumeId string) IGetUnderBlockVolumeIdRequest {
opt := new(GetUnderBlockVolumeIdRequest)
opt.BlockVolumeId = pvolumeId
return opt
}

const (
CreateFromNew = CreateVolumeFrom("NEW")
CreateFromSnapshot = CreateVolumeFrom("SNAPSHOT")
Expand Down Expand Up @@ -90,6 +96,10 @@ type GetBlockVolumeByIdRequest struct {
lscommon.BlockVolumeCommon
}

type GetUnderBlockVolumeIdRequest struct {
lscommon.BlockVolumeCommon
}

type (
CreateVolumeFrom string
EncryptType string
Expand Down
10 changes: 10 additions & 0 deletions vngcloud/services/volume/v2/blockvolume_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type ResizeBlockVolumeByIdResponse struct {
Data BlockVolume `json:"data"`
}

type GetUnderBlockVolumeIdResponse struct {
Uuid string `json:"uuid"`
}

func (s *GetBlockVolumeByIdResponse) ToEntityVolume() *lsentity.Volume {
return s.Data.toEntityVolume()
}
Expand Down Expand Up @@ -101,3 +105,9 @@ func (s *BlockVolume) toEntityVolume() *lsentity.Volume {
func (s *ResizeBlockVolumeByIdResponse) ToEntityVolume() *lsentity.Volume {
return s.Data.toEntityVolume()
}

func (s *GetUnderBlockVolumeIdResponse) ToEntityVolume() *lsentity.Volume {
return &lsentity.Volume{
UnderId: s.Uuid,
}
}
4 changes: 4 additions & 0 deletions vngcloud/services/volume/v2/irequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ type IDeleteSnapshotByIdRequest interface {
GetSnapshotId() string
GetBlockVolumeId() string
}

type IGetUnderBlockVolumeIdRequest interface {
GetBlockVolumeId() string
}
9 changes: 9 additions & 0 deletions vngcloud/services/volume/v2/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,12 @@ func deleteSnapshotByIdUrl(psc lsclient.IServiceClient, popts IDeleteSnapshotByI
popts.GetSnapshotId(),
)
}

func getUnderBlockVolumeIdUrl(psc lsclient.IServiceClient, popts IGetUnderBlockVolumeIdRequest) string {
return psc.ServiceURL(
psc.GetProjectId(),
"volumes",
popts.GetBlockVolumeId(),
"mapping",
)
}

0 comments on commit ce611cd

Please sign in to comment.