provider/aws: Re-implement api gateway parameter handling (#7794)

* provider/aws: Re-implement api gateway parameter handling

this PR cleans up some left overs from PR #4295, namely the parameter handling.

now that GH-2143 is finally closed this PR does away with the ugly
`request_parameters_in_json` and `response_parameters_in_json` hack.

* Add deprecation message and conflictsWith settings

following @radeksimko s advice, keeping the old code around with a deprecation
warning.

this should be cleaned up in a few releases

* provider/aws: fix missing append operation

* provider/aws: mark old parameters clearly as deprecated

* provider/aws work around #8104

following @radeksimko s lead

* provider/aws fix cnp error
This commit is contained in:
Raphael Randschau 2016-08-11 12:49:59 +02:00 committed by Radek Simko
parent d7285d1b14
commit 66a14cb3b7
13 changed files with 201 additions and 78 deletions

View File

@ -75,9 +75,18 @@ func resourceAwsApiGatewayIntegration() *schema.Resource {
Elem: schema.TypeString,
},
"request_parameters": &schema.Schema{
Type: schema.TypeMap,
Elem: schema.TypeString,
Optional: true,
ConflictsWith: []string{"request_parameters_in_json"},
},
"request_parameters_in_json": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"request_parameters"},
Deprecated: "Use field request_parameters instead",
},
"passthrough_behavior": &schema.Schema{
@ -107,6 +116,12 @@ func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interfa
}
parameters := make(map[string]string)
if kv, ok := d.GetOk("request_parameters"); ok {
for k, v := range kv.(map[string]interface{}) {
parameters[k] = v.(string)
}
}
if v, ok := d.GetOk("request_parameters_in_json"); ok {
if err := json.Unmarshal([]byte(v.(string)), &parameters); err != nil {
return fmt.Errorf("Error unmarshaling request_parameters_in_json: %s", err)
@ -129,8 +144,7 @@ func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interfa
RestApiId: aws.String(d.Get("rest_api_id").(string)),
Type: aws.String(d.Get("type").(string)),
IntegrationHttpMethod: integrationHttpMethod,
Uri: uri,
// TODO reimplement once [GH-2143](https://github.com/hashicorp/terraform/issues/2143) has been implemented
Uri: uri,
RequestParameters: aws.StringMap(parameters),
RequestTemplates: aws.StringMap(templates),
Credentials: credentials,
@ -175,6 +189,7 @@ func resourceAwsApiGatewayIntegrationRead(d *schema.ResourceData, meta interface
d.Set("credentials", integration.Credentials)
d.Set("type", integration.Type)
d.Set("uri", integration.Uri)
d.Set("request_parameters", aws.StringValueMap(integration.RequestParameters))
d.Set("request_parameters_in_json", aws.StringValueMap(integration.RequestParameters))
d.Set("passthrough_behavior", integration.PassthroughBehavior)

View File

@ -56,9 +56,18 @@ func resourceAwsApiGatewayIntegrationResponse() *schema.Resource {
Elem: schema.TypeString,
},
"response_parameters": &schema.Schema{
Type: schema.TypeMap,
Elem: schema.TypeString,
Optional: true,
ConflictsWith: []string{"response_parameters_in_json"},
},
"response_parameters_in_json": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"response_parameters"},
Deprecated: "Use field response_parameters instead",
},
},
}
@ -73,6 +82,11 @@ func resourceAwsApiGatewayIntegrationResponseCreate(d *schema.ResourceData, meta
}
parameters := make(map[string]string)
if kv, ok := d.GetOk("response_parameters"); ok {
for k, v := range kv.(map[string]interface{}) {
parameters[k] = v.(string)
}
}
if v, ok := d.GetOk("response_parameters_in_json"); ok {
if err := json.Unmarshal([]byte(v.(string)), &parameters); err != nil {
return fmt.Errorf("Error unmarshaling response_parameters_in_json: %s", err)
@ -80,12 +94,11 @@ func resourceAwsApiGatewayIntegrationResponseCreate(d *schema.ResourceData, meta
}
input := apigateway.PutIntegrationResponseInput{
HttpMethod: aws.String(d.Get("http_method").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
StatusCode: aws.String(d.Get("status_code").(string)),
ResponseTemplates: aws.StringMap(templates),
// TODO reimplement once [GH-2143](https://github.com/hashicorp/terraform/issues/2143) has been implemented
HttpMethod: aws.String(d.Get("http_method").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
StatusCode: aws.String(d.Get("status_code").(string)),
ResponseTemplates: aws.StringMap(templates),
ResponseParameters: aws.StringMap(parameters),
}
if v, ok := d.GetOk("selection_pattern"); ok {
@ -125,6 +138,7 @@ func resourceAwsApiGatewayIntegrationResponseRead(d *schema.ResourceData, meta i
d.SetId(fmt.Sprintf("agir-%s-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string), d.Get("status_code").(string)))
d.Set("response_templates", integrationResponse.ResponseTemplates)
d.Set("selection_pattern", integrationResponse.SelectionPattern)
d.Set("response_parameters", aws.StringValueMap(integrationResponse.ResponseParameters))
d.Set("response_parameters_in_json", aws.StringValueMap(integrationResponse.ResponseParameters))
return nil
}

View File

@ -185,11 +185,9 @@ resource "aws_api_gateway_method_response" "error" {
"application/json" = "Error"
}
response_parameters_in_json = <<PARAMS
{
"method.response.header.Content-Type": true
response_parameters = {
"method.response.header.Content-Type" = true
}
PARAMS
}
resource "aws_api_gateway_integration" "test" {
@ -217,11 +215,9 @@ resource "aws_api_gateway_integration_response" "test" {
"application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
}
response_parameters_in_json = <<PARAMS
{
"method.response.header.Content-Type": "integration.response.body.type"
response_parameters = {
"method.response.header.Content-Type" = "integration.response.body.type"
}
PARAMS
}
`
@ -257,11 +253,9 @@ resource "aws_api_gateway_method_response" "error" {
"application/json" = "Error"
}
response_parameters_in_json = <<PARAMS
{
"method.response.header.Content-Type": true
response_parameters = {
"method.response.header.Content-Type" = true
}
PARAMS
}
resource "aws_api_gateway_integration" "test" {

View File

@ -188,11 +188,9 @@ resource "aws_api_gateway_integration" "test" {
"application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
}
request_parameters_in_json = <<PARAMS
{
"integration.request.header.X-Authorization": "'static'"
request_parameters = {
"integration.request.header.X-Authorization" = "'static'"
}
PARAMS
type = "HTTP"
uri = "https://www.google.de"
@ -228,11 +226,9 @@ resource "aws_api_gateway_integration" "test" {
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_method.test.http_method}"
request_parameters_in_json = <<PARAMS
{
"integration.request.header.X-Authorization": "'updated'"
request_parameters = {
"integration.request.header.X-Authorization" = "'updated'"
}
PARAMS
type = "MOCK"
passthrough_behavior = "NEVER"

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws"
@ -57,9 +58,18 @@ func resourceAwsApiGatewayMethod() *schema.Resource {
Elem: schema.TypeString,
},
"request_parameters": &schema.Schema{
Type: schema.TypeMap,
Elem: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"request_parameters_in_json"},
},
"request_parameters_in_json": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"request_parameters"},
Deprecated: "Use field request_parameters instead",
},
},
}
@ -74,6 +84,15 @@ func resourceAwsApiGatewayMethodCreate(d *schema.ResourceData, meta interface{})
}
parameters := make(map[string]bool)
if kv, ok := d.GetOk("request_parameters"); ok {
for k, v := range kv.(map[string]interface{}) {
parameters[k], ok = v.(bool)
if !ok {
value, _ := strconv.ParseBool(v.(string))
parameters[k] = value
}
}
}
if v, ok := d.GetOk("request_parameters_in_json"); ok {
if err := json.Unmarshal([]byte(v.(string)), &parameters); err != nil {
return fmt.Errorf("Error unmarshaling request_parameters_in_json: %s", err)
@ -86,7 +105,6 @@ func resourceAwsApiGatewayMethodCreate(d *schema.ResourceData, meta interface{})
ResourceId: aws.String(d.Get("resource_id").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
RequestModels: aws.StringMap(models),
// TODO reimplement once [GH-2143](https://github.com/hashicorp/terraform/issues/2143) has been implemented
RequestParameters: aws.BoolMap(parameters),
ApiKeyRequired: aws.Bool(d.Get("api_key_required").(bool)),
})
@ -118,6 +136,7 @@ func resourceAwsApiGatewayMethodRead(d *schema.ResourceData, meta interface{}) e
}
log.Printf("[DEBUG] Received API Gateway Method: %s", out)
d.SetId(fmt.Sprintf("agm-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string)))
d.Set("request_parameters", aws.BoolValueMap(out.RequestParameters))
d.Set("request_parameters_in_json", aws.BoolValueMap(out.RequestParameters))
return nil
@ -141,7 +160,24 @@ func resourceAwsApiGatewayMethodUpdate(d *schema.ResourceData, meta interface{})
}
if d.HasChange("request_parameters_in_json") {
ops, err := expandApiGatewayMethodParametersJSONOperations(d, "request_parameters_in_json", "requestParameters")
ops, err := deprecatedExpandApiGatewayMethodParametersJSONOperations(d, "request_parameters_in_json", "requestParameters")
if err != nil {
return err
}
operations = append(operations, ops...)
}
if d.HasChange("request_parameters") {
parameters := make(map[string]bool)
var ok bool
for k, v := range d.Get("request_parameters").(map[string]interface{}) {
parameters[k], ok = v.(bool)
if !ok {
value, _ := strconv.ParseBool(v.(string))
parameters[k] = value
}
}
ops, err := expandApiGatewayMethodParametersOperations(d, "request_parameters", "requestParameters")
if err != nil {
return err
}

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws"
@ -51,9 +52,18 @@ func resourceAwsApiGatewayMethodResponse() *schema.Resource {
Elem: schema.TypeString,
},
"response_parameters": &schema.Schema{
Type: schema.TypeMap,
Elem: schema.TypeBool,
Optional: true,
ConflictsWith: []string{"response_parameters_in_json"},
},
"response_parameters_in_json": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"response_parameters"},
Deprecated: "Use field response_parameters instead",
},
},
}
@ -68,6 +78,15 @@ func resourceAwsApiGatewayMethodResponseCreate(d *schema.ResourceData, meta inte
}
parameters := make(map[string]bool)
if kv, ok := d.GetOk("response_parameters"); ok {
for k, v := range kv.(map[string]interface{}) {
parameters[k], ok = v.(bool)
if !ok {
value, _ := strconv.ParseBool(v.(string))
parameters[k] = value
}
}
}
if v, ok := d.GetOk("response_parameters_in_json"); ok {
if err := json.Unmarshal([]byte(v.(string)), &parameters); err != nil {
return fmt.Errorf("Error unmarshaling request_parameters_in_json: %s", err)
@ -75,12 +94,11 @@ func resourceAwsApiGatewayMethodResponseCreate(d *schema.ResourceData, meta inte
}
_, err := conn.PutMethodResponse(&apigateway.PutMethodResponseInput{
HttpMethod: aws.String(d.Get("http_method").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
StatusCode: aws.String(d.Get("status_code").(string)),
ResponseModels: aws.StringMap(models),
// TODO reimplement once [GH-2143](https://github.com/hashicorp/terraform/issues/2143) has been implemented
HttpMethod: aws.String(d.Get("http_method").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)),
StatusCode: aws.String(d.Get("status_code").(string)),
ResponseModels: aws.StringMap(models),
ResponseParameters: aws.BoolMap(parameters),
})
if err != nil {
@ -113,6 +131,7 @@ func resourceAwsApiGatewayMethodResponseRead(d *schema.ResourceData, meta interf
log.Printf("[DEBUG] Received API Gateway Method: %s", methodResponse)
d.Set("response_models", aws.StringValueMap(methodResponse.ResponseModels))
d.Set("response_parameters", aws.BoolValueMap(methodResponse.ResponseParameters))
d.Set("response_parameters_in_json", aws.BoolValueMap(methodResponse.ResponseParameters))
d.SetId(fmt.Sprintf("agmr-%s-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string), d.Get("status_code").(string)))
@ -130,7 +149,15 @@ func resourceAwsApiGatewayMethodResponseUpdate(d *schema.ResourceData, meta inte
}
if d.HasChange("response_parameters_in_json") {
ops, err := expandApiGatewayMethodParametersJSONOperations(d, "response_parameters_in_json", "responseParameters")
ops, err := deprecatedExpandApiGatewayMethodParametersJSONOperations(d, "response_parameters_in_json", "responseParameters")
if err != nil {
return err
}
operations = append(operations, ops...)
}
if d.HasChange("response_parameters") {
ops, err := expandApiGatewayMethodParametersOperations(d, "response_parameters", "responseParameters")
if err != nil {
return err
}

View File

@ -184,11 +184,9 @@ resource "aws_api_gateway_method_response" "error" {
"application/json" = "Error"
}
response_parameters_in_json = <<PARAMS
{
"method.response.header.Content-Type": true
response_parameters = {
"method.response.header.Content-Type" = true
}
PARAMS
}
`
@ -224,11 +222,8 @@ resource "aws_api_gateway_method_response" "error" {
"application/json" = "Empty"
}
response_parameters_in_json = <<PARAMS
{
"method.response.header.Host": true
response_parameters = {
"method.response.header.Host" = true
}
PARAMS
}
`

View File

@ -175,12 +175,10 @@ resource "aws_api_gateway_method" "test" {
"application/json" = "Error"
}
request_parameters_in_json = <<PARAMS
{
"method.request.header.Content-Type": false,
"method.request.querystring.page": true
request_parameters = {
"method.request.header.Content-Type" = false,
"method.request.querystring.page" = true
}
PARAMS
}
`
@ -205,10 +203,8 @@ resource "aws_api_gateway_method" "test" {
"application/json" = "Error"
}
request_parameters_in_json = <<PARAMS
{
"method.request.querystring.page": false
request_parameters = {
"method.request.querystring.page" = false
}
PARAMS
}
`

View File

@ -1090,9 +1090,8 @@ func expandApiGatewayRequestResponseModelOperations(d *schema.ResourceData, key
return operations
}
func expandApiGatewayMethodParametersJSONOperations(d *schema.ResourceData, key string, prefix string) ([]*apigateway.PatchOperation, error) {
func deprecatedExpandApiGatewayMethodParametersJSONOperations(d *schema.ResourceData, key string, prefix string) ([]*apigateway.PatchOperation, error) {
operations := make([]*apigateway.PatchOperation, 0)
oldParameters, newParameters := d.GetChange(key)
oldParametersMap := make(map[string]interface{})
newParametersMap := make(map[string]interface{})
@ -1143,6 +1142,59 @@ func expandApiGatewayMethodParametersJSONOperations(d *schema.ResourceData, key
return operations, nil
}
func expandApiGatewayMethodParametersOperations(d *schema.ResourceData, key string, prefix string) ([]*apigateway.PatchOperation, error) {
operations := make([]*apigateway.PatchOperation, 0)
oldParameters, newParameters := d.GetChange(key)
oldParametersMap := oldParameters.(map[string]interface{})
newParametersMap := newParameters.(map[string]interface{})
for k, _ := range oldParametersMap {
operation := apigateway.PatchOperation{
Op: aws.String("remove"),
Path: aws.String(fmt.Sprintf("/%s/%s", prefix, k)),
}
for nK, nV := range newParametersMap {
b, ok := nV.(bool)
if !ok {
value, _ := strconv.ParseBool(nV.(string))
b = value
}
if nK == k {
operation.Op = aws.String("replace")
operation.Value = aws.String(strconv.FormatBool(b))
}
}
operations = append(operations, &operation)
}
for nK, nV := range newParametersMap {
exists := false
for k, _ := range oldParametersMap {
if k == nK {
exists = true
}
}
if !exists {
b, ok := nV.(bool)
if !ok {
value, _ := strconv.ParseBool(nV.(string))
b = value
}
operation := apigateway.PatchOperation{
Op: aws.String("add"),
Path: aws.String(fmt.Sprintf("/%s/%s", prefix, nK)),
Value: aws.String(strconv.FormatBool(b)),
}
operations = append(operations, &operation)
}
}
return operations, nil
}
func expandApiGatewayStageKeyOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
operations := make([]*apigateway.PatchOperation, 0)

View File

@ -56,9 +56,6 @@ The following arguments are supported:
Not all methods are compatible with all `AWS` integrations.
e.g. Lambda function [can only be invoked](https://github.com/awslabs/aws-apigateway-importer/issues/9#issuecomment-129651005) via `POST`.
* `request_templates` - (Optional) A map of the integration's request templates.
* `request_parameters` - (Optional) Request query string parameters and headers that should be passed to the
* `passthrough_behavior` - (Optional) The integration passthrough behavior (`WHEN_NO_MATCH`, `WHEN_NO_TEMPLATES`, `NEVER`). **Required** if `request_templates` is used.
* `request_parameters_in_json` - (Optional) A map written as a JSON string specifying
the request query string parameters and headers that should be passed to the
backend responder.
For example: `request_parameters_in_json = "{\"integration.request.header.X-Some-Other-Header\":\"method.request.header.X-Some-Header\"}"`
would add the header `X-Some-Header` from method to the integration as the header `X-Some-Other-Header`.
* `request_parameters_in_json` - **Deprecated**, use `request_parameters` instead.

View File

@ -69,6 +69,6 @@ The following arguments are supported:
If the backend is an `AWS` Lambda function, the AWS Lambda function error header is matched.
For all other `HTTP` and `AWS` backends, the HTTP status code is matched.
* `response_templates` - (Optional) A map specifying the templates used to transform the integration response body
* `response_parameters_in_json` - (Optional) A map written as JSON string specifying response parameters that can be read from the backend response
For example: `response_parameters_in_json = "{\"method.response.header.X-Some-Header\":\"integration.response.header.X-Some-Other-Header\"}"`,
would add the header `X-Some-Other-Header` from the integration response to the method response as the header `X-Some-Header`.
* `response_parameters` - (Optional) Specify the response parameters that can be read from the backend response
For example: `response_parameters = { "method.response.header.X-Some-Header" = "integration.response.header.X-Some-Other-Header" }`,
* `response_parameters_in_json` - **Deprecated**, use `response_parameters` instead.

View File

@ -44,7 +44,7 @@ The following arguments are supported:
* `request_models` - (Optional) A map of the API models used for the request's content type
where key is the content type (e.g. `application/json`)
and value is either `Error`, `Empty` (built-in models) or `aws_api_gateway_model`'s `name`.
* `request_parameters_in_json` - (Optional) A map written as a JSON string specifying
the request query string parameters and headers that should be passed to the integration
For example: `request_parameters_in_json = "{\"method.request.header.X-Some-Header\":true}"`
* `request_parameters` - (Optional) Specify which request query string parameters and headers that should be passed to the integration
For example: `request_parameters = { "method.request.header.X-Some-Header" = true }`
would define that the header X-Some-Header must be provided on the request.
* `request_parameters_in_json` - **Deprecated**, use `request_parameters` instead.

View File

@ -55,6 +55,7 @@ The following arguments are supported:
* `http_method` - (Required) The HTTP Method (`GET`, `POST`, `PUT`, `DELETE`, `HEAD`, `OPTION`)
* `status_code` - (Required) The HTTP status code
* `response_models` - (Optional) A map of the API models used for the response's content type
* `response_parameters_in_json` - (Optional) A map written as a JSON string representing response parameters that can be sent to the caller
For example: `response_parameters_in_json = "{\"method.response.header.X-Some-Header\":true}"`
* `response_parameters` - (Optional) Response parameters that can be sent to the caller
For example: `response_parameters = { "method.response.header.X-Some-Header" = true }`
would define that the header X-Some-Header can be provided on the response.
* `response_parameters_in_json` - **Deprecated**, use `response_parameters` instead.