Skip to content

Commit

Permalink
[enhance] rollbackcode
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed May 21, 2024
1 parent 0dc856f commit 3b26908
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
4 changes: 3 additions & 1 deletion vngcloud/sdk_error/error_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ const (
// vServer server
const (
EcVServerServerNotFound = ErrorCode("VngCloudVServerServerNotFound")
EcVServerServerNotAllowDeleteServer = ErrorCode("VngCloudVServerServerNotAllowDeleteServer")
EcVServerServerDeleteCreatingServer = ErrorCode("VngCloudVServerServerDeleteCreatingServer")
EcVServerServerExceedQuota = ErrorCode("VngCloudVServerServerExceedQuota")
EcVServerServerDeleteDeletingServer = ErrorCode("VngCloudVServerServerDeleteDeletingServer")
EcVServerServerDeleteBillingServer = ErrorCode("VngCloudVServerServerDeleteBillingServer")
)
53 changes: 39 additions & 14 deletions vngcloud/sdk_error/server.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package sdk_error

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

const (
patternServerNotFound = "cannot get server with id" // "Cannot get volume type with id vtype-6790f903-38d2-454d-919e-5b49184b5927"
patternServerExceedQuota = "exceeded vm quota" // "The number of servers exceeds the quota"
patternNotAllowDelete = `cannot delete server with status (creating|deleting|creating-billing)`
)

var (
regexErrorServerNotAllowDeleteServer = lregexp.MustCompile(patternNotAllowDelete)
patternServerNotFound = "cannot get server with id" // "Cannot get volume type with id vtype-6790f903-38d2-454d-919e-5b49184b5927"
patternServerCreating = "cannot delete server with status creating" // "Server is creating"
patternServerExceedQuota = "exceeded vm quota" // "The number of servers exceeds the quota"
patternServerDeleting = "cannot delete server with status deleting" // "Server is deleting"
patternServerBilling = "cannot delete server with status creating-billing"
)

func WithErrorServerNotFound(perrResp IErrorRespone) func(sdkError ISdkError) {
Expand All @@ -30,15 +25,15 @@ func WithErrorServerNotFound(perrResp IErrorRespone) func(sdkError ISdkError) {
}
}

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

errMsg := perrResp.GetMessage()
if regexErrorServerNotAllowDeleteServer.FindString(lstr.ToLower(lstr.TrimSpace(errMsg))) != "" {
sdkError.WithErrorCode(EcVServerServerNotAllowDeleteServer).
if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternServerCreating) {
sdkError.WithErrorCode(EcVServerServerDeleteCreatingServer).
WithMessage(errMsg).
WithErrors(perrResp.GetError())
}
Expand All @@ -59,3 +54,33 @@ func WithErrorServerExceedQuota(perrResp IErrorRespone) func(sdkError ISdkError)
}
}
}

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

errMsg := perrResp.GetMessage()
if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternServerDeleting) {
sdkError.WithErrorCode(EcVServerServerDeleteDeletingServer).
WithMessage(errMsg).
WithErrors(perrResp.GetError())
}
}
}

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

errMsg := perrResp.GetMessage()
if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternServerBilling) {
sdkError.WithErrorCode(EcVServerServerDeleteBillingServer).
WithMessage(errMsg).
WithErrors(perrResp.GetError())
}
}
}
4 changes: 3 additions & 1 deletion vngcloud/services/compute/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ func (s *ComputeServiceV2) DeleteServerById(popts IDeleteServerByIdRequest) lser
if _, sdkErr := s.VserverClient.Delete(url, req); sdkErr != nil {
return lserr.SdkErrorHandler(sdkErr, errResp,
lserr.WithErrorServerNotFound(errResp),
lserr.WithErrorServerNotAllowDeleteServer(errResp)).
lserr.WithErrorServerDeleteDeletingServer(errResp),
lserr.WithErrorServerDeleteBillingServer(errResp),
lserr.WithErrorServerDeleteCreatingServer(errResp)).
WithKVparameters("projectId", s.getProjectId(),
"serverId", popts.GetServerId())
}
Expand Down

0 comments on commit 3b26908

Please sign in to comment.