Skip to content
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

r/aws_apigatewayv2_integration: Data mapping in HTTP APIs #17043

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Comment on lines +348 to +351
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think has go common enough at this point, should we maybe encapsulate it into a function and reuse? or is it too simple?

Copy link
Contributor Author

@ewbankkit ewbankkit Jan 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like we do for diffStringMaps?
For example:

func diffSets(oldSet, newSet *schema.Set) (create, remove, unchanged *schema.Set) {
...
}

Copy link
Collaborator

@DrFaust92 DrFaust92 Jan 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.


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