-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to pass and override default hard coded api version #274
Merged
Merged
Changes from 4 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ff1b8db
Add change which allows to pass and override default hard coded api v…
vbauzys 1666e87
Add back function to be back compatible
vbauzys 5fd40cb
Improve docs
vbauzys a4a3f60
Add changelog
vbauzys 398d990
Improve docs and changelog
vbauzys 1ae3d74
Improve docs
vbauzys 4b22e8b
Add test
vbauzys 7d62c82
Merge branch 'master' into api-version-override
vbauzys 448b61f
Add directive
vbauzys dd83fc4
Improved test
vbauzys 4c4f580
Merge branch 'master' into api-version-override
vbauzys fdedb01
Fixes
vbauzys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -34,6 +34,8 @@ type Client struct { | |||||
// where vCloud director may take time to respond and retry mechanism is needed. | ||||||
// This must be >0 to avoid instant timeout errors. | ||||||
MaxRetryTimeout int | ||||||
|
||||||
supportedVersions SupportedVersions // Versions from /api/versions endpoint | ||||||
} | ||||||
|
||||||
// The header key used by default to set the authorization token. | ||||||
|
@@ -127,6 +129,12 @@ func ContainsNotFound(err error) bool { | |||||
|
||||||
// NewRequestWitNotEncodedParams allows passing complex values params that shouldn't be encoded like for queries. e.g. /query?filter=name=foo | ||||||
func (cli *Client) NewRequestWitNotEncodedParams(params map[string]string, notEncodedParams map[string]string, method string, reqUrl url.URL, body io.Reader) *http.Request { | ||||||
return cli.NewRequestWitNotEncodedParamsWithApiVersion(params, notEncodedParams, method, reqUrl, body, cli.APIVersion) | ||||||
} | ||||||
|
||||||
// NewRequestWitNotEncodedParamsWithApiVersion allows passing complex values params that shouldn't be encoded like for queries. e.g. /query?filter=name=foo | ||||||
// Passed Api version allows to you override default values used in request parameters. | ||||||
func (cli *Client) NewRequestWitNotEncodedParamsWithApiVersion(params map[string]string, notEncodedParams map[string]string, method string, reqUrl url.URL, body io.Reader, apiVersion string) *http.Request { | ||||||
vbauzys marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
reqValues := url.Values{} | ||||||
|
||||||
// Build up our request parameters | ||||||
|
@@ -152,7 +160,7 @@ func (cli *Client) NewRequestWitNotEncodedParams(params map[string]string, notEn | |||||
// Add the authorization header | ||||||
req.Header.Add(cli.VCDAuthHeader, cli.VCDToken) | ||||||
// Add the Accept header for VCD | ||||||
req.Header.Add("Accept", "application/*+xml;version="+cli.APIVersion) | ||||||
req.Header.Add("Accept", "application/*+xml;version="+apiVersion) | ||||||
} | ||||||
|
||||||
// Avoids passing data if the logging of requests is disabled | ||||||
|
@@ -188,6 +196,12 @@ func (cli *Client) NewRequest(params map[string]string, method string, reqUrl ur | |||||
return cli.NewRequestWitNotEncodedParams(params, nil, method, reqUrl, body) | ||||||
} | ||||||
|
||||||
// NewRequest creates a new HTTP request and applies necessary auth headers if set. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. udpated |
||||||
// Allows to override default request API Version | ||||||
func (cli *Client) NewRequestWithApiVersion(params map[string]string, method string, reqUrl url.URL, body io.Reader, apiVersion string) *http.Request { | ||||||
return cli.NewRequestWitNotEncodedParamsWithApiVersion(params, nil, method, reqUrl, body, apiVersion) | ||||||
} | ||||||
|
||||||
// ParseErr takes an error XML resp, error interface for unmarshaling and returns a single string for | ||||||
// use in error messages. | ||||||
func ParseErr(resp *http.Response, errType error) error { | ||||||
|
@@ -284,12 +298,36 @@ func checkRespWithErrType(resp *http.Response, err, errType error) (*http.Respon | |||||
// payload - XML struct which will be marshalled and added as body/payload | ||||||
// E.g. client.ExecuteTaskRequest(updateDiskLink.HREF, http.MethodPut, updateDiskLink.Type, "error updating disk: %s", xmlPayload) | ||||||
func (client *Client) ExecuteTaskRequest(pathURL, requestType, contentType, errorMessage string, payload interface{}) (Task, error) { | ||||||
return client.executeTaskRequest(pathURL, requestType, contentType, errorMessage, payload, client.APIVersion) | ||||||
} | ||||||
|
||||||
// Helper function creates request, runs it, checks response and parses task from response. | ||||||
// pathURL - request URL | ||||||
// requestType - HTTP method type | ||||||
// contentType - value to set for "Content-Type" | ||||||
// errorMessage - error message to return when error happens | ||||||
// payload - XML struct which will be marshalled and added as body/payload | ||||||
// apiVersion - api version which will be used in request | ||||||
// E.g. client.ExecuteTaskRequest(updateDiskLink.HREF, http.MethodPut, updateDiskLink.Type, "error updating disk: %s", xmlPayload) | ||||||
func (client *Client) ExecuteTaskRequestWithApiVersion(pathURL, requestType, contentType, errorMessage string, payload interface{}, apiVersion string) (Task, error) { | ||||||
return client.executeTaskRequest(pathURL, requestType, contentType, errorMessage, payload, apiVersion) | ||||||
} | ||||||
|
||||||
// Helper function creates request, runs it, checks response and parses task from response. | ||||||
// pathURL - request URL | ||||||
// requestType - HTTP method type | ||||||
// contentType - value to set for "Content-Type" | ||||||
// errorMessage - error message to return when error happens | ||||||
// payload - XML struct which will be marshalled and added as body/payload | ||||||
// apiVersion - api version which will be used in request | ||||||
// E.g. client.ExecuteTaskRequest(updateDiskLink.HREF, http.MethodPut, updateDiskLink.Type, "error updating disk: %s", xmlPayload) | ||||||
func (client *Client) executeTaskRequest(pathURL, requestType, contentType, errorMessage string, payload interface{}, apiVersion string) (Task, error) { | ||||||
|
||||||
if !isMessageWithPlaceHolder(errorMessage) { | ||||||
return Task{}, fmt.Errorf("error message has to include place holder for error") | ||||||
} | ||||||
|
||||||
resp, err := executeRequest(pathURL, requestType, contentType, payload, client) | ||||||
resp, err := executeRequestWithApiVersion(pathURL, requestType, contentType, payload, client, apiVersion) | ||||||
if err != nil { | ||||||
return Task{}, fmt.Errorf(errorMessage, err) | ||||||
} | ||||||
|
@@ -317,12 +355,36 @@ func (client *Client) ExecuteTaskRequest(pathURL, requestType, contentType, erro | |||||
// payload - XML struct which will be marshalled and added as body/payload | ||||||
// E.g. client.ExecuteRequestWithoutResponse(catalogItemHREF.String(), http.MethodDelete, "", "error deleting Catalog item: %s", nil) | ||||||
func (client *Client) ExecuteRequestWithoutResponse(pathURL, requestType, contentType, errorMessage string, payload interface{}) error { | ||||||
return client.executeRequestWithoutResponse(pathURL, requestType, contentType, errorMessage, payload, client.APIVersion) | ||||||
} | ||||||
|
||||||
// Helper function creates request, runs it, checks response and do not expect any values from it. | ||||||
// pathURL - request URL | ||||||
// requestType - HTTP method type | ||||||
// contentType - value to set for "Content-Type" | ||||||
// errorMessage - error message to return when error happens | ||||||
// payload - XML struct which will be marshalled and added as body/payload | ||||||
// apiVersion - api version which will be used in request | ||||||
// E.g. client.ExecuteRequestWithoutResponse(catalogItemHREF.String(), http.MethodDelete, "", "error deleting Catalog item: %s", nil) | ||||||
func (client *Client) ExecuteRequestWithoutResponseWithApiVersion(pathURL, requestType, contentType, errorMessage string, payload interface{}, apiVersion string) error { | ||||||
return client.executeRequestWithoutResponse(pathURL, requestType, contentType, errorMessage, payload, apiVersion) | ||||||
} | ||||||
|
||||||
// Helper function creates request, runs it, checks response and do not expect any values from it. | ||||||
// pathURL - request URL | ||||||
// requestType - HTTP method type | ||||||
// contentType - value to set for "Content-Type" | ||||||
// errorMessage - error message to return when error happens | ||||||
// payload - XML struct which will be marshalled and added as body/payload | ||||||
// apiVersion - api version which will be used in request | ||||||
// E.g. client.ExecuteRequestWithoutResponse(catalogItemHREF.String(), http.MethodDelete, "", "error deleting Catalog item: %s", nil) | ||||||
func (client *Client) executeRequestWithoutResponse(pathURL, requestType, contentType, errorMessage string, payload interface{}, apiVersion string) error { | ||||||
|
||||||
if !isMessageWithPlaceHolder(errorMessage) { | ||||||
return fmt.Errorf("error message has to include place holder for error") | ||||||
} | ||||||
|
||||||
resp, err := executeRequest(pathURL, requestType, contentType, payload, client) | ||||||
resp, err := executeRequestWithApiVersion(pathURL, requestType, contentType, payload, client, apiVersion) | ||||||
if err != nil { | ||||||
return fmt.Errorf(errorMessage, err) | ||||||
} | ||||||
|
@@ -350,12 +412,40 @@ func (client *Client) ExecuteRequestWithoutResponse(pathURL, requestType, conten | |||||
// E.g. unmarshalledAdminOrg := &types.AdminOrg{} | ||||||
// client.ExecuteRequest(adminOrg.AdminOrg.HREF, http.MethodGet, "", "error refreshing organization: %s", nil, unmarshalledAdminOrg) | ||||||
func (client *Client) ExecuteRequest(pathURL, requestType, contentType, errorMessage string, payload, out interface{}) (*http.Response, error) { | ||||||
return client.executeRequest(pathURL, requestType, contentType, errorMessage, payload, out, client.APIVersion) | ||||||
} | ||||||
|
||||||
// Helper function creates request, runs it, check responses and parses out interface from response. | ||||||
// pathURL - request URL | ||||||
// requestType - HTTP method type | ||||||
// contentType - value to set for "Content-Type" | ||||||
// errorMessage - error message to return when error happens | ||||||
// payload - XML struct which will be marshalled and added as body/payload | ||||||
// out - structure to be used for unmarshalling xml | ||||||
// apiVersion - api version which will be used in request | ||||||
// E.g. unmarshalledAdminOrg := &types.AdminOrg{} | ||||||
// client.ExecuteRequest(adminOrg.AdminOrg.HREF, http.MethodGet, "", "error refreshing organization: %s", nil, unmarshalledAdminOrg) | ||||||
func (client *Client) ExecuteRequestWithApiVersion(pathURL, requestType, contentType, errorMessage string, payload, out interface{}, apiVersion string) (*http.Response, error) { | ||||||
return client.executeRequest(pathURL, requestType, contentType, errorMessage, payload, out, apiVersion) | ||||||
} | ||||||
|
||||||
// Helper function creates request, runs it, check responses and parses out interface from response. | ||||||
// pathURL - request URL | ||||||
// requestType - HTTP method type | ||||||
// contentType - value to set for "Content-Type" | ||||||
// errorMessage - error message to return when error happens | ||||||
// payload - XML struct which will be marshalled and added as body/payload | ||||||
// out - structure to be used for unmarshalling xml | ||||||
// apiVersion - api version which will be used in request | ||||||
// E.g. unmarshalledAdminOrg := &types.AdminOrg{} | ||||||
// client.ExecuteRequest(adminOrg.AdminOrg.HREF, http.MethodGet, "", "error refreshing organization: %s", nil, unmarshalledAdminOrg) | ||||||
func (client *Client) executeRequest(pathURL, requestType, contentType, errorMessage string, payload, out interface{}, apiVersion string) (*http.Response, error) { | ||||||
|
||||||
if !isMessageWithPlaceHolder(errorMessage) { | ||||||
return &http.Response{}, fmt.Errorf("error message has to include place holder for error") | ||||||
} | ||||||
|
||||||
resp, err := executeRequest(pathURL, requestType, contentType, payload, client) | ||||||
resp, err := executeRequestWithApiVersion(pathURL, requestType, contentType, payload, client, apiVersion) | ||||||
if err != nil { | ||||||
return resp, fmt.Errorf(errorMessage, err) | ||||||
} | ||||||
|
@@ -390,7 +480,7 @@ func (client *Client) ExecuteParamRequestWithCustomError(pathURL string, params | |||||
return &http.Response{}, fmt.Errorf("error message has to include place holder for error") | ||||||
} | ||||||
|
||||||
resp, err := executeRequestCustomErr(pathURL, params, requestType, contentType, payload, client, errType) | ||||||
resp, err := executeRequestCustomErr(pathURL, params, requestType, contentType, payload, client, errType, client.APIVersion) | ||||||
if err != nil { | ||||||
return &http.Response{}, fmt.Errorf(errorMessage, err) | ||||||
} | ||||||
|
@@ -413,12 +503,12 @@ func (client *Client) ExecuteParamRequestWithCustomError(pathURL string, params | |||||
} | ||||||
|
||||||
// executeRequest does executeRequestCustomErr and checks for vCD errors in API response | ||||||
func executeRequest(pathURL, requestType, contentType string, payload interface{}, client *Client) (*http.Response, error) { | ||||||
return executeRequestCustomErr(pathURL, map[string]string{}, requestType, contentType, payload, client, &types.Error{}) | ||||||
func executeRequestWithApiVersion(pathURL, requestType, contentType string, payload interface{}, client *Client, apiVersion string) (*http.Response, error) { | ||||||
return executeRequestCustomErr(pathURL, map[string]string{}, requestType, contentType, payload, client, &types.Error{}, apiVersion) | ||||||
} | ||||||
|
||||||
// executeRequestCustomErr performs request and unmarshals API error to errType if not 2xx status was returned | ||||||
func executeRequestCustomErr(pathURL string, params map[string]string, requestType, contentType string, payload interface{}, client *Client, errType error) (*http.Response, error) { | ||||||
func executeRequestCustomErr(pathURL string, params map[string]string, requestType, contentType string, payload interface{}, client *Client, errType error, apiVersion string) (*http.Response, error) { | ||||||
url, _ := url.ParseRequestURI(pathURL) | ||||||
|
||||||
var req *http.Request | ||||||
|
@@ -431,10 +521,10 @@ func executeRequestCustomErr(pathURL string, params map[string]string, requestTy | |||||
} | ||||||
body := bytes.NewBufferString(xml.Header + string(marshaledXml)) | ||||||
|
||||||
req = client.NewRequest(params, requestType, *url, body) | ||||||
req = client.NewRequestWithApiVersion(params, requestType, *url, body, apiVersion) | ||||||
|
||||||
default: | ||||||
req = client.NewRequest(params, requestType, *url, nil) | ||||||
req = client.NewRequestWithApiVersion(params, requestType, *url, nil, apiVersion) | ||||||
} | ||||||
|
||||||
if contentType != "" { | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see a few tests for the new functions, to make sure that:
Execute*
methods are not affected.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you share your idea how to test that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a possible way:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two more ideas:
or
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
first option could be valid with on specific vCD version or I have to find valid for all versions, which is lottery
I'll check possibility with response headers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test added