Skip to content

Commit

Permalink
[feat] api create/get/delete server
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed May 19, 2024
1 parent d81932c commit ad2e635
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 0 deletions.
57 changes: 57 additions & 0 deletions test/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,60 @@ func TestCreateServerSuccess(t *ltesting.T) {
t.Log("Result: ", server)
t.Log("PASS")
}

func TestGetServerByIdFailure(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lscomputeSvcV2.NewGetServerByIdRequest("server-1")
server, sdkerr := vngcloud.VServerGateway().V2().ComputeService().GetServerById(opt)
if sdkerr == nil {
t.Fatalf("Expect error but got nil")
}

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

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

func TestGetServerByIdSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lscomputeSvcV2.NewGetServerByIdRequest(getValueOfEnv("SERVER_ID"))
server, sdkerr := vngcloud.VServerGateway().V2().ComputeService().GetServerById(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")
}

func TestDeleteServerByIdFailure(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lscomputeSvcV2.NewDeleteServerByIdRequest("this-is-fake-id")
sdkerr := vngcloud.VServerGateway().V2().ComputeService().DeleteServerById(opt)

if sdkerr == nil {
t.Fatalf("Expect error but got nil")
}

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

func TestDeleteServerByIdSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lscomputeSvcV2.NewDeleteServerByIdRequest(getValueOfEnv("DELETE_SERVER_ID"))
sdkerr := vngcloud.VServerGateway().V2().ComputeService().DeleteServerById(opt)

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

t.Log("PASS")
}
6 changes: 6 additions & 0 deletions vngcloud/sdk_error/error_codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,9 @@ const (
const (
EcBillingOutOfPoc = ErrorCode("BillingOutOfPoc")
)

// vServer server
const (
EcVServerServerNotFound = ErrorCode("VngCloudVServerServerNotFound")
EcVServerServerDeleteCreatingServer = ErrorCode("VngCloudVServerServerDeleteCreatingServer")
)
38 changes: 38 additions & 0 deletions vngcloud/sdk_error/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package sdk_error

import 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"
)

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

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

func WithErrorServerDeleteCreatingServer(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).
WithMessage(errMsg).
WithErrors(perrResp.GetError())
}
}
}
2 changes: 2 additions & 0 deletions vngcloud/services/compute/icompute.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ import (

type IComputeServiceV2 interface {
CreateServer(popts lscomputeSvcV2.ICreateServerRequest) (*lsentity.Server, lserr.ISdkError)
GetServerById(popts lscomputeSvcV2.IGetServerByIdRequest) (*lsentity.Server, lserr.ISdkError)
DeleteServerById(popts lscomputeSvcV2.IDeleteServerByIdRequest) lserr.ISdkError
}
10 changes: 10 additions & 0 deletions vngcloud/services/compute/v2/irequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ type ICreateServerRequest interface {
WithType(ptype string) ICreateServerRequest
WithProduct(pproduct string) ICreateServerRequest
}

type IGetServerByIdRequest interface {
GetServerId() string
}

type IDeleteServerByIdRequest interface {
GetServerId() string
WithDeleteAllVolume(pok bool) IDeleteServerByIdRequest
ToRequestBody() interface{}
}
38 changes: 38 additions & 0 deletions vngcloud/services/compute/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,41 @@ func (s *ComputeServiceV2) CreateServer(popts ICreateServerRequest) (*lsentity.S

return resp.ToEntityServer(), nil
}

func (s *ComputeServiceV2) GetServerById(popts IGetServerByIdRequest) (*lsentity.Server, lserr.ISdkError) {
url := getServerByIdUrl(s.VserverClient, popts)
resp := new(GetServerByIdResponse)
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.WithErrorServerNotFound(errResp)).
WithKVparameters("projectId", s.getProjectId(),
"serverId", popts.GetServerId())
}

return resp.ToEntityServer(), nil
}

func (s *ComputeServiceV2) DeleteServerById(popts IDeleteServerByIdRequest) lserr.ISdkError {
url := deleteServerByIdUrl(s.VserverClient, popts)
errResp := lserr.NewErrorResponse(lserr.NormalErrorType)
req := lsclient.NewRequest().
WithOkCodes(202).
WithJsonBody(popts.ToRequestBody()).
WithJsonError(errResp)

if _, sdkErr := s.VserverClient.Delete(url, req); sdkErr != nil {
return lserr.SdkErrorHandler(sdkErr, errResp,
lserr.WithErrorServerNotFound(errResp),
lserr.WithErrorServerDeleteCreatingServer(errResp)).
WithKVparameters("projectId", s.getProjectId(),
"serverId", popts.GetServerId())
}

return nil
}
31 changes: 31 additions & 0 deletions vngcloud/services/compute/v2/server_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ func NewCreateServerRequest(pname, pimageId, pflavorId, pnetworkId, psubnetId, p
return opt
}

func NewGetServerByIdRequest(pserverId string) IGetServerByIdRequest {
opt := new(GetServerByIdRequest)
opt.ServerId = pserverId
return opt
}

func NewDeleteServerByIdRequest(pserverId string) IDeleteServerByIdRequest {
opt := new(DeleteServerByIdRequest)
opt.ServerId = pserverId
opt.DeleteAllVolume = false
return opt
}

const (
DataDiskEncryptionAesXtsType DataDiskEncryptionType = "aes-xts-plain64_256"
)
Expand Down Expand Up @@ -89,3 +102,21 @@ func (s *CreateServerRequest) WithProduct(pproduct string) ICreateServerRequest
s.Product = pproduct
return s
}

type GetServerByIdRequest struct {
ServerCommon
}

type DeleteServerByIdRequest struct {
DeleteAllVolume bool `json:"deleteAllVolume"`
ServerCommon
}

func (s *DeleteServerByIdRequest) WithDeleteAllVolume(pok bool) IDeleteServerByIdRequest {
s.DeleteAllVolume = pok
return s
}

func (s *DeleteServerByIdRequest) ToRequestBody() interface{} {
return s
}
8 changes: 8 additions & 0 deletions vngcloud/services/compute/v2/server_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,11 @@ type CreateServerResponse struct {
func (s *CreateServerResponse) ToEntityServer() *lsentity.Server {
return s.Data.toEntityServer()
}

type GetServerByIdResponse struct {
Data Server `json:"data"`
}

func (s *GetServerByIdResponse) ToEntityServer() *lsentity.Server {
return s.Data.toEntityServer()
}
14 changes: 14 additions & 0 deletions vngcloud/services/compute/v2/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,17 @@ func createServerUrl(psc lsclient.IServiceClient) string {
psc.GetProjectId(),
"servers")
}

func getServerByIdUrl(psc lsclient.IServiceClient, popts IGetServerByIdRequest) string {
return psc.ServiceURL(
psc.GetProjectId(),
"servers",
popts.GetServerId())
}

func deleteServerByIdUrl(psc lsclient.IServiceClient, popts IDeleteServerByIdRequest) string {
return psc.ServiceURL(
psc.GetProjectId(),
"servers",
popts.GetServerId())
}

0 comments on commit ad2e635

Please sign in to comment.