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 669aa90  Add explicit POST functions (#42)
669aa90 is described below

commit 669aa90436c40755289f1cbf8d7f1e8ea6b1a679
Author: Marcus Sorensen <[email protected]>
AuthorDate: Fri Oct 7 00:05:44 2022 -0600

    Add explicit POST functions (#42)
    
    Allows for API to eventually specify whether post or get is preferred
    Also allows to select POST for custom APIs
    
    Co-authored-by: Marcus Sorensen <[email protected]>
---
 cloudstack/AuthenticationService.go |  2 +-
 cloudstack/CustomService.go         |  8 ++++++++
 cloudstack/VirtualMachineService.go |  4 ++--
 cloudstack/cloudstack.go            | 16 +++++++++++++++-
 generate/generate.go                | 30 ++++++++++++++++++++++++++++--
 5 files changed, 54 insertions(+), 6 deletions(-)

diff --git a/cloudstack/AuthenticationService.go 
b/cloudstack/AuthenticationService.go
index f64ad09..653f3cb 100644
--- a/cloudstack/AuthenticationService.go
+++ b/cloudstack/AuthenticationService.go
@@ -129,7 +129,7 @@ func (s *AuthenticationService) NewLoginParams(password 
string, username string)
 
 // Logs a user into the CloudStack. A successful login attempt will generate a 
JSESSIONID cookie value that can be passed in subsequent Query command calls 
until the "logout" command has been issued or the session has expired.
 func (s *AuthenticationService) Login(p *LoginParams) (*LoginResponse, error) {
-       resp, err := s.cs.newRequest("login", p.toURLValues())
+       resp, err := s.cs.newPostRequest("login", p.toURLValues())
        if err != nil {
                return nil, err
        }
diff --git a/cloudstack/CustomService.go b/cloudstack/CustomService.go
index b21c24c..d0e05f1 100644
--- a/cloudstack/CustomService.go
+++ b/cloudstack/CustomService.go
@@ -84,6 +84,14 @@ func (s *CustomService) CustomRequest(api string, p 
*CustomServiceParams, result
 
        return json.Unmarshal(resp, result)
 }
+func (s *CustomService) CustomPostRequest(api string, p *CustomServiceParams, 
result interface{}) error {
+       resp, err := s.cs.newPostRequest(api, p.toURLValues())
+       if err != nil {
+               return err
+       }
+
+       return json.Unmarshal(resp, result)
+}
 
 type CustomServiceIface interface {
 }
diff --git a/cloudstack/VirtualMachineService.go 
b/cloudstack/VirtualMachineService.go
index 88b55fd..912f2a1 100644
--- a/cloudstack/VirtualMachineService.go
+++ b/cloudstack/VirtualMachineService.go
@@ -1854,7 +1854,7 @@ func (s *VirtualMachineService) 
NewDeployVirtualMachineParams(serviceofferingid
 
 // Creates and automatically starts a virtual machine based on a service 
offering, disk offering, and template.
 func (s *VirtualMachineService) DeployVirtualMachine(p 
*DeployVirtualMachineParams) (*DeployVirtualMachineResponse, error) {
-       resp, err := s.cs.newRequest("deployVirtualMachine", p.toURLValues())
+       resp, err := s.cs.newPostRequest("deployVirtualMachine", 
p.toURLValues())
        if err != nil {
                return nil, err
        }
@@ -7225,7 +7225,7 @@ func (s *VirtualMachineService) 
NewUpdateVirtualMachineParams(id string) *Update
 
 // Updates properties of a virtual machine. The VM has to be stopped and 
restarted for the new properties to take effect. UpdateVirtualMachine does not 
first check whether the VM is stopped. Therefore, stop the VM manually before 
issuing this call.
 func (s *VirtualMachineService) UpdateVirtualMachine(p 
*UpdateVirtualMachineParams) (*UpdateVirtualMachineResponse, error) {
-       resp, err := s.cs.newRequest("updateVirtualMachine", p.toURLValues())
+       resp, err := s.cs.newPostRequest("updateVirtualMachine", 
p.toURLValues())
        if err != nil {
                return nil, err
        }
diff --git a/cloudstack/cloudstack.go b/cloudstack/cloudstack.go
index 256a257..79633b1 100644
--- a/cloudstack/cloudstack.go
+++ b/cloudstack/cloudstack.go
@@ -446,6 +446,20 @@ func (cs *CloudStackClient) GetAsyncJobResult(jobid 
string, timeout int64) (json
 // no error occured. If the API returns an error the result will be nil and 
the HTTP error code and CS
 // error details. If a processing (code) error occurs the result will be nil 
and the generated error
 func (cs *CloudStackClient) newRequest(api string, params url.Values) 
(json.RawMessage, error) {
+       return cs.newRawRequest(api, false, params)
+}
+
+// Execute the request against a CS API using POST. Will return the raw JSON 
data returned by the API and
+// nil if no error occured. If the API returns an error the result will be nil 
and the HTTP error code
+// and CS error details. If a processing (code) error occurs the result will 
be nil and the generated error
+func (cs *CloudStackClient) newPostRequest(api string, params url.Values) 
(json.RawMessage, error) {
+       return cs.newRawRequest(api, true, params)
+}
+
+// Execute a raw request against a CS API. Will return the raw JSON data 
returned by the API and nil if
+// no error occured. If the API returns an error the result will be nil and 
the HTTP error code and CS
+// error details. If a processing (code) error occurs the result will be nil 
and the generated error
+func (cs *CloudStackClient) newRawRequest(api string, post bool, params 
url.Values) (json.RawMessage, error) {
        params.Set("apiKey", cs.apiKey)
        params.Set("command", api)
        params.Set("response", "json")
@@ -465,7 +479,7 @@ func (cs *CloudStackClient) newRequest(api string, params 
url.Values) (json.RawM
 
        var err error
        var resp *http.Response
-       if !cs.HTTPGETOnly && (api == "deployVirtualMachine" || api == "login" 
|| api == "updateVirtualMachine") {
+       if !cs.HTTPGETOnly && post {
                // The deployVirtualMachine API should be called using a POST 
call
                // so we don't have to worry about the userdata size
 
diff --git a/generate/generate.go b/generate/generate.go
index 55f7d2b..c82f2d7 100644
--- a/generate/generate.go
+++ b/generate/generate.go
@@ -503,6 +503,20 @@ func (as *allServices) GeneralCode() ([]byte, error) {
        pn("// no error occured. If the API returns an error the result will be 
nil and the HTTP error code and CS")
        pn("// error details. If a processing (code) error occurs the result 
will be nil and the generated error")
        pn("func (cs *CloudStackClient) newRequest(api string, params 
url.Values) (json.RawMessage, error) {")
+       pn("            return cs.newRawRequest(api, false, params)")
+       pn("}")
+       pn("")
+       pn("// Execute the request against a CS API using POST. Will return the 
raw JSON data returned by the API and")
+       pn("// nil if no error occured. If the API returns an error the result 
will be nil and the HTTP error code")
+       pn("// and CS error details. If a processing (code) error occurs the 
result will be nil and the generated error")
+       pn("func (cs *CloudStackClient) newPostRequest(api string, params 
url.Values) (json.RawMessage, error) {")
+       pn("            return cs.newRawRequest(api, true, params)")
+       pn("}")
+       pn("")
+       pn("// Execute a raw request against a CS API. Will return the raw JSON 
data returned by the API and nil if")
+       pn("// no error occured. If the API returns an error the result will be 
nil and the HTTP error code and CS")
+       pn("// error details. If a processing (code) error occurs the result 
will be nil and the generated error")
+       pn("func (cs *CloudStackClient) newRawRequest(api string, post bool, 
params url.Values) (json.RawMessage, error) {")
        pn("    params.Set(\"apiKey\", cs.apiKey)")
        pn("    params.Set(\"command\", api)")
        pn("    params.Set(\"response\", \"json\")")
@@ -522,7 +536,7 @@ func (as *allServices) GeneralCode() ([]byte, error) {
        pn("")
        pn("    var err error")
        pn("    var resp *http.Response")
-       pn("    if !cs.HTTPGETOnly && (api == \"deployVirtualMachine\" || api 
== \"login\" || api == \"updateVirtualMachine\") {")
+       pn("    if !cs.HTTPGETOnly && post {")
        pn("            // The deployVirtualMachine API should be called using 
a POST call")
        pn("    // so we don't have to worry about the userdata size")
        pn("")
@@ -996,6 +1010,14 @@ func (s *service) GenerateCode() ([]byte, error) {
                pn("")
                pn("    return json.Unmarshal(resp, result)")
                pn("}")
+               pn("func (s *CustomService) CustomPostRequest(api string, p 
*CustomServiceParams, result interface{}) error {")
+               pn("    resp, err := s.cs.newPostRequest(api, p.toURLValues())")
+               pn("    if err != nil {")
+               pn("            return err")
+               pn("    }")
+               pn("")
+               pn("    return json.Unmarshal(resp, result)")
+               pn("}")
        }
 
        s.generateInterfaceType()
@@ -1635,7 +1657,11 @@ func (s *service) generateNewAPICallFunc(a *API) {
                pn("            time.Sleep(500 * time.Millisecond)")
                pn("    }")
        } else {
-               pn("    resp, err := s.cs.newRequest(\"%s\", p.toURLValues())", 
a.Name)
+               if a.Name == "deployVirtualMachine" || a.Name == "login" || 
a.Name == "updateVirtualMachine" {
+                       pn("    resp, err := s.cs.newPostRequest(\"%s\", 
p.toURLValues())", a.Name)
+               } else {
+                       pn("    resp, err := s.cs.newRequest(\"%s\", 
p.toURLValues())", a.Name)
+               }
        }
        pn("    if err != nil {")
        pn("            return nil, err")

Reply via email to