provider/aws: AWS API Gateway request parameters json (#6501)

* Update docs with new parameters

* Add request parameters as JSON

* Update function name and error statements
This commit is contained in:
Greg Thole 2016-05-05 16:14:51 -04:00 committed by Radek Simko
parent 03bc54b438
commit ebfc701265
10 changed files with 154 additions and 23 deletions

View File

@ -1,6 +1,7 @@
package aws package aws
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"time" "time"
@ -73,6 +74,11 @@ func resourceAwsApiGatewayIntegration() *schema.Resource {
Optional: true, Optional: true,
Elem: schema.TypeString, Elem: schema.TypeString,
}, },
"request_parameters_in_json": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
}, },
} }
} }
@ -93,6 +99,13 @@ func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interfa
templates[k] = v.(string) templates[k] = v.(string)
} }
parameters := make(map[string]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)
}
}
var credentials *string var credentials *string
if val, ok := d.GetOk("credentials"); ok { if val, ok := d.GetOk("credentials"); ok {
credentials = aws.String(val.(string)) credentials = aws.String(val.(string))
@ -105,8 +118,8 @@ func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interfa
Type: aws.String(d.Get("type").(string)), Type: aws.String(d.Get("type").(string)),
IntegrationHttpMethod: integrationHttpMethod, IntegrationHttpMethod: integrationHttpMethod,
Uri: uri, Uri: uri,
// TODO implement once [GH-2143](https://github.com/hashicorp/terraform/issues/2143) has been implemented // TODO reimplement once [GH-2143](https://github.com/hashicorp/terraform/issues/2143) has been implemented
RequestParameters: nil, RequestParameters: aws.StringMap(parameters),
RequestTemplates: aws.StringMap(templates), RequestTemplates: aws.StringMap(templates),
Credentials: credentials, Credentials: credentials,
CacheNamespace: nil, CacheNamespace: nil,
@ -149,6 +162,7 @@ func resourceAwsApiGatewayIntegrationRead(d *schema.ResourceData, meta interface
d.Set("credentials", integration.Credentials) d.Set("credentials", integration.Credentials)
d.Set("type", integration.Type) d.Set("type", integration.Type)
d.Set("uri", integration.Uri) d.Set("uri", integration.Uri)
d.Set("request_parameters_in_json", aws.StringValueMap(integration.RequestParameters))
return nil return nil
} }

View File

@ -75,7 +75,7 @@ func resourceAwsApiGatewayIntegrationResponseCreate(d *schema.ResourceData, meta
parameters := make(map[string]string) parameters := make(map[string]string)
if v, ok := d.GetOk("response_parameters_in_json"); ok { if v, ok := d.GetOk("response_parameters_in_json"); ok {
if err := json.Unmarshal([]byte(v.(string)), &parameters); err != nil { if err := json.Unmarshal([]byte(v.(string)), &parameters); err != nil {
return fmt.Errorf("Error unmarshaling request_parameters_in_json: %s", err) return fmt.Errorf("Error unmarshaling response_parameters_in_json: %s", err)
} }
} }

View File

@ -59,6 +59,9 @@ func testAccCheckAWSAPIGatewayMockIntegrationAttributes(conf *apigateway.Integra
if *conf.Type != "MOCK" { if *conf.Type != "MOCK" {
return fmt.Errorf("Wrong Type: %q", *conf.Type) return fmt.Errorf("Wrong Type: %q", *conf.Type)
} }
if *conf.RequestParameters["integration.request.header.X-Authorization"] != "'updated'" {
return fmt.Errorf("wrong updated RequestParameters for header.X-Authorization")
}
return nil return nil
} }
} }
@ -80,6 +83,9 @@ func testAccCheckAWSAPIGatewayIntegrationAttributes(conf *apigateway.Integration
if *conf.RequestTemplates["application/xml"] != "#set($inputRoot = $input.path('$'))\n{ }" { if *conf.RequestTemplates["application/xml"] != "#set($inputRoot = $input.path('$'))\n{ }" {
return fmt.Errorf("wrong RequestTemplate for application/xml") return fmt.Errorf("wrong RequestTemplate for application/xml")
} }
if *conf.RequestParameters["integration.request.header.X-Authorization"] != "'static'" {
return fmt.Errorf("wrong RequestParameters for header.X-Authorization")
}
return nil return nil
} }
} }
@ -178,6 +184,12 @@ resource "aws_api_gateway_integration" "test" {
"application/xml" = "#set($inputRoot = $input.path('$'))\n{ }" "application/xml" = "#set($inputRoot = $input.path('$'))\n{ }"
} }
request_parameters_in_json = <<PARAMS
{
"integration.request.header.X-Authorization": "'static'"
}
PARAMS
type = "HTTP" type = "HTTP"
uri = "https://www.google.de" uri = "https://www.google.de"
integration_http_method = "GET" integration_http_method = "GET"
@ -211,6 +223,12 @@ resource "aws_api_gateway_integration" "test" {
resource_id = "${aws_api_gateway_resource.test.id}" resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "${aws_api_gateway_method.test.http_method}" http_method = "${aws_api_gateway_method.test.http_method}"
request_parameters_in_json = <<PARAMS
{
"integration.request.header.X-Authorization": "'updated'"
}
PARAMS
type = "MOCK" type = "MOCK"
} }
` `

