Skip to content

Commit

Permalink
Fix for endpoint_params Mastercard#202
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavmb committed Aug 12, 2023
1 parent 9e200d3 commit 46a0cb2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ Required:

Optional:

- `endpoint_params` (Map of List of String) Additional key/values to pass to the underlying Oauth client library (as EndpointParams)
- `endpoint_params` (Map of Strings) Additional key/values to pass to the underlying Oauth client library (as EndpointParams). Look in examples/ directory for an example
- `oauth_scopes` (List of String) scopes
13 changes: 9 additions & 4 deletions examples/workingexamples/provider_with_oauth.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ provider "restapi" {
write_returns_object = true

oauth_client_credentials {
oauth_client_id = "example"
oauth_client_secret = "example"
oauth_token_endpoint = "https://example.com/tokenendpoint"
oauth_scopes = ["openid"]
oauth_client_id = "example"
oauth_client_secret = "example"
oauth_token_endpoint = "https://example.com/tokenendpoint"
oauth_scopes = ["openid"]
endpoint_params = <<ENDPOINT_PARAMS
{
"resource": "http://127.0.0.1/"
}
ENDPOINT_PARAMS
}
}
27 changes: 14 additions & 13 deletions restapi/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

/*Provider implements the REST API provider*/
Expand Down Expand Up @@ -157,13 +159,11 @@ func Provider() *schema.Provider {
Description: "scopes",
},
"endpoint_params": {
Type: schema.TypeMap,
Optional: true,
Description: "Additional key/values to pass to the underlying Oauth client library (as EndpointParams)",
Elem: &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
},
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringIsJSON,
Description: "Additional key/values to pass to the underlying Oauth client library (as EndpointParams)",
DiffSuppressFunc: structure.SuppressJsonDiff,
},
},
},
Expand Down Expand Up @@ -261,13 +261,14 @@ func configureProvider(d *schema.ResourceData) (interface{}, error) {
opt.oauthTokenURL = oauthConfig["oauth_token_endpoint"].(string)
opt.oauthScopes = expandStringSet(oauthConfig["oauth_scopes"].([]interface{}))

if tmp, ok := oauthConfig["endpoint_params"]; ok {
m := tmp.(map[string]interface{})
if endPointParamString := oauthConfig["endpoint_params"].(string); endPointParamString != "" {
endPointParams, err := structure.ExpandJsonFromString(endPointParamString)
if err != nil {
return fmt.Errorf("unable to parse settings: %s", err), err
}
setVals := url.Values{}
for k, vals := range m {
for _, val := range vals.([]string) {
setVals.Add(k, val)
}
for k, vals := range endPointParams {
setVals.Set(k, vals.(string))
}
opt.oauthEndpointParams = setVals
}
Expand Down

0 comments on commit 46a0cb2

Please sign in to comment.