Skip to content

Commit

Permalink
[enhance] using regex to detect error deleting server error
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed May 21, 2024
1 parent def7f8a commit 0dc856f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 27 deletions.
20 changes: 20 additions & 0 deletions test/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ func getEnv() (string, string) {
return clientId, clientSecret
}

func getEnvDevOps() (string, string) {
envFile, _ := lgodotenv.Read("/mnt/vayne/git-vngcloud/vngcloud-go-sdk/secret/env")
clientId := envFile["CLIENT_ID_DEVOPS"]
clientSecret := envFile["CLIENT_SECRET_DEVOPS"]

return clientId, clientSecret
}

func getValueOfEnv(pkey string) string {
envFile, _ := lgodotenv.Read("/mnt/vayne/git-vngcloud/vngcloud-go-sdk/secret/env")
value := envFile[pkey]
Expand All @@ -37,6 +45,18 @@ func validSdkConfig() lsclient.IClient {
return lsclient.NewClient(lctx.TODO()).WithRetryCount(1).WithSleep(10).Configure(sdkConfig)
}

func validSdkConfigDevops() lsclient.IClient {
clientId, clientSecret := getEnvDevOps()
sdkConfig := lsclient.NewSdkConfigure().
WithClientId(clientId).
WithClientSecret(clientSecret).
WithProjectId(getValueOfEnv("PROJECT_ID_DEVOPS")).
WithIamEndpoint("https://iamapis.vngcloud.vn/accounts-api").
WithVServerEndpoint("https://hcm-3.api.vngcloud.vn/vserver/vserver-gateway")

return lsclient.NewClient(lctx.TODO()).WithRetryCount(1).WithSleep(10).Configure(sdkConfig)
}

func invalidSdkConfig() lsclient.IClient {
clientId := "___"
clientSecret := "___"
Expand Down
28 changes: 28 additions & 0 deletions test/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,31 @@ func TestUpdateServerSecgroupsByServerIdSuccess(t *ltesting.T) {
t.Log("Result: ", server)
t.Log("PASS")
}

func TestCreateServerWithAutoRenew(t *ltesting.T) {
vngcloud := validSdkConfigDevops()
opt := lscomputeSvcV2.NewCreateServerRequest(
"cuongdm3-dep-trai-vo-dich-sieu-cap-vu-tru-4",
"img-108b3a77-ab58-4000-9b3e-190d0b4b07fc",
"flav-3929c073-9da9-486f-a96f-9282dbb8d83f",
"net-dae83c7a-f837-4227-bcfa-ec0755549724",
"sub-f7770744-6aa4-4292-9ff9-b43b44716ede",
"vtype-61c3fc5b-f4e9-45b4-8957-8aa7b6029018",
30).
WithTags("cuongdm3", "deptrai", "wife", "unknown").
WithAutoRenew(false).
WithType("VKS").WithProduct("VKS").
WithPoc(false)

server, sdkerr := vngcloud.VServerGateway().V2().ComputeService().CreateServer(opt)
if sdkerr != nil {
t.Fatalf("Expect nil but got %v", sdkerr)
}

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

t.Log("Result: ", server)
t.Log("PASS")
}
3 changes: 1 addition & 2 deletions vngcloud/sdk_error/error_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const (
// vServer server
const (
EcVServerServerNotFound = ErrorCode("VngCloudVServerServerNotFound")
EcVServerServerDeleteCreatingServer = ErrorCode("VngCloudVServerServerDeleteCreatingServer")
EcVServerServerNotAllowDeleteServer = ErrorCode("VngCloudVServerServerNotAllowDeleteServer")
EcVServerServerExceedQuota = ErrorCode("VngCloudVServerServerExceedQuota")
EcVServerServerDeleteDeletingServer = ErrorCode("VngCloudVServerServerDeleteDeletingServer")
)
30 changes: 30 additions & 0 deletions vngcloud/sdk_error/sdk_error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sdk_error

import (
lregexp "regexp"
"testing"
)

func TestRegex(t *testing.T) {
regexErrorSubnetNotFound = lregexp.MustCompile(patternNotAllowDelete)
patternServerCreating1 := "cannot delete server with status creating" // "Server is creating"
patternServerCreatingBilling1 := "cannot delete server with status creating-billing" // "The number of servers exceeds the quota"
patternServerDeleting1 := "cannot delete server with status deleting"
patternFake := "cannot delete server with status fake"

if regexErrorSubnetNotFound.FindString(patternServerCreating1) == "" {
t.Errorf("patternServerCreating should match patternNotAllowDelete")
}

if regexErrorSubnetNotFound.FindString(patternServerCreatingBilling1) == "" {
t.Errorf("patternServerCreatingBilling should match patternNotAllowDelete")
}

if regexErrorSubnetNotFound.FindString(patternServerDeleting1) == "" {
t.Errorf("patternServerDeleting should match patternNotAllowDelete")
}

if regexErrorSubnetNotFound.FindString(patternFake) != "" {
t.Errorf("patternFake should not match patternNotAllowDelete")
}
}
37 changes: 14 additions & 23 deletions vngcloud/sdk_error/server.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package sdk_error

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

const (
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"
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)
)

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

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

errMsg := perrResp.GetMessage()
if lstr.Contains(lstr.ToLower(lstr.TrimSpace(errMsg)), patternServerCreating) {
sdkError.WithErrorCode(EcVServerServerDeleteCreatingServer).
if regexErrorServerNotAllowDeleteServer.FindString(lstr.ToLower(lstr.TrimSpace(errMsg))) != "" {
sdkError.WithErrorCode(EcVServerServerNotAllowDeleteServer).
WithMessage(errMsg).
WithErrors(perrResp.GetError())
}
Expand All @@ -53,18 +59,3 @@ 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())
}
}
}
3 changes: 1 addition & 2 deletions vngcloud/services/compute/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ 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.WithErrorServerDeleteDeletingServer(errResp),
lserr.WithErrorServerDeleteCreatingServer(errResp)).
lserr.WithErrorServerNotAllowDeleteServer(errResp)).
WithKVparameters("projectId", s.getProjectId(),
"serverId", popts.GetServerId())
}
Expand Down

0 comments on commit 0dc856f

Please sign in to comment.