Skip to content

Commit 822a5d2

Browse files
mlsorensenMarcus Sorensen
andauthored
FindHostsForMigration returns a list response (#41)
* FindHostsForMigration returns a list response Signed-off-by: Marcus Sorensen <[email protected]> * Add codegen fixes for findHostsForMigration * Allow make to specify MOCKGEN location * Fix generation of test for findHostsForMigration Signed-off-by: Marcus Sorensen <[email protected]> Co-authored-by: Marcus Sorensen <[email protected]>
1 parent 396c624 commit 822a5d2

File tree

5 files changed

+73
-7
lines changed

5 files changed

+73
-7
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mocks:
2525
test:
2626
go test -v github.com/apache/cloudstack-go/v2/test
2727

28-
MOCKGEN := $(shell pwd)/bin/mockgen
28+
MOCKGEN ?= $(shell pwd)/bin/mockgen
2929
mockgen: ## Download conversion-gen locally if necessary.
3030
$(call go-get-tool,$(MOCKGEN),github.com/golang/mock/mockgen)
3131

cloudstack/HostService.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,6 +1805,11 @@ func (s *HostService) FindHostsForMigration(p *FindHostsForMigrationParams) (*Fi
18051805
}
18061806

18071807
type FindHostsForMigrationResponse struct {
1808+
Count int `json:"count"`
1809+
Host []*HostForMigration `json:"host"`
1810+
}
1811+
1812+
type HostForMigration struct {
18081813
Averageload int64 `json:"averageload"`
18091814
Capabilities string `json:"capabilities"`
18101815
Clusterid string `json:"clusterid"`

generate/generate.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ var longToStringConvertedParams = map[string]bool{
7979
"managementserverid": true,
8080
}
8181

82+
// customResponseStructTypes maps the API call to a custom struct name
83+
// This is to change the struct type name to something other than the API name
84+
var customResponseStructTypes = map[string]string{
85+
"findHostsForMigration": "HostForMigration",
86+
}
87+
8288
// We prefill this one value to make sure it is not
8389
// created twice, as this is also a top level type.
8490
var typeNames = map[string]bool{"Nic": true}
@@ -1088,7 +1094,7 @@ func (s *service) generateAPITest(a *API) {
10881094
}
10891095
pn(")")
10901096
idPresent := false
1091-
if !(strings.HasPrefix(a.Name, "list") || a.Name == "registerTemplate") {
1097+
if !(strings.HasPrefix(a.Name, "list") || a.Name == "registerTemplate" || a.Name == "findHostsForMigration") {
10921098
for _, ap := range a.Response {
10931099
if ap.Name == "id" && ap.Type == "string" {
10941100
pn(" r, err := client.%s.%s(p)", strings.TrimSuffix(s.name, "Service"), capitalize(a.Name))
@@ -1766,7 +1772,7 @@ func (s *service) generateResponseType(a *API) {
17661772
// If this is a 'list' response, we need an separate list struct. There seem to be other
17671773
// types of responses that also need a separate list struct, so checking on exact matches
17681774
// for those once.
1769-
if strings.HasPrefix(a.Name, "list") || a.Name == "registerTemplate" {
1775+
if strings.HasPrefix(a.Name, "list") || a.Name == "registerTemplate" || a.Name == "findHostsForMigration" {
17701776
pn("type %s struct {", tn)
17711777

17721778
// This nasty check is for some specific response that do not behave consistent
@@ -1792,6 +1798,9 @@ func (s *service) generateResponseType(a *API) {
17921798
case "listDomainChildren":
17931799
pn(" Count int `json:\"count\"`")
17941800
pn(" %s []*%s `json:\"%s\"`", ln, parseSingular(ln), "domain")
1801+
case "findHostsForMigration":
1802+
pn(" Count int `json:\"count\"`")
1803+
pn(" Host []*%s `json:\"%s\"`", customResponseStructTypes[a.Name], "host")
17951804
default:
17961805
pn(" Count int `json:\"count\"`")
17971806
pn(" %s []*%s `json:\"%s\"`", ln, parseSingular(ln), strings.ToLower(parseSingular(ln)))
@@ -1850,6 +1859,10 @@ func (s *service) recusiveGenerateResponseType(aName string, tn string, resp API
18501859
customMarshal := false
18511860
found := make(map[string]bool)
18521861

1862+
if val, ok := customResponseStructTypes[aName]; ok {
1863+
tn = val
1864+
}
1865+
18531866
pn("type %s struct {", tn)
18541867

18551868
for _, r := range resp {

test/HostService_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,10 @@ func TestHostService(t *testing.T) {
187187
t.Skipf("Skipping as no json response is provided in testdata")
188188
}
189189
p := client.Host.NewFindHostsForMigrationParams("virtualmachineid")
190-
r, err := client.Host.FindHostsForMigration(p)
190+
_, err := client.Host.FindHostsForMigration(p)
191191
if err != nil {
192192
t.Errorf(err.Error())
193193
}
194-
if r.Id == "" {
195-
t.Errorf("Failed to parse response. ID not found")
196-
}
197194
}
198195
t.Run("FindHostsForMigration", testfindHostsForMigration)
199196

test/testdata/HostService.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,5 +317,56 @@
317317
"completed": "2021-10-03T07:10:09+0000",
318318
"jobid": "79da9d4b-6368-4ce5-9054-952295fc2c8a"
319319
}
320+
},
321+
"findHostsForMigration": {
322+
"findhostsformigrationresponse": {
323+
"count": 1,
324+
"host": [
325+
{
326+
"capabilities": "hvm",
327+
"clusterid": "3bf3ffc6-7f75-487c-a4b0-307b8fc96119",
328+
"clustername": "C1",
329+
"clustertype": "CloudManaged",
330+
"cpuallocated": "0%",
331+
"cpuallocatedpercentage": "0%",
332+
"cpuallocatedvalue": 0,
333+
"cpuallocatedwithoverprovisioning": "0%",
334+
"cpunumber": 4,
335+
"cpuspeed": 8000,
336+
"cpuused": "0%",
337+
"cpuwithoverprovisioning": "32000",
338+
"created": "2021-12-01T00:15:50+0000",
339+
"events": "ShutdownRequested; AgentConnected; AgentDisconnected; PingTimeout; StartAgentRebalance; Remove; Ping; HostDown; ManagementServerDown",
340+
"hahost": false,
341+
"hypervisor": "Simulator",
342+
"hypervisorversion": "4.16.0.0",
343+
"id": "6a5b8975-225f-4630-9093-3d55cec439d8",
344+
"ipaddress": "172.16.15.16",
345+
"islocalstorageactive": false,
346+
"jobstatus": 0,
347+
"lastpinged": "1970-01-19T18:10:51+0000",
348+
"managementserverid": 2886860803,
349+
"memoryallocated": "0%",
350+
"memoryallocatedbytes": 0,
351+
"memoryallocatedpercentage": "0%",
352+
"memorytotal": 8589934592,
353+
"memoryused": 0,
354+
"memorywithoverprovisioning": "8589934592",
355+
"name": "SimulatedAgent.c5e22bf2-9444-4de9-a731-f6001a33ef3a",
356+
"networkkbsread": 32768,
357+
"networkkbswrite": 16384,
358+
"podid": "5fe0ee61-581b-425a-afb4-d9301589b11e",
359+
"podname": "POD0",
360+
"requiresStorageMotion": false,
361+
"resourcestate": "Enabled",
362+
"state": "Up",
363+
"suitableformigration": true,
364+
"type": "Routing",
365+
"version": "4.16.0.0",
366+
"zoneid": "a6166bb3-d997-4093-acfe-a5c51c38a9e3",
367+
"zonename": "Sandbox-simulator"
368+
}
369+
]
370+
}
320371
}
321372
}

0 commit comments

Comments
 (0)