Skip to content

Commit

Permalink
[build][feat] api delete volume
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed May 26, 2024
1 parent 8fcb841 commit 1695256
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 1 deletion.
24 changes: 24 additions & 0 deletions test/volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,27 @@ func TestCreateVolumeSuccess(t *ltesting.T) {
t.Log("Result: ", volume)
t.Log("PASS")
}

func TestDeleteVolumeByIdFailure(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := v2.NewDeleteBlockVolumeByIdRequest("this-is-fake")
sdkerr := vngcloud.VServerGateway().V2().VolumeService().DeleteBlockVolumeById(opt)
if sdkerr == nil {
t.Fatalf("Expect error but got nil")
}

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

func TestDeleteVolumeByIdSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := v2.NewDeleteBlockVolumeByIdRequest("vol-51f71146-9c20-4615-a73e-a43a39bf03ea")
sdkerr := vngcloud.VServerGateway().V2().VolumeService().DeleteBlockVolumeById(opt)
if sdkerr != nil {
t.Fatalf("Expect nil but got %v", sdkerr)
}

t.Log("Result: ", sdkerr)
t.Log("PASS")
}
1 change: 1 addition & 0 deletions vngcloud/sdk_error/error_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const (
EcVServerVolumeTypeNotFound = ErrorCode("VngCloudVServerVolumeTypeNotFound")
EcVServerVolumeNameNotValid = ErrorCode("VngCloudVServerVolumeNameNotValid")
EcVServerVolumeSizeOutOfRange = ErrorCode("VngCloudVServerVolumeSizeOutOfRange")
EcVServerVolumeNotFound = ErrorCode("VngCloudVServerVolumeNotFound")
)

// Billing
Expand Down
25 changes: 24 additions & 1 deletion vngcloud/sdk_error/volume.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package sdk_error

import lstr "strings"
import (
lregexp "regexp"
lstr "strings"
)

const (
patternVolumeTypeNotFound = "cannot get volume type with id" // "Cannot get volume type with id vtype-6790f903-38d2-454d-919e-5b49184b5927"
patternVolumeNameNotValid = "only letters (a-z, a-z, 0-9, '.', '@', '_', '-', space) are allowed. your input data length must be between 5 and 50" // "Volume name is not valid"
patternVolumeSizeOutOfRange = "field volume_size must from"
patternVolumeNotFound = `volume with id [^.]+ is not found`
)

var (
regexErrorVolumeNotFound = lregexp.MustCompile(patternVolumeNotFound)
)

func WithErrorVolumeTypeNotFound(perrResp IErrorRespone) func(sdkError ISdkError) {
Expand Down Expand Up @@ -52,3 +60,18 @@ func WithErrorVolumeSizeOutOfRange(perrResp IErrorRespone) func(sdkError ISdkErr
}
}
}

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

errMsg := lstr.ToLower(lstr.TrimSpace(perrResp.GetMessage()))
if regexErrorVolumeNotFound.FindString(errMsg) != "" {
sdkError.WithErrorCode(EcVServerVolumeNotFound).
WithMessage(errMsg).
WithErrors(perrResp.GetError())
}
}
}
1 change: 1 addition & 0 deletions vngcloud/services/volume/ivolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ import (

type IVolumeServiceV2 interface {
CreateBlockVolume(popts lsvolumeSvcV2.ICreateBlockVolumeRequest) (*lsentity.Volume, lserr.ISdkError)
DeleteBlockVolumeById(popts lsvolumeSvcV2.IDeleteBlockVolumeByIdRequest) lserr.ISdkError
}
18 changes: 18 additions & 0 deletions vngcloud/services/volume/v2/blockvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,21 @@ func (s *VolumeServiceV2) CreateBlockVolume(popts ICreateBlockVolumeRequest) (*l

return resp.ToEntityVolume(), nil
}

func (s *VolumeServiceV2) DeleteBlockVolumeById(popts IDeleteBlockVolumeByIdRequest) lserr.ISdkError {
url := deleteBlockVolumeByIdUrl(s.VServerClient, popts)
errResp := lserr.NewErrorResponse(lserr.NormalErrorType)
req := lsclient.NewRequest().
WithOkCodes(202).
WithJsonError(errResp)

if _, sdkErr := s.VServerClient.Delete(url, req); sdkErr != nil {
return lserr.SdkErrorHandler(sdkErr, errResp,
lserr.WithErrorVolumeNotFound(errResp)).
WithKVparameters(
"projectId", s.getProjectId(),
"volumeId", popts.GetBlockVolumeId())
}

return 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 @@ -10,6 +10,12 @@ func NewCreateBlockVolumeRequest(pvolumeName, pvolumeType string, psize int64) I
return opt
}

func NewDeleteBlockVolumeByIdRequest(pvolumeId string) IDeleteBlockVolumeByIdRequest {
opt := new(DeleteBlockVolumeByIdRequest)
opt.BlockVolumeId = pvolumeId
return opt
}

const (
CreateFromNew = CreateVolumeFrom("NEW")
CreateFromSnapshot = CreateVolumeFrom("SNAPSHOT")
Expand All @@ -29,6 +35,10 @@ type CreateBlockVolumeRequest struct {
ConfigureVolumeRestore *ConfigureVolumeRestore `json:"configVolumeRestore,omitempty"`
}

type DeleteBlockVolumeByIdRequest struct {
BlockVolumeCommon
}

type (
CreateVolumeFrom string

Expand Down
8 changes: 8 additions & 0 deletions vngcloud/services/volume/v2/common.go
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
package v2

type BlockVolumeCommon struct {
BlockVolumeId string
}

func (s *BlockVolumeCommon) GetBlockVolumeId() string {
return s.BlockVolumeId
}
4 changes: 4 additions & 0 deletions vngcloud/services/volume/v2/irequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ type ICreateBlockVolumeRequest interface {
WithVolumeRestoreFromSnapshot(psnapshotID, pvolumeTypeID string) ICreateBlockVolumeRequest
WithTags(ptags ...string) ICreateBlockVolumeRequest
}

type IDeleteBlockVolumeByIdRequest interface {
GetBlockVolumeId() string
}
7 changes: 7 additions & 0 deletions vngcloud/services/volume/v2/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ func createBlockVolumeUrl(psc lsclient.IServiceClient) string {
psc.GetProjectId(),
"volumes")
}

func deleteBlockVolumeByIdUrl(psc lsclient.IServiceClient, popts IDeleteBlockVolumeByIdRequest) string {
return psc.ServiceURL(
psc.GetProjectId(),
"volumes",
popts.GetBlockVolumeId())
}

0 comments on commit 1695256

Please sign in to comment.