Skip to content

Commit 669aa90

Browse files
mlsorensenMarcus Sorensen
andauthored
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]>
1 parent 822a5d2 commit 669aa90

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

cloudstack/AuthenticationService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func (s *AuthenticationService) NewLoginParams(password string, username string)
129129

130130
// 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.
131131
func (s *AuthenticationService) Login(p *LoginParams) (*LoginResponse, error) {
132-
resp, err := s.cs.newRequest("login", p.toURLValues())
132+
resp, err := s.cs.newPostRequest("login", p.toURLValues())
133133
if err != nil {
134134
return nil, err
135135
}

cloudstack/CustomService.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ func (s *CustomService) CustomRequest(api string, p *CustomServiceParams, result
8484

8585
return json.Unmarshal(resp, result)
8686
}
87+
func (s *CustomService) CustomPostRequest(api string, p *CustomServiceParams, result interface{}) error {
88+
resp, err := s.cs.newPostRequest(api, p.toURLValues())
89+
if err != nil {
90+
return err
91+
}
92+
93+
return json.Unmarshal(resp, result)
94+
}
8795

8896
type CustomServiceIface interface {
8997
}

cloudstack/VirtualMachineService.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,7 +1854,7 @@ func (s *VirtualMachineService) NewDeployVirtualMachineParams(serviceofferingid
18541854

18551855
// Creates and automatically starts a virtual machine based on a service offering, disk offering, and template.
18561856
func (s *VirtualMachineService) DeployVirtualMachine(p *DeployVirtualMachineParams) (*DeployVirtualMachineResponse, error) {
1857-
resp, err := s.cs.newRequest("deployVirtualMachine", p.toURLValues())
1857+
resp, err := s.cs.newPostRequest("deployVirtualMachine", p.toURLValues())
18581858
if err != nil {
18591859
return nil, err
18601860
}
@@ -7225,7 +7225,7 @@ func (s *VirtualMachineService) NewUpdateVirtualMachineParams(id string) *Update
72257225

72267226
// 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.
72277227
func (s *VirtualMachineService) UpdateVirtualMachine(p *UpdateVirtualMachineParams) (*UpdateVirtualMachineResponse, error) {
7228-
resp, err := s.cs.newRequest("updateVirtualMachine", p.toURLValues())
7228+
resp, err := s.cs.newPostRequest("updateVirtualMachine", p.toURLValues())
72297229
if err != nil {
72307230
return nil, err
72317231
}

cloudstack/cloudstack.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,20 @@ func (cs *CloudStackClient) GetAsyncJobResult(jobid string, timeout int64) (json
446446
// no error occured. If the API returns an error the result will be nil and the HTTP error code and CS
447447
// error details. If a processing (code) error occurs the result will be nil and the generated error
448448
func (cs *CloudStackClient) newRequest(api string, params url.Values) (json.RawMessage, error) {
449+
return cs.newRawRequest(api, false, params)
450+
}
451+
452+
// Execute the request against a CS API using POST. Will return the raw JSON data returned by the API and
453+
// nil if no error occured. If the API returns an error the result will be nil and the HTTP error code
454+
// and CS error details. If a processing (code) error occurs the result will be nil and the generated error
455+
func (cs *CloudStackClient) newPostRequest(api string, params url.Values) (json.RawMessage, error) {
456+
return cs.newRawRequest(api, true, params)
457+
}
458+
459+
// Execute a raw request against a CS API. Will return the raw JSON data returned by the API and nil if
460+
// no error occured. If the API returns an error the result will be nil and the HTTP error code and CS
461+
// error details. If a processing (code) error occurs the result will be nil and the generated error
462+
func (cs *CloudStackClient) newRawRequest(api string, post bool, params url.Values) (json.RawMessage, error) {
449463
params.Set("apiKey", cs.apiKey)
450464
params.Set("command", api)
451465
params.Set("response", "json")
@@ -465,7 +479,7 @@ func (cs *CloudStackClient) newRequest(api string, params url.Values) (json.RawM
465479

466480
var err error
467481
var resp *http.Response
468-
if !cs.HTTPGETOnly && (api == "deployVirtualMachine" || api == "login" || api == "updateVirtualMachine") {
482+
if !cs.HTTPGETOnly && post {
469483
// The deployVirtualMachine API should be called using a POST call
470484
// so we don't have to worry about the userdata size
471485

generate/generate.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,20 @@ func (as *allServices) GeneralCode() ([]byte, error) {
503503
pn("// no error occured. If the API returns an error the result will be nil and the HTTP error code and CS")
504504
pn("// error details. If a processing (code) error occurs the result will be nil and the generated error")
505505
pn("func (cs *CloudStackClient) newRequest(api string, params url.Values) (json.RawMessage, error) {")
506+
pn(" return cs.newRawRequest(api, false, params)")
507+
pn("}")
508+
pn("")
509+
pn("// Execute the request against a CS API using POST. Will return the raw JSON data returned by the API and")
510+
pn("// nil if no error occured. If the API returns an error the result will be nil and the HTTP error code")
511+
pn("// and CS error details. If a processing (code) error occurs the result will be nil and the generated error")
512+
pn("func (cs *CloudStackClient) newPostRequest(api string, params url.Values) (json.RawMessage, error) {")
513+
pn(" return cs.newRawRequest(api, true, params)")
514+
pn("}")
515+
pn("")
516+
pn("// Execute a raw request against a CS API. Will return the raw JSON data returned by the API and nil if")
517+
pn("// no error occured. If the API returns an error the result will be nil and the HTTP error code and CS")
518+
pn("// error details. If a processing (code) error occurs the result will be nil and the generated error")
519+
pn("func (cs *CloudStackClient) newRawRequest(api string, post bool, params url.Values) (json.RawMessage, error) {")
506520
pn(" params.Set(\"apiKey\", cs.apiKey)")
507521
pn(" params.Set(\"command\", api)")
508522
pn(" params.Set(\"response\", \"json\")")
@@ -522,7 +536,7 @@ func (as *allServices) GeneralCode() ([]byte, error) {
522536
pn("")
523537
pn(" var err error")
524538
pn(" var resp *http.Response")
525-
pn(" if !cs.HTTPGETOnly && (api == \"deployVirtualMachine\" || api == \"login\" || api == \"updateVirtualMachine\") {")
539+
pn(" if !cs.HTTPGETOnly && post {")
526540
pn(" // The deployVirtualMachine API should be called using a POST call")
527541
pn(" // so we don't have to worry about the userdata size")
528542
pn("")
@@ -996,6 +1010,14 @@ func (s *service) GenerateCode() ([]byte, error) {
9961010
pn("")
9971011
pn(" return json.Unmarshal(resp, result)")
9981012
pn("}")
1013+
pn("func (s *CustomService) CustomPostRequest(api string, p *CustomServiceParams, result interface{}) error {")
1014+
pn(" resp, err := s.cs.newPostRequest(api, p.toURLValues())")
1015+
pn(" if err != nil {")
1016+
pn(" return err")
1017+
pn(" }")
1018+
pn("")
1019+
pn(" return json.Unmarshal(resp, result)")
1020+
pn("}")
9991021
}
10001022

10011023
s.generateInterfaceType()
@@ -1635,7 +1657,11 @@ func (s *service) generateNewAPICallFunc(a *API) {
16351657
pn(" time.Sleep(500 * time.Millisecond)")
16361658
pn(" }")
16371659
} else {
1638-
pn(" resp, err := s.cs.newRequest(\"%s\", p.toURLValues())", a.Name)
1660+
if a.Name == "deployVirtualMachine" || a.Name == "login" || a.Name == "updateVirtualMachine" {
1661+
pn(" resp, err := s.cs.newPostRequest(\"%s\", p.toURLValues())", a.Name)
1662+
} else {
1663+
pn(" resp, err := s.cs.newRequest(\"%s\", p.toURLValues())", a.Name)
1664+
}
16391665
}
16401666
pn(" if err != nil {")
16411667
pn(" return nil, err")

0 commit comments

Comments
 (0)