Skip to content

Commit

Permalink
r/aws_apigatewayv2_integration: Data mapping in HTTP APIs.
Browse files Browse the repository at this point in the history
Acceptance test output:

$ make testacc TEST=./aws/ TESTARGS='-run=TestAccAWSAPIGatewayV2Integration_'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -count 1 -parallel 20 -run=TestAccAWSAPIGatewayV2Integration_ -timeout 120m
=== RUN   TestAccAWSAPIGatewayV2Integration_basicWebSocket
=== PAUSE TestAccAWSAPIGatewayV2Integration_basicWebSocket
=== RUN   TestAccAWSAPIGatewayV2Integration_basicHttp
=== PAUSE TestAccAWSAPIGatewayV2Integration_basicHttp
=== RUN   TestAccAWSAPIGatewayV2Integration_disappears
=== PAUSE TestAccAWSAPIGatewayV2Integration_disappears
=== RUN   TestAccAWSAPIGatewayV2Integration_DataMappingHttp
=== PAUSE TestAccAWSAPIGatewayV2Integration_DataMappingHttp
=== RUN   TestAccAWSAPIGatewayV2Integration_IntegrationTypeHttp
=== PAUSE TestAccAWSAPIGatewayV2Integration_IntegrationTypeHttp
=== RUN   TestAccAWSAPIGatewayV2Integration_LambdaWebSocket
=== PAUSE TestAccAWSAPIGatewayV2Integration_LambdaWebSocket
=== RUN   TestAccAWSAPIGatewayV2Integration_LambdaHttp
=== PAUSE TestAccAWSAPIGatewayV2Integration_LambdaHttp
=== RUN   TestAccAWSAPIGatewayV2Integration_VpcLinkWebSocket
=== PAUSE TestAccAWSAPIGatewayV2Integration_VpcLinkWebSocket
=== RUN   TestAccAWSAPIGatewayV2Integration_VpcLinkHttp
=== PAUSE TestAccAWSAPIGatewayV2Integration_VpcLinkHttp
=== RUN   TestAccAWSAPIGatewayV2Integration_AwsServiceIntegration
=== PAUSE TestAccAWSAPIGatewayV2Integration_AwsServiceIntegration
=== CONT  TestAccAWSAPIGatewayV2Integration_basicWebSocket
=== CONT  TestAccAWSAPIGatewayV2Integration_LambdaHttp
=== CONT  TestAccAWSAPIGatewayV2Integration_AwsServiceIntegration
=== CONT  TestAccAWSAPIGatewayV2Integration_VpcLinkHttp
=== CONT  TestAccAWSAPIGatewayV2Integration_VpcLinkWebSocket
=== CONT  TestAccAWSAPIGatewayV2Integration_DataMappingHttp
=== CONT  TestAccAWSAPIGatewayV2Integration_LambdaWebSocket
=== CONT  TestAccAWSAPIGatewayV2Integration_IntegrationTypeHttp
=== CONT  TestAccAWSAPIGatewayV2Integration_disappears
=== CONT  TestAccAWSAPIGatewayV2Integration_basicHttp
--- PASS: TestAccAWSAPIGatewayV2Integration_disappears (29.08s)
--- PASS: TestAccAWSAPIGatewayV2Integration_basicWebSocket (30.68s)
--- PASS: TestAccAWSAPIGatewayV2Integration_basicHttp (32.61s)
--- PASS: TestAccAWSAPIGatewayV2Integration_IntegrationTypeHttp (46.05s)
--- PASS: TestAccAWSAPIGatewayV2Integration_DataMappingHttp (48.71s)
--- PASS: TestAccAWSAPIGatewayV2Integration_AwsServiceIntegration (48.96s)
--- PASS: TestAccAWSAPIGatewayV2Integration_LambdaWebSocket (49.47s)
--- PASS: TestAccAWSAPIGatewayV2Integration_LambdaHttp (53.71s)
--- PASS: TestAccAWSAPIGatewayV2Integration_VpcLinkHttp (412.36s)
--- PASS: TestAccAWSAPIGatewayV2Integration_VpcLinkWebSocket (718.91s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	719.017s
  • Loading branch information
ewbankkit committed Jan 11, 2021
1 parent 3bd91cf commit 8aa6d44
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 3 deletions.
99 changes: 99 additions & 0 deletions aws/resource_aws_apigatewayv2_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ func resourceAwsApiGatewayV2Integration() *schema.Resource {
// Length between [0-32768].
Elem: &schema.Schema{Type: schema.TypeString},
},
"response_parameters": {
Type: schema.TypeSet,
Optional: true,
MinItems: 0,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"mappings": {
Type: schema.TypeMap,
Required: true,
// Length between [1-512].
Elem: &schema.Schema{Type: schema.TypeString},
},
"status_code": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"template_selection_expression": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -189,6 +208,9 @@ func resourceAwsApiGatewayV2IntegrationCreate(d *schema.ResourceData, meta inter
if v, ok := d.GetOk("request_templates"); ok {
req.RequestTemplates = stringMapToPointers(v.(map[string]interface{}))
}
if v, ok := d.GetOk("response_parameters"); ok && v.(*schema.Set).Len() > 0 {
req.ResponseParameters = expandApiGateway2IntegrationResponseParameters(v.(*schema.Set).List())
}
if v, ok := d.GetOk("template_selection_expression"); ok {
req.TemplateSelectionExpression = aws.String(v.(string))
}
Expand Down Expand Up @@ -246,6 +268,10 @@ func resourceAwsApiGatewayV2IntegrationRead(d *schema.ResourceData, meta interfa
if err != nil {
return fmt.Errorf("error setting request_templates: %s", err)
}
err = d.Set("response_parameters", flattenApiGateway2IntegrationResponseParameters(resp.ResponseParameters))
if err != nil {
return fmt.Errorf("error setting response_parameters: %s", err)
}
d.Set("template_selection_expression", resp.TemplateSelectionExpression)
d.Set("timeout_milliseconds", resp.TimeoutInMillis)
if err := d.Set("tls_config", flattenApiGateway2TlsConfig(resp.TlsConfig)); err != nil {
Expand Down Expand Up @@ -298,6 +324,7 @@ func resourceAwsApiGatewayV2IntegrationUpdate(d *schema.ResourceData, meta inter
if d.HasChange("request_parameters") {
o, n := d.GetChange("request_parameters")
add, del, nop := diffStringMaps(o.(map[string]interface{}), n.(map[string]interface{}))

// Parameters are removed by setting the associated value to "".
for k := range del {
del[k] = aws.String("")
Expand All @@ -311,11 +338,36 @@ func resourceAwsApiGatewayV2IntegrationUpdate(d *schema.ResourceData, meta inter
for k, v := range nop {
variables[k] = v
}

req.RequestParameters = variables
}
if d.HasChange("request_templates") {
req.RequestTemplates = stringMapToPointers(d.Get("request_templates").(map[string]interface{}))
}
if d.HasChange("response_parameters") {
o, n := d.GetChange("response_parameters")
os := o.(*schema.Set)
ns := n.(*schema.Set)
del := os.Difference(ns).List()

req.ResponseParameters = expandApiGateway2IntegrationResponseParameters(ns.List())

// Parameters are removed by setting the associated value to {}.
for _, tfMapRaw := range del {
tfMap, ok := tfMapRaw.(map[string]interface{})

if !ok {
continue
}

if v, ok := tfMap["status_code"].(string); ok && v != "" {
if req.ResponseParameters == nil {
req.ResponseParameters = map[string]map[string]*string{}
}
req.ResponseParameters[v] = map[string]*string{}
}
}
}
if d.HasChange("template_selection_expression") {
req.TemplateSelectionExpression = aws.String(d.Get("template_selection_expression").(string))
}
Expand Down Expand Up @@ -406,3 +458,50 @@ func flattenApiGateway2TlsConfig(config *apigatewayv2.TlsConfig) []interface{} {
"server_name_to_verify": aws.StringValue(config.ServerNameToVerify),
}}
}

func expandApiGateway2IntegrationResponseParameters(tfList []interface{}) map[string]map[string]*string {
if len(tfList) == 0 {
return nil
}

responseParameters := map[string]map[string]*string{}

for _, tfMapRaw := range tfList {
tfMap, ok := tfMapRaw.(map[string]interface{})

if !ok {
continue
}

if vStatusCode, ok := tfMap["status_code"].(string); ok && vStatusCode != "" {
if v, ok := tfMap["mappings"].(map[string]interface{}); ok && len(v) > 0 {
responseParameters[vStatusCode] = stringMapToPointers(v)
}
}
}

return responseParameters
}

func flattenApiGateway2IntegrationResponseParameters(responseParameters map[string]map[string]*string) []interface{} {
if len(responseParameters) == 0 {
return nil
}

var tfList []interface{}

for statusCode, mappings := range responseParameters {
if len(mappings) == 0 {
continue
}

tfMap := map[string]interface{}{}

tfMap["status_code"] = statusCode
tfMap["mappings"] = aws.StringValueMap(mappings)

tfList = append(tfList, tfMap)
}

return tfList
}
Loading

0 comments on commit 8aa6d44

Please sign in to comment.