Skip to content

Commit

Permalink
Merge pull request #5573 from terraform-providers/f-aws_api_gateway_r…
Browse files Browse the repository at this point in the history
…equest_validator-import

resource/aws_api_gateway_request_validator: Support resource import
  • Loading branch information
bflad authored Aug 20, 2018
2 parents c5d7fd4 + 832837e commit d15b30c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
19 changes: 17 additions & 2 deletions aws/resource_aws_api_gateway_request_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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": {
Expand Down
17 changes: 17 additions & 0 deletions aws/resource_aws_api_gateway_request_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
})
}
Expand Down Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/api_gateway_request_validator.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit d15b30c

Please sign in to comment.