From c1927edb604a7d169a790440525b5c6c87bd06ac Mon Sep 17 00:00:00 2001 From: "Cuong. Duong Manh" Date: Sat, 2 Nov 2024 17:30:29 +0700 Subject: [PATCH] [chore] nothing --- test/server_test.go | 48 ++++++++-- vngcloud/services/compute/v2/irequest.go | 4 + vngcloud/services/compute/v2/requests.go | 6 +- vngcloud/services/compute/v2/server.go | 1 + .../services/compute/v2/server_request.go | 95 +++++++++++++------ 5 files changed, 110 insertions(+), 44 deletions(-) diff --git a/test/server_test.go b/test/server_test.go index b4237c6..a029fb3 100644 --- a/test/server_test.go +++ b/test/server_test.go @@ -1,8 +1,9 @@ package test import ( - lscomputeSvcV2 "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/services/compute/v2" ltesting "testing" + + lscomputeSvcV2 "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/services/compute/v2" ) func TestCreateServerFailed(t *ltesting.T) { @@ -11,11 +12,10 @@ func TestCreateServerFailed(t *ltesting.T) { "cuongdm3-test", "img-b5bf635e-0456-4765-b493-31d5fcfc05aa", "flav-3929c073-9da9-486f-a96f-9282dbb8d83f", - "net-4f35f173-e0fe-4202-9c2b-5121b558bcd2", - "sub-1f98ff1e-2e36-4a40-a0f4-4eadfeb1ea63", "vtype-61c3fc5b-f4e9-45b4-8957-8aa7b6029018", 30, - ) + ).WithNetwork("net-4f35f173-e0fe-4202-9c2b-5121b558bcd2", + "sub-1f98ff1e-2e36-4a40-a0f4-4eadfeb1ea63") server, sdkerr := vngcloud.VServerGateway().V2().ComputeService().CreateServer(opt) if sdkerr == nil { t.Fatalf("Expect error but got nil") @@ -35,11 +35,11 @@ func TestCreateServerSuccess(t *ltesting.T) { "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") + ).WithTags("cuongdm3", "deptrai", "wife", "unknown"). + WithNetwork("net-4f35f173-e0fe-4202-9c2b-5121b558bcd3", + "sub-1f98ff1e-2e36-4a40-a0f4-4eadfeb1ea63") server, sdkerr := vngcloud.VServerGateway().V2().ComputeService().CreateServer(opt) if sdkerr != nil { t.Fatalf("Expect nil but got %v", sdkerr) @@ -182,10 +182,10 @@ func TestCreateServerWithAutoRenew(t *ltesting.T) { "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). + WithNetwork("net-dae83c7a-f837-4227-bcfa-ec0755549724", + "sub-f7770744-6aa4-4292-9ff9-b43b44716ede"). WithTags("cuongdm3", "deptrai", "wife", "unknown"). WithAutoRenew(false). WithType("VKS").WithProduct("VKS"). @@ -285,3 +285,33 @@ func TestDetachFloatingIp(t *ltesting.T) { t.Log("PASS") } + +func TestCreateDnsServer(t *ltesting.T) { + vngcloud := validSdkConfigHanRegion() + opt := lscomputeSvcV2.NewCreateServerRequest( + "cuongdm3-test", + "img-b5bf635e-0456-4765-b493-31d5fcfc05aa", + "flav-8066e9ff-5d80-4e8f-aeae-9e8a934bfc44", + "vtype-7a7a8610-34f5-11ee-be56-0242ac120002", + 30, + ).WithNetwork("", "").WithServerNetworkInterface( + "pro-5ce9da27-8ac9-40db-8743-d80f6cbf1491", + "net-6ad5cc2d-5dfe-4632-a578-6446e6503dd0", + "sub-33ec4719-915f-4818-85b0-f17bdf7f899b", + true, + ).WithServerNetworkInterface( + "pro-e5af9dda-cccb-4f49-bb15-de890cb015c7", + "net-0dc4cf1e-d961-4483-b848-62ed86fa69f1", + "sub-1a7e3339-5a73-4cd4-a998-77df31e39303", + false, + ) + + server, sdkerr := vngcloud.VServerGateway().V2().ComputeService().CreateServer(opt) + + if sdkerr != nil { + t.Fatalf("Expect nil but got %v", sdkerr) + } + + t.Log("Result: ", server) + t.Log("PASS") +} diff --git a/vngcloud/services/compute/v2/irequest.go b/vngcloud/services/compute/v2/irequest.go index c33d390..8abf786 100644 --- a/vngcloud/services/compute/v2/irequest.go +++ b/vngcloud/services/compute/v2/irequest.go @@ -11,7 +11,11 @@ type ICreateServerRequest interface { WithSecgroups(psecgroups ...string) ICreateServerRequest WithPoc(pisPoc bool) ICreateServerRequest WithType(ptype string) ICreateServerRequest + WithNetwork(pnetworkId, psubnetId string) ICreateServerRequest WithProduct(pproduct string) ICreateServerRequest + WithServerNetworkInterface(pprojectId, pnetworkId, psubnetId string, pattachFloating bool) ICreateServerRequest + AddUserAgent(pagent ...string) ICreateServerRequest + ParseUserAgent() string } type IGetServerByIdRequest interface { diff --git a/vngcloud/services/compute/v2/requests.go b/vngcloud/services/compute/v2/requests.go index 688c663..7309919 100644 --- a/vngcloud/services/compute/v2/requests.go +++ b/vngcloud/services/compute/v2/requests.go @@ -1,12 +1,10 @@ package v2 -func NewCreateServerRequest(pname, pimageId, pflavorId, pnetworkId, psubnetId, prootDiskType string, prootDiskSize int) ICreateServerRequest { +func NewCreateServerRequest(pname, pimageId, pflavorId, prootDiskType string, prootDiskSize int) ICreateServerRequest { opt := new(CreateServerRequest) opt.Name = pname opt.ImageId = pimageId opt.FlavorId = pflavorId - opt.NetworkId = pnetworkId - opt.SubnetId = psubnetId opt.RootDiskTypeId = prootDiskType opt.RootDiskSize = prootDiskSize return opt @@ -61,4 +59,4 @@ func NewDetachFloatingIpRequest(pserverId, pwanId, pniid string) IDetachFloating opt.NetworkInterfaceId = pniid opt.WanId = pwanId return opt -} +} \ No newline at end of file diff --git a/vngcloud/services/compute/v2/server.go b/vngcloud/services/compute/v2/server.go index 1df00b1..728354f 100644 --- a/vngcloud/services/compute/v2/server.go +++ b/vngcloud/services/compute/v2/server.go @@ -11,6 +11,7 @@ func (s *ComputeServiceV2) CreateServer(popts ICreateServerRequest) (*lsentity.S resp := new(CreateServerResponse) errResp := lserr.NewErrorResponse(lserr.NormalErrorType) req := lsclient.NewRequest(). + WithHeader("User-Agent", popts.ParseUserAgent()). WithOkCodes(202). WithJsonBody(popts.ToRequestBody()). WithJsonResponse(resp). diff --git a/vngcloud/services/compute/v2/server_request.go b/vngcloud/services/compute/v2/server_request.go index 2e63565..3828ac3 100644 --- a/vngcloud/services/compute/v2/server_request.go +++ b/vngcloud/services/compute/v2/server_request.go @@ -8,37 +8,47 @@ const ( ) type CreateServerRequest struct { - AttachFloating bool `json:"attachFloating,omitempty"` - BackupInstancePointId string `json:"backupInstancePointId,omitempty"` - DataDiskEncryptionType DataDiskEncryptionType `json:"dataDiskEncryptionType,omitempty"` - DataDiskName string `json:"dataDiskName,omitempty"` - DataDiskSize int `json:"dataDiskSize,omitempty"` - DataDiskTypeId string `json:"dataDiskTypeId,omitempty"` - EnableBackup bool `json:"enableBackup,omitempty"` - EncryptionVolume bool `json:"encryptionVolume"` - ExpirePassword bool `json:"expirePassword,omitempty"` - FlavorId string `json:"flavorId"` - ImageId string `json:"imageId"` - Name string `json:"name"` - NetworkId string `json:"networkId"` - OsLicence bool `json:"osLicence,omitempty"` - RestoreBackup bool `json:"restoreBackup,omitempty"` - RootDiskEncryptionType DataDiskEncryptionType `json:"rootDiskEncryptionType,omitempty"` - RootDiskSize int `json:"rootDiskSize"` - RootDiskTypeId string `json:"rootDiskTypeId"` - SecurityGroup []string `json:"securityGroup,omitempty"` - ServerGroupId string `json:"serverGroupId,omitempty"` - SshKeyId string `json:"sshKeyId,omitempty"` - SubnetId string `json:"subnetId"` - UserData string `json:"userData,omitempty"` - UserDataBase64Encoded bool `json:"userDataBase64Encoded,omitempty"` - UserName string `json:"userName,omitempty"` - UserPassword string `json:"userPassword,omitempty"` - IsPoc bool `json:"isPoc,omitempty"` - Product string `json:"product,omitempty"` - Type string `json:"type,omitempty"` - Tags []ServerTag `json:"tags,omitempty"` - AutoRenew bool `json:"isEnableAutoRenew,omitempty"` + AttachFloating bool `json:"attachFloating,omitempty"` + BackupInstancePointId string `json:"backupInstancePointId,omitempty"` + DataDiskEncryptionType DataDiskEncryptionType `json:"dataDiskEncryptionType,omitempty"` + DataDiskName string `json:"dataDiskName,omitempty"` + DataDiskSize int `json:"dataDiskSize,omitempty"` + DataDiskTypeId string `json:"dataDiskTypeId,omitempty"` + EnableBackup bool `json:"enableBackup,omitempty"` + EncryptionVolume bool `json:"encryptionVolume"` + ExpirePassword bool `json:"expirePassword,omitempty"` + FlavorId string `json:"flavorId"` + ImageId string `json:"imageId"` + Name string `json:"name"` + NetworkId *string `json:"networkId,omitempty"` + SubnetId *string `json:"subnetId,omitempty"` + OsLicence bool `json:"osLicence,omitempty"` + RestoreBackup bool `json:"restoreBackup,omitempty"` + RootDiskEncryptionType DataDiskEncryptionType `json:"rootDiskEncryptionType,omitempty"` + RootDiskSize int `json:"rootDiskSize"` + RootDiskTypeId string `json:"rootDiskTypeId"` + SecurityGroup []string `json:"securityGroup,omitempty"` + ServerGroupId string `json:"serverGroupId,omitempty"` + SshKeyId string `json:"sshKeyId,omitempty"` + UserData string `json:"userData,omitempty"` + UserDataBase64Encoded bool `json:"userDataBase64Encoded,omitempty"` + UserName string `json:"userName,omitempty"` + UserPassword string `json:"userPassword,omitempty"` + IsPoc bool `json:"isPoc,omitempty"` + Product string `json:"product,omitempty"` + Type string `json:"type,omitempty"` + Tags []ServerTag `json:"tags,omitempty"` + AutoRenew bool `json:"isEnableAutoRenew,omitempty"` + Networks []ServerNetworkInterface `json:"networks,omitempty"` + + lscommon.UserAgent +} + +type ServerNetworkInterface struct { + ProjectId string `json:"projectId"` + NetworkId string `json:"networkId"` + SubnetId string `json:"subnetId"` + AttachFloating bool `json:"attachFloating"` } type AttachBlockVolumeRequest struct { @@ -62,6 +72,17 @@ func (s *CreateServerRequest) ToRequestBody() interface{} { return s } +func (s *CreateServerRequest) WithServerNetworkInterface(pprojectId, pnetworkId, psubnetId string, pattachFloating bool) ICreateServerRequest { + s.Networks = append(s.Networks, ServerNetworkInterface{ + ProjectId: pprojectId, + NetworkId: pnetworkId, + SubnetId: psubnetId, + AttachFloating: pattachFloating, + }) + + return s +} + func (s *CreateServerRequest) WithRootDiskEncryptionType(pdataDisk DataDiskEncryptionType) ICreateServerRequest { s.EncryptionVolume = true s.RootDiskEncryptionType = pdataDisk @@ -124,6 +145,18 @@ func (s *CreateServerRequest) WithProduct(pproduct string) ICreateServerRequest return s } +func (s *CreateServerRequest) WithNetwork(pnetworkId, psubnetId string) ICreateServerRequest { + s.NetworkId = &pnetworkId + s.SubnetId = &psubnetId + + return s +} + +func (s *CreateServerRequest) AddUserAgent(pagent ...string) ICreateServerRequest { + s.UserAgent.AddUserAgent(pagent...) + return s +} + type GetServerByIdRequest struct { lscommon.ServerCommon }