This is an automated email from the ASF dual-hosted git repository.

rohit pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudstack-go.git


The following commit(s) were added to refs/heads/main by this push:
     new 822a5d2  FindHostsForMigration returns a list response (#41)
822a5d2 is described below

commit 822a5d25894da771c27640fdfa8f533ad0202475
Author: Marcus Sorensen <[email protected]>
AuthorDate: Fri Oct 7 00:05:27 2022 -0600

    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]>
---
 Makefile                       |  2 +-
 cloudstack/HostService.go      |  5 +++++
 generate/generate.go           | 17 ++++++++++++--
 test/HostService_test.go       |  5 +----
 test/testdata/HostService.json | 51 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 73 insertions(+), 7 deletions(-)

diff --git a/Makefile b/Makefile
index 01def1a..6911430 100644
--- a/Makefile
+++ b/Makefile
@@ -25,7 +25,7 @@ mocks:
 test:
        go test -v github.com/apache/cloudstack-go/v2/test
 
-MOCKGEN := $(shell pwd)/bin/mockgen
+MOCKGEN ?= $(shell pwd)/bin/mockgen
 mockgen: ## Download conversion-gen locally if necessary.
        $(call go-get-tool,$(MOCKGEN),github.com/golang/mock/mockgen)
 
diff --git a/cloudstack/HostService.go b/cloudstack/HostService.go
index 54e3b44..d96c596 100644
--- a/cloudstack/HostService.go
+++ b/cloudstack/HostService.go
@@ -1805,6 +1805,11 @@ func (s *HostService) FindHostsForMigration(p 
*FindHostsForMigrationParams) (*Fi
 }
 
 type FindHostsForMigrationResponse struct {
+       Count int                 `json:"count"`
+       Host  []*HostForMigration `json:"host"`
+}
+
+type HostForMigration struct {
        Averageload                      int64  `json:"averageload"`
        Capabilities                     string `json:"capabilities"`
        Clusterid                        string `json:"clusterid"`
diff --git a/generate/generate.go b/generate/generate.go
index 70a199b..55f7d2b 100644
--- a/generate/generate.go
+++ b/generate/generate.go
@@ -79,6 +79,12 @@ var longToStringConvertedParams = map[string]bool{
        "managementserverid": true,
 }
 
+// customResponseStructTypes maps the API call to a custom struct name
+// This is to change the struct type name to something other than the API name
+var customResponseStructTypes = map[string]string{
+       "findHostsForMigration": "HostForMigration",
+}
+
 // We prefill this one value to make sure it is not
 // created twice, as this is also a top level type.
 var typeNames = map[string]bool{"Nic": true}
@@ -1088,7 +1094,7 @@ func (s *service) generateAPITest(a *API) {
        }
        pn(")")
        idPresent := false
-       if !(strings.HasPrefix(a.Name, "list") || a.Name == "registerTemplate") 
{
+       if !(strings.HasPrefix(a.Name, "list") || a.Name == "registerTemplate" 
|| a.Name == "findHostsForMigration") {
                for _, ap := range a.Response {
                        if ap.Name == "id" && ap.Type == "string" {
                                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) {
        // If this is a 'list' response, we need an separate list struct. There 
seem to be other
        // types of responses that also need a separate list struct, so 
checking on exact matches
        // for those once.
-       if strings.HasPrefix(a.Name, "list") || a.Name == "registerTemplate" {
+       if strings.HasPrefix(a.Name, "list") || a.Name == "registerTemplate" || 
a.Name == "findHostsForMigration" {
                pn("type %s struct {", tn)
 
                // This nasty check is for some specific response that do not 
behave consistent
@@ -1792,6 +1798,9 @@ func (s *service) generateResponseType(a *API) {
                case "listDomainChildren":
                        pn("    Count int `json:\"count\"`")
                        pn("    %s []*%s `json:\"%s\"`", ln, parseSingular(ln), 
"domain")
+               case "findHostsForMigration":
+                       pn(" Count int `json:\"count\"`")
+                       pn(" Host []*%s `json:\"%s\"`", 
customResponseStructTypes[a.Name], "host")
                default:
                        pn("    Count int `json:\"count\"`")
                        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
        customMarshal := false
        found := make(map[string]bool)
 
+       if val, ok := customResponseStructTypes[aName]; ok {
+               tn = val
+       }
+
        pn("type %s struct {", tn)
 
        for _, r := range resp {
diff --git a/test/HostService_test.go b/test/HostService_test.go
index ab46ec9..30b32c3 100644
--- a/test/HostService_test.go
+++ b/test/HostService_test.go
@@ -187,13 +187,10 @@ func TestHostService(t *testing.T) {
                        t.Skipf("Skipping as no json response is provided in 
testdata")
                }
                p := 
client.Host.NewFindHostsForMigrationParams("virtualmachineid")
-               r, err := client.Host.FindHostsForMigration(p)
+               _, err := client.Host.FindHostsForMigration(p)
                if err != nil {
                        t.Errorf(err.Error())
                }
-               if r.Id == "" {
-                       t.Errorf("Failed to parse response. ID not found")
-               }
        }
        t.Run("FindHostsForMigration", testfindHostsForMigration)
 
diff --git a/test/testdata/HostService.json b/test/testdata/HostService.json
index 5badd62..a503e66 100644
--- a/test/testdata/HostService.json
+++ b/test/testdata/HostService.json
@@ -317,5 +317,56 @@
       "completed": "2021-10-03T07:10:09+0000",
       "jobid": "79da9d4b-6368-4ce5-9054-952295fc2c8a"
     }
+  },
+  "findHostsForMigration": {
+    "findhostsformigrationresponse": {
+      "count": 1,
+      "host": [
+        {
+          "capabilities": "hvm",
+          "clusterid": "3bf3ffc6-7f75-487c-a4b0-307b8fc96119",
+          "clustername": "C1",
+          "clustertype": "CloudManaged",
+          "cpuallocated": "0%",
+          "cpuallocatedpercentage": "0%",
+          "cpuallocatedvalue": 0,
+          "cpuallocatedwithoverprovisioning": "0%",
+          "cpunumber": 4,
+          "cpuspeed": 8000,
+          "cpuused": "0%",
+          "cpuwithoverprovisioning": "32000",
+          "created": "2021-12-01T00:15:50+0000",
+          "events": "ShutdownRequested; AgentConnected; AgentDisconnected; 
PingTimeout; StartAgentRebalance; Remove; Ping; HostDown; ManagementServerDown",
+          "hahost": false,
+          "hypervisor": "Simulator",
+          "hypervisorversion": "4.16.0.0",
+          "id": "6a5b8975-225f-4630-9093-3d55cec439d8",
+          "ipaddress": "172.16.15.16",
+          "islocalstorageactive": false,
+          "jobstatus": 0,
+          "lastpinged": "1970-01-19T18:10:51+0000",
+          "managementserverid": 2886860803,
+          "memoryallocated": "0%",
+          "memoryallocatedbytes": 0,
+          "memoryallocatedpercentage": "0%",
+          "memorytotal": 8589934592,
+          "memoryused": 0,
+          "memorywithoverprovisioning": "8589934592",
+          "name": "SimulatedAgent.c5e22bf2-9444-4de9-a731-f6001a33ef3a",
+          "networkkbsread": 32768,
+          "networkkbswrite": 16384,
+          "podid": "5fe0ee61-581b-425a-afb4-d9301589b11e",
+          "podname": "POD0",
+          "requiresStorageMotion": false,
+          "resourcestate": "Enabled",
+          "state": "Up",
+          "suitableformigration": true,
+          "type": "Routing",
+          "version": "4.16.0.0",
+          "zoneid": "a6166bb3-d997-4093-acfe-a5c51c38a9e3",
+          "zonename": "Sandbox-simulator"
+        }
+      ]
+    }
   }
 }
\ No newline at end of file

Reply via email to