From 0da6dd8be0211e55f12df932fc2618795c7fa07f Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Thu, 16 Aug 2018 00:18:41 -0400 Subject: [PATCH] resource/aws_api_gateway_gateway_response: Support resource import * Support, test, and document resource import make testacc TEST=./aws TESTARGS='-run=TestAccAWSAPIGatewayGatewayResponse_' ==> Checking that code complies with gofmt requirements... TF_ACC=1 go test ./aws -v -run=TestAccAWSAPIGatewayGatewayResponse_ -timeout 120m === RUN TestAccAWSAPIGatewayGatewayResponse_basic --- PASS: TestAccAWSAPIGatewayGatewayResponse_basic (21.66s) PASS ok github.com/terraform-providers/terraform-provider-aws/aws 22.291s --- ...resource_aws_api_gateway_gateway_response.go | 15 +++++++++++++++ ...rce_aws_api_gateway_gateway_response_test.go | 17 +++++++++++++++++ .../r/api_gateway_gateway_response.markdown | 8 ++++++++ 3 files changed, 40 insertions(+) diff --git a/aws/resource_aws_api_gateway_gateway_response.go b/aws/resource_aws_api_gateway_gateway_response.go index bf9639960d1..cddf6e16105 100644 --- a/aws/resource_aws_api_gateway_gateway_response.go +++ b/aws/resource_aws_api_gateway_gateway_response.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "log" + "strings" "time" "github.com/aws/aws-sdk-go/aws" @@ -18,6 +19,20 @@ func resourceAwsApiGatewayGatewayResponse() *schema.Resource { Read: resourceAwsApiGatewayGatewayResponseRead, Update: resourceAwsApiGatewayGatewayResponsePut, Delete: resourceAwsApiGatewayGatewayResponseDelete, + Importer: &schema.ResourceImporter{ + State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + idParts := strings.Split(d.Id(), "/") + if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { + return nil, fmt.Errorf("Unexpected format of ID (%q), expected REST-API-ID/RESPONSE-TYPE", d.Id()) + } + restApiID := idParts[0] + responseType := idParts[1] + d.Set("response_type", responseType) + d.Set("rest_api_id", restApiID) + d.SetId(fmt.Sprintf("aggr-%s-%s", restApiID, responseType)) + return []*schema.ResourceData{d}, nil + }, + }, Schema: map[string]*schema.Schema{ "rest_api_id": { diff --git a/aws/resource_aws_api_gateway_gateway_response_test.go b/aws/resource_aws_api_gateway_gateway_response_test.go index c2dc74362f3..6cb99ae7552 100644 --- a/aws/resource_aws_api_gateway_gateway_response_test.go +++ b/aws/resource_aws_api_gateway_gateway_response_test.go @@ -43,6 +43,12 @@ func TestAccAWSAPIGatewayGatewayResponse_basic(t *testing.T) { resource.TestCheckNoResourceAttr("aws_api_gateway_gateway_response.test", "response_parameters.gatewayresponse.header.Authorization"), ), }, + { + ResourceName: "aws_api_gateway_gateway_response.test", + ImportState: true, + ImportStateIdFunc: testAccAWSAPIGatewayGatewayResponseImportStateIdFunc("aws_api_gateway_gateway_response.test"), + ImportStateVerify: true, + }, }, }) } @@ -107,6 +113,17 @@ func testAccCheckAWSAPIGatewayGatewayResponseDestroy(s *terraform.State) error { return nil } +func testAccAWSAPIGatewayGatewayResponseImportStateIdFunc(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", rs.Primary.Attributes["rest_api_id"], rs.Primary.Attributes["response_type"]), nil + } +} + func testAccAWSAPIGatewayGatewayResponseConfig(rName string) string { return fmt.Sprintf(` resource "aws_api_gateway_rest_api" "main" { diff --git a/website/docs/r/api_gateway_gateway_response.markdown b/website/docs/r/api_gateway_gateway_response.markdown index 41813f6d9bd..a7d5cb9f087 100644 --- a/website/docs/r/api_gateway_gateway_response.markdown +++ b/website/docs/r/api_gateway_gateway_response.markdown @@ -41,3 +41,11 @@ The following arguments are supported: * `status_code` - (Optional) The HTTP status code of the Gateway Response. * `response_parameters` - (Optional) A map specifying the templates used to transform the response body. * `response_templates` - (Optional) A map specifying the parameters (paths, query strings and headers) of the Gateway Response. + +## Import + +`aws_api_gateway_gateway_response` can be imported using `REST-API-ID/RESPONSE-TYPE`, e.g. + +``` +$ terraform import aws_api_gateway_gateway_response.example 12345abcde/UNAUTHORIZED +```