diff --git a/aws/resource_aws_api_gateway_request_validator.go b/aws/resource_aws_api_gateway_request_validator.go index 045ad007f0e..52fb872822b 100644 --- a/aws/resource_aws_api_gateway_request_validator.go +++ b/aws/resource_aws_api_gateway_request_validator.go @@ -2,12 +2,13 @@ package aws import ( "fmt" + "log" + "strings" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/apigateway" "github.com/hashicorp/terraform/helper/schema" - "log" - "strings" ) func resourceAwsApiGatewayRequestValidator() *schema.Resource { @@ -16,6 +17,20 @@ func resourceAwsApiGatewayRequestValidator() *schema.Resource { Read: resourceAwsApiGatewayRequestValidatorRead, Update: resourceAwsApiGatewayRequestValidatorUpdate, Delete: resourceAwsApiGatewayRequestValidatorDelete, + 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/REQUEST-VALIDATOR-ID", d.Id()) + } + restApiID := idParts[0] + requestValidatorID := idParts[1] + d.Set("request_validator_id", requestValidatorID) + d.Set("rest_api_id", restApiID) + d.SetId(requestValidatorID) + return []*schema.ResourceData{d}, nil + }, + }, Schema: map[string]*schema.Schema{ "rest_api_id": { diff --git a/aws/resource_aws_api_gateway_request_validator_test.go b/aws/resource_aws_api_gateway_request_validator_test.go index 1f7c81162b6..8ee66e6b9a2 100644 --- a/aws/resource_aws_api_gateway_request_validator_test.go +++ b/aws/resource_aws_api_gateway_request_validator_test.go @@ -43,6 +43,12 @@ func TestAccAWSAPIGatewayRequestValidator_basic(t *testing.T) { resource.TestCheckResourceAttr("aws_api_gateway_request_validator.test", "validate_request_parameters", "true"), ), }, + { + ResourceName: "aws_api_gateway_request_validator.test", + ImportState: true, + ImportStateIdFunc: testAccAWSAPIGatewayRequestValidatorImportStateIdFunc("aws_api_gateway_request_validator.test"), + ImportStateVerify: true, + }, }, }) } @@ -143,6 +149,17 @@ func testAccCheckAWSAPIGatewayRequestValidatorDestroy(s *terraform.State) error return nil } +func testAccAWSAPIGatewayRequestValidatorImportStateIdFunc(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.ID), nil + } +} + const testAccAWSAPIGatewayRequestValidatorConfig_base = ` resource "aws_api_gateway_rest_api" "test" { name = "tf-request-validator-test" diff --git a/website/docs/r/api_gateway_request_validator.html.markdown b/website/docs/r/api_gateway_request_validator.html.markdown index 4fd41b4c688..5b07dc8fed7 100644 --- a/website/docs/r/api_gateway_request_validator.html.markdown +++ b/website/docs/r/api_gateway_request_validator.html.markdown @@ -35,3 +35,11 @@ The following argument is supported: The following attribute is exported in addition to the arguments listed above: * `id` - The unique ID of the request validator + +## Import + +`aws_api_gateway_request_validator` can be imported using `REST-API-ID/REQUEST-VALIDATOR-ID`, e.g. + +``` +$ terraform import aws_api_gateway_request_validator.example 12345abcde/67890fghij +```