Skip to content

Commit

Permalink
[feat] api update secgroup for server
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed May 19, 2024
1 parent 2f4abf0 commit 8369ce2
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 7 deletions.
54 changes: 54 additions & 0 deletions test/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,57 @@ func TestDeleteServerByIdSuccess(t *ltesting.T) {

t.Log("PASS")
}

func TestUpdateServerSecgroupsByServerIdFailure(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lscomputeSvcV2.NewUpdateServerSecgroupsRequest("this-is-fake-id")
server, sdkerr := vngcloud.VServerGateway().V2().ComputeService().UpdateServerSecgroupsByServerId(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 TestUpdateServerSecgroupsByServerIdFailure2(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lscomputeSvcV2.NewUpdateServerSecgroupsRequest(getValueOfEnv("SERVER_ID"),
"secg-93c6c259-0ec7-4905-a232-fe5dccb6674c",
"this-is-fake-secgroup-id")
server, sdkerr := vngcloud.VServerGateway().V2().ComputeService().UpdateServerSecgroupsByServerId(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 TestUpdateServerSecgroupsByServerIdSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lscomputeSvcV2.NewUpdateServerSecgroupsRequest(getValueOfEnv("SERVER_ID"),
"secg-93c6c259-0ec7-4905-a232-fe5dccb6674c")
server, sdkerr := vngcloud.VServerGateway().V2().ComputeService().UpdateServerSecgroupsByServerId(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")
}
1 change: 1 addition & 0 deletions vngcloud/client/iservice_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type IServiceClient interface {
Post(purl string, preq IRequest) (*lreq.Response, lsdkErr.ISdkError)
Get(purl string, preq IRequest) (*lreq.Response, lsdkErr.ISdkError)
Delete(purl string, preq IRequest) (*lreq.Response, lsdkErr.ISdkError)
Put(purl string, preq IRequest) (*lreq.Response, lsdkErr.ISdkError)
}

type ISdkAuthentication interface {
Expand Down
4 changes: 4 additions & 0 deletions vngcloud/client/service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ func (s *serviceClient) Delete(purl string, preq IRequest) (*lreq.Response, lsdk
return s.client.DoRequest(purl, preq.WithRequestMethod(MethodDelete))
}

func (s *serviceClient) Put(purl string, preq IRequest) (*lreq.Response, lsdkErr.ISdkError) {
return s.client.DoRequest(purl, preq.WithRequestMethod(MethodPut))
}

func (s *serviceClient) GetProjectId() string {
return s.projectId
}
Expand Down
1 change: 1 addition & 0 deletions vngcloud/services/compute/icompute.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ 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
UpdateServerSecgroupsByServerId(popts lscomputeSvcV2.IUpdateServerSecgroupsByServerIdRequest) (*lsentity.Server, lserr.ISdkError)
}
6 changes: 6 additions & 0 deletions vngcloud/services/compute/v2/irequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ type IDeleteServerByIdRequest interface {
WithDeleteAllVolume(pok bool) IDeleteServerByIdRequest
ToRequestBody() interface{}
}

type IUpdateServerSecgroupsByServerIdRequest interface {
GetServerId() string
ToRequestBody() interface{}
GetListSecgroupsIds() []string
}
22 changes: 22 additions & 0 deletions vngcloud/services/compute/v2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,25 @@ func (s *ComputeServiceV2) DeleteServerById(popts IDeleteServerByIdRequest) lser

return nil
}

func (s *ComputeServiceV2) UpdateServerSecgroupsByServerId(popts IUpdateServerSecgroupsByServerIdRequest) (*lsentity.Server, lserr.ISdkError) {
url := updateServerSecgroupsByServerIdUrl(s.VserverClient, popts)
resp := new(UpdateServerSecgroupsByServerIdResponse)
errResp := lserr.NewErrorResponse(lserr.NormalErrorType)
req := lsclient.NewRequest().
WithOkCodes(202).
WithJsonBody(popts.ToRequestBody()).
WithJsonResponse(resp).
WithJsonError(errResp)

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

return resp.ToEntityServer(), nil
}
21 changes: 21 additions & 0 deletions vngcloud/services/compute/v2/server_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ func NewDeleteServerByIdRequest(pserverId string) IDeleteServerByIdRequest {
return opt
}

func NewUpdateServerSecgroupsRequest(pserverId string, psecgroups ...string) IUpdateServerSecgroupsByServerIdRequest {
opt := new(UpdateServerSecgroupsByServerIdRequest)
opt.ServerId = pserverId
opt.Secgroups = psecgroups
return opt
}

const (
DataDiskEncryptionAesXtsType DataDiskEncryptionType = "aes-xts-plain64_256"
)
Expand Down Expand Up @@ -141,3 +148,17 @@ func (s *DeleteServerByIdRequest) WithDeleteAllVolume(pok bool) IDeleteServerByI
func (s *DeleteServerByIdRequest) ToRequestBody() interface{} {
return s
}

type UpdateServerSecgroupsByServerIdRequest struct {
Secgroups []string `json:"securityGroup"`

ServerCommon
}

func (s *UpdateServerSecgroupsByServerIdRequest) ToRequestBody() interface{} {
return s
}

func (s *UpdateServerSecgroupsByServerIdRequest) GetListSecgroupsIds() []string {
return s.Secgroups
}
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 @@ -191,3 +191,11 @@ type GetServerByIdResponse struct {
func (s *GetServerByIdResponse) ToEntityServer() *lsentity.Server {
return s.Data.toEntityServer()
}

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

func (s *UpdateServerSecgroupsByServerIdResponse) ToEntityServer() *lsentity.Server {
return s.Data.toEntityServer()
}
8 changes: 8 additions & 0 deletions vngcloud/services/compute/v2/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ func deleteServerByIdUrl(psc lsclient.IServiceClient, popts IDeleteServerByIdReq
"servers",
popts.GetServerId())
}

func updateServerSecgroupsByServerIdUrl(psc lsclient.IServiceClient, popts IUpdateServerSecgroupsByServerIdRequest) string {
return psc.ServiceURL(
psc.GetProjectId(),
"servers",
popts.GetServerId(),
"update-sec-group")
}
7 changes: 0 additions & 7 deletions vngcloud/services/portal/v1/iresponse.go

This file was deleted.

0 comments on commit 8369ce2

Please sign in to comment.