diff --git a/test/server_test.go b/test/server_test.go index c17f15f..a467f1d 100644 --- a/test/server_test.go +++ b/test/server_test.go @@ -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") +} diff --git a/vngcloud/sdk_error/error_codes.go b/vngcloud/sdk_error/error_codes.go index 639df0b..13e4a7c 100644 --- a/vngcloud/sdk_error/error_codes.go +++ b/vngcloud/sdk_error/error_codes.go @@ -55,3 +55,9 @@ const ( const ( EcBillingOutOfPoc = ErrorCode("BillingOutOfPoc") ) + +// vServer server +const ( + EcVServerServerNotFound = ErrorCode("VngCloudVServerServerNotFound") + EcVServerServerDeleteCreatingServer = ErrorCode("VngCloudVServerServerDeleteCreatingServer") +) diff --git a/vngcloud/sdk_error/server.go b/vngcloud/sdk_error/server.go new file mode 100644 index 0000000..cd8b241 --- /dev/null +++ b/vngcloud/sdk_error/server.go @@ -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()) + } + } +} diff --git a/vngcloud/services/compute/icompute.go b/vngcloud/services/compute/icompute.go index 0fca3d8..4be5fc9 100644 --- a/vngcloud/services/compute/icompute.go +++ b/vngcloud/services/compute/icompute.go @@ -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 } diff --git a/vngcloud/services/compute/v2/irequest.go b/vngcloud/services/compute/v2/irequest.go index 0109bb5..5be03c9 100644 --- a/vngcloud/services/compute/v2/irequest.go +++ b/vngcloud/services/compute/v2/irequest.go @@ -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{} +} diff --git a/vngcloud/services/compute/v2/server.go b/vngcloud/services/compute/v2/server.go index 7a0f51f..556df12 100644 --- a/vngcloud/services/compute/v2/server.go +++ b/vngcloud/services/compute/v2/server.go @@ -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 +} diff --git a/vngcloud/services/compute/v2/server_request.go b/vngcloud/services/compute/v2/server_request.go index 2768e9a..55eee4c 100644 --- a/vngcloud/services/compute/v2/server_request.go +++ b/vngcloud/services/compute/v2/server_request.go @@ -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" ) @@ -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 +} diff --git a/vngcloud/services/compute/v2/server_response.go b/vngcloud/services/compute/v2/server_response.go index 81a9246..9c382b7 100644 --- a/vngcloud/services/compute/v2/server_response.go +++ b/vngcloud/services/compute/v2/server_response.go @@ -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() +} diff --git a/vngcloud/services/compute/v2/url.go b/vngcloud/services/compute/v2/url.go index 8a8b223..640ef20 100644 --- a/vngcloud/services/compute/v2/url.go +++ b/vngcloud/services/compute/v2/url.go @@ -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()) +}