Skip to content

Commit

Permalink
[feat] allow creating server with tag
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongpiger committed May 19, 2024
1 parent ad2e635 commit 2f4abf0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions test/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ func TestCreateServerFailed(t *ltesting.T) {
func TestCreateServerSuccess(t *ltesting.T) {
vngcloud := validSdkConfig()
opt := lscomputeSvcV2.NewCreateServerRequest(
"cuongdm3-test",
"cuongdm3-test-tags",
"img-b5bf635e-0456-4765-b493-31d5fcfc05aa",
"flav-3929c073-9da9-486f-a96f-9282dbb8d83f",
"net-4f35f173-e0fe-4202-9c2b-5121b558bcd3",
"sub-1f98ff1e-2e36-4a40-a0f4-4eadfeb1ea63",
"vtype-61c3fc5b-f4e9-45b4-8957-8aa7b6029018",
30,
)
).WithTags("cuongdm3", "deptrai", "wife", "unknown")
server, sdkerr := vngcloud.VServerGateway().V2().ComputeService().CreateServer(opt)
if sdkerr != nil {
t.Fatalf("Expect nil but got %v", sdkerr)
Expand Down
1 change: 1 addition & 0 deletions vngcloud/services/compute/v2/irequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type ICreateServerRequest interface {
ToRequestBody() interface{}
WithEncryptionVolume(pencryptionVolume bool) ICreateServerRequest
WithUserData(puserData string, pbase64Encode bool) ICreateServerRequest
WithTags(ptags ...string) ICreateServerRequest
WithAttachFloating(pattachFloating bool) ICreateServerRequest
WithSecgroups(psecgroups ...string) ICreateServerRequest
WithPoct(pisPoc bool) ICreateServerRequest
Expand Down
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 @@ -59,10 +59,16 @@ type CreateServerRequest struct {
IsPoc bool `json:"isPoc,omitempty"`
Product string `json:"product,omitempty"`
Type string `json:"type,omitempty"`
Tags []ServerTag `json:"tags,omitempty"`
}

type DataDiskEncryptionType string

type ServerTag struct {
Key string `json:"key"`
Value string `json:"value"`
}

func (s *CreateServerRequest) ToRequestBody() interface{} {
return s
}
Expand All @@ -78,6 +84,21 @@ func (s *CreateServerRequest) WithUserData(puserData string, pbase64Encode bool)
return s
}

func (s *CreateServerRequest) WithTags(ptags ...string) ICreateServerRequest {
if s.Tags == nil {
s.Tags = make([]ServerTag, 0)
}

if len(ptags)%2 != 0 {
ptags = append(ptags, "none")
}

for i := 0; i < len(ptags); i += 2 {
s.Tags = append(s.Tags, ServerTag{Key: ptags[i], Value: ptags[i+1]})
}
return s
}

func (s *CreateServerRequest) WithAttachFloating(pattachFloating bool) ICreateServerRequest {
s.AttachFloating = pattachFloating
return s
Expand Down

0 comments on commit 2f4abf0

Please sign in to comment.