View File

@ -1,6 +1,7 @@
package aws package aws
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"time" "time"
@ -55,6 +56,11 @@ func resourceAwsApiGatewayMethod() *schema.Resource {
Optional: true, Optional: true,
Elem: schema.TypeString, Elem: schema.TypeString,
}, },
"request_parameters_in_json": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
}, },
} }
} }
@ -68,10 +74,9 @@ func resourceAwsApiGatewayMethodCreate(d *schema.ResourceData, meta interface{})
} }
parameters := make(map[string]bool) parameters := make(map[string]bool)
if parameterData, ok := d.GetOk("request_parameters"); ok { if v, ok := d.GetOk("request_parameters_in_json"); ok {
params := parameterData.(*schema.Set).List() if err := json.Unmarshal([]byte(v.(string)), &parameters); err != nil {
for k := range params { return fmt.Errorf("Error unmarshaling request_parameters_in_json: %s", err)
parameters[params[k].(string)] = true
} }
} }
@ -81,8 +86,8 @@ func resourceAwsApiGatewayMethodCreate(d *schema.ResourceData, meta interface{})
ResourceId: aws.String(d.Get("resource_id").(string)), ResourceId: aws.String(d.Get("resource_id").(string)),
RestApiId: aws.String(d.Get("rest_api_id").(string)), RestApiId: aws.String(d.Get("rest_api_id").(string)),
RequestModels: aws.StringMap(models), RequestModels: aws.StringMap(models),
// TODO implement once [GH-2143](https://github.com/hashicorp/terraform/issues/2143) has been implemented // TODO reimplement once [GH-2143](https://github.com/hashicorp/terraform/issues/2143) has been implemented
RequestParameters: nil, RequestParameters: aws.BoolMap(parameters),
ApiKeyRequired: aws.Bool(d.Get("api_key_required").(bool)), ApiKeyRequired: aws.Bool(d.Get("api_key_required").(bool)),
}) })
if err != nil { if err != nil {
@ -113,6 +118,7 @@ func resourceAwsApiGatewayMethodRead(d *schema.ResourceData, meta interface{}) e
} }
log.Printf("[DEBUG] Received API Gateway Method: %s", out) 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.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_in_json", aws.BoolValueMap(out.RequestParameters))
return nil return nil
} }
@ -134,6 +140,14 @@ func resourceAwsApiGatewayMethodUpdate(d *schema.ResourceData, meta interface{})
operations = append(operations, expandApiGatewayRequestResponseModelOperations(d, "request_models", "requestModels")...) operations = append(operations, expandApiGatewayRequestResponseModelOperations(d, "request_models", "requestModels")...)
} }
if d.HasChange("request_parameters_in_json") {
ops, err := expandApiGatewayMethodParametersJSONOperations(d, "request_parameters_in_json", "requestParameters")
if err != nil {
return err
}
operations = append(operations, ops...)
}
method, err := conn.UpdateMethod(&apigateway.UpdateMethodInput{ method, err := conn.UpdateMethod(&apigateway.UpdateMethodInput{
HttpMethod: aws.String(d.Get("http_method").(string)), HttpMethod: aws.String(d.Get("http_method").(string)),
ResourceId: aws.String(d.Get("resource_id").(string)), ResourceId: aws.String(d.Get("resource_id").(string)),

View File

@ -130,7 +130,7 @@ func resourceAwsApiGatewayMethodResponseUpdate(d *schema.ResourceData, meta inte
} }
if d.HasChange("response_parameters_in_json") { if d.HasChange("response_parameters_in_json") {
ops, err := expandApiGatewayMethodResponseParametersJSONOperations(d, "response_parameters_in_json", "responseParameters") ops, err := expandApiGatewayMethodParametersJSONOperations(d, "response_parameters_in_json", "responseParameters")
if err != nil { if err != nil {
return err return err
} }

View File

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

View File

@ -32,6 +32,14 @@ func TestAccAWSAPIGatewayMethod_basic(t *testing.T) {
"aws_api_gateway_method.test", "request_models.application/json", "Error"), "aws_api_gateway_method.test", "request_models.application/json", "Error"),
), ),
}, },
resource.TestStep{
Config: testAccAWSAPIGatewayMethodConfigUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayMethodExists("aws_api_gateway_method.test", &conf),
testAccCheckAWSAPIGatewayMethodAttributesUpdate(&conf),
),
},
}, },
}) })
} }
@ -44,6 +52,42 @@ func testAccCheckAWSAPIGatewayMethodAttributes(conf *apigateway.Method) resource
if *conf.AuthorizationType != "NONE" { if *conf.AuthorizationType != "NONE" {
return fmt.Errorf("Wrong Authorization: %q", *conf.AuthorizationType) return fmt.Errorf("Wrong Authorization: %q", *conf.AuthorizationType)
} }
if val, ok := conf.RequestParameters["method.request.header.Content-Type"]; !ok {
return fmt.Errorf("missing Content-Type RequestParameters")
} else {
if *val != false {
return fmt.Errorf("wrong Content-Type RequestParameters value")
}
}
if val, ok := conf.RequestParameters["method.request.querystring.page"]; !ok {
return fmt.Errorf("missing page RequestParameters")
} else {
if *val != true {
return fmt.Errorf("wrong query string RequestParameters value")
}
}
return nil
}
}
func testAccCheckAWSAPIGatewayMethodAttributesUpdate(conf *apigateway.Method) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *conf.HttpMethod != "GET" {
return fmt.Errorf("Wrong HttpMethod: %q", *conf.HttpMethod)
}
if conf.RequestParameters["method.request.header.Content-Type"] != nil {
return fmt.Errorf("Content-Type RequestParameters shouldn't exist")
}
if val, ok := conf.RequestParameters["method.request.querystring.page"]; !ok {
return fmt.Errorf("missing updated page RequestParameters")
} else {
if *val != false {
return fmt.Errorf("wrong query string RequestParameters updated value")
}
}
return nil return nil
} }
} }
@ -130,5 +174,41 @@ resource "aws_api_gateway_method" "test" {
request_models = { request_models = {
"application/json" = "Error" "application/json" = "Error"
} }
request_parameters_in_json = <<PARAMS
{
"method.request.header.Content-Type": false,
"method.request.querystring.page": true
}
PARAMS
}
`
const testAccAWSAPIGatewayMethodConfigUpdate = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
}
resource "aws_api_gateway_resource" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}"
path_part = "test"
}
resource "aws_api_gateway_method" "test" {
rest_api_id = "${aws_api_gateway_rest_api.test.id}"
resource_id = "${aws_api_gateway_resource.test.id}"
http_method = "GET"
authorization = "NONE"
request_models = {
"application/json" = "Error"
}
request_parameters_in_json = <<PARAMS
{
"method.request.querystring.page": false
}
PARAMS
} }
` `

View File

@ -948,7 +948,7 @@ func expandApiGatewayRequestResponseModelOperations(d *schema.ResourceData, key
return operations return operations
} }
func expandApiGatewayMethodResponseParametersJSONOperations(d *schema.ResourceData, key string, prefix string) ([]*apigateway.PatchOperation, error) { func expandApiGatewayMethodParametersJSONOperations(d *schema.ResourceData, key string, prefix string) ([]*apigateway.PatchOperation, error) {
operations := make([]*apigateway.PatchOperation, 0) operations := make([]*apigateway.PatchOperation, 0)
oldParameters, newParameters := d.GetChange(key) oldParameters, newParameters := d.GetChange(key)
@ -956,12 +956,12 @@ func expandApiGatewayMethodResponseParametersJSONOperations(d *schema.ResourceDa
newParametersMap := make(map[string]interface{}) newParametersMap := make(map[string]interface{})
if err := json.Unmarshal([]byte(oldParameters.(string)), &oldParametersMap); err != nil { if err := json.Unmarshal([]byte(oldParameters.(string)), &oldParametersMap); err != nil {
err := fmt.Errorf("Error unmarshaling old response_parameters_in_json: %s", err) err := fmt.Errorf("Error unmarshaling old %s: %s", key, err)
return operations, err return operations, err
} }
if err := json.Unmarshal([]byte(newParameters.(string)), &newParametersMap); err != nil { if err := json.Unmarshal([]byte(newParameters.(string)), &newParametersMap); err != nil {
err := fmt.Errorf("Error unmarshaling new response_parameters_in_json: %s", err) err := fmt.Errorf("Error unmarshaling new %s: %s", key, err)
return operations, err return operations, err
} }

View File

@ -56,3 +56,6 @@ The following arguments are supported:
Not all methods are compatible with all `AWS` integrations. 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`. 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_templates` - (Optional) A map of the integration's request templates.
* `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

View File

@ -44,3 +44,5 @@ The following arguments are supported:
* `request_models` - (Optional) A map of the API models used for the request's content type * `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`) 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`. 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