Skip to content

Commit 5f93b25

Browse files
committed
Initial support for deploying uncustomised servers.
1 parent c9b78ea commit 5f93b25

File tree

3 files changed

+231
-1
lines changed

3 files changed

+231
-1
lines changed

compute/assert_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ func (expect expectHelper) IsFalse(description string, condition bool) {
2929

3030
func (expect expectHelper) IsNil(description string, actual interface{}) {
3131
if !reflect.ValueOf(actual).IsNil() {
32-
expect.test.Fatalf("%s was not nil.", description)
32+
// Convenience
33+
str, ok := actual.(*string)
34+
if ok {
35+
actual = *str
36+
}
37+
38+
expect.test.Fatalf("%s was not nil (%#v).", description, actual)
3339
}
3440
}
3541

compute/servers.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ type ServerDeploymentConfiguration struct {
100100
Start bool `json:"start"`
101101
}
102102

103+
// UncustomizedServerDeploymentConfiguration represents the configuration for deploying a virtual machine without guest OS customisation.
104+
type UncustomizedServerDeploymentConfiguration struct {
105+
Name string `json:"name"`
106+
Description string `json:"description"`
107+
ImageID string `json:"imageId"`
108+
CPU VirtualMachineCPU `json:"cpu"`
109+
MemoryGB int `json:"memoryGb,omitempty"`
110+
Disks VirtualMachineDisks `json:"disk"`
111+
Network VirtualMachineNetwork `json:"networkInfo"`
112+
Start bool `json:"start"`
113+
}
114+
103115
// editServerMetadata represents the request body when modifying server metadata.
104116
type editServerMetadata struct {
105117
ID string `json:"id"`
@@ -337,6 +349,40 @@ func (client *Client) DeployServer(serverConfiguration ServerDeploymentConfigura
337349
return *serverIDMessage, nil
338350
}
339351

352+
// DeployUncustomizedServer deploys a new virtual machine.
353+
func (client *Client) DeployUncustomizedServer(serverConfiguration UncustomizedServerDeploymentConfiguration) (serverID string, err error) {
354+
organizationID, err := client.getOrganizationID()
355+
if err != nil {
356+
return "", err
357+
}
358+
359+
requestURI := fmt.Sprintf("%s/server/deployUncustomizedServer",
360+
url.QueryEscape(organizationID),
361+
)
362+
request, err := client.newRequestV25(requestURI, http.MethodPost, &serverConfiguration)
363+
responseBody, statusCode, err := client.executeRequest(request)
364+
if err != nil {
365+
return "", err
366+
}
367+
368+
apiResponse, err := readAPIResponseAsJSON(responseBody, statusCode)
369+
if err != nil {
370+
return "", err
371+
}
372+
373+
if apiResponse.ResponseCode != ResponseCodeInProgress {
374+
return "", apiResponse.ToError("Request to deploy uncustomised server '%s' failed with status code %d (%s): %s", serverConfiguration.Name, statusCode, apiResponse.ResponseCode, apiResponse.Message)
375+
}
376+
377+
// Expected: "info" { "name": "serverId", "value": "the-Id-of-the-new-server" }
378+
serverIDMessage := apiResponse.GetFieldMessage("serverId")
379+
if serverIDMessage == nil {
380+
return "", apiResponse.ToError("Received an unexpected response (missing 'serverId') with status code %d (%s): %s", statusCode, apiResponse.ResponseCode, apiResponse.Message)
381+
}
382+
383+
return *serverIDMessage, nil
384+
}
385+
340386
// EditServerMetadata modifies a server's name and / or description.
341387
//
342388
// Pass nil for values you don't want to modify.

compute/servers_test.go

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,74 @@ func TestClient_DeployServer_Success(test *testing.T) {
108108
expect.EqualsString("serverID", "7b62aae5-bdbe-4595-b58d-c78f95db2a7f", serverID)
109109
}
110110

111+
// Deploy uncustomised server (successful).
112+
func TestClient_DeployUncustomizedServer_Success(test *testing.T) {
113+
expect := expect(test)
114+
115+
testServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
116+
deploymentConfiguration := &UncustomizedServerDeploymentConfiguration{}
117+
err := readRequestBodyAsJSON(request, deploymentConfiguration)
118+
if err != nil {
119+
test.Fatal(err.Error())
120+
}
121+
122+
verifyDeployUncustomizedServerTestRequest(test, deploymentConfiguration)
123+
124+
writer.Header().Set("Content-Type", "application/json")
125+
writer.WriteHeader(http.StatusOK)
126+
127+
fmt.Fprintln(writer, deployUncustomizedServerTestResponse)
128+
}))
129+
defer testServer.Close()
130+
131+
client := NewClientWithBaseAddress(testServer.URL, "user1", "password")
132+
client.setAccount(&Account{
133+
OrganizationID: "dummy-organization-id",
134+
})
135+
136+
serverConfiguration := UncustomizedServerDeploymentConfiguration{
137+
Name: "Production Server",
138+
Description: "Uncustomized appliance server.",
139+
ImageID: "e926545b-1b9c-4068-8cef-076830a9a0bc",
140+
CPU: VirtualMachineCPU{
141+
Count: 9,
142+
CoresPerSocket: 3,
143+
Speed: "ECONOMY",
144+
},
145+
MemoryGB: 2,
146+
Network: VirtualMachineNetwork{
147+
NetworkDomainID: "e926545b-1b9c-4068-8cef-076830a9a0bc",
148+
PrimaryAdapter: VirtualMachineNetworkAdapter{
149+
PrivateIPv4Address: stringToPtr("10.0.1.15"),
150+
AdapterType: stringToPtr(NetworkAdapterTypeVMXNET3),
151+
},
152+
AdditionalNetworkAdapters: []VirtualMachineNetworkAdapter{
153+
VirtualMachineNetworkAdapter{
154+
VLANID: stringToPtr("e0b4d43c-c648-11e4-b33a-72802a5322b2"),
155+
AdapterType: stringToPtr(NetworkAdapterTypeVMXNET3),
156+
},
157+
},
158+
},
159+
Disks: []VirtualMachineDisk{
160+
VirtualMachineDisk{
161+
ID: "d99e4d2a-24c0-4c54-b491-e56697b8f004",
162+
Speed: "ECONOMY",
163+
},
164+
VirtualMachineDisk{
165+
ID: "e6a3c0b7-cd32-4224-b8ec-5f1359940204",
166+
Speed: "HIGHPERFORMANCE",
167+
},
168+
},
169+
}
170+
171+
serverID, err := client.DeployUncustomizedServer(serverConfiguration)
172+
if err != nil {
173+
test.Fatal(err)
174+
}
175+
176+
expect.EqualsString("serverID", "7b62aae5-bdbe-4595-b58d-c78f95db2a7f", serverID)
177+
}
178+
111179
// Add disk to server (successful).
112180
func TestClient_AddServerDisk_Success(test *testing.T) {
113181
expect := expect(test)
@@ -417,6 +485,99 @@ func verifyDeployServerTestRequest(test *testing.T, deploymentConfiguration *Ser
417485
expect.EqualsString("ServerDeploymentConfiguration.SCSIControllers[0].Disks[1].Speed", "HIGHPERFORMANCE", deploymentConfiguration.SCSIControllers[0].Disks[1].Speed)
418486
}
419487

488+
const deployUncustomizedServerTestRequest = `
489+
{
490+
"name": "Production Server",
491+
"description": "Uncustomized appliance server.",
492+
"imageId": "e926545b-1b9c-4068-8cef-076830a9a0bc",
493+
"start": false,
494+
"cpu": {
495+
"speed": "ECONOMY",
496+
"count": "9",
497+
"coresPerSocket": "3"
498+
},
499+
"memoryGb": "2",
500+
"clusterId": "NA9-01",
501+
"networkInfo": {
502+
"networkDomainId": "e926545b-1b9c-4068-8cef-076830a9a0bc",
503+
"primaryNic": {
504+
"privateIpv4": "10.0.1.15",
505+
"networkAdapter": "VMXNET3"
506+
},
507+
"additionalNic": [
508+
{
509+
"vlanId": "e0b4d43c-c648-11e4-b33a-72802a5322b2",
510+
"networkAdapter": "VMXNET3"
511+
}
512+
]
513+
},
514+
"disk": [
515+
{
516+
"id": "d99e4d2a-24c0-4c54-b491-e56697b8f004",
517+
"speed": "ECONOMY"
518+
},
519+
{
520+
"id": "e6a3c0b7-cd32-4224-b8ec-5f1359940204",
521+
"speed": "HIGHPERFORMANCE"
522+
}
523+
],
524+
"tag": [
525+
{
526+
"tagKeyName": "department",
527+
"value": "IT"
528+
},
529+
{
530+
"tagKeyName": "backup",
531+
"value": "nope"
532+
}
533+
]
534+
}
535+
`
536+
537+
func verifyDeployUncustomizedServerTestRequest(test *testing.T, deploymentConfiguration *UncustomizedServerDeploymentConfiguration) {
538+
expect := expect(test)
539+
540+
expect.NotNil("UncustomizedServerDeploymentConfiguration", deploymentConfiguration)
541+
expect.EqualsString("UncustomizedServerDeploymentConfiguration.Name", "Production Server", deploymentConfiguration.Name)
542+
expect.EqualsString("UncustomizedServerDeploymentConfiguration.Description", "Uncustomized appliance server.", deploymentConfiguration.Description)
543+
expect.EqualsString("UncustomizedServerDeploymentConfiguration.ImageID", "e926545b-1b9c-4068-8cef-076830a9a0bc", deploymentConfiguration.ImageID)
544+
545+
// CPU
546+
expect.EqualsInt("UncustomizedServerDeploymentConfiguration.CPU.Count", 9, deploymentConfiguration.CPU.Count)
547+
expect.EqualsInt("UncustomizedServerDeploymentConfiguration.CPU.CoresPerSocket", 3, deploymentConfiguration.CPU.CoresPerSocket)
548+
expect.EqualsString("UncustomizedServerDeploymentConfiguration.CPU.Speed", "ECONOMY", deploymentConfiguration.CPU.Speed)
549+
550+
// Memory
551+
expect.EqualsInt("UncustomizedServerDeploymentConfiguration.MemoryGB", 2, deploymentConfiguration.MemoryGB)
552+
553+
// Network.
554+
network := deploymentConfiguration.Network
555+
expect.EqualsString("UncustomizedServerDeploymentConfiguration.Network.NetworkDomainID", "e926545b-1b9c-4068-8cef-076830a9a0bc", network.NetworkDomainID)
556+
557+
expect.IsNil("UncustomizedServerDeploymentConfiguration.Network.PrimaryAdapter.VLANID", network.PrimaryAdapter.VLANID)
558+
expect.NotNil("UncustomizedServerDeploymentConfiguration.Network.PrimaryAdapter.PrivateIPv4Address", network.PrimaryAdapter.PrivateIPv4Address)
559+
expect.EqualsString("UncustomizedServerDeploymentConfiguration.Network.PrimaryAdapter.PrivateIPv4Address", "10.0.1.15", *network.PrimaryAdapter.PrivateIPv4Address)
560+
expect.EqualsString("UncustomizedServerDeploymentConfiguration.Network.PrimaryAdapter.AdapterType", "VMXNET3", *network.AdditionalNetworkAdapters[0].AdapterType)
561+
562+
// Network adapters.
563+
expect.EqualsInt("UncustomizedServerDeploymentConfiguration.Network.AdditionalNetworkAdapters.Length", 1, len(network.AdditionalNetworkAdapters))
564+
565+
expect.NotNil("UncustomizedServerDeploymentConfiguration.Network.AdditionalNetworkAdapters[0].VLANID", network.AdditionalNetworkAdapters[0].VLANID)
566+
expect.EqualsString("UncustomizedServerDeploymentConfiguration.Network.AdditionalNetworkAdapters[0].VLANID", "e0b4d43c-c648-11e4-b33a-72802a5322b2", *network.AdditionalNetworkAdapters[0].VLANID)
567+
expect.IsNil("UncustomizedServerDeploymentConfiguration.Network.AdditionalNetworkAdapters[0].PrivateIPv4Address", network.AdditionalNetworkAdapters[0].PrivateIPv4Address)
568+
expect.NotNil("UncustomizedServerDeploymentConfiguration.Network.AdditionalNetworkAdapters[0].AdapterType", network.AdditionalNetworkAdapters[0].AdapterType)
569+
expect.EqualsString("UncustomizedServerDeploymentConfiguration.Network.AdditionalNetworkAdapters[0].AdapterType", "VMXNET3", *network.AdditionalNetworkAdapters[0].AdapterType)
570+
571+
// Disks.
572+
expect.EqualsInt("UncustomizedServerDeploymentConfiguration.Disks.Length", 2, len(deploymentConfiguration.Disks))
573+
574+
expect.EqualsString("UncustomizedServerDeploymentConfiguration.Disks[0].ID", "d99e4d2a-24c0-4c54-b491-e56697b8f004", deploymentConfiguration.Disks[0].ID)
575+
expect.EqualsString("UncustomizedServerDeploymentConfiguration.Disks[0].Speed", "ECONOMY", deploymentConfiguration.Disks[0].Speed)
576+
577+
expect.EqualsString("UncustomizedServerDeploymentConfiguration.Disks[1].ID", "e6a3c0b7-cd32-4224-b8ec-5f1359940204", deploymentConfiguration.Disks[1].ID)
578+
expect.EqualsString("UncustomizedServerDeploymentConfiguration.Disks[1].Speed", "HIGHPERFORMANCE", deploymentConfiguration.Disks[1].Speed)
579+
}
580+
420581
const addDiskToServerTestRequest = `
421582
{
422583
"id": "7b62aae5-bdbe-4595-b58d-c78f95db2a7f",
@@ -653,6 +814,23 @@ const deployServerTestResponse = `
653814
}
654815
`
655816

817+
const deployUncustomizedServerTestResponse = `
818+
{
819+
"operation": "DEPLOY_UNCUSTOMIZED_SERVER",
820+
"responseCode": "IN_PROGRESS",
821+
"message": "Request to deploy uncustomized Server 'Production Server' has been accepted and is being processed.",
822+
"info": [
823+
{
824+
"name": "serverId",
825+
"value": "7b62aae5-bdbe-4595-b58d-c78f95db2a7f"
826+
}
827+
],
828+
"warning": [],
829+
"error": [],
830+
"requestId": "na9_20160321T074626030-0400_7e9fffe7-190b-46f2-9107-9d52fe57d0ad"
831+
}
832+
`
833+
656834
func verifyDeployServerTestResponse(test *testing.T, response *APIResponseV2) {
657835
expect := expect(test)
658836

0 commit comments

Comments
 (0)