Skip to content

Commit

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

resource/aws_api_gateway_resource: Support resource import
  • Loading branch information
bflad authored Aug 20, 2018
2 parents 1bd075f + 9eaa34e commit 4995ca3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
15 changes: 15 additions & 0 deletions aws/resource_aws_api_gateway_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -18,6 +19,20 @@ func resourceAwsApiGatewayResource() *schema.Resource {
Read: resourceAwsApiGatewayResourceRead,
Update: resourceAwsApiGatewayResourceUpdate,
Delete: resourceAwsApiGatewayResourceDelete,
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/RESOURCE-ID", d.Id())
}
restApiID := idParts[0]
resourceID := idParts[1]
d.Set("request_validator_id", resourceID)
d.Set("rest_api_id", restApiID)
d.SetId(resourceID)
return []*schema.ResourceData{d}, nil
},
},

Schema: map[string]*schema.Schema{
"rest_api_id": &schema.Schema{
Expand Down
23 changes: 23 additions & 0 deletions aws/resource_aws_api_gateway_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func TestAccAWSAPIGatewayResource_basic(t *testing.T) {
"aws_api_gateway_resource.test", "path", "/test"),
),
},
{
ResourceName: "aws_api_gateway_resource.test",
ImportState: true,
ImportStateIdFunc: testAccAWSAPIGatewayResourceImportStateIdFunc("aws_api_gateway_resource.test"),
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -65,6 +71,12 @@ func TestAccAWSAPIGatewayResource_update(t *testing.T) {
"aws_api_gateway_resource.test", "path", "/test_changed"),
),
},
{
ResourceName: "aws_api_gateway_resource.test",
ImportState: true,
ImportStateIdFunc: testAccAWSAPIGatewayResourceImportStateIdFunc("aws_api_gateway_resource.test"),
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -145,6 +157,17 @@ func testAccCheckAWSAPIGatewayResourceDestroy(s *terraform.State) error {
return nil
}

func testAccAWSAPIGatewayResourceImportStateIdFunc(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 testAccAWSAPIGatewayResourceConfig = `
resource "aws_api_gateway_rest_api" "test" {
name = "test"
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/api_gateway_resource.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ In addition to all arguments above, the following attributes are exported:

* `id` - The resource's identifier.
* `path` - The complete path for this API resource, including all parent paths.

## Import

`aws_api_gateway_resource` can be imported using `REST-API-ID/RESOURCE-ID`, e.g.

```
$ terraform import aws_api_gateway_resource.example 12345abcde/67890fghij
```

0 comments on commit 4995ca3

Please sign in to comment.