From 7804ec93e6d7cb6bbd3cffd9c114314d02b8f2b8 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Thu, 16 Aug 2018 00:24:00 -0400 Subject: [PATCH] resource/aws_api_gateway_integration_response: Support resource import * Support, test, and document resource import * Properly read content_handling into Terraform state for drift detection * Properly read response_templates into Terraform state for drift detection make testacc TEST=./aws TESTARGS='-run=TestAccAWSAPIGatewayIntegrationResponse_' ==> Checking that code complies with gofmt requirements... TF_ACC=1 go test ./aws -v -run=TestAccAWSAPIGatewayIntegrationResponse_ -timeout 120m === RUN TestAccAWSAPIGatewayIntegrationResponse_basic --- PASS: TestAccAWSAPIGatewayIntegrationResponse_basic (28.89s) PASS ok github.com/terraform-providers/terraform-provider-aws/aws 29.523s --- ...ce_aws_api_gateway_integration_response.go | 47 +++++++++++++++++-- ...s_api_gateway_integration_response_test.go | 21 ++++++++- ...gateway_integration_response.html.markdown | 8 ++++ 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/aws/resource_aws_api_gateway_integration_response.go b/aws/resource_aws_api_gateway_integration_response.go index 6faab5f1821..1da6a179955 100644 --- a/aws/resource_aws_api_gateway_integration_response.go +++ b/aws/resource_aws_api_gateway_integration_response.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "log" + "strings" "time" "github.com/aws/aws-sdk-go/aws" @@ -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": { @@ -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 } diff --git a/aws/resource_aws_api_gateway_integration_response_test.go b/aws/resource_aws_api_gateway_integration_response_test.go index 5eef59cd88c..2829bfa487a 100644 --- a/aws/resource_aws_api_gateway_integration_response_test.go +++ b/aws/resource_aws_api_gateway_integration_response_test.go @@ -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", ""), ), }, @@ -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, + }, }, }) } @@ -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" diff --git a/website/docs/r/api_gateway_integration_response.html.markdown b/website/docs/r/api_gateway_integration_response.html.markdown index 43f86032ce5..1c0e9a394b7 100644 --- a/website/docs/r/api_gateway_integration_response.html.markdown +++ b/website/docs/r/api_gateway_integration_response.html.markdown @@ -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 +```