Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/aws_api_gateway_request_validator: Support resource import #5573

Merged
merged 1 commit into from
Aug 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just call d.SetID and then the Read function? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, the read function needs the REST API ID as well as the request validator ID to perform the read, but the existing resource ID is missing the REST API ID. At some point we should adjust the resource ID to include the REST API ID so we can perform reads correctly from ID, but for now we can support import without adjusting other code.

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
```