From ce611cde4917914537c6b40e5ecb1ecc55816e26 Mon Sep 17 00:00:00 2001 From: "Cuong. Duong Manh" Date: Thu, 30 May 2024 18:14:04 +0700 Subject: [PATCH] [feat] api get under volume id --- test/volume_test.go | 32 +++++++++++++++++++ vngcloud/entity/volume.go | 1 + vngcloud/sdk_error/volume.go | 4 ++- vngcloud/services/volume/ivolume.go | 1 + vngcloud/services/volume/v2/blockvolume.go | 20 ++++++++++++ .../services/volume/v2/blockvolume_request.go | 10 ++++++ .../volume/v2/blockvolume_response.go | 10 ++++++ vngcloud/services/volume/v2/irequest.go | 4 +++ vngcloud/services/volume/v2/url.go | 9 ++++++ 9 files changed, 90 insertions(+), 1 deletion(-) diff --git a/test/volume_test.go b/test/volume_test.go index 6a567d2..c833f82 100644 --- a/test/volume_test.go +++ b/test/volume_test.go @@ -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") +} diff --git a/vngcloud/entity/volume.go b/vngcloud/entity/volume.go index 23c73d2..d5ec12b 100644 --- a/vngcloud/entity/volume.go +++ b/vngcloud/entity/volume.go @@ -13,6 +13,7 @@ type Volume struct { UpdatedAt *string PersistentVolume bool AttachedMachine []string + UnderId string } type ListVolumes struct { diff --git a/vngcloud/sdk_error/volume.go b/vngcloud/sdk_error/volume.go index 052045f..cab6f93 100644 --- a/vngcloud/sdk_error/volume.go +++ b/vngcloud/sdk_error/volume.go @@ -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" @@ -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()) diff --git a/vngcloud/services/volume/ivolume.go b/vngcloud/services/volume/ivolume.go index c7cc0d8..6b65058 100644 --- a/vngcloud/services/volume/ivolume.go +++ b/vngcloud/services/volume/ivolume.go @@ -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) diff --git a/vngcloud/services/volume/v2/blockvolume.go b/vngcloud/services/volume/v2/blockvolume.go index c18a5bc..3d031d9 100644 --- a/vngcloud/services/volume/v2/blockvolume.go +++ b/vngcloud/services/volume/v2/blockvolume.go @@ -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 +} diff --git a/vngcloud/services/volume/v2/blockvolume_request.go b/vngcloud/services/volume/v2/blockvolume_request.go index a625244..533d164 100644 --- a/vngcloud/services/volume/v2/blockvolume_request.go +++ b/vngcloud/services/volume/v2/blockvolume_request.go @@ -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") @@ -90,6 +96,10 @@ type GetBlockVolumeByIdRequest struct { lscommon.BlockVolumeCommon } +type GetUnderBlockVolumeIdRequest struct { + lscommon.BlockVolumeCommon +} + type ( CreateVolumeFrom string EncryptType string diff --git a/vngcloud/services/volume/v2/blockvolume_response.go b/vngcloud/services/volume/v2/blockvolume_response.go index 53bc4ca..bcb1fc5 100644 --- a/vngcloud/services/volume/v2/blockvolume_response.go +++ b/vngcloud/services/volume/v2/blockvolume_response.go @@ -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() } @@ -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, + } +} diff --git a/vngcloud/services/volume/v2/irequest.go b/vngcloud/services/volume/v2/irequest.go index 76ae79a..4d6819f 100644 --- a/vngcloud/services/volume/v2/irequest.go +++ b/vngcloud/services/volume/v2/irequest.go @@ -57,3 +57,7 @@ type IDeleteSnapshotByIdRequest interface { GetSnapshotId() string GetBlockVolumeId() string } + +type IGetUnderBlockVolumeIdRequest interface { + GetBlockVolumeId() string +} diff --git a/vngcloud/services/volume/v2/url.go b/vngcloud/services/volume/v2/url.go index 6cce398..2d99f36 100644 --- a/vngcloud/services/volume/v2/url.go +++ b/vngcloud/services/volume/v2/url.go @@ -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", + ) +}