Skip to content

Commit

Permalink
Merge pull request #5569 from terraform-providers/f-aws_api_gateway_i…
Browse files Browse the repository at this point in the history
…ntegration_response-import

resource/aws_api_gateway_integration_response: Support resource import
  • Loading branch information
bflad authored Aug 22, 2018
2 parents 269f409 + 7804ec9 commit 3e2e1ad
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 6 deletions.
47 changes: 43 additions & 4 deletions aws/resource_aws_api_gateway_integration_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -19,6 +20,24 @@ func resourceAwsApiGatewayIntegrationResponse() *schema.Resource {
Read: resourceAwsApiGatewayIntegrationResponseRead,
Update: resourceAwsApiGatewayIntegrationResponseCreate,
Delete: resourceAwsApiGatewayIntegrationResponseDelete,
Importer: &schema.ResourceImporter{
State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.Split(d.Id(), "/")
if len(idParts) != 4 || idParts[0] == "" || idParts[1] == "" || idParts[2] == "" || idParts[3] == "" {
return nil, fmt.Errorf("Unexpected format of ID (%q), expected REST-API-ID/RESOURCE-ID/HTTP-METHOD/STATUS-CODE", d.Id())
}
restApiID := idParts[0]
resourceID := idParts[1]
httpMethod := idParts[2]
statusCode := idParts[3]
d.Set("http_method", httpMethod)
d.Set("status_code", statusCode)
d.Set("resource_id", resourceID)
d.Set("rest_api_id", restApiID)
d.SetId(fmt.Sprintf("agir-%s-%s-%s-%s", restApiID, resourceID, httpMethod, statusCode))
return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"rest_api_id": {
Expand Down Expand Up @@ -148,11 +167,31 @@ func resourceAwsApiGatewayIntegrationResponseRead(d *schema.ResourceData, meta i

log.Printf("[DEBUG] Received API Gateway Integration Response: %s", integrationResponse)

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("content_handling", integrationResponse.ContentHandling)

if err := d.Set("response_parameters", aws.StringValueMap(integrationResponse.ResponseParameters)); err != nil {
return fmt.Errorf("error setting response_parameters: %s", err)
}

// KNOWN ISSUE: This next d.Set() is broken as it should be a JSON string of the map,
// however leaving as-is since this attribute has been deprecated
// for a very long time and will be removed soon in the next major release.
// Not worth the effort of fixing, acceptance testing, and potential JSON equivalence bugs.
if _, ok := d.GetOk("response_parameters_in_json"); ok {
d.Set("response_parameters_in_json", aws.StringValueMap(integrationResponse.ResponseParameters))
}

// We need to explicitly convert key = nil values into key = "", which aws.StringValueMap() removes
responseTemplateMap := make(map[string]string)
for key, valuePointer := range integrationResponse.ResponseTemplates {
responseTemplateMap[key] = aws.StringValue(valuePointer)
}
if err := d.Set("response_templates", responseTemplateMap); err != nil {
return fmt.Errorf("error setting response_templates: %s", err)
}

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
}

Expand Down
21 changes: 19 additions & 2 deletions aws/resource_aws_api_gateway_integration_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ func TestAccAWSAPIGatewayIntegrationResponse_basic(t *testing.T) {
"aws_api_gateway_integration_response.test", "response_templates.application/json", ""),
resource.TestCheckResourceAttr(
"aws_api_gateway_integration_response.test", "response_templates.application/xml", "#set($inputRoot = $input.path('$'))\n{ }"),
resource.TestCheckNoResourceAttr(
"aws_api_gateway_integration_response.test", "content_handling"),
resource.TestCheckResourceAttr(
"aws_api_gateway_integration_response.test", "content_handling", ""),
),
},

Expand All @@ -46,6 +46,12 @@ func TestAccAWSAPIGatewayIntegrationResponse_basic(t *testing.T) {
"aws_api_gateway_integration_response.test", "content_handling", "CONVERT_TO_BINARY"),
),
},
{
ResourceName: "aws_api_gateway_integration_response.test",
ImportState: true,
ImportStateIdFunc: testAccAWSAPIGatewayIntegrationResponseImportStateIdFunc("aws_api_gateway_integration_response.test"),
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -157,6 +163,17 @@ func testAccCheckAWSAPIGatewayIntegrationResponseDestroy(s *terraform.State) err
return nil
}

func testAccAWSAPIGatewayIntegrationResponseImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}

return fmt.Sprintf("%s/%s/%s/%s", rs.Primary.Attributes["rest_api_id"], rs.Primary.Attributes["resource_id"], rs.Primary.Attributes["http_method"], rs.Primary.Attributes["status_code"]), nil
}
}

const testAccAWSAPIGatewayIntegrationResponseConfig = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/api_gateway_integration_response.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,11 @@ The following arguments are supported:
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.
* `content_handling` - (Optional) Specifies how to handle request payload content type conversions. Supported values are `CONVERT_TO_BINARY` and `CONVERT_TO_TEXT`. If this property is not defined, the response payload will be passed through from the integration response to the method response without modification.

## Import

`aws_api_gateway_integration_response` can be imported using `REST-API-ID/RESOURCE-ID/HTTP-METHOD/STATUS-CODE`, e.g.

```
$ terraform import aws_api_gateway_integration_response.example 12345abcde/67890fghij/GET/200
```

0 comments on commit 3e2e1ad

Please sign in to comment